@db-auto/tables
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -7,1 +7,2 @@ export * from './src/tables'; | ||
export * from './src/query.fixture'; | ||
export * from './src/queryParams'; |
@@ -24,1 +24,2 @@ "use strict"; | ||
__exportStar(require("./src/query.fixture"), exports); | ||
__exportStar(require("./src/queryParams"), exports); |
@@ -56,24 +56,14 @@ "use strict"; | ||
expect((0, tables_1.cleanTable)(query_fixture_1.tables.mission, "mission")).toEqual({ | ||
"dataColumns": { | ||
"age": { "type": "integer" }, | ||
"dateOfBirth": { "type": "date" }, | ||
"livesIn": { "type": "string" }, | ||
"personalCar": {} | ||
}, | ||
"links": { | ||
"audit": { "idHereAndThere": "driverId", "type": "one-to-many", "table": "driver_aud" }, | ||
"mission": { "idHereAndThere": "driverId", "type": "one-to-many" } | ||
"driver": { "idHereAndThere": "driverId", "type": "many-to-one" }, | ||
"mission_aud": { "idHereAndThere": "missionId", "type": "one-to-many" } | ||
}, | ||
"primary": { "name": "id", "type": "integer" }, | ||
"primary": "id", | ||
"queries": { | ||
"employeeNum": { "type": "string" }, | ||
"name": { "description": "driver name ", "type": "string" } | ||
"date": { "type": "date" } | ||
}, | ||
"table": "DriverTable", | ||
"views": { | ||
"all": "*", | ||
"short": ["id", "name"] | ||
} | ||
"table": "mission", | ||
"views": { "all": "*", "short": ["id", "driverId"] } | ||
}); | ||
}); | ||
}); |
@@ -9,2 +9,3 @@ "use strict"; | ||
"links": { title: "Links", dataFn: (t) => (0, utils_1.mapEntries)(t[1].links, (l, name) => name).join(',') }, | ||
"params": { title: "Params", dataFn: (t) => (0, utils_1.mapEntries)(t[1].queries, (q, name) => name).join(',') }, | ||
"views": { title: "Views", dataFn: (t) => (0, utils_1.mapEntries)(t[1].views, (v, name) => name).join(',') } | ||
@@ -11,0 +12,0 @@ }; |
@@ -18,2 +18,2 @@ import { ErrorsAnd, NameAnd } from "@db-auto/utils"; | ||
} | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, path: string[], id?: string): ErrorsAnd<Plan | undefined>; | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, path: string[], id?: string, queryParams?: NameAnd<string>): ErrorsAnd<Plan | undefined>; |
@@ -6,12 +6,22 @@ "use strict"; | ||
function quoteIfNeeded(type, s) { | ||
return type === 'string' || type.includes('char') ? `'${s}'` : s; | ||
return type === 'string' || type.includes('char') || type.includes('date') || type.includes('time') ? `'${s}'` : s; | ||
} | ||
function buildPlan(tables, path, id) { | ||
function makeWhere(id, queryParams, alias, table) { | ||
const whereFromId = id ? [`${alias}.${table.primary}=${quoteIfNeeded(table.keys[table.primary].type, id)}`] : []; | ||
const whereFromParams = (0, utils_1.flatMapEntries)(table.queries, (q, n) => { | ||
const name = q.name || n; | ||
const inParams = queryParams[name]; | ||
return inParams !== undefined ? [`${alias}.${n}=${quoteIfNeeded(q.type, inParams)}`] : []; | ||
}); | ||
return [...whereFromId, ...whereFromParams]; | ||
} | ||
function buildPlan(tables, path, id, queryParams) { | ||
if (path.length === 0) | ||
return ['Cannot build plan for empty path']; | ||
const params = queryParams || {}; | ||
let table = tables[path[0]]; | ||
let alias = `T${0}`; | ||
const where = id ? [`${alias}.${table.primary}=${quoteIfNeeded(table.keys[table.primary].type, id)}`] : []; | ||
const where = makeWhere(id, params, alias, table); | ||
const plan = { table, alias: alias, where }; | ||
return buildNextStep(tables, path, plan, 1); | ||
return buildNextStep(tables, path, params, plan, 1); | ||
} | ||
@@ -25,3 +35,3 @@ exports.buildPlan = buildPlan; | ||
} | ||
function buildNextStep(tables, path, previousPlan, index) { | ||
function buildNextStep(tables, path, params, previousPlan, index) { | ||
if (index >= path.length) | ||
@@ -35,6 +45,8 @@ return previousPlan; | ||
const table = tables[link.table]; | ||
let alias = `T${index}`; | ||
const where = makeWhere(undefined, params, alias, table); | ||
if (table === undefined) | ||
return [`Cannot find table ${p} in tables. Path is ${path.slice(0, index)}. Available tables are: ${Object.keys(tables)}`]; | ||
const plan = { table, alias: `T${index}`, linkToPrevious: { link, linkTo: previousPlan }, where: [] }; | ||
return buildNextStep(tables, path, plan, index + 1); | ||
const plan = { table, alias, linkToPrevious: { link, linkTo: previousPlan }, where }; | ||
return buildNextStep(tables, path, params, plan, index + 1); | ||
} |
{ | ||
"name": "@db-auto/tables", | ||
"description": "", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"main": "dist/index", | ||
@@ -20,3 +20,3 @@ "types": "dist/index", | ||
"dependencies": { | ||
"@db-auto/utils": "0.0.11" | ||
"@db-auto/utils": "0.0.12" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
32714
25
684
+ Added@db-auto/utils@0.0.12(transitive)
- Removed@db-auto/utils@0.0.11(transitive)
Updated@db-auto/utils@0.0.12