@zenstackhq/runtime 3.0.0-alpha.32 → 3.0.0-alpha.33
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/{contract-CToGslMD.d.cts → contract-CusA0mQO.d.cts} +3 -1
- package/dist/{contract-CToGslMD.d.ts → contract-CusA0mQO.d.ts} +3 -1
- package/dist/index.cjs +259 -185
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +259 -185
- package/dist/index.js.map +1 -1
- package/dist/plugins/policy/index.cjs +109 -70
- package/dist/plugins/policy/index.cjs.map +1 -1
- package/dist/plugins/policy/index.d.cts +1 -1
- package/dist/plugins/policy/index.d.ts +1 -1
- package/dist/plugins/policy/index.js +109 -70
- package/dist/plugins/policy/index.js.map +1 -1
- package/dist/schema.cjs +4 -1
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +1 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.js +4 -1
- package/dist/schema.js.map +1 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -128,7 +128,10 @@ var ExpressionUtils = {
|
|
|
128
128
|
isUnary: /* @__PURE__ */ __name((value) => ExpressionUtils.is(value, "unary"), "isUnary"),
|
|
129
129
|
isBinary: /* @__PURE__ */ __name((value) => ExpressionUtils.is(value, "binary"), "isBinary"),
|
|
130
130
|
isField: /* @__PURE__ */ __name((value) => ExpressionUtils.is(value, "field"), "isField"),
|
|
131
|
-
isMember: /* @__PURE__ */ __name((value) => ExpressionUtils.is(value, "member"), "isMember")
|
|
131
|
+
isMember: /* @__PURE__ */ __name((value) => ExpressionUtils.is(value, "member"), "isMember"),
|
|
132
|
+
getLiteralValue: /* @__PURE__ */ __name((expr2) => {
|
|
133
|
+
return ExpressionUtils.isLiteral(expr2) ? expr2.value : void 0;
|
|
134
|
+
}, "getLiteralValue")
|
|
132
135
|
};
|
|
133
136
|
|
|
134
137
|
// src/utils/object-utils.ts
|
|
@@ -1052,6 +1055,16 @@ var BaseCrudDialect = class {
|
|
|
1052
1055
|
}
|
|
1053
1056
|
return result;
|
|
1054
1057
|
}
|
|
1058
|
+
buildModelSelect(eb, model, subQueryAlias, payload, selectAllFields) {
|
|
1059
|
+
let subQuery = this.buildSelectModel(eb, model, subQueryAlias);
|
|
1060
|
+
if (selectAllFields) {
|
|
1061
|
+
subQuery = this.buildSelectAllFields(model, subQuery, typeof payload === "object" ? payload?.omit : void 0, subQueryAlias);
|
|
1062
|
+
}
|
|
1063
|
+
if (payload && typeof payload === "object") {
|
|
1064
|
+
subQuery = this.buildFilterSortTake(model, payload, subQuery, subQueryAlias);
|
|
1065
|
+
}
|
|
1066
|
+
return subQuery;
|
|
1067
|
+
}
|
|
1055
1068
|
buildSelectField(query, model, modelAlias, field) {
|
|
1056
1069
|
const fieldDef = requireField(this.schema, model, field);
|
|
1057
1070
|
if (fieldDef.computed) {
|
|
@@ -1149,6 +1162,18 @@ var BaseCrudDialect = class {
|
|
|
1149
1162
|
fieldRef(model, field, eb, modelAlias, inlineComputedField = true) {
|
|
1150
1163
|
return buildFieldRef(this.schema, model, field, this.options, eb, modelAlias, inlineComputedField);
|
|
1151
1164
|
}
|
|
1165
|
+
canJoinWithoutNestedSelect(modelDef, payload) {
|
|
1166
|
+
if (modelDef.computedFields) {
|
|
1167
|
+
return false;
|
|
1168
|
+
}
|
|
1169
|
+
if (modelDef.baseModel || modelDef.isDelegate) {
|
|
1170
|
+
return false;
|
|
1171
|
+
}
|
|
1172
|
+
if (typeof payload === "object" && (payload.orderBy || payload.skip !== void 0 || payload.take !== void 0 || payload.cursor || payload.distinct)) {
|
|
1173
|
+
return false;
|
|
1174
|
+
}
|
|
1175
|
+
return true;
|
|
1176
|
+
}
|
|
1152
1177
|
};
|
|
1153
1178
|
|
|
1154
1179
|
// src/client/crud/dialects/postgresql.ts
|
|
@@ -1174,52 +1199,58 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1174
1199
|
}
|
|
1175
1200
|
}
|
|
1176
1201
|
buildRelationSelection(query, model, relationField, parentAlias, payload) {
|
|
1177
|
-
const
|
|
1178
|
-
|
|
1202
|
+
const relationResultName = `${parentAlias}$${relationField}`;
|
|
1203
|
+
const joinedQuery = this.buildRelationJSON(model, query, relationField, parentAlias, payload, relationResultName);
|
|
1204
|
+
return joinedQuery.select(`${relationResultName}.$data as ${relationField}`);
|
|
1179
1205
|
}
|
|
1180
|
-
buildRelationJSON(model, qb, relationField,
|
|
1206
|
+
buildRelationJSON(model, qb, relationField, parentAlias, payload, resultName) {
|
|
1181
1207
|
const relationFieldDef = requireField(this.schema, model, relationField);
|
|
1182
1208
|
const relationModel = relationFieldDef.type;
|
|
1183
1209
|
return qb.leftJoinLateral((eb) => {
|
|
1184
|
-
const
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
} else {
|
|
1201
|
-
const joinPairs = buildJoinPairs(this.schema, model, parentName, relationField, subQueryAlias);
|
|
1202
|
-
subQuery = subQuery.where((eb2) => this.and(eb2, ...joinPairs.map(([left, right]) => eb2(import_kysely2.sql.ref(left), "=", import_kysely2.sql.ref(right)))));
|
|
1203
|
-
}
|
|
1204
|
-
return subQuery.as(joinTableName);
|
|
1205
|
-
});
|
|
1206
|
-
result = this.buildRelationObjectSelect(relationModel, joinTableName, relationField, relationFieldDef, result, payload, parentName);
|
|
1207
|
-
result = this.buildRelationJoins(relationModel, relationField, result, payload, parentName);
|
|
1208
|
-
return result.as(joinTableName);
|
|
1210
|
+
const relationSelectName = `${resultName}$sub`;
|
|
1211
|
+
const relationModelDef = requireModel(this.schema, relationModel);
|
|
1212
|
+
let tbl;
|
|
1213
|
+
if (this.canJoinWithoutNestedSelect(relationModelDef, payload)) {
|
|
1214
|
+
tbl = this.buildModelSelect(eb, relationModel, relationSelectName, payload, false);
|
|
1215
|
+
tbl = this.buildRelationJoinFilter(tbl, model, relationField, relationModel, relationSelectName, parentAlias);
|
|
1216
|
+
} else {
|
|
1217
|
+
tbl = eb.selectFrom(() => {
|
|
1218
|
+
let subQuery = this.buildModelSelect(eb, relationModel, `${relationSelectName}$t`, payload, true);
|
|
1219
|
+
subQuery = this.buildRelationJoinFilter(subQuery, model, relationField, relationModel, `${relationSelectName}$t`, parentAlias);
|
|
1220
|
+
return subQuery.as(relationSelectName);
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
tbl = this.buildRelationObjectSelect(relationModel, relationSelectName, relationFieldDef, tbl, payload, resultName);
|
|
1224
|
+
tbl = this.buildRelationJoins(tbl, relationModel, relationSelectName, payload, resultName);
|
|
1225
|
+
return tbl.as(resultName);
|
|
1209
1226
|
}, (join) => join.onTrue());
|
|
1210
1227
|
}
|
|
1211
|
-
|
|
1228
|
+
buildRelationJoinFilter(query, model, relationField, relationModel, relationModelAlias, parentAlias) {
|
|
1229
|
+
const m2m = getManyToManyRelation(this.schema, model, relationField);
|
|
1230
|
+
if (m2m) {
|
|
1231
|
+
const parentIds = getIdFields(this.schema, model);
|
|
1232
|
+
const relationIds = getIdFields(this.schema, relationModel);
|
|
1233
|
+
(0, import_common_helpers2.invariant)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1234
|
+
(0, import_common_helpers2.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1235
|
+
query = query.where((eb) => eb(eb.ref(`${relationModelAlias}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentAlias}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
|
|
1236
|
+
} else {
|
|
1237
|
+
const joinPairs = buildJoinPairs(this.schema, model, parentAlias, relationField, relationModelAlias);
|
|
1238
|
+
query = query.where((eb) => this.and(eb, ...joinPairs.map(([left, right]) => eb(import_kysely2.sql.ref(left), "=", import_kysely2.sql.ref(right)))));
|
|
1239
|
+
}
|
|
1240
|
+
return query;
|
|
1241
|
+
}
|
|
1242
|
+
buildRelationObjectSelect(relationModel, relationModelAlias, relationFieldDef, qb, payload, parentResultName) {
|
|
1212
1243
|
qb = qb.select((eb) => {
|
|
1213
|
-
const objArgs = this.buildRelationObjectArgs(relationModel, relationModelAlias,
|
|
1244
|
+
const objArgs = this.buildRelationObjectArgs(relationModel, relationModelAlias, eb, payload, parentResultName);
|
|
1214
1245
|
if (relationFieldDef.array) {
|
|
1215
|
-
return eb.fn.coalesce(import_kysely2.sql`jsonb_agg(jsonb_build_object(${import_kysely2.sql.join(objArgs)}))`, import_kysely2.sql`'[]'::jsonb`).as("$
|
|
1246
|
+
return eb.fn.coalesce(import_kysely2.sql`jsonb_agg(jsonb_build_object(${import_kysely2.sql.join(objArgs)}))`, import_kysely2.sql`'[]'::jsonb`).as("$data");
|
|
1216
1247
|
} else {
|
|
1217
|
-
return import_kysely2.sql`jsonb_build_object(${import_kysely2.sql.join(objArgs)})`.as("$
|
|
1248
|
+
return import_kysely2.sql`jsonb_build_object(${import_kysely2.sql.join(objArgs)})`.as("$data");
|
|
1218
1249
|
}
|
|
1219
1250
|
});
|
|
1220
1251
|
return qb;
|
|
1221
1252
|
}
|
|
1222
|
-
buildRelationObjectArgs(relationModel, relationModelAlias,
|
|
1253
|
+
buildRelationObjectArgs(relationModel, relationModelAlias, eb, payload, parentResultName) {
|
|
1223
1254
|
const relationModelDef = requireModel(this.schema, relationModel);
|
|
1224
1255
|
const objArgs = [];
|
|
1225
1256
|
const descendantModels = getDelegateDescendantModels(this.schema, relationModel);
|
|
@@ -1237,14 +1268,14 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1237
1268
|
} else if (payload.select) {
|
|
1238
1269
|
objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
|
|
1239
1270
|
if (field === "_count") {
|
|
1240
|
-
const subJson = this.buildCountJson(relationModel, eb,
|
|
1271
|
+
const subJson = this.buildCountJson(relationModel, eb, relationModelAlias, value);
|
|
1241
1272
|
return [
|
|
1242
1273
|
import_kysely2.sql.lit(field),
|
|
1243
1274
|
subJson
|
|
1244
1275
|
];
|
|
1245
1276
|
} else {
|
|
1246
1277
|
const fieldDef = requireField(this.schema, relationModel, field);
|
|
1247
|
-
const fieldValue = fieldDef.relation ? eb.ref(`${
|
|
1278
|
+
const fieldValue = fieldDef.relation ? eb.ref(`${parentResultName}$${field}.$data`) : this.fieldRef(relationModel, field, eb, relationModelAlias, false);
|
|
1248
1279
|
return [
|
|
1249
1280
|
import_kysely2.sql.lit(field),
|
|
1250
1281
|
fieldValue
|
|
@@ -1256,18 +1287,18 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1256
1287
|
objArgs.push(...Object.entries(payload.include).filter(([, value]) => value).map(([field]) => [
|
|
1257
1288
|
import_kysely2.sql.lit(field),
|
|
1258
1289
|
// reference the synthesized JSON field
|
|
1259
|
-
eb.ref(`${
|
|
1290
|
+
eb.ref(`${parentResultName}$${field}.$data`)
|
|
1260
1291
|
]).flatMap((v) => v));
|
|
1261
1292
|
}
|
|
1262
1293
|
return objArgs;
|
|
1263
1294
|
}
|
|
1264
|
-
buildRelationJoins(
|
|
1265
|
-
let result =
|
|
1295
|
+
buildRelationJoins(query, relationModel, relationModelAlias, payload, parentResultName) {
|
|
1296
|
+
let result = query;
|
|
1266
1297
|
if (typeof payload === "object") {
|
|
1267
1298
|
const selectInclude = payload.include ?? payload.select;
|
|
1268
1299
|
if (selectInclude && typeof selectInclude === "object") {
|
|
1269
1300
|
Object.entries(selectInclude).filter(([, value]) => value).filter(([field]) => isRelationField(this.schema, relationModel, field)).forEach(([field, value]) => {
|
|
1270
|
-
result = this.buildRelationJSON(relationModel, result, field, `${
|
|
1301
|
+
result = this.buildRelationJSON(relationModel, result, field, relationModelAlias, value, `${parentResultName}$${field}`);
|
|
1271
1302
|
});
|
|
1272
1303
|
}
|
|
1273
1304
|
}
|
|
@@ -1347,32 +1378,18 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1347
1378
|
const relationModel = relationFieldDef.type;
|
|
1348
1379
|
const relationModelDef = requireModel(this.schema, relationModel);
|
|
1349
1380
|
const subQueryName = `${parentAlias}$${relationField}`;
|
|
1350
|
-
let tbl
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
(0, import_common_helpers3.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1363
|
-
subQuery = subQuery.where(eb(eb.ref(`${subQueryAlias}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentAlias}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
|
|
1364
|
-
} else {
|
|
1365
|
-
const { keyPairs, ownedByModel } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
|
|
1366
|
-
keyPairs.forEach(({ fk, pk }) => {
|
|
1367
|
-
if (ownedByModel) {
|
|
1368
|
-
subQuery = subQuery.whereRef(`${subQueryAlias}.${pk}`, "=", `${parentAlias}.${fk}`);
|
|
1369
|
-
} else {
|
|
1370
|
-
subQuery = subQuery.whereRef(`${subQueryAlias}.${fk}`, "=", `${parentAlias}.${pk}`);
|
|
1371
|
-
}
|
|
1372
|
-
});
|
|
1373
|
-
}
|
|
1374
|
-
return subQuery.as(subQueryName);
|
|
1375
|
-
});
|
|
1381
|
+
let tbl;
|
|
1382
|
+
if (this.canJoinWithoutNestedSelect(relationModelDef, payload)) {
|
|
1383
|
+
tbl = this.buildModelSelect(eb, relationModel, subQueryName, payload, false);
|
|
1384
|
+
tbl = this.buildRelationJoinFilter(tbl, model, relationField, subQueryName, parentAlias);
|
|
1385
|
+
} else {
|
|
1386
|
+
tbl = eb.selectFrom(() => {
|
|
1387
|
+
const selectModelAlias = `${parentAlias}$${relationField}$sub`;
|
|
1388
|
+
let selectModelQuery = this.buildModelSelect(eb, relationModel, selectModelAlias, payload, true);
|
|
1389
|
+
selectModelQuery = this.buildRelationJoinFilter(selectModelQuery, model, relationField, selectModelAlias, parentAlias);
|
|
1390
|
+
return selectModelQuery.as(subQueryName);
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1376
1393
|
tbl = tbl.select(() => {
|
|
1377
1394
|
const objArgs = [];
|
|
1378
1395
|
const descendantModels = getDelegateDescendantModels(this.schema, relationModel);
|
|
@@ -1385,7 +1402,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1385
1402
|
if (payload === true || !payload.select) {
|
|
1386
1403
|
objArgs.push(...Object.entries(relationModelDef.fields).filter(([, value]) => !value.relation).filter(([name]) => !(typeof payload === "object" && payload.omit?.[name] === true)).map(([field]) => [
|
|
1387
1404
|
import_kysely3.sql.lit(field),
|
|
1388
|
-
this.fieldRef(relationModel, field, eb,
|
|
1405
|
+
this.fieldRef(relationModel, field, eb, subQueryName, false)
|
|
1389
1406
|
]).flatMap((v) => v));
|
|
1390
1407
|
} else if (payload.select) {
|
|
1391
1408
|
objArgs.push(...Object.entries(payload.select).filter(([, value]) => value).map(([field, value]) => {
|
|
@@ -1406,7 +1423,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1406
1423
|
} else {
|
|
1407
1424
|
return [
|
|
1408
1425
|
import_kysely3.sql.lit(field),
|
|
1409
|
-
this.fieldRef(relationModel, field, eb,
|
|
1426
|
+
this.fieldRef(relationModel, field, eb, subQueryName, false)
|
|
1410
1427
|
];
|
|
1411
1428
|
}
|
|
1412
1429
|
}
|
|
@@ -1422,13 +1439,35 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1422
1439
|
}).flatMap((v) => v));
|
|
1423
1440
|
}
|
|
1424
1441
|
if (relationFieldDef.array) {
|
|
1425
|
-
return eb.fn.coalesce(import_kysely3.sql`json_group_array(json_object(${import_kysely3.sql.join(objArgs)}))`, import_kysely3.sql`json_array()`).as("$
|
|
1442
|
+
return eb.fn.coalesce(import_kysely3.sql`json_group_array(json_object(${import_kysely3.sql.join(objArgs)}))`, import_kysely3.sql`json_array()`).as("$data");
|
|
1426
1443
|
} else {
|
|
1427
|
-
return import_kysely3.sql`json_object(${import_kysely3.sql.join(objArgs)})`.as("data");
|
|
1444
|
+
return import_kysely3.sql`json_object(${import_kysely3.sql.join(objArgs)})`.as("$data");
|
|
1428
1445
|
}
|
|
1429
1446
|
});
|
|
1430
1447
|
return tbl;
|
|
1431
1448
|
}
|
|
1449
|
+
buildRelationJoinFilter(selectModelQuery, model, relationField, relationModelAlias, parentAlias) {
|
|
1450
|
+
const fieldDef = requireField(this.schema, model, relationField);
|
|
1451
|
+
const relationModel = fieldDef.type;
|
|
1452
|
+
const m2m = getManyToManyRelation(this.schema, model, relationField);
|
|
1453
|
+
if (m2m) {
|
|
1454
|
+
const parentIds = getIdFields(this.schema, model);
|
|
1455
|
+
const relationIds = getIdFields(this.schema, relationModel);
|
|
1456
|
+
(0, import_common_helpers3.invariant)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1457
|
+
(0, import_common_helpers3.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1458
|
+
selectModelQuery = selectModelQuery.where((eb) => eb(eb.ref(`${relationModelAlias}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentAlias}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
|
|
1459
|
+
} else {
|
|
1460
|
+
const { keyPairs, ownedByModel } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
|
|
1461
|
+
keyPairs.forEach(({ fk, pk }) => {
|
|
1462
|
+
if (ownedByModel) {
|
|
1463
|
+
selectModelQuery = selectModelQuery.whereRef(`${relationModelAlias}.${pk}`, "=", `${parentAlias}.${fk}`);
|
|
1464
|
+
} else {
|
|
1465
|
+
selectModelQuery = selectModelQuery.whereRef(`${relationModelAlias}.${fk}`, "=", `${parentAlias}.${pk}`);
|
|
1466
|
+
}
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
return selectModelQuery;
|
|
1470
|
+
}
|
|
1432
1471
|
buildSkipTake(query, skip, take) {
|
|
1433
1472
|
if (take !== void 0) {
|
|
1434
1473
|
query = query.limit(take);
|
|
@@ -5709,7 +5748,7 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5709
5748
|
schema;
|
|
5710
5749
|
modelToTableMap = /* @__PURE__ */ new Map();
|
|
5711
5750
|
fieldToColumnMap = /* @__PURE__ */ new Map();
|
|
5712
|
-
|
|
5751
|
+
scopes = [];
|
|
5713
5752
|
constructor(schema) {
|
|
5714
5753
|
super(), this.schema = schema;
|
|
5715
5754
|
for (const [modelName, modelDef] of Object.entries(schema.models)) {
|
|
@@ -5730,12 +5769,23 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5730
5769
|
if (!node.from?.froms) {
|
|
5731
5770
|
return super.transformSelectQuery(node);
|
|
5732
5771
|
}
|
|
5733
|
-
const
|
|
5772
|
+
const processedFroms = node.from.froms.map((from) => this.processSelectTable(from));
|
|
5773
|
+
const processedJoins = (node.joins ?? []).map((join) => this.processSelectTable(join.table));
|
|
5774
|
+
const scopes = [
|
|
5775
|
+
...processedFroms.map(({ scope }) => scope),
|
|
5776
|
+
...processedJoins.map(({ scope }) => scope)
|
|
5777
|
+
];
|
|
5734
5778
|
return this.withScopes(scopes, () => {
|
|
5779
|
+
const joins = node.joins ? node.joins.map((join, i) => ({
|
|
5780
|
+
...join,
|
|
5781
|
+
table: processedJoins[i].node,
|
|
5782
|
+
on: this.transformNode(join.on)
|
|
5783
|
+
})) : void 0;
|
|
5735
5784
|
return {
|
|
5736
5785
|
...super.transformSelectQuery(node),
|
|
5737
|
-
|
|
5738
|
-
|
|
5786
|
+
from: import_kysely13.FromNode.create(processedFroms.map((f) => f.node)),
|
|
5787
|
+
joins,
|
|
5788
|
+
selections: this.processSelectQuerySelections(node)
|
|
5739
5789
|
};
|
|
5740
5790
|
});
|
|
5741
5791
|
}
|
|
@@ -5758,27 +5808,13 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5758
5808
|
selections: this.processSelections(node.selections)
|
|
5759
5809
|
};
|
|
5760
5810
|
}
|
|
5761
|
-
transformJoin(node) {
|
|
5762
|
-
const { alias, node: innerNode } = stripAlias(node.table);
|
|
5763
|
-
if (import_kysely13.TableNode.is(innerNode)) {
|
|
5764
|
-
const modelName = innerNode.table.identifier.name;
|
|
5765
|
-
if (this.hasMappedColumns(modelName)) {
|
|
5766
|
-
const select = this.createSelectAll(modelName);
|
|
5767
|
-
return {
|
|
5768
|
-
...super.transformJoin(node),
|
|
5769
|
-
table: this.wrapAlias(select, alias ?? modelName)
|
|
5770
|
-
};
|
|
5771
|
-
}
|
|
5772
|
-
}
|
|
5773
|
-
return super.transformJoin(node);
|
|
5774
|
-
}
|
|
5775
5811
|
transformReference(node) {
|
|
5776
5812
|
if (!import_kysely13.ColumnNode.is(node.column)) {
|
|
5777
5813
|
return super.transformReference(node);
|
|
5778
5814
|
}
|
|
5779
|
-
const
|
|
5780
|
-
if (
|
|
5781
|
-
const mappedFieldName = this.mapFieldName(
|
|
5815
|
+
const scope = this.resolveFieldFromScopes(node.column.column.name, node.table?.table.identifier.name);
|
|
5816
|
+
if (scope && !scope.namesMapped && scope.model) {
|
|
5817
|
+
const mappedFieldName = this.mapFieldName(scope.model, node.column.column.name);
|
|
5782
5818
|
let mappedTableName = node.table?.table.identifier.name;
|
|
5783
5819
|
if (mappedTableName) {
|
|
5784
5820
|
if (scope.alias === mappedTableName) {
|
|
@@ -5792,11 +5828,11 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5792
5828
|
}
|
|
5793
5829
|
}
|
|
5794
5830
|
transformColumn(node) {
|
|
5795
|
-
const
|
|
5796
|
-
if (!
|
|
5831
|
+
const scope = this.resolveFieldFromScopes(node.column.name);
|
|
5832
|
+
if (!scope || scope.namesMapped || !scope.model) {
|
|
5797
5833
|
return super.transformColumn(node);
|
|
5798
5834
|
}
|
|
5799
|
-
const mappedName = this.mapFieldName(
|
|
5835
|
+
const mappedName = this.mapFieldName(scope.model, node.column.name);
|
|
5800
5836
|
return import_kysely13.ColumnNode.create(mappedName);
|
|
5801
5837
|
}
|
|
5802
5838
|
transformUpdateQuery(node) {
|
|
@@ -5819,7 +5855,14 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5819
5855
|
});
|
|
5820
5856
|
}
|
|
5821
5857
|
transformDeleteQuery(node) {
|
|
5822
|
-
const scopes =
|
|
5858
|
+
const scopes = node.from.froms.map((node2) => {
|
|
5859
|
+
const { alias, node: innerNode } = stripAlias(node2);
|
|
5860
|
+
return {
|
|
5861
|
+
model: this.extractModelName(innerNode),
|
|
5862
|
+
alias,
|
|
5863
|
+
namesMapped: false
|
|
5864
|
+
};
|
|
5865
|
+
});
|
|
5823
5866
|
const froms = node.from.froms.map((from) => {
|
|
5824
5867
|
const { alias, node: innerNode } = stripAlias(from);
|
|
5825
5868
|
if (import_kysely13.TableNode.is(innerNode)) {
|
|
@@ -5837,46 +5880,75 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5837
5880
|
}
|
|
5838
5881
|
// #endregion
|
|
5839
5882
|
// #region utils
|
|
5883
|
+
processSelectQuerySelections(node) {
|
|
5884
|
+
const selections = [];
|
|
5885
|
+
for (const selection of node.selections ?? []) {
|
|
5886
|
+
if (import_kysely13.SelectAllNode.is(selection.selection)) {
|
|
5887
|
+
const scope = this.scopes[this.scopes.length - 1];
|
|
5888
|
+
if (scope?.model && !scope.namesMapped) {
|
|
5889
|
+
selections.push(...this.createSelectAllFields(scope.model, scope.alias));
|
|
5890
|
+
} else {
|
|
5891
|
+
selections.push(super.transformSelection(selection));
|
|
5892
|
+
}
|
|
5893
|
+
} else if (import_kysely13.ReferenceNode.is(selection.selection) || import_kysely13.ColumnNode.is(selection.selection)) {
|
|
5894
|
+
const transformed = this.transformNode(selection.selection);
|
|
5895
|
+
if (import_kysely13.AliasNode.is(transformed)) {
|
|
5896
|
+
selections.push(import_kysely13.SelectionNode.create(transformed));
|
|
5897
|
+
} else {
|
|
5898
|
+
const origFieldName = this.extractFieldName(selection.selection);
|
|
5899
|
+
const fieldName = this.extractFieldName(transformed);
|
|
5900
|
+
if (fieldName !== origFieldName) {
|
|
5901
|
+
selections.push(import_kysely13.SelectionNode.create(this.wrapAlias(transformed, origFieldName)));
|
|
5902
|
+
} else {
|
|
5903
|
+
selections.push(import_kysely13.SelectionNode.create(transformed));
|
|
5904
|
+
}
|
|
5905
|
+
}
|
|
5906
|
+
} else {
|
|
5907
|
+
selections.push(super.transformSelection(selection));
|
|
5908
|
+
}
|
|
5909
|
+
}
|
|
5910
|
+
return selections;
|
|
5911
|
+
}
|
|
5840
5912
|
resolveFieldFromScopes(name, qualifier) {
|
|
5841
|
-
for (
|
|
5913
|
+
for (let i = this.scopes.length - 1; i >= 0; i--) {
|
|
5914
|
+
const scope = this.scopes[i];
|
|
5842
5915
|
if (qualifier) {
|
|
5843
5916
|
if (scope.alias) {
|
|
5844
|
-
if (
|
|
5917
|
+
if (scope.alias === qualifier) {
|
|
5918
|
+
return scope;
|
|
5919
|
+
} else {
|
|
5845
5920
|
continue;
|
|
5846
5921
|
}
|
|
5847
|
-
} else {
|
|
5848
|
-
if (
|
|
5922
|
+
} else if (scope.model) {
|
|
5923
|
+
if (scope.model === qualifier) {
|
|
5924
|
+
return scope;
|
|
5925
|
+
} else {
|
|
5849
5926
|
continue;
|
|
5850
5927
|
}
|
|
5851
5928
|
}
|
|
5852
|
-
}
|
|
5853
|
-
|
|
5854
|
-
|
|
5855
|
-
|
|
5856
|
-
|
|
5857
|
-
|
|
5858
|
-
|
|
5859
|
-
|
|
5860
|
-
|
|
5861
|
-
|
|
5862
|
-
};
|
|
5929
|
+
} else {
|
|
5930
|
+
if (scope.model) {
|
|
5931
|
+
const modelDef = getModel(this.schema, scope.model);
|
|
5932
|
+
if (!modelDef) {
|
|
5933
|
+
continue;
|
|
5934
|
+
}
|
|
5935
|
+
if (modelDef.fields[name]) {
|
|
5936
|
+
return scope;
|
|
5937
|
+
}
|
|
5938
|
+
}
|
|
5863
5939
|
}
|
|
5864
5940
|
}
|
|
5865
|
-
return
|
|
5866
|
-
modelDef: void 0,
|
|
5867
|
-
fieldDef: void 0,
|
|
5868
|
-
scope: void 0
|
|
5869
|
-
};
|
|
5941
|
+
return void 0;
|
|
5870
5942
|
}
|
|
5871
5943
|
pushScope(scope) {
|
|
5872
|
-
this.
|
|
5944
|
+
this.scopes.push(scope);
|
|
5873
5945
|
}
|
|
5874
5946
|
withScope(scope, fn) {
|
|
5875
5947
|
this.pushScope(scope);
|
|
5876
5948
|
try {
|
|
5877
5949
|
return fn();
|
|
5878
5950
|
} finally {
|
|
5879
|
-
this.
|
|
5951
|
+
this.scopes.pop();
|
|
5880
5952
|
}
|
|
5881
5953
|
}
|
|
5882
5954
|
withScopes(scopes, fn) {
|
|
@@ -5884,18 +5956,12 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5884
5956
|
try {
|
|
5885
5957
|
return fn();
|
|
5886
5958
|
} finally {
|
|
5887
|
-
scopes.forEach(() => this.
|
|
5959
|
+
scopes.forEach(() => this.scopes.pop());
|
|
5888
5960
|
}
|
|
5889
5961
|
}
|
|
5890
5962
|
wrapAlias(node, alias) {
|
|
5891
5963
|
return alias ? import_kysely13.AliasNode.create(node, import_kysely13.IdentifierNode.create(alias)) : node;
|
|
5892
5964
|
}
|
|
5893
|
-
ensureAlias(node, alias, fallbackName) {
|
|
5894
|
-
if (!node) {
|
|
5895
|
-
return node;
|
|
5896
|
-
}
|
|
5897
|
-
return alias ? import_kysely13.AliasNode.create(node, import_kysely13.IdentifierNode.create(alias)) : import_kysely13.AliasNode.create(node, import_kysely13.IdentifierNode.create(fallbackName));
|
|
5898
|
-
}
|
|
5899
5965
|
processTableRef(node) {
|
|
5900
5966
|
if (!node) {
|
|
5901
5967
|
return node;
|
|
@@ -5936,62 +6002,44 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
5936
6002
|
...this.fieldToColumnMap.keys()
|
|
5937
6003
|
].some((key) => key.startsWith(modelName + "."));
|
|
5938
6004
|
}
|
|
5939
|
-
createScopesFromFroms(node, namesMapped) {
|
|
5940
|
-
if (!node) {
|
|
5941
|
-
return [];
|
|
5942
|
-
}
|
|
5943
|
-
return node.froms.map((from) => {
|
|
5944
|
-
const { alias, node: innerNode } = stripAlias(from);
|
|
5945
|
-
if (innerNode && import_kysely13.TableNode.is(innerNode)) {
|
|
5946
|
-
return {
|
|
5947
|
-
model: innerNode.table.identifier.name,
|
|
5948
|
-
alias,
|
|
5949
|
-
namesMapped
|
|
5950
|
-
};
|
|
5951
|
-
} else {
|
|
5952
|
-
return void 0;
|
|
5953
|
-
}
|
|
5954
|
-
}).filter((s) => !!s);
|
|
5955
|
-
}
|
|
5956
6005
|
// convert a "from" node to a nested query if there are columns with name mapping
|
|
5957
|
-
|
|
5958
|
-
|
|
5959
|
-
|
|
5960
|
-
|
|
5961
|
-
|
|
5962
|
-
|
|
5963
|
-
|
|
5964
|
-
|
|
5965
|
-
|
|
5966
|
-
|
|
5967
|
-
|
|
5968
|
-
|
|
5969
|
-
}
|
|
6006
|
+
processSelectTable(node) {
|
|
6007
|
+
const { alias, node: innerNode } = stripAlias(node);
|
|
6008
|
+
if (innerNode && import_kysely13.TableNode.is(innerNode)) {
|
|
6009
|
+
const modelName = innerNode.table.identifier.name;
|
|
6010
|
+
const mappedName = this.mapTableName(modelName);
|
|
6011
|
+
const finalAlias = alias ?? (mappedName !== modelName ? modelName : void 0);
|
|
6012
|
+
return {
|
|
6013
|
+
node: this.wrapAlias(import_kysely13.TableNode.create(mappedName), finalAlias),
|
|
6014
|
+
scope: {
|
|
6015
|
+
alias: alias ?? modelName,
|
|
6016
|
+
model: modelName,
|
|
6017
|
+
namesMapped: !this.hasMappedColumns(modelName)
|
|
5970
6018
|
}
|
|
5971
|
-
|
|
5972
|
-
|
|
5973
|
-
|
|
6019
|
+
};
|
|
6020
|
+
} else {
|
|
6021
|
+
return {
|
|
6022
|
+
node: super.transformNode(node),
|
|
6023
|
+
scope: {
|
|
6024
|
+
alias,
|
|
6025
|
+
model: void 0,
|
|
6026
|
+
namesMapped: true
|
|
6027
|
+
}
|
|
6028
|
+
};
|
|
6029
|
+
}
|
|
5974
6030
|
}
|
|
5975
|
-
|
|
5976
|
-
createSelectAll(model) {
|
|
6031
|
+
createSelectAllFields(model, alias) {
|
|
5977
6032
|
const modelDef = requireModel(this.schema, model);
|
|
5978
|
-
|
|
5979
|
-
|
|
5980
|
-
|
|
5981
|
-
|
|
5982
|
-
import_kysely13.
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
const aliased = import_kysely13.AliasNode.create(columnRef, import_kysely13.IdentifierNode.create(fieldDef.name));
|
|
5989
|
-
return import_kysely13.SelectionNode.create(aliased);
|
|
5990
|
-
} else {
|
|
5991
|
-
return import_kysely13.SelectionNode.create(columnRef);
|
|
5992
|
-
}
|
|
5993
|
-
})
|
|
5994
|
-
};
|
|
6033
|
+
return this.getModelFields(modelDef).map((fieldDef) => {
|
|
6034
|
+
const columnName = this.mapFieldName(model, fieldDef.name);
|
|
6035
|
+
const columnRef = import_kysely13.ReferenceNode.create(import_kysely13.ColumnNode.create(columnName), alias ? import_kysely13.TableNode.create(alias) : void 0);
|
|
6036
|
+
if (columnName !== fieldDef.name) {
|
|
6037
|
+
const aliased = import_kysely13.AliasNode.create(columnRef, import_kysely13.IdentifierNode.create(fieldDef.name));
|
|
6038
|
+
return import_kysely13.SelectionNode.create(aliased);
|
|
6039
|
+
} else {
|
|
6040
|
+
return import_kysely13.SelectionNode.create(columnRef);
|
|
6041
|
+
}
|
|
6042
|
+
});
|
|
5995
6043
|
}
|
|
5996
6044
|
getModelFields(modelDef) {
|
|
5997
6045
|
return Object.values(modelDef.fields).filter((f) => !f.relation && !f.computed && !f.originModel);
|
|
@@ -6021,18 +6069,22 @@ var QueryNameMapper = class extends import_kysely13.OperationNodeTransformer {
|
|
|
6021
6069
|
return this.wrapAlias(result, alias);
|
|
6022
6070
|
}
|
|
6023
6071
|
processSelectAll(node) {
|
|
6024
|
-
const scope = this.
|
|
6072
|
+
const scope = this.scopes[this.scopes.length - 1];
|
|
6025
6073
|
(0, import_common_helpers11.invariant)(scope);
|
|
6026
|
-
if (!this.hasMappedColumns(scope.model)) {
|
|
6074
|
+
if (!scope.model || !this.hasMappedColumns(scope.model)) {
|
|
6027
6075
|
return super.transformSelectAll(node);
|
|
6028
6076
|
}
|
|
6029
6077
|
const modelDef = requireModel(this.schema, scope.model);
|
|
6030
6078
|
return this.getModelFields(modelDef).map((fieldDef) => {
|
|
6031
|
-
const columnName = this.mapFieldName(
|
|
6079
|
+
const columnName = this.mapFieldName(modelDef.name, fieldDef.name);
|
|
6032
6080
|
const columnRef = import_kysely13.ReferenceNode.create(import_kysely13.ColumnNode.create(columnName));
|
|
6033
6081
|
return columnName !== fieldDef.name ? this.wrapAlias(columnRef, fieldDef.name) : columnRef;
|
|
6034
6082
|
});
|
|
6035
6083
|
}
|
|
6084
|
+
extractModelName(node) {
|
|
6085
|
+
const { node: innerNode } = stripAlias(node);
|
|
6086
|
+
return import_kysely13.TableNode.is(innerNode) ? innerNode.table.identifier.name : void 0;
|
|
6087
|
+
}
|
|
6036
6088
|
extractFieldName(node) {
|
|
6037
6089
|
if (import_kysely13.ReferenceNode.is(node) && import_kysely13.ColumnNode.is(node.column)) {
|
|
6038
6090
|
return node.column.column.name;
|
|
@@ -6532,7 +6584,7 @@ var SchemaDbPusher = class {
|
|
|
6532
6584
|
return (0, import_toposort.default)(graph).reverse().filter((m) => !!m);
|
|
6533
6585
|
}
|
|
6534
6586
|
createModelTable(kysely, modelDef) {
|
|
6535
|
-
let table = kysely.schema.createTable(modelDef
|
|
6587
|
+
let table = kysely.schema.createTable(this.getTableName(modelDef)).ifNotExists();
|
|
6536
6588
|
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
6537
6589
|
if (fieldDef.originModel && !fieldDef.id) {
|
|
6538
6590
|
continue;
|
|
@@ -6551,6 +6603,26 @@ var SchemaDbPusher = class {
|
|
|
6551
6603
|
table = this.addUniqueConstraint(table, modelDef);
|
|
6552
6604
|
return table;
|
|
6553
6605
|
}
|
|
6606
|
+
getTableName(modelDef) {
|
|
6607
|
+
const mapAttr = modelDef.attributes?.find((a) => a.name === "@@map");
|
|
6608
|
+
if (mapAttr && mapAttr.args?.[0]) {
|
|
6609
|
+
const mappedName = ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
6610
|
+
if (mappedName) {
|
|
6611
|
+
return mappedName;
|
|
6612
|
+
}
|
|
6613
|
+
}
|
|
6614
|
+
return modelDef.name;
|
|
6615
|
+
}
|
|
6616
|
+
getColumnName(fieldDef) {
|
|
6617
|
+
const mapAttr = fieldDef.attributes?.find((a) => a.name === "@map");
|
|
6618
|
+
if (mapAttr && mapAttr.args?.[0]) {
|
|
6619
|
+
const mappedName = ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
6620
|
+
if (mappedName) {
|
|
6621
|
+
return mappedName;
|
|
6622
|
+
}
|
|
6623
|
+
}
|
|
6624
|
+
return fieldDef.name;
|
|
6625
|
+
}
|
|
6554
6626
|
isComputedField(fieldDef) {
|
|
6555
6627
|
return fieldDef.attributes?.some((a) => a.name === "@computed");
|
|
6556
6628
|
}
|
|
@@ -6561,7 +6633,7 @@ var SchemaDbPusher = class {
|
|
|
6561
6633
|
}
|
|
6562
6634
|
}
|
|
6563
6635
|
if (modelDef.idFields.length > 0) {
|
|
6564
|
-
table = table.addPrimaryKeyConstraint(`pk_${modelDef.name}`, modelDef.idFields);
|
|
6636
|
+
table = table.addPrimaryKeyConstraint(`pk_${modelDef.name}`, modelDef.idFields.map((f) => this.getColumnName(modelDef.fields[f])));
|
|
6565
6637
|
}
|
|
6566
6638
|
return table;
|
|
6567
6639
|
}
|
|
@@ -6574,16 +6646,16 @@ var SchemaDbPusher = class {
|
|
|
6574
6646
|
continue;
|
|
6575
6647
|
}
|
|
6576
6648
|
table = table.addUniqueConstraint(`unique_${modelDef.name}_${key}`, [
|
|
6577
|
-
|
|
6649
|
+
this.getColumnName(fieldDef)
|
|
6578
6650
|
]);
|
|
6579
6651
|
} else {
|
|
6580
|
-
table = table.addUniqueConstraint(`unique_${modelDef.name}_${key}`, Object.keys(value));
|
|
6652
|
+
table = table.addUniqueConstraint(`unique_${modelDef.name}_${key}`, Object.keys(value).map((f) => this.getColumnName(modelDef.fields[f])));
|
|
6581
6653
|
}
|
|
6582
6654
|
}
|
|
6583
6655
|
return table;
|
|
6584
6656
|
}
|
|
6585
6657
|
createModelField(table, fieldDef, modelDef) {
|
|
6586
|
-
return table.addColumn(fieldDef
|
|
6658
|
+
return table.addColumn(this.getColumnName(fieldDef), this.mapFieldType(fieldDef), (col) => {
|
|
6587
6659
|
if (fieldDef.id && modelDef.idFields.length === 1) {
|
|
6588
6660
|
col = col.primaryKey();
|
|
6589
6661
|
}
|
|
@@ -6639,7 +6711,9 @@ var SchemaDbPusher = class {
|
|
|
6639
6711
|
if (!fieldDef.relation.fields || !fieldDef.relation.references) {
|
|
6640
6712
|
return table;
|
|
6641
6713
|
}
|
|
6642
|
-
|
|
6714
|
+
const modelDef = requireModel(this.schema, model);
|
|
6715
|
+
const relationModelDef = requireModel(this.schema, fieldDef.type);
|
|
6716
|
+
table = table.addForeignKeyConstraint(`fk_${model}_${fieldName}`, fieldDef.relation.fields.map((f) => this.getColumnName(modelDef.fields[f])), this.getTableName(relationModelDef), fieldDef.relation.references.map((f) => this.getColumnName(relationModelDef.fields[f])), (cb) => {
|
|
6643
6717
|
if (fieldDef.relation?.onDelete) {
|
|
6644
6718
|
cb = cb.onDelete(this.mapCascadeAction(fieldDef.relation.onDelete));
|
|
6645
6719
|
}
|