@db-auto/tables
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -12,2 +12,3 @@ import { ErrorsAnd, NameAnd } from "@db-auto/utils"; | ||
alias: string; | ||
where: string[]; | ||
} | ||
@@ -18,2 +19,2 @@ export interface PlanLink { | ||
} | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, path: string[]): ErrorsAnd<Plan | undefined>; | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, path: string[], id?: string): ErrorsAnd<Plan | undefined>; |
@@ -5,7 +5,12 @@ "use strict"; | ||
const utils_1 = require("@db-auto/utils"); | ||
function buildPlan(tables, path) { | ||
function quoteIfNeeded(type, s) { | ||
return type === 'string' || type.includes('char') ? `'${s}'` : s; | ||
} | ||
function buildPlan(tables, path, id) { | ||
if (path.length === 0) | ||
return ['Cannot build plan for empty path']; | ||
let table = tables[path[0]]; | ||
const plan = { table, alias: `T${0}` }; | ||
let alias = `T${0}`; | ||
const where = id ? [`${alias}.${table.primary}=${quoteIfNeeded(table.keys[table.primary].type, id)}`] : []; | ||
const plan = { table, alias: alias, where }; | ||
return buildNextStep(tables, path, plan, 1); | ||
@@ -31,4 +36,4 @@ } | ||
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 } }; | ||
const plan = { table, alias: `T${index}`, linkToPrevious: { link, linkTo: previousPlan }, where: [] }; | ||
return buildNextStep(tables, path, plan, index + 1); | ||
} |
@@ -30,8 +30,8 @@ "use strict"; | ||
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" }]); | ||
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: [] }]); | ||
}); | ||
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([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable" }, | ||
{ "alias": "T1", "columns": ["*"], "table": "mission", "where": "T0.driverId = T1.driverId" } | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", where: [] }, | ||
{ "alias": "T1", "columns": ["*"], "table": "mission", "where": ["T0.driverId = T1.driverId"] } | ||
]); | ||
@@ -41,11 +41,11 @@ }); | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "audit"]), (0, sql_1.selectData)("all"))).toEqual([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable" }, | ||
{ "alias": "T1", "columns": ["*"], "table": "driver_aud", "where": "T0.driverId = T1.driverId" } | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", where: [] }, | ||
{ "alias": "T1", "columns": ["*"], "table": "driver_aud", "where": ["T0.driverId = T1.driverId"] } | ||
]); | ||
}); | ||
it("should build selectData with three steps", () => { | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, ["driver", "mission", "mission_aud"]), (0, sql_1.selectData)("all"))).toEqual([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable" }, | ||
{ "alias": "T1", "columns": ["*"], "table": "mission", "where": "T0.driverId = T1.driverId" }, | ||
{ "alias": "T2", "columns": ["*"], "table": "mission_aud", "where": "T1.missionId = T2.missionId" } | ||
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([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", "where": ["T0.id=123"] }, | ||
{ "alias": "T1", "columns": ["*"], "table": "mission", "where": ["T0.driverId = T1.driverId"] }, | ||
{ "alias": "T2", "columns": ["*"], "table": "mission_aud", "where": ["T1.missionId = T2.missionId"] } | ||
]); | ||
@@ -110,2 +110,8 @@ }); | ||
}); | ||
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([ | ||
"select T0.*, T1.*", | ||
" from DriverTable T0, driver_aud T1 where T0.id=123 and T0.driverId = T1.driverId" | ||
]); | ||
}); | ||
}); |
@@ -6,5 +6,5 @@ import { Plan } from "./query"; | ||
columns: string[]; | ||
where?: string; | ||
where: string[]; | ||
} | ||
export declare function whereFor(plan: Plan): string; | ||
export declare function whereFor(plan: Plan): string[]; | ||
export declare function selectDataForOne(plan: Plan, view: string): SelectData; | ||
@@ -21,5 +21,5 @@ export declare const selectData: (view: string) => (plan: Plan) => SelectData[]; | ||
}[]; | ||
where?: string[]; | ||
where: string[]; | ||
} | ||
export declare function mergeSelectData(selectData: SelectData[]): MergedSelectData; | ||
export declare function sqlFor(m: MergedSelectData): string[]; |
@@ -9,6 +9,6 @@ "use strict"; | ||
if (planLink === undefined) | ||
return undefined; | ||
return plan.where; | ||
const link = planLink.link; | ||
const previousTable = planLink.linkTo; | ||
return `${previousTable.alias}.${(0, tables_1.idThere)(link)} = ${plan.alias}.${(0, tables_1.idHere)(link)}`; | ||
return [...plan.where, `${previousTable.alias}.${(0, tables_1.idThere)(link)} = ${plan.alias}.${(0, tables_1.idHere)(link)}`]; | ||
} | ||
@@ -34,3 +34,3 @@ exports.whereFor = whereFor; | ||
const columns = (0, utils_1.flatMap)(selectData, s => s.columns.map(c => ({ alias: s.alias, column: c }))); | ||
const where = selectData.map(s => s.where).filter(w => w !== undefined); | ||
const where = (0, utils_1.flatMap)(selectData, s => s.where).filter(w => w !== undefined); | ||
return { tables, columns, where }; | ||
@@ -37,0 +37,0 @@ } |
{ | ||
"name": "@db-auto/tables", | ||
"description": "", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"main": "dist/index", | ||
@@ -20,3 +20,3 @@ "types": "dist/index", | ||
"dependencies": { | ||
"@db-auto/utils": "0.0.9" | ||
"@db-auto/utils": "0.0.10" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
31514
663
+ Added@db-auto/utils@0.0.10(transitive)
- Removed@db-auto/utils@0.0.9(transitive)
Updated@db-auto/utils@0.0.10