@db-auto/tables
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -18,2 +18,13 @@ import { ErrorsAnd, NameAnd } from "@db-auto/utils"; | ||
} | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, path: string[], id?: string, queryParams?: NameAnd<string>, wheres?: string[]): ErrorsAnd<Plan | undefined>; | ||
export interface PathSpecForWheres { | ||
id: string | undefined; | ||
queryParams: NameAnd<string>; | ||
} | ||
export interface PathSpec extends PathSpecForWheres { | ||
path: string[]; | ||
id: string | undefined; | ||
queryParams: NameAnd<string>; | ||
wheres: string[]; | ||
} | ||
export declare function makePathSpec(path: string, id?: string, queryParams?: NameAnd<string>, wheres?: string[]): PathSpec; | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, pathSpec: PathSpec): ErrorsAnd<Plan | undefined>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildPlan = void 0; | ||
exports.buildPlan = exports.makePathSpec = void 0; | ||
const utils_1 = require("@db-auto/utils"); | ||
function makePathSpec(path, id, queryParams, wheres) { | ||
return { | ||
path: path.split('.').filter(p => p !== ''), | ||
id, | ||
queryParams: (0, utils_1.safeObject)(queryParams), | ||
wheres: (0, utils_1.safeArray)(wheres) | ||
}; | ||
} | ||
exports.makePathSpec = makePathSpec; | ||
function quoteIfNeeded(type, s) { | ||
return type === 'string' || type.includes('char') || type.includes('date') || type.includes('time') ? `'${s}'` : s; | ||
} | ||
function makeWhere(id, queryParams, alias, table) { | ||
function makeWhere(pathSpecForWheres, alias, table) { | ||
const { id, queryParams } = pathSpecForWheres; | ||
const whereFromId = id ? [`${alias}.${table.primary}=${quoteIfNeeded(table.keys[table.primary].type, id)}`] : []; | ||
@@ -17,6 +27,7 @@ const whereFromParams = (0, utils_1.flatMapEntries)(table.queries, (q, n) => { | ||
} | ||
function buildPlan(tables, path, id, queryParams, wheres) { | ||
function buildPlan(tables, pathSpec) { | ||
const path = pathSpec.path; | ||
if (path.length === 0) | ||
return ['Cannot build plan for empty path']; | ||
const params = queryParams || {}; | ||
// const params = queryParams || {}; | ||
let table = tables[path[0]]; | ||
@@ -26,5 +37,5 @@ if (table === undefined) | ||
let alias = `T${0}`; | ||
const where = [...(0, utils_1.safeArray)(wheres), ...makeWhere(id, params, alias, table)]; | ||
const where = [...pathSpec.wheres, ...makeWhere(pathSpec, alias, table)]; | ||
const plan = { table, alias: alias, where }; | ||
return buildNextStep(tables, path, params, plan, 1); | ||
return buildNextStep(tables, Object.assign(Object.assign({}, pathSpec), { id: undefined }), plan, 1); | ||
} | ||
@@ -38,3 +49,4 @@ exports.buildPlan = buildPlan; | ||
} | ||
function buildNextStep(tables, path, params, previousPlan, index) { | ||
function buildNextStep(tables, pathSpec, previousPlan, index) { | ||
const path = pathSpec.path; | ||
if (index >= path.length) | ||
@@ -52,5 +64,5 @@ return previousPlan; | ||
let alias = `T${index}`; | ||
const where = makeWhere(undefined, params, alias, table); | ||
const where = makeWhere(pathSpec, alias, table); | ||
const plan = { table, alias, linkToPrevious: { link, linkTo: previousPlan }, where }; | ||
return buildNextStep(tables, path, params, plan, index + 1); | ||
return buildNextStep(tables, pathSpec, plan, index + 1); | ||
} |
@@ -19,9 +19,9 @@ "use strict"; | ||
it("should build a plan with just one step", () => { | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, ["driver"]))).toEqual('DriverTable'); | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver")))).toEqual('DriverTable'); | ||
}); | ||
it("should build a plan with two steps", () => { | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission"]))).toEqual('DriverTable one-to-many mission'); | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission")))).toEqual('DriverTable one-to-many mission'); | ||
}); | ||
it("should build a plan with three steps", () => { | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission", "mission_aud"]))).toEqual('DriverTable one-to-many mission one-to-many mission_aud'); | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission.mission_aud")))).toEqual('DriverTable one-to-many mission one-to-many mission_aud'); | ||
}); | ||
@@ -31,6 +31,6 @@ }); | ||
it("should build selectData with just one step", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver"]), (0, sql_1.selectData)("all"))).toEqual([{ "columns": ["*"], "table": "DriverTable", alias: "T0", where: [] }]); | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver")), (0, sql_1.selectData)("all"))).toEqual([{ "columns": ["*"], "table": "DriverTable", alias: "T0", where: [] }]); | ||
}); | ||
it("should build selectData with two steps", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission"]), (0, sql_1.selectData)("all"))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission")), (0, sql_1.selectData)("all"))).toEqual([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", where: [] }, | ||
@@ -41,3 +41,3 @@ { "alias": "T1", "columns": ["*"], "table": "mission", "where": ["T0.driverId = T1.driverId"] } | ||
it("should build selectData when the link isn't the table name", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "audit"]), (0, sql_1.selectData)("all"))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.audit")), (0, sql_1.selectData)("all"))).toEqual([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", where: [] }, | ||
@@ -48,3 +48,3 @@ { "alias": "T1", "columns": ["*"], "table": "driver_aud", "where": ["T0.driverId = T1.driverId"] } | ||
it("should build selectData with three steps and an id", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission", "mission_aud"], "123"), (0, sql_1.selectData)("all"))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission.mission_aud", "123")), (0, sql_1.selectData)("all"))).toEqual([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", "where": ["T0.id=123"] }, | ||
@@ -58,3 +58,3 @@ { "alias": "T1", "columns": ["*"], "table": "mission", "where": ["T0.driverId = T1.driverId"] }, | ||
it("should mergeSelectData just one step", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver"]), plan => (0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan)))).toEqual({ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver")), plan => (0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan)))).toEqual({ | ||
"columns": [{ "alias": "T0", "column": "*" }], | ||
@@ -66,3 +66,3 @@ "tables": [{ "alias": "T0", "table": "DriverTable" }], | ||
it("should mergeSelectData with two steps", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission"]), plan => (0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan)))).toEqual({ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission")), plan => (0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan)))).toEqual({ | ||
"columns": [{ "alias": "T0", "column": "*" }, { "alias": "T1", "column": "*" }], | ||
@@ -74,3 +74,3 @@ "tables": [{ "alias": "T0", "table": "DriverTable" }, { "alias": "T1", "table": "mission" }], | ||
it("should mergeSelectData with three steps", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission", "mission_aud"]), plan => (0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan)))).toEqual({ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission.mission_aud")), plan => (0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan)))).toEqual({ | ||
"columns": [ | ||
@@ -92,3 +92,3 @@ { "alias": "T0", "column": "*" }, | ||
it("should make sql for a single step", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver"]), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver")), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
"select T0.*", | ||
@@ -99,3 +99,3 @@ " from DriverTable T0" | ||
it("should make sql for two steps", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission"]), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission")), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
"select T0.*, T1.*", | ||
@@ -106,3 +106,3 @@ " from DriverTable T0, mission T1 where T0.driverId = T1.driverId" | ||
it("should make sql for three steps", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission", "mission_aud"]), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission.mission_aud")), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
"select T0.*, T1.*, T2.*", | ||
@@ -113,3 +113,3 @@ " from DriverTable T0, mission T1, mission_aud T2 where T0.driverId = T1.driverId and T1.missionId = T2.missionId" | ||
it("should make sql when link name isn't table name", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "audit"]), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.audit")), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
"select T0.*, T1.*", | ||
@@ -120,3 +120,3 @@ " from DriverTable T0, driver_aud T1 where T0.driverId = T1.driverId" | ||
it("should make sql when link name isn't table name and there is an id", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "audit"], "123"), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.audit", "123", {}, [])), plan => (0, sql_1.sqlFor)((0, sql_1.mergeSelectData)((0, sql_1.selectData)("all")(plan))))).toEqual([ | ||
"select T0.*, T1.*", | ||
@@ -123,0 +123,0 @@ " from DriverTable T0, driver_aud T1 where T0.id=123 and T0.driverId = T1.driverId" |
{ | ||
"name": "@db-auto/tables", | ||
"description": "", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"main": "dist/index", | ||
@@ -20,3 +20,3 @@ "types": "dist/index", | ||
"dependencies": { | ||
"@db-auto/utils": "0.0.16" | ||
"@db-auto/utils": "0.0.17" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
34131
710
+ Added@db-auto/utils@0.0.17(transitive)
- Removed@db-auto/utils@0.0.16(transitive)
Updated@db-auto/utils@0.0.17