@dbpath/tables
Advanced tools
Comparing version 0.2.4 to 0.2.6
@@ -5,4 +5,5 @@ export * from './src/clean'; | ||
export * from './src/sql'; | ||
export * from './src/sql2'; | ||
export * from './src/prettyprint'; | ||
export * from './src/query.fixture'; | ||
export * from './src/queryParams'; |
@@ -21,4 +21,5 @@ "use strict"; | ||
__exportStar(require("./src/sql"), exports); | ||
__exportStar(require("./src/sql2"), exports); | ||
__exportStar(require("./src/prettyprint"), exports); | ||
__exportStar(require("./src/query.fixture"), exports); | ||
__exportStar(require("./src/queryParams"), exports); |
@@ -50,3 +50,3 @@ "use strict"; | ||
}, | ||
"table": "DriverTable", | ||
"table": "drivertable", | ||
"views": { "all": "*", "short": ["id", "name"] } | ||
@@ -53,0 +53,0 @@ }); |
import { ErrorsAnd, NameAnd } from "@dbpath/utils"; | ||
import { Link } from "./tables"; | ||
import { CleanTable } from "./clean"; | ||
import { HasPk } from "@dbpath/dal"; | ||
export interface DbAutoQuery { | ||
@@ -19,13 +20,16 @@ path: string[]; | ||
export interface PathSpecForWheres { | ||
id: string | undefined; | ||
queryParams: NameAnd<string>; | ||
id?: string; | ||
table2Pk: NameAnd<HasPk>; | ||
wheres?: string[]; | ||
queryParams?: NameAnd<string>; | ||
} | ||
export interface PathSpec extends PathSpecForWheres { | ||
rawPath: string; | ||
path: string[]; | ||
id: string | undefined; | ||
queryParams: NameAnd<string>; | ||
wheres: string[]; | ||
limit?: number; | ||
} | ||
export declare function makePathSpec(path: string, id?: string, queryParams?: NameAnd<string>, wheres?: string[]): PathSpec; | ||
export declare function makePathSpec(path: string, table2Pk?: NameAnd<HasPk>, id?: string, queryParams?: NameAnd<string>, wheres?: string[]): PathSpec; | ||
export declare function quoteIfNeeded(type: string, s: string): string; | ||
export declare function buildPlan(tables: NameAnd<CleanTable>, pathSpec: PathSpec): ErrorsAnd<Plan | undefined>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
"driver": { | ||
"table": "DriverTable", | ||
"table": "drivertable", | ||
"primary": { "name": "id", "type": "integer" }, | ||
@@ -12,0 +12,0 @@ "queries": { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildPlan = exports.makePathSpec = void 0; | ||
exports.buildPlan = exports.quoteIfNeeded = exports.makePathSpec = void 0; | ||
const utils_1 = require("@dbpath/utils"); | ||
function makePathSpec(path, id, queryParams, wheres) { | ||
function makePathSpec(path, table2Pk, id, queryParams, wheres) { | ||
return { | ||
rawPath: path, | ||
path: path.split('.').filter(p => p !== ''), | ||
id, | ||
table2Pk, | ||
queryParams: (0, utils_1.safeObject)(queryParams), | ||
@@ -15,4 +17,5 @@ wheres: (0, utils_1.safeArray)(wheres) | ||
function quoteIfNeeded(type, s) { | ||
return type === 'string' || type.includes('char') || type.includes('date') || type.includes('time') ? `'${s}'` : s; | ||
return type === 'string' || type.includes('char') || type.includes('text') || type.includes('date') || type.includes('time') ? `'${s}'` : s; | ||
} | ||
exports.quoteIfNeeded = quoteIfNeeded; | ||
function makeWhere(pathSpecForWheres, alias, table) { | ||
@@ -19,0 +22,0 @@ const { id, queryParams } = pathSpecForWheres; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const query_fixture_1 = require("./query.fixture"); | ||
const fixtures_1 = require("@dbpath/fixtures"); | ||
function previousToString(p) { | ||
@@ -20,9 +21,9 @@ return p.link.type ? p.link.type : ''; | ||
it("should build a plan with just one step", () => { | ||
expect(planToString((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("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, (0, query_1.makePathSpec)("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, (0, query_1.makePathSpec)("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'); | ||
}); | ||
@@ -32,7 +33,7 @@ }); | ||
it("should build selectData with just one step", () => { | ||
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: [] }]); | ||
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, (0, query_1.makePathSpec)("driver.mission")), (0, sql_1.selectData)("all"))).toEqual([ | ||
{ "alias": "T0", "columns": ["*"], "table": "DriverTable", where: [] }, | ||
{ "alias": "T0", "columns": ["*"], "table": "drivertable", where: [] }, | ||
{ "alias": "T1", "columns": ["*"], "table": "mission", "where": ["T0.driverId = T1.driverId"] } | ||
@@ -43,3 +44,3 @@ ]); | ||
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: [] }, | ||
{ "alias": "T0", "columns": ["*"], "table": "drivertable", where: [] }, | ||
{ "alias": "T1", "columns": ["*"], "table": "driver_aud", "where": ["T0.driverId = T1.driverId"] } | ||
@@ -49,4 +50,4 @@ ]); | ||
it("should build selectData with three steps and an id", () => { | ||
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"] }, | ||
expect((0, utils_1.mapErrors)((0, query_1.buildPlan)(query_fixture_1.clean, (0, query_1.makePathSpec)("driver.mission.mission_aud", fixtures_1.sampleMeta.tables, "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"] }, | ||
@@ -61,3 +62,3 @@ { "alias": "T2", "columns": ["*"], "table": "mission_aud", "where": ["T1.missionId = T2.missionId"] } | ||
"columns": [{ "alias": "T0", "column": "*" }], | ||
"tables": [{ "alias": "T0", "table": "DriverTable" }], | ||
"tables": [{ "alias": "T0", "table": "drivertable" }], | ||
"where": [] | ||
@@ -69,3 +70,3 @@ }); | ||
"columns": [{ "alias": "T0", "column": "*" }, { "alias": "T1", "column": "*" }], | ||
"tables": [{ "alias": "T0", "table": "DriverTable" }, { "alias": "T1", "table": "mission" }], | ||
"tables": [{ "alias": "T0", "table": "drivertable" }, { "alias": "T1", "table": "mission" }], | ||
"where": ["T0.driverId = T1.driverId"] | ||
@@ -82,3 +83,3 @@ }); | ||
"tables": [ | ||
{ "alias": "T0", "table": "DriverTable" }, | ||
{ "alias": "T0", "table": "drivertable" }, | ||
{ "alias": "T1", "table": "mission" }, | ||
@@ -95,3 +96,3 @@ { "alias": "T2", "table": "mission_aud" } | ||
"select T0.*", | ||
" from DriverTable T0" | ||
" from drivertable T0" | ||
]); | ||
@@ -102,3 +103,3 @@ }); | ||
"select T0.*, T1.*", | ||
" from DriverTable T0, mission T1 where T0.driverId = T1.driverId" | ||
" from drivertable T0, mission T1 where T0.driverId = T1.driverId" | ||
]); | ||
@@ -109,3 +110,3 @@ }); | ||
"select T0.*, T1.*, T2.*", | ||
" from DriverTable T0, mission T1, mission_aud T2 where T0.driverId = T1.driverId and T1.missionId = T2.missionId" | ||
" from drivertable T0, mission T1, mission_aud T2 where T0.driverId = T1.driverId and T1.missionId = T2.missionId" | ||
]); | ||
@@ -116,11 +117,11 @@ }); | ||
"select T0.*, T1.*", | ||
" from DriverTable T0, driver_aud T1 where T0.driverId = T1.driverId" | ||
" 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, (0, query_1.makePathSpec)("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", fixtures_1.sampleMeta.tables, "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" | ||
" from drivertable T0, driver_aud T1 where T0.id=123 and T0.driverId = T1.driverId" | ||
]); | ||
}); | ||
}); |
import { PathItem } from "@dbpath/types"; | ||
export declare function sqlFor(p: PathItem): void; | ||
import { SelectData, SqlOptions } from "./sql"; | ||
import { PathSpecForWheres } from "./query"; | ||
export declare const mapOverPath: <T>(p: PathItem, fn: (p: PathItem) => T) => T[]; | ||
export declare function pathToSelectData(p: PathItem, pathSpecForWhere: PathSpecForWheres): SelectData[]; | ||
export declare function pathToSql(sqlOptions: SqlOptions, p: PathItem, pathSpec: PathSpecForWheres): string[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sqlFor = void 0; | ||
exports.pathToSql = exports.pathToSelectData = exports.mapOverPath = void 0; | ||
const types_1 = require("@dbpath/types"); | ||
const mapOverPath = (p, fn) => (0, types_1.isLinkInPath)(p) ? [fn(p), ...mapOverPath(p.previousLink, fn)] : [fn(p)]; | ||
function sqlFor(p) { | ||
const sql_1 = require("./sql"); | ||
const query_1 = require("./query"); | ||
const utils_1 = require("@dbpath/utils"); | ||
const mapOverPath = (p, fn) => (0, types_1.isLinkInPath)(p) ? [...(0, exports.mapOverPath)(p.previousLink, fn), fn(p)] : [fn(p)]; | ||
exports.mapOverPath = mapOverPath; | ||
function makePkWhere(pathSpecForWhere, p, alias) { | ||
const pks = pathSpecForWhere.table2Pk[p.table].pk; | ||
return pks.map(pk => `${alias}.${pk.name}=${(0, query_1.quoteIfNeeded)(pk.type, pathSpecForWhere.id)}`); | ||
} | ||
exports.sqlFor = sqlFor; | ||
function pathToSelectData(p, pathSpecForWhere) { | ||
const parts = (0, exports.mapOverPath)(p, p => p); | ||
return parts.map((p, i) => { | ||
let alias = `T${i}`; | ||
let TableWheres = pathSpecForWhere.id && (0, types_1.isTableInPath)(p) ? makePkWhere(pathSpecForWhere, p, alias) : []; | ||
let pathWheres = (0, types_1.isTableInPath)(p) ? (0, utils_1.safeArray)(pathSpecForWhere.wheres) : []; | ||
let linkWheres = (0, types_1.isLinkInPath)(p) ? p.idEquals.map(({ fromId, toId }) => `T${i - 1}.${fromId} = ${alias}.${toId}`) : []; | ||
const selectDataForTable = { | ||
columns: p.fields.length > 0 ? p.fields : ['*'], | ||
table: p.table, | ||
alias, | ||
where: [...linkWheres, ...TableWheres, ...pathWheres] | ||
}; | ||
return selectDataForTable; | ||
}); | ||
} | ||
exports.pathToSelectData = pathToSelectData; | ||
function pathToMergedData(p, pathSpec) { | ||
return (0, sql_1.mergeSelectData)(pathToSelectData(p, pathSpec)); | ||
} | ||
function pathToSql(sqlOptions, p, pathSpec) { | ||
return (0, sql_1.sqlFor)(sqlOptions)(pathToMergedData(p, pathSpec)); | ||
} | ||
exports.pathToSql = pathToSql; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fixtures_1 = require("@dbpath/fixtures"); | ||
const sql2_1 = require("./sql2"); | ||
describe("mapOverPath", () => { | ||
it("should return mergedData", () => { | ||
it("should walk over the path and return values", () => { | ||
expect((0, sql2_1.mapOverPath)(fixtures_1.driverPath, p => p.table)).toEqual(["drivertable"]); | ||
expect((0, sql2_1.mapOverPath)(fixtures_1.driverMissionAuditWithFieldsAndLinksPath, p => p.table)).toEqual(["drivertable", "mission", "audit"]); | ||
}); | ||
}); | ||
const table2Pk = fixtures_1.sampleMeta.tables; | ||
describe("pathToSelectData", () => { | ||
it("should make an array of select data", () => { | ||
expect((0, sql2_1.pathToSelectData)(fixtures_1.driverPath, { table2Pk })).toEqual([{ "alias": "T0", "columns": ['*'], "table": "drivertable", "where": [] }]); | ||
}); | ||
it("should make an array of select data with id", () => { | ||
expect((0, sql2_1.pathToSelectData)(fixtures_1.driverPath, { id: '1', table2Pk })).toEqual([ | ||
{ | ||
"alias": "T0", | ||
"columns": ["*"], | ||
"table": "drivertable", | ||
"where": ["T0.driverid=1"] | ||
} | ||
]); | ||
}); | ||
it("should make an array of select data for driver.mission.audit", () => { | ||
expect((0, sql2_1.pathToSelectData)(fixtures_1.driverMissionAuditWithFieldsAndLinksPath, { table2Pk })).toEqual([ | ||
{ "alias": "T0", "columns": ['*'], "table": "drivertable", "where": [] }, | ||
{ "alias": "T1", "columns": ['*'], "table": "mission", "where": ["T0.id1 = T1.id2"] }, | ||
{ "alias": "T2", "columns": ["f3", "f4"], "table": "audit", "where": ["T1.id2 = T2.id3"] } | ||
]); | ||
}); | ||
it("should make an array of select data for driver.mission.audit 123", () => { | ||
expect((0, sql2_1.pathToSelectData)(fixtures_1.driverMissionAuditWithFieldsAndLinksPath, { id: '1', table2Pk })).toEqual([ | ||
{ "alias": "T0", "columns": ['*'], "table": "drivertable", "where": ["T0.driverid=1"] }, | ||
{ "alias": "T1", "columns": ['*'], "table": "mission", "where": ["T0.id1 = T1.id2"] }, | ||
{ "alias": "T2", "columns": ["f3", "f4"], "table": "audit", "where": ["T1.id2 = T2.id3"] } | ||
]); | ||
}); | ||
}); | ||
describe("pathToSql", () => { | ||
it("should make sql without ids", () => { | ||
expect((0, sql2_1.pathToSql)({}, fixtures_1.driverMissionAuditWithFieldsAndLinksPath, { table2Pk })).toEqual([ | ||
"select T0.*, T1.*, T2.f3, T2.f4", | ||
" from drivertable T0, mission T1, audit T2 where T0.id1 = T1.id2 and T1.id2 = T2.id3" | ||
]); | ||
}); | ||
it("should make sql with integerids", () => { | ||
expect((0, sql2_1.pathToSql)({}, fixtures_1.driverMissionAuditWithFieldsAndLinksPath, { id: '123', table2Pk })).toEqual([ | ||
"select T0.*, T1.*, T2.f3, T2.f4", | ||
" from drivertable T0, mission T1, audit T2 where T0.driverid=123 and T0.id1 = T1.id2 and T1.id2 = T2.id3" | ||
]); | ||
}); | ||
}); |
{ | ||
"name": "@dbpath/tables", | ||
"description": "", | ||
"version": "0.2.4", | ||
"version": "0.2.6", | ||
"main": "dist/index", | ||
@@ -20,5 +20,5 @@ "types": "dist/index", | ||
"dependencies": { | ||
"@dbpath/utils": "0.2.4", | ||
"@dbpath/types": "0.2.4", | ||
"@dbpath/fixtures": "0.2.4" | ||
"@dbpath/utils": "0.2.6", | ||
"@dbpath/types": "0.2.6", | ||
"@dbpath/fixtures": "0.2.6" | ||
}, | ||
@@ -25,0 +25,0 @@ "devDependencies": { |
39279
767
+ Added@dbpath/fixtures@0.2.6(transitive)
+ Added@dbpath/types@0.2.6(transitive)
+ Added@dbpath/utils@0.2.6(transitive)
- Removed@dbpath/config@0.2.4(transitive)
- Removed@dbpath/dal@0.2.4(transitive)
- Removed@dbpath/fixtures@0.2.4(transitive)
- Removed@dbpath/types@0.2.4(transitive)
- Removed@dbpath/utils@0.2.4(transitive)
Updated@dbpath/fixtures@0.2.6
Updated@dbpath/types@0.2.6
Updated@dbpath/utils@0.2.6