@technicity/data-service-generator 0.23.0-next.13 → 0.23.0-next.14
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/generation/generate.js +52 -20
- package/package.json +1 -1
|
@@ -106,17 +106,16 @@ async function generate(input) {
|
|
|
106
106
|
tables = tables.filter((x) => !input.excludeTables?.includes(x));
|
|
107
107
|
}
|
|
108
108
|
ctx.log.debug({ tableCount: tables.length, tables }, "tables after filter");
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
getGetOneData(x, includeMappedFields, tables),
|
|
109
|
+
const data = await Promise.all(tables.flatMap((x) => [
|
|
110
|
+
getGetOneData(x, includeMappedFields),
|
|
112
111
|
getGetListData(x),
|
|
113
112
|
getGetListPaginatedData(x),
|
|
114
|
-
getPostOneData(x, specialCaseUuidColumn, includeMappedFields
|
|
113
|
+
getPostOneData(x, specialCaseUuidColumn, includeMappedFields),
|
|
115
114
|
getPatchOneData(x, specialCaseUuidColumn, includeMappedFields),
|
|
116
115
|
getPatchListData(x),
|
|
117
116
|
getDeleteOneData(x),
|
|
118
117
|
getDeleteListData(x)
|
|
119
|
-
]))
|
|
118
|
+
]));
|
|
120
119
|
ctx.log.debug({ inputLength: data.length }, "SDK input data collected");
|
|
121
120
|
const artifacts = await getArtifacts(tables, includeMappedFields, specialCaseUuidColumn);
|
|
122
121
|
ctx.log.debug("getArtifacts() completed");
|
|
@@ -885,20 +884,22 @@ function getTypeOrderByName(table) {
|
|
|
885
884
|
function getTypeDataPostName(table) {
|
|
886
885
|
return "DataPost" + changeCase.pascalCase(table);
|
|
887
886
|
}
|
|
888
|
-
async function getGetOneData(table, includeMappedFields
|
|
887
|
+
async function getGetOneData(table, includeMappedFields) {
|
|
888
|
+
getCtx().log.debug({ table }, "getGetOneData() start");
|
|
889
889
|
const typeFieldsName = getTypeFieldsName(table);
|
|
890
890
|
const typeReturnBaseName = getTypeReturnBaseName(table);
|
|
891
891
|
return {
|
|
892
892
|
kind: "getOne",
|
|
893
893
|
table,
|
|
894
894
|
methodName: "get" + changeCase.pascalCase(table),
|
|
895
|
-
typeFields: await getTypeFields(table, typeFieldsName, includeMappedFields
|
|
895
|
+
typeFields: await getTypeFields(table, typeFieldsName, includeMappedFields),
|
|
896
896
|
typeFieldsName,
|
|
897
|
-
typeReturnBase: await getTypeReturnBase(table, typeReturnBaseName, includeMappedFields
|
|
897
|
+
typeReturnBase: await getTypeReturnBase(table, typeReturnBaseName, includeMappedFields),
|
|
898
898
|
typeReturnBaseName
|
|
899
899
|
};
|
|
900
900
|
}
|
|
901
901
|
async function getGetListData(table) {
|
|
902
|
+
getCtx().log.debug({ table }, "getGetListData() start");
|
|
902
903
|
const typeFieldsName = getTypeFieldsName(table);
|
|
903
904
|
const typeReturnBaseName = getTypeReturnBaseName(table);
|
|
904
905
|
const typeWhereName = getTypeWhereName(table);
|
|
@@ -918,6 +919,7 @@ async function getGetListData(table) {
|
|
|
918
919
|
};
|
|
919
920
|
}
|
|
920
921
|
async function getGetListPaginatedData(table) {
|
|
922
|
+
getCtx().log.debug({ table }, "getGetListPaginatedData() start");
|
|
921
923
|
const typeFieldsName = getTypeFieldsName(table);
|
|
922
924
|
const typeReturnBaseName = getTypeReturnBaseName(table);
|
|
923
925
|
const typeWhereName = getTypeWhereName(table);
|
|
@@ -932,7 +934,8 @@ async function getGetListPaginatedData(table) {
|
|
|
932
934
|
typeOrderByName
|
|
933
935
|
};
|
|
934
936
|
}
|
|
935
|
-
async function getPostOneData(table, specialCaseUuidColumn, includeMappedFields
|
|
937
|
+
async function getPostOneData(table, specialCaseUuidColumn, includeMappedFields) {
|
|
938
|
+
getCtx().log.debug({ table }, "getPostOneData() start");
|
|
936
939
|
const typeFieldsName = getTypeFieldsName(table);
|
|
937
940
|
const typeReturnBaseName = getTypeReturnBaseName(table);
|
|
938
941
|
const typeDataName = getTypeDataPostName(table);
|
|
@@ -942,11 +945,12 @@ async function getPostOneData(table, specialCaseUuidColumn, includeMappedFields,
|
|
|
942
945
|
methodName: "post" + changeCase.pascalCase(table),
|
|
943
946
|
typeFieldsName,
|
|
944
947
|
typeReturnBaseName,
|
|
945
|
-
typeData: await getTypeDataPost(table, typeDataName, specialCaseUuidColumn, includeMappedFields
|
|
948
|
+
typeData: await getTypeDataPost(table, typeDataName, specialCaseUuidColumn, includeMappedFields),
|
|
946
949
|
typeDataName
|
|
947
950
|
};
|
|
948
951
|
}
|
|
949
952
|
async function getPatchOneData(table, specialCaseUuidColumn, includeMappedFields) {
|
|
953
|
+
getCtx().log.debug({ table }, "getPatchOneData() start");
|
|
950
954
|
const typeFieldsName = getTypeFieldsName(table);
|
|
951
955
|
const typeReturnBaseName = getTypeReturnBaseName(table);
|
|
952
956
|
const typeDataName = "DataPatch" + changeCase.pascalCase(table);
|
|
@@ -961,6 +965,7 @@ async function getPatchOneData(table, specialCaseUuidColumn, includeMappedFields
|
|
|
961
965
|
};
|
|
962
966
|
}
|
|
963
967
|
async function getPatchListData(table) {
|
|
968
|
+
getCtx().log.debug({ table }, "getPatchListData() start");
|
|
964
969
|
const typeFieldsName = getTypeFieldsName(table);
|
|
965
970
|
const typeReturnBaseName = getTypeReturnBaseName(table);
|
|
966
971
|
const typeWhereName = getTypeWhereName(table);
|
|
@@ -978,6 +983,7 @@ async function getPatchListData(table) {
|
|
|
978
983
|
};
|
|
979
984
|
}
|
|
980
985
|
function getDeleteOneData(table) {
|
|
986
|
+
getCtx().log.debug({ table }, "getDeleteOneData() start");
|
|
981
987
|
return {
|
|
982
988
|
kind: "deleteOne",
|
|
983
989
|
table,
|
|
@@ -985,6 +991,7 @@ function getDeleteOneData(table) {
|
|
|
985
991
|
};
|
|
986
992
|
}
|
|
987
993
|
function getDeleteListData(table) {
|
|
994
|
+
getCtx().log.debug({ table }, "getDeleteListData() start");
|
|
988
995
|
const typeWhereName = getTypeWhereName(table);
|
|
989
996
|
return {
|
|
990
997
|
kind: "deleteList",
|
|
@@ -994,10 +1001,12 @@ function getDeleteListData(table) {
|
|
|
994
1001
|
};
|
|
995
1002
|
}
|
|
996
1003
|
async function getTypeWhere(table, name) {
|
|
1004
|
+
getCtx().log.debug({ table, name }, "getTypeWhere() start");
|
|
997
1005
|
const jsonSchemaWhere = await getJSONSchemaWhere(table);
|
|
998
1006
|
return (0, json_schema_to_typescript_1.compile)(jsonSchemaWhere, name, json2TsOpts);
|
|
999
1007
|
}
|
|
1000
|
-
async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMappedFields
|
|
1008
|
+
async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMappedFields) {
|
|
1009
|
+
getCtx().log.debug({ table, name }, "getTypeDataPost() start");
|
|
1001
1010
|
const primaryColumn = await getPrimaryColumn(table);
|
|
1002
1011
|
const tableMeta = (await getTableMeta(table)).filter((x) => x.Field !== primaryColumn.name);
|
|
1003
1012
|
const nullable = tableMeta.reduce((acc, m) => ({
|
|
@@ -1010,7 +1019,7 @@ async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMapped
|
|
|
1010
1019
|
}), {});
|
|
1011
1020
|
let properties = getJSONSchemaObjProperties(tableMeta);
|
|
1012
1021
|
let notRequiredList = [];
|
|
1013
|
-
const oneToManyRelations = (await getRelationInfo(table
|
|
1022
|
+
const oneToManyRelations = (await getRelationInfo(table)).filter((x) => x.type === "one-to-many__many-to-one" && x.kind === "one-to-many");
|
|
1014
1023
|
const mappedFields = includeMappedFields ? await getMappedFields(table) : [];
|
|
1015
1024
|
let mappedFieldsMap = new Map();
|
|
1016
1025
|
if (includeMappedFields) {
|
|
@@ -1068,6 +1077,7 @@ async function getTypeDataPost(table, name, specialCaseUuidColumn, includeMapped
|
|
|
1068
1077
|
return type;
|
|
1069
1078
|
}
|
|
1070
1079
|
async function getTypeDataPatch(table, name, specialCaseUuidColumn, includeMappedFields) {
|
|
1080
|
+
getCtx().log.debug({ table, name }, "getTypeDataPatch() start");
|
|
1071
1081
|
const primaryColumn = await getPrimaryColumn(table);
|
|
1072
1082
|
let tableMeta = (await getTableMeta(table)).filter((x) => x.Field !== primaryColumn.name);
|
|
1073
1083
|
if (specialCaseUuidColumn) {
|
|
@@ -1134,6 +1144,7 @@ function unwrapJSONType(type) {
|
|
|
1134
1144
|
return type;
|
|
1135
1145
|
}
|
|
1136
1146
|
async function getMappedFields(table) {
|
|
1147
|
+
getCtx().log.debug({ table }, "getMappedFields() start");
|
|
1137
1148
|
const relationsManyToOne = await getRelationsManyToOne(table).then((xs) => xs.filter((x) => x.foreignKey.endsWith("Id")));
|
|
1138
1149
|
let out = [];
|
|
1139
1150
|
for (let x of relationsManyToOne) {
|
|
@@ -1157,6 +1168,7 @@ async function getMappedFields(table) {
|
|
|
1157
1168
|
return out;
|
|
1158
1169
|
}
|
|
1159
1170
|
async function getJSONSchemaWhere(table) {
|
|
1171
|
+
getCtx().log.debug({ table }, "getJSONSchemaWhere() start");
|
|
1160
1172
|
const whereSchemaName = `_Where${changeCase.pascalCase(table)}`;
|
|
1161
1173
|
const defWhere = {
|
|
1162
1174
|
oneOf: [
|
|
@@ -1310,9 +1322,11 @@ async function getJSONSchemaWhere(table) {
|
|
|
1310
1322
|
};
|
|
1311
1323
|
}
|
|
1312
1324
|
async function getTypeOrderBy(table, name) {
|
|
1325
|
+
getCtx().log.debug({ table, name }, "getTypeOrderBy() start");
|
|
1313
1326
|
return (0, json_schema_to_typescript_1.compile)((await getJSONSchemaOrderBy(table, name)), name, json2TsOpts);
|
|
1314
1327
|
}
|
|
1315
1328
|
async function getJSONSchemaOrderBy(table, name) {
|
|
1329
|
+
getCtx().log.debug({ table, name }, "getJSONSchemaOrderBy() start");
|
|
1316
1330
|
const fieldNames = await getTableMeta(table).then((xs) => xs.map((x) => x.Field));
|
|
1317
1331
|
const def = {
|
|
1318
1332
|
oneOf: fieldNames.map((k) => ({
|
|
@@ -1391,9 +1405,10 @@ export type TUpdateOperationsNumber = {$increment: number} | {$decrement: number
|
|
|
1391
1405
|
`;
|
|
1392
1406
|
return prettier.format(src, { parser: "typescript" });
|
|
1393
1407
|
}
|
|
1394
|
-
async function getTypeFields(table, name, includeMappedFields
|
|
1408
|
+
async function getTypeFields(table, name, includeMappedFields) {
|
|
1409
|
+
getCtx().log.debug({ table, name }, "getTypeFields() start");
|
|
1395
1410
|
const scalarKeys = Object.keys(getJSONSchemaObjProperties(await getTableMeta(table)));
|
|
1396
|
-
const relations = await getRelationInfo(table
|
|
1411
|
+
const relations = await getRelationInfo(table);
|
|
1397
1412
|
const mappedFields = includeMappedFields ? await getMappedFields(table) : [];
|
|
1398
1413
|
const keyWhere = "$where";
|
|
1399
1414
|
const keyOrderBy = "$orderBy";
|
|
@@ -1453,10 +1468,11 @@ async function getTypeFields(table, name, includeMappedFields, tables) {
|
|
|
1453
1468
|
type = imports + "\n\n" + type;
|
|
1454
1469
|
return type;
|
|
1455
1470
|
}
|
|
1456
|
-
async function getTypeReturnBase(table, name, includeMappedFields
|
|
1471
|
+
async function getTypeReturnBase(table, name, includeMappedFields) {
|
|
1472
|
+
getCtx().log.debug({ table, name }, "getTypeReturnBase() start");
|
|
1457
1473
|
const tableMeta = await getTableMeta(table);
|
|
1458
1474
|
const scalarProperties = getJSONSchemaObjProperties(tableMeta);
|
|
1459
|
-
const relations = await getRelationInfo(table
|
|
1475
|
+
const relations = await getRelationInfo(table);
|
|
1460
1476
|
const mappedFields = includeMappedFields ? await getMappedFields(table) : [];
|
|
1461
1477
|
const jsonSchemaReturn = {
|
|
1462
1478
|
type: "object",
|
|
@@ -1515,7 +1531,7 @@ async function getArtifacts(tables, includeMappedFields, specialCaseUuidColumn)
|
|
|
1515
1531
|
getShowCreateTable(table)
|
|
1516
1532
|
]);
|
|
1517
1533
|
const scalarFields = tableMeta.map((x) => x.Field);
|
|
1518
|
-
const relationInfo = await getRelationInfo(table
|
|
1534
|
+
const relationInfo = await getRelationInfo(table);
|
|
1519
1535
|
const relationFields = relationInfo.reduce((acc, x) => {
|
|
1520
1536
|
if (x.type === "one-to-many__many-to-one") {
|
|
1521
1537
|
acc[x.name] = {
|
|
@@ -1618,9 +1634,13 @@ async function getArtifacts(tables, includeMappedFields, specialCaseUuidColumn)
|
|
|
1618
1634
|
}, {});
|
|
1619
1635
|
return artifacts;
|
|
1620
1636
|
}
|
|
1621
|
-
const getRelationInfo = (0, memoize_1.default)(async function getRelationInfo(table
|
|
1637
|
+
const getRelationInfo = (0, memoize_1.default)(async function getRelationInfo(table) {
|
|
1638
|
+
const ctx = getCtx();
|
|
1639
|
+
ctx.log.debug({ table }, "getRelationInfo() start");
|
|
1622
1640
|
const relationsManyToOne = await getRelationsManyToOne(table);
|
|
1641
|
+
ctx.log.debug({ table, count: relationsManyToOne.length }, "getRelationInfo() getRelationsManyToOne done");
|
|
1623
1642
|
const relationsOneToMany = await getRelationsOneToMany(table);
|
|
1643
|
+
ctx.log.debug({ table, count: relationsOneToMany.length }, "getRelationInfo() getRelationsOneToMany done");
|
|
1624
1644
|
let out = [];
|
|
1625
1645
|
out = out.concat(relationsManyToOne.reduce((acc, x) => {
|
|
1626
1646
|
if (!x.foreignKey.endsWith("Id")) {
|
|
@@ -1638,6 +1658,7 @@ const getRelationInfo = (0, memoize_1.default)(async function getRelationInfo(ta
|
|
|
1638
1658
|
});
|
|
1639
1659
|
return acc;
|
|
1640
1660
|
}, []));
|
|
1661
|
+
ctx.log.debug({ table, manyToOneOutCount: out.length }, "getRelationInfo() many-to-one concat done");
|
|
1641
1662
|
const relationsOneToManyDuplicates = (0, getDuplicates_1.getDuplicates)(relationsOneToMany.map((x) => x.referencedTable));
|
|
1642
1663
|
out = out.concat(relationsOneToMany.reduce((acc, x) => {
|
|
1643
1664
|
if (!x.referencedKey.endsWith("Id")) {
|
|
@@ -1665,7 +1686,11 @@ const getRelationInfo = (0, memoize_1.default)(async function getRelationInfo(ta
|
|
|
1665
1686
|
});
|
|
1666
1687
|
return acc;
|
|
1667
1688
|
}, []));
|
|
1668
|
-
|
|
1689
|
+
ctx.log.debug({ table, afterOneToManyCount: out.length }, "getRelationInfo() one-to-many concat done");
|
|
1690
|
+
ctx.log.debug({ table }, "getRelationInfo() getJunctionTables() calling");
|
|
1691
|
+
const junctionTables = await getJunctionTables();
|
|
1692
|
+
ctx.log.debug({ table, junctionTableCount: junctionTables.length }, "getRelationInfo() getJunctionTables() done");
|
|
1693
|
+
const relationsManyToMany = junctionTables.reduce((acc, x) => {
|
|
1669
1694
|
const dataForParentTable = x.relations.find((r) => r.referencedTable === table);
|
|
1670
1695
|
if (dataForParentTable == null) {
|
|
1671
1696
|
return acc;
|
|
@@ -1685,15 +1710,19 @@ const getRelationInfo = (0, memoize_1.default)(async function getRelationInfo(ta
|
|
|
1685
1710
|
});
|
|
1686
1711
|
return acc;
|
|
1687
1712
|
}, []);
|
|
1713
|
+
ctx.log.debug({ table, manyToManyCount: relationsManyToMany.length }, "getRelationInfo() many-to-many reduce done");
|
|
1688
1714
|
out = out.concat(relationsManyToMany);
|
|
1689
1715
|
out = _.sortBy([(x) => x.table, (x) => x.name], out);
|
|
1716
|
+
ctx.log.debug({ table, totalCount: out.length }, "getRelationInfo() done");
|
|
1690
1717
|
return out;
|
|
1691
1718
|
}, (table) => getCtx().runId + ":" + table);
|
|
1692
1719
|
function getRelationManyToOneFieldName(x) {
|
|
1693
1720
|
return changeCase.camelCase(x.foreignKey.replace(new RegExp(x.referencedKey + "$", "i"), ""));
|
|
1694
1721
|
}
|
|
1695
1722
|
// TODO: not sure if this logic is correct
|
|
1696
|
-
|
|
1723
|
+
// Note: intentionally not the filtered tables
|
|
1724
|
+
const getJunctionTables = (0, memoize_1.default)(async function getJunctionTables() {
|
|
1725
|
+
const tables = await getTableNames();
|
|
1697
1726
|
return (await Promise.all(tables.map(async (table) => {
|
|
1698
1727
|
const relations = await getRelationsManyToOne(table);
|
|
1699
1728
|
if (relations.length === 2 &&
|
|
@@ -1711,6 +1740,7 @@ const getJunctionTables = (0, memoize_1.default)(async function getJunctionTable
|
|
|
1711
1740
|
// `from` relations
|
|
1712
1741
|
// https://stackoverflow.com/a/54732547
|
|
1713
1742
|
const getRelationsManyToOne = (0, memoize_1.default)(async function getRelationsManyToOne(table) {
|
|
1743
|
+
getCtx().log.debug({ table }, "getRelationsManyToOne() start");
|
|
1714
1744
|
const { dialect, query } = getCtx();
|
|
1715
1745
|
const tableMeta = await getTableMeta(table);
|
|
1716
1746
|
let rs;
|
|
@@ -1756,6 +1786,7 @@ const getRelationsManyToOne = (0, memoize_1.default)(async function getRelations
|
|
|
1756
1786
|
}, (table) => getCtx().runId + ":" + table);
|
|
1757
1787
|
// `to` relations
|
|
1758
1788
|
const getRelationsOneToMany = (0, memoize_1.default)(async function getRelationsOneToMany(table) {
|
|
1789
|
+
getCtx().log.debug({ table }, "getRelationsOneToMany() start");
|
|
1759
1790
|
const { dialect, query } = getCtx();
|
|
1760
1791
|
let rs;
|
|
1761
1792
|
if (dialect === "mysql") {
|
|
@@ -1799,6 +1830,7 @@ const getRelationsOneToMany = (0, memoize_1.default)(async function getRelations
|
|
|
1799
1830
|
return _.sortBy([(x) => x.referencedTable, (x) => x.referencedKey, (x) => x.foreignKey], xs);
|
|
1800
1831
|
}, (table) => getCtx().runId + ":" + table);
|
|
1801
1832
|
async function getPrimaryColumn(table) {
|
|
1833
|
+
getCtx().log.debug({ table }, "getPrimaryColumn() start");
|
|
1802
1834
|
const tableMeta = await getTableMeta(table);
|
|
1803
1835
|
const columns = tableMeta.filter((x) => x.Key === "PRI");
|
|
1804
1836
|
if (columns.length !== 1) {
|