prostgles-server 4.2.616 → 4.2.617
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DBSchemaBuilder/getColumnTypescriptDefinition.d.ts.map +1 -1
- package/dist/DBSchemaBuilder/getColumnTypescriptDefinition.js +8 -23
- package/dist/DBSchemaBuilder/getColumnTypescriptDefinition.js.map +1 -1
- package/dist/DBSchemaBuilder/getLookupTableValues.d.ts +11 -0
- package/dist/DBSchemaBuilder/getLookupTableValues.d.ts.map +1 -0
- package/dist/DBSchemaBuilder/getLookupTableValues.js +40 -0
- package/dist/DBSchemaBuilder/getLookupTableValues.js.map +1 -0
- package/lib/DBSchemaBuilder/getColumnTypescriptDefinition.ts +7 -25
- package/lib/DBSchemaBuilder/getLookupTableValues.ts +50 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getColumnTypescriptDefinition.d.ts","sourceRoot":"","sources":["../../lib/DBSchemaBuilder/getColumnTypescriptDefinition.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"getColumnTypescriptDefinition.d.ts","sourceRoot":"","sources":["../../lib/DBSchemaBuilder/getColumnTypescriptDefinition.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,WAAW,EACjB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAInE,eAAO,MAAM,6BAA6B,GAAI,iDAK3C;IACD,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC,aAAa,EAAE,WAAW,EAAE,CAAC;IAC7B,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;CACxC,WAeA,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,iDAKzB;IACD,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC,aAAa,EAAE,WAAW,EAAE,CAAC;IAC7B,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;CACxC,WAuCA,CAAC"}
|
|
@@ -4,6 +4,7 @@ exports.getDataType = exports.getColumnTypescriptDefinition = void 0;
|
|
|
4
4
|
const prostgles_types_1 = require("prostgles-types");
|
|
5
5
|
const utils_1 = require("../utils/utils");
|
|
6
6
|
const getColumnConfig_1 = require("../TableConfig/getColumnConfig");
|
|
7
|
+
const getLookupTableValues_1 = require("./getLookupTableValues");
|
|
7
8
|
const getColumnTypescriptDefinition = ({ config, tablesOrViews, tableOrView, column, }) => {
|
|
8
9
|
/**
|
|
9
10
|
* Columns that are nullable or have default values can be ommitted from an insert
|
|
@@ -34,39 +35,23 @@ const getDataType = ({ config, tablesOrViews, tableOrView, column, }) => {
|
|
|
34
35
|
const enumValues = Object.keys(tableConfig.isLookupTable.values);
|
|
35
36
|
return buildEnumTypeDefinition(enumValues);
|
|
36
37
|
}
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
if (colConf.jsonbSchema || colConf.jsonbSchemaType) {
|
|
38
|
+
const columnConfig = config && (0, getColumnConfig_1.getColumnConfig)(config, tableOrView.name, column.name);
|
|
39
|
+
const colConf = (0, prostgles_types_1.isObject)(columnConfig) ? columnConfig : undefined;
|
|
40
|
+
if (colConf?.jsonbSchema || colConf?.jsonbSchemaType) {
|
|
42
41
|
const schema = colConf.jsonbSchema || {
|
|
43
42
|
...colConf,
|
|
44
43
|
type: colConf.jsonbSchemaType,
|
|
45
44
|
};
|
|
46
45
|
return (0, prostgles_types_1.getJSONBSchemaTSTypes)(schema, { nullable: colConf.nullable }, " ", tablesOrViews);
|
|
47
46
|
}
|
|
48
|
-
if ("enum" in colConf) {
|
|
47
|
+
if (colConf && "enum" in colConf) {
|
|
49
48
|
if (!colConf.enum)
|
|
50
49
|
throw "colConf.enum missing";
|
|
51
50
|
return buildEnumTypeDefinition(colConf.enum);
|
|
52
51
|
}
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
config[colConf.references.tableName]
|
|
57
|
-
: column.references
|
|
58
|
-
?.map((ref) => {
|
|
59
|
-
const refTableConfig = config[ref.ftable];
|
|
60
|
-
if (refTableConfig && "isLookupTable" in refTableConfig) {
|
|
61
|
-
return refTableConfig;
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
.find(prostgles_types_1.isDefined);
|
|
65
|
-
if (lookupTableConfig && "isLookupTable" in lookupTableConfig) {
|
|
66
|
-
const enumValus = Object.keys(lookupTableConfig.isLookupTable.values);
|
|
67
|
-
return buildEnumTypeDefinition(enumValus);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
52
|
+
const lookupValues = (0, getLookupTableValues_1.getLookupTableValues)({ config, tablesOrViews, tableOrView, column });
|
|
53
|
+
if (lookupValues)
|
|
54
|
+
return buildEnumTypeDefinition(lookupValues);
|
|
70
55
|
return typeFromUdtName;
|
|
71
56
|
};
|
|
72
57
|
exports.getDataType = getDataType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getColumnTypescriptDefinition.js","sourceRoot":"","sources":["../../lib/DBSchemaBuilder/getColumnTypescriptDefinition.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"getColumnTypescriptDefinition.js","sourceRoot":"","sources":["../../lib/DBSchemaBuilder/getColumnTypescriptDefinition.ts"],"names":[],"mappings":";;;AAAA,qDAQyB;AAEzB,0CAA+C;AAE/C,oEAAiE;AACjE,iEAA8D;AAEvD,MAAM,6BAA6B,GAAG,CAAC,EAC5C,MAAM,EACN,aAAa,EACb,WAAW,EACX,MAAM,GAMP,EAAE,EAAE;IACH;;;OAGG;IAEH,MAAM,QAAQ,GAAG,IAAA,mBAAW,EAAC;QAC3B,MAAM;QACN,aAAa;QACb,WAAW;QACX,MAAM;KACP,CAAC,CAAC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EAAE,SAAS,EAAE,cAAc,CAAC,CAAC;QAEzF,OAAO,KAAK,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IAC/D,CAAC;IAED,MAAM,MAAM,GAAG,IAAA,kCAAgB,EAAC,QAAQ,CAAC,CAAC;IAC1C,IAAI,MAAM,KAAK,QAAQ,IAAI,IAAA,0BAAQ,EAAC,6BAAW,EAAE,QAAQ,CAAC,EAAE,CAAC;QAC3D,OAAO,iBAAiB,CAAC;IAC3B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type TableSchema } from "prostgles-types";
|
|
2
|
+
import type { TableConfig } from "../TableConfig/TableConfigTypes";
|
|
3
|
+
/** Follow the referenced column at each hop, stopping at a lookup primary key. */
|
|
4
|
+
export declare const getLookupTableValues: ({ config, tablesOrViews, tableOrView, column, visited, }: {
|
|
5
|
+
config: TableConfig | undefined;
|
|
6
|
+
tablesOrViews: TableSchema[];
|
|
7
|
+
tableOrView: TableSchema;
|
|
8
|
+
column: TableSchema["columns"][number];
|
|
9
|
+
visited?: Set<TableSchema["columns"][number]>;
|
|
10
|
+
}) => string[] | undefined;
|
|
11
|
+
//# sourceMappingURL=getLookupTableValues.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLookupTableValues.d.ts","sourceRoot":"","sources":["../../lib/DBSchemaBuilder/getLookupTableValues.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAGnE,kFAAkF;AAClF,eAAO,MAAM,oBAAoB,GAAI,0DAMlC;IACD,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC,aAAa,EAAE,WAAW,EAAE,CAAC;IAC7B,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;IACvC,OAAO,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;CAC/C,KAAG,MAAM,EAAE,GAAG,SAgCd,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLookupTableValues = void 0;
|
|
4
|
+
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
+
const getColumnConfig_1 = require("../TableConfig/getColumnConfig");
|
|
6
|
+
/** Follow the referenced column at each hop, stopping at a lookup primary key. */
|
|
7
|
+
const getLookupTableValues = ({ config, tablesOrViews, tableOrView, column, visited = new Set(), }) => {
|
|
8
|
+
if (!config || visited.has(column))
|
|
9
|
+
return;
|
|
10
|
+
visited.add(column);
|
|
11
|
+
const tableConfig = config[tableOrView.name];
|
|
12
|
+
if (tableConfig && "isLookupTable" in tableConfig && column.is_pkey) {
|
|
13
|
+
return Object.keys(tableConfig.isLookupTable.values);
|
|
14
|
+
}
|
|
15
|
+
const columnConfig = (0, getColumnConfig_1.getColumnConfig)(config, tableOrView.name, column.name);
|
|
16
|
+
const reference = (0, prostgles_types_1.isObject)(columnConfig) ? columnConfig.references : undefined;
|
|
17
|
+
const targets = reference ?
|
|
18
|
+
[{ tableName: reference.tableName, columnName: reference.columnName ?? "id" }]
|
|
19
|
+
: (column.references ?? []).map((ref) => ({
|
|
20
|
+
tableName: ref.ftable,
|
|
21
|
+
columnName: ref.fcols[ref.cols.indexOf(column.name)],
|
|
22
|
+
}));
|
|
23
|
+
for (const target of targets) {
|
|
24
|
+
const targetTable = tablesOrViews.find((table) => table.name === target.tableName);
|
|
25
|
+
const targetColumn = targetTable?.columns.find((col) => col.name === target.columnName);
|
|
26
|
+
if (!targetTable || !targetColumn)
|
|
27
|
+
continue;
|
|
28
|
+
const values = (0, exports.getLookupTableValues)({
|
|
29
|
+
config,
|
|
30
|
+
tablesOrViews,
|
|
31
|
+
tableOrView: targetTable,
|
|
32
|
+
column: targetColumn,
|
|
33
|
+
visited,
|
|
34
|
+
});
|
|
35
|
+
if (values)
|
|
36
|
+
return values;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.getLookupTableValues = getLookupTableValues;
|
|
40
|
+
//# sourceMappingURL=getLookupTableValues.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLookupTableValues.js","sourceRoot":"","sources":["../../lib/DBSchemaBuilder/getLookupTableValues.ts"],"names":[],"mappings":";;;AAAA,qDAA6D;AAE7D,oEAAiE;AAEjE,kFAAkF;AAC3E,MAAM,oBAAoB,GAAG,CAAC,EACnC,MAAM,EACN,aAAa,EACb,WAAW,EACX,MAAM,EACN,OAAO,GAAG,IAAI,GAAG,EAAkC,GAOpD,EAAwB,EAAE;IACzB,IAAI,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,OAAO;IAC3C,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAEpB,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAC7C,IAAI,WAAW,IAAI,eAAe,IAAI,WAAW,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACpE,OAAO,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,YAAY,GAAG,IAAA,iCAAe,EAAC,MAAM,EAAE,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAC5E,MAAM,SAAS,GAAG,IAAA,0BAAQ,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,SA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
|
8
7
|
type JSONB,
|
|
@@ -12,6 +11,7 @@ import type { TableSchemaColumn } from "../DboBuilder/DboBuilder";
|
|
|
12
11
|
import { escapeTSNames } from "../utils/utils";
|
|
13
12
|
import type { TableConfig } from "../TableConfig/TableConfigTypes";
|
|
14
13
|
import { getColumnConfig } from "../TableConfig/getColumnConfig";
|
|
14
|
+
import { getLookupTableValues } from "./getLookupTableValues";
|
|
15
15
|
|
|
16
16
|
export const getColumnTypescriptDefinition = ({
|
|
17
17
|
config,
|
|
@@ -69,12 +69,10 @@ export const getDataType = ({
|
|
|
69
69
|
return buildEnumTypeDefinition(enumValues);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
return typeFromUdtName;
|
|
75
|
-
}
|
|
72
|
+
const columnConfig = config && getColumnConfig(config, tableOrView.name, column.name);
|
|
73
|
+
const colConf = isObject(columnConfig) ? columnConfig : undefined;
|
|
76
74
|
|
|
77
|
-
if (colConf
|
|
75
|
+
if (colConf?.jsonbSchema || colConf?.jsonbSchemaType) {
|
|
78
76
|
const schema: JSONB.JSONBSchema = colConf.jsonbSchema || {
|
|
79
77
|
...colConf,
|
|
80
78
|
type: colConf.jsonbSchemaType,
|
|
@@ -82,29 +80,13 @@ export const getDataType = ({
|
|
|
82
80
|
return getJSONBSchemaTSTypes(schema, { nullable: colConf.nullable }, " ", tablesOrViews);
|
|
83
81
|
}
|
|
84
82
|
|
|
85
|
-
if ("enum" in colConf) {
|
|
83
|
+
if (colConf && "enum" in colConf) {
|
|
86
84
|
if (!colConf.enum) throw "colConf.enum missing";
|
|
87
85
|
return buildEnumTypeDefinition(colConf.enum);
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
const lookupTableConfig =
|
|
93
|
-
colConf.references ?
|
|
94
|
-
config[colConf.references.tableName]
|
|
95
|
-
: column.references
|
|
96
|
-
?.map((ref) => {
|
|
97
|
-
const refTableConfig = config[ref.ftable];
|
|
98
|
-
if (refTableConfig && "isLookupTable" in refTableConfig) {
|
|
99
|
-
return refTableConfig;
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
.find(isDefined);
|
|
103
|
-
if (lookupTableConfig && "isLookupTable" in lookupTableConfig) {
|
|
104
|
-
const enumValus = Object.keys(lookupTableConfig.isLookupTable.values);
|
|
105
|
-
return buildEnumTypeDefinition(enumValus);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
88
|
+
const lookupValues = getLookupTableValues({ config, tablesOrViews, tableOrView, column });
|
|
89
|
+
if (lookupValues) return buildEnumTypeDefinition(lookupValues);
|
|
108
90
|
|
|
109
91
|
return typeFromUdtName;
|
|
110
92
|
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isObject, type TableSchema } from "prostgles-types";
|
|
2
|
+
import type { TableConfig } from "../TableConfig/TableConfigTypes";
|
|
3
|
+
import { getColumnConfig } from "../TableConfig/getColumnConfig";
|
|
4
|
+
|
|
5
|
+
/** Follow the referenced column at each hop, stopping at a lookup primary key. */
|
|
6
|
+
export const getLookupTableValues = ({
|
|
7
|
+
config,
|
|
8
|
+
tablesOrViews,
|
|
9
|
+
tableOrView,
|
|
10
|
+
column,
|
|
11
|
+
visited = new Set<TableSchema["columns"][number]>(),
|
|
12
|
+
}: {
|
|
13
|
+
config: TableConfig | undefined;
|
|
14
|
+
tablesOrViews: TableSchema[];
|
|
15
|
+
tableOrView: TableSchema;
|
|
16
|
+
column: TableSchema["columns"][number];
|
|
17
|
+
visited?: Set<TableSchema["columns"][number]>;
|
|
18
|
+
}): string[] | undefined => {
|
|
19
|
+
if (!config || visited.has(column)) return;
|
|
20
|
+
visited.add(column);
|
|
21
|
+
|
|
22
|
+
const tableConfig = config[tableOrView.name];
|
|
23
|
+
if (tableConfig && "isLookupTable" in tableConfig && column.is_pkey) {
|
|
24
|
+
return Object.keys(tableConfig.isLookupTable.values);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const columnConfig = getColumnConfig(config, tableOrView.name, column.name);
|
|
28
|
+
const reference = isObject(columnConfig) ? columnConfig.references : undefined;
|
|
29
|
+
const targets =
|
|
30
|
+
reference ?
|
|
31
|
+
[{ tableName: reference.tableName, columnName: reference.columnName ?? "id" }]
|
|
32
|
+
: (column.references ?? []).map((ref) => ({
|
|
33
|
+
tableName: ref.ftable,
|
|
34
|
+
columnName: ref.fcols[ref.cols.indexOf(column.name)],
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
for (const target of targets) {
|
|
38
|
+
const targetTable = tablesOrViews.find((table) => table.name === target.tableName);
|
|
39
|
+
const targetColumn = targetTable?.columns.find((col) => col.name === target.columnName);
|
|
40
|
+
if (!targetTable || !targetColumn) continue;
|
|
41
|
+
const values = getLookupTableValues({
|
|
42
|
+
config,
|
|
43
|
+
tablesOrViews,
|
|
44
|
+
tableOrView: targetTable,
|
|
45
|
+
column: targetColumn,
|
|
46
|
+
visited,
|
|
47
|
+
});
|
|
48
|
+
if (values) return values;
|
|
49
|
+
}
|
|
50
|
+
};
|