@zenstackhq/orm 3.1.1 → 3.2.1
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/index.cjs +237 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +241 -28
- package/dist/index.d.ts +241 -28
- package/dist/index.js +236 -46
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
package/dist/index.js
CHANGED
|
@@ -617,6 +617,7 @@ import { match as match5 } from "ts-pattern";
|
|
|
617
617
|
import { invariant as invariant3 } from "@zenstackhq/common-helpers";
|
|
618
618
|
import Decimal from "decimal.js";
|
|
619
619
|
import { sql as sql2 } from "kysely";
|
|
620
|
+
import { parse as parsePostgresArray } from "postgres-array";
|
|
620
621
|
import { match as match3 } from "ts-pattern";
|
|
621
622
|
import z from "zod";
|
|
622
623
|
|
|
@@ -664,7 +665,7 @@ var BaseCrudDialect = class {
|
|
|
664
665
|
transformPrimitive(value, _type, _forArrayField) {
|
|
665
666
|
return value;
|
|
666
667
|
}
|
|
667
|
-
transformOutput(value, _type) {
|
|
668
|
+
transformOutput(value, _type, _array) {
|
|
668
669
|
return value;
|
|
669
670
|
}
|
|
670
671
|
// #region common query builders
|
|
@@ -1521,6 +1522,9 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1521
1522
|
} else if (Array.isArray(value)) {
|
|
1522
1523
|
if (type === "Json" && !forArrayField) {
|
|
1523
1524
|
return JSON.stringify(value);
|
|
1525
|
+
}
|
|
1526
|
+
if (isEnum(this.schema, type)) {
|
|
1527
|
+
return this.eb.cast(sql2`ARRAY[${sql2.join(value.map((v) => this.transformPrimitive(v, type, false)), sql2.raw(","))}]`, this.createSchemaQualifiedEnumType(type, true));
|
|
1524
1528
|
} else {
|
|
1525
1529
|
return value.map((v) => this.transformPrimitive(v, type, false));
|
|
1526
1530
|
}
|
|
@@ -1534,11 +1538,29 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1534
1538
|
}).otherwise(() => value);
|
|
1535
1539
|
}
|
|
1536
1540
|
}
|
|
1537
|
-
|
|
1541
|
+
createSchemaQualifiedEnumType(type, array) {
|
|
1542
|
+
let qualified = type;
|
|
1543
|
+
const enumDef = getEnum(this.schema, type);
|
|
1544
|
+
if (enumDef) {
|
|
1545
|
+
const schemaAttr = enumDef.attributes?.find((attr) => attr.name === "@@schema");
|
|
1546
|
+
if (schemaAttr) {
|
|
1547
|
+
const mapArg = schemaAttr.args?.find((arg) => arg.name === "map");
|
|
1548
|
+
if (mapArg && mapArg.value.kind === "literal") {
|
|
1549
|
+
const schemaName = mapArg.value.value;
|
|
1550
|
+
qualified = `"${schemaName}"."${type}"`;
|
|
1551
|
+
}
|
|
1552
|
+
} else {
|
|
1553
|
+
const defaultSchema = this.schema.provider.defaultSchema ?? "public";
|
|
1554
|
+
qualified = `"${defaultSchema}"."${type}"`;
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
return array ? sql2.raw(`${qualified}[]`) : sql2.raw(qualified);
|
|
1558
|
+
}
|
|
1559
|
+
transformOutput(value, type, array) {
|
|
1538
1560
|
if (value === null || value === void 0) {
|
|
1539
1561
|
return value;
|
|
1540
1562
|
}
|
|
1541
|
-
return match3(type).with("DateTime", () => this.transformOutputDate(value)).with("Bytes", () => this.transformOutputBytes(value)).with("BigInt", () => this.transformOutputBigInt(value)).with("Decimal", () => this.transformDecimal(value)).otherwise(() => super.transformOutput(value, type));
|
|
1563
|
+
return match3(type).with("DateTime", () => this.transformOutputDate(value)).with("Bytes", () => this.transformOutputBytes(value)).with("BigInt", () => this.transformOutputBigInt(value)).with("Decimal", () => this.transformDecimal(value)).when((type2) => isEnum(this.schema, type2), () => this.transformOutputEnum(value, array)).otherwise(() => super.transformOutput(value, type, array));
|
|
1542
1564
|
}
|
|
1543
1565
|
transformOutputBigInt(value) {
|
|
1544
1566
|
if (typeof value === "bigint") {
|
|
@@ -1571,6 +1593,15 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1571
1593
|
transformOutputBytes(value) {
|
|
1572
1594
|
return Buffer.isBuffer(value) ? Uint8Array.from(value) : typeof value === "string" && value.startsWith("\\x") ? Uint8Array.from(Buffer.from(value.slice(2), "hex")) : value;
|
|
1573
1595
|
}
|
|
1596
|
+
transformOutputEnum(value, array) {
|
|
1597
|
+
if (array && typeof value === "string") {
|
|
1598
|
+
try {
|
|
1599
|
+
return parsePostgresArray(value);
|
|
1600
|
+
} catch {
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
return value;
|
|
1604
|
+
}
|
|
1574
1605
|
buildRelationSelection(query, model, relationField, parentAlias, payload) {
|
|
1575
1606
|
const relationResultName = `${parentAlias}$${relationField}`;
|
|
1576
1607
|
const joinedQuery = this.buildRelationJSON(model, query, relationField, parentAlias, payload, relationResultName);
|
|
@@ -1801,13 +1832,13 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1801
1832
|
return match4(type).with("Boolean", () => value ? 1 : 0).with("DateTime", () => value instanceof Date ? value.toISOString() : typeof value === "string" ? new Date(value).toISOString() : value).with("Decimal", () => value.toString()).with("Bytes", () => Buffer.from(value)).otherwise(() => value);
|
|
1802
1833
|
}
|
|
1803
1834
|
}
|
|
1804
|
-
transformOutput(value, type) {
|
|
1835
|
+
transformOutput(value, type, array) {
|
|
1805
1836
|
if (value === null || value === void 0) {
|
|
1806
1837
|
return value;
|
|
1807
1838
|
} else if (this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
1808
1839
|
return this.transformOutputJson(value);
|
|
1809
1840
|
} else {
|
|
1810
|
-
return match4(type).with("Boolean", () => this.transformOutputBoolean(value)).with("DateTime", () => this.transformOutputDate(value)).with("Bytes", () => this.transformOutputBytes(value)).with("Decimal", () => this.transformOutputDecimal(value)).with("BigInt", () => this.transformOutputBigInt(value)).with("Json", () => this.transformOutputJson(value)).otherwise(() => super.transformOutput(value, type));
|
|
1841
|
+
return match4(type).with("Boolean", () => this.transformOutputBoolean(value)).with("DateTime", () => this.transformOutputDate(value)).with("Bytes", () => this.transformOutputBytes(value)).with("Decimal", () => this.transformOutputDecimal(value)).with("BigInt", () => this.transformOutputBigInt(value)).with("Json", () => this.transformOutputJson(value)).otherwise(() => super.transformOutput(value, type, array));
|
|
1811
1842
|
}
|
|
1812
1843
|
}
|
|
1813
1844
|
transformOutputDecimal(value) {
|
|
@@ -2087,6 +2118,21 @@ var BaseOperationHandler = class {
|
|
|
2087
2118
|
select: this.makeIdSelect(model)
|
|
2088
2119
|
});
|
|
2089
2120
|
}
|
|
2121
|
+
async existsNonUnique(kysely, model, filter) {
|
|
2122
|
+
const query = kysely.selectNoFrom((eb) => eb.exists(this.dialect.buildSelectModel(model, model).select(sql4.lit(1).as("$t")).where(() => this.dialect.buildFilter(model, model, filter))).as("exists")).modifyEnd(this.makeContextComment({
|
|
2123
|
+
model,
|
|
2124
|
+
operation: "read"
|
|
2125
|
+
}));
|
|
2126
|
+
let result = [];
|
|
2127
|
+
const compiled = kysely.getExecutor().compileQuery(query.toOperationNode(), createQueryId());
|
|
2128
|
+
try {
|
|
2129
|
+
const r = await kysely.getExecutor().executeQuery(compiled);
|
|
2130
|
+
result = r.rows;
|
|
2131
|
+
} catch (err) {
|
|
2132
|
+
throw createDBQueryError(`Failed to execute query: ${err}`, err, compiled.sql, compiled.parameters);
|
|
2133
|
+
}
|
|
2134
|
+
return !!result[0]?.exists;
|
|
2135
|
+
}
|
|
2090
2136
|
async read(kysely, model, args) {
|
|
2091
2137
|
let query = this.dialect.buildSelectModel(model, model);
|
|
2092
2138
|
if (args) {
|
|
@@ -2110,7 +2156,7 @@ var BaseOperationHandler = class {
|
|
|
2110
2156
|
const r = await kysely.getExecutor().executeQuery(compiled);
|
|
2111
2157
|
result = r.rows;
|
|
2112
2158
|
} catch (err) {
|
|
2113
|
-
throw createDBQueryError(
|
|
2159
|
+
throw createDBQueryError(`Failed to execute query: ${err}`, err, compiled.sql, compiled.parameters);
|
|
2114
2160
|
}
|
|
2115
2161
|
return result;
|
|
2116
2162
|
}
|
|
@@ -3698,6 +3744,18 @@ var FindOperationHandler = class extends BaseOperationHandler {
|
|
|
3698
3744
|
}
|
|
3699
3745
|
};
|
|
3700
3746
|
|
|
3747
|
+
// src/client/crud/operations/exists.ts
|
|
3748
|
+
var ExistsOperationHandler = class extends BaseOperationHandler {
|
|
3749
|
+
static {
|
|
3750
|
+
__name(this, "ExistsOperationHandler");
|
|
3751
|
+
}
|
|
3752
|
+
async handle(_operation, args) {
|
|
3753
|
+
const normalizedArgs = this.normalizeArgs(args);
|
|
3754
|
+
const parsedArgs = this.inputValidator.validateExistsArgs(this.model, normalizedArgs);
|
|
3755
|
+
return await this.existsNonUnique(this.client.$qb, this.model, parsedArgs?.where);
|
|
3756
|
+
}
|
|
3757
|
+
};
|
|
3758
|
+
|
|
3701
3759
|
// src/client/crud/operations/group-by.ts
|
|
3702
3760
|
import { match as match10 } from "ts-pattern";
|
|
3703
3761
|
var GroupByOperationHandler = class extends BaseOperationHandler {
|
|
@@ -3816,7 +3874,7 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3816
3874
|
const result = await this.safeTransaction(async (tx) => {
|
|
3817
3875
|
const updateResult = await this.update(tx, this.model, args.where, args.data, void 0, void 0, void 0, selectedFields);
|
|
3818
3876
|
if (needReadBack) {
|
|
3819
|
-
const readFilter = updateResult
|
|
3877
|
+
const readFilter = updateResult ? getIdValues(this.schema, this.model, updateResult) : args.where;
|
|
3820
3878
|
let readBackResult = void 0;
|
|
3821
3879
|
readBackResult = await this.readUnique(tx, this.model, {
|
|
3822
3880
|
select: args.select,
|
|
@@ -4314,9 +4372,96 @@ var InputValidator = class {
|
|
|
4314
4372
|
get extraValidationsEnabled() {
|
|
4315
4373
|
return this.client.$options.validateInput !== false;
|
|
4316
4374
|
}
|
|
4375
|
+
validateProcedureInput(proc, input) {
|
|
4376
|
+
const procDef = (this.schema.procedures ?? {})[proc];
|
|
4377
|
+
invariant7(procDef, `Procedure "${proc}" not found in schema`);
|
|
4378
|
+
const params = Object.values(procDef.params ?? {});
|
|
4379
|
+
if (typeof input === "undefined") {
|
|
4380
|
+
if (params.length === 0) {
|
|
4381
|
+
return void 0;
|
|
4382
|
+
}
|
|
4383
|
+
if (params.every((p) => p.optional)) {
|
|
4384
|
+
return void 0;
|
|
4385
|
+
}
|
|
4386
|
+
throw createInvalidInputError("Missing procedure arguments", `$procs.${proc}`);
|
|
4387
|
+
}
|
|
4388
|
+
if (typeof input !== "object") {
|
|
4389
|
+
throw createInvalidInputError("Procedure input must be an object", `$procs.${proc}`);
|
|
4390
|
+
}
|
|
4391
|
+
const envelope = input;
|
|
4392
|
+
const argsPayload = Object.prototype.hasOwnProperty.call(envelope, "args") ? envelope.args : void 0;
|
|
4393
|
+
if (params.length === 0) {
|
|
4394
|
+
if (typeof argsPayload === "undefined") {
|
|
4395
|
+
return input;
|
|
4396
|
+
}
|
|
4397
|
+
if (!argsPayload || typeof argsPayload !== "object" || Array.isArray(argsPayload)) {
|
|
4398
|
+
throw createInvalidInputError("Procedure `args` must be an object", `$procs.${proc}`);
|
|
4399
|
+
}
|
|
4400
|
+
if (Object.keys(argsPayload).length === 0) {
|
|
4401
|
+
return input;
|
|
4402
|
+
}
|
|
4403
|
+
throw createInvalidInputError("Procedure does not accept arguments", `$procs.${proc}`);
|
|
4404
|
+
}
|
|
4405
|
+
if (typeof argsPayload === "undefined") {
|
|
4406
|
+
if (params.every((p) => p.optional)) {
|
|
4407
|
+
return input;
|
|
4408
|
+
}
|
|
4409
|
+
throw createInvalidInputError("Missing procedure arguments", `$procs.${proc}`);
|
|
4410
|
+
}
|
|
4411
|
+
if (!argsPayload || typeof argsPayload !== "object" || Array.isArray(argsPayload)) {
|
|
4412
|
+
throw createInvalidInputError("Procedure `args` must be an object", `$procs.${proc}`);
|
|
4413
|
+
}
|
|
4414
|
+
const obj = argsPayload;
|
|
4415
|
+
for (const param of params) {
|
|
4416
|
+
const value = obj[param.name];
|
|
4417
|
+
if (!Object.prototype.hasOwnProperty.call(obj, param.name)) {
|
|
4418
|
+
if (param.optional) {
|
|
4419
|
+
continue;
|
|
4420
|
+
}
|
|
4421
|
+
throw createInvalidInputError(`Missing procedure argument: ${param.name}`, `$procs.${proc}`);
|
|
4422
|
+
}
|
|
4423
|
+
if (typeof value === "undefined") {
|
|
4424
|
+
if (param.optional) {
|
|
4425
|
+
continue;
|
|
4426
|
+
}
|
|
4427
|
+
throw createInvalidInputError(`Invalid procedure argument: ${param.name} is required`, `$procs.${proc}`);
|
|
4428
|
+
}
|
|
4429
|
+
const schema = this.makeProcedureParamSchema(param);
|
|
4430
|
+
const parsed = schema.safeParse(value);
|
|
4431
|
+
if (!parsed.success) {
|
|
4432
|
+
throw createInvalidInputError(`Invalid procedure argument: ${param.name}: ${formatError(parsed.error)}`, `$procs.${proc}`);
|
|
4433
|
+
}
|
|
4434
|
+
}
|
|
4435
|
+
return input;
|
|
4436
|
+
}
|
|
4437
|
+
makeProcedureParamSchema(param) {
|
|
4438
|
+
let schema;
|
|
4439
|
+
if (isTypeDef(this.schema, param.type)) {
|
|
4440
|
+
schema = this.makeTypeDefSchema(param.type);
|
|
4441
|
+
} else if (isEnum(this.schema, param.type)) {
|
|
4442
|
+
schema = this.makeEnumSchema(param.type);
|
|
4443
|
+
} else if (param.type in (this.schema.models ?? {})) {
|
|
4444
|
+
schema = z3.record(z3.string(), z3.unknown());
|
|
4445
|
+
} else {
|
|
4446
|
+
schema = this.makeScalarSchema(param.type);
|
|
4447
|
+
if (schema instanceof z3.ZodUnknown) {
|
|
4448
|
+
throw createInternalError(`Unsupported procedure parameter type: ${param.type}`);
|
|
4449
|
+
}
|
|
4450
|
+
}
|
|
4451
|
+
if (param.array) {
|
|
4452
|
+
schema = schema.array();
|
|
4453
|
+
}
|
|
4454
|
+
if (param.optional) {
|
|
4455
|
+
schema = schema.optional();
|
|
4456
|
+
}
|
|
4457
|
+
return schema;
|
|
4458
|
+
}
|
|
4317
4459
|
validateFindArgs(model, args, options) {
|
|
4318
4460
|
return this.validate(model, "find", options, (model2, options2) => this.makeFindSchema(model2, options2), args);
|
|
4319
4461
|
}
|
|
4462
|
+
validateExistsArgs(model, args) {
|
|
4463
|
+
return this.validate(model, "exists", void 0, (model2) => this.makeExistsSchema(model2), args);
|
|
4464
|
+
}
|
|
4320
4465
|
validateCreateArgs(model, args) {
|
|
4321
4466
|
return this.validate(model, "create", void 0, (model2) => this.makeCreateSchema(model2), args);
|
|
4322
4467
|
}
|
|
@@ -4421,6 +4566,11 @@ var InputValidator = class {
|
|
|
4421
4566
|
}
|
|
4422
4567
|
return result;
|
|
4423
4568
|
}
|
|
4569
|
+
makeExistsSchema(model) {
|
|
4570
|
+
return z3.strictObject({
|
|
4571
|
+
where: this.makeWhereSchema(model, false).optional()
|
|
4572
|
+
}).optional();
|
|
4573
|
+
}
|
|
4424
4574
|
makeScalarSchema(type, attributes) {
|
|
4425
4575
|
if (this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
4426
4576
|
return this.makeTypeDefSchema(type);
|
|
@@ -4525,7 +4675,7 @@ var InputValidator = class {
|
|
|
4525
4675
|
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
4526
4676
|
if (enumDef) {
|
|
4527
4677
|
if (Object.keys(enumDef.values).length > 0) {
|
|
4528
|
-
fieldSchema = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, withAggregations);
|
|
4678
|
+
fieldSchema = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, withAggregations, !!fieldDef.array);
|
|
4529
4679
|
}
|
|
4530
4680
|
} else if (fieldDef.array) {
|
|
4531
4681
|
fieldSchema = this.makeArrayFilterSchema(fieldDef.type);
|
|
@@ -4549,7 +4699,7 @@ var InputValidator = class {
|
|
|
4549
4699
|
const enumDef = getEnum(this.schema, def.type);
|
|
4550
4700
|
if (enumDef) {
|
|
4551
4701
|
if (Object.keys(enumDef.values).length > 0) {
|
|
4552
|
-
fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false);
|
|
4702
|
+
fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false, false);
|
|
4553
4703
|
} else {
|
|
4554
4704
|
fieldSchema = z3.never();
|
|
4555
4705
|
}
|
|
@@ -4599,15 +4749,13 @@ var InputValidator = class {
|
|
|
4599
4749
|
if (this.isTypeDefType(fieldDef.type)) {
|
|
4600
4750
|
fieldSchemas[fieldName] = this.makeTypedJsonFilterSchema(fieldDef.type, !!fieldDef.optional, !!fieldDef.array).optional();
|
|
4601
4751
|
} else {
|
|
4602
|
-
|
|
4752
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
4753
|
+
if (enumDef) {
|
|
4754
|
+
fieldSchemas[fieldName] = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, false, !!fieldDef.array).optional();
|
|
4755
|
+
} else if (fieldDef.array) {
|
|
4603
4756
|
fieldSchemas[fieldName] = this.makeArrayFilterSchema(fieldDef.type).optional();
|
|
4604
4757
|
} else {
|
|
4605
|
-
|
|
4606
|
-
if (enumDef) {
|
|
4607
|
-
fieldSchemas[fieldName] = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, false).optional();
|
|
4608
|
-
} else {
|
|
4609
|
-
fieldSchemas[fieldName] = this.makePrimitiveFilterSchema(fieldDef.type, !!fieldDef.optional, false).optional();
|
|
4610
|
-
}
|
|
4758
|
+
fieldSchemas[fieldName] = this.makePrimitiveFilterSchema(fieldDef.type, !!fieldDef.optional, false).optional();
|
|
4611
4759
|
}
|
|
4612
4760
|
}
|
|
4613
4761
|
}
|
|
@@ -4635,9 +4783,12 @@ var InputValidator = class {
|
|
|
4635
4783
|
isTypeDefType(type) {
|
|
4636
4784
|
return this.schema.typeDefs && type in this.schema.typeDefs;
|
|
4637
4785
|
}
|
|
4638
|
-
makeEnumFilterSchema(enumDef, optional, withAggregations) {
|
|
4786
|
+
makeEnumFilterSchema(enumDef, optional, withAggregations, array) {
|
|
4639
4787
|
const baseSchema = z3.enum(Object.keys(enumDef.values));
|
|
4640
|
-
|
|
4788
|
+
if (array) {
|
|
4789
|
+
return this.internalMakeArrayFilterSchema(baseSchema);
|
|
4790
|
+
}
|
|
4791
|
+
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z3.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations, array)), [
|
|
4641
4792
|
"equals",
|
|
4642
4793
|
"in",
|
|
4643
4794
|
"notIn",
|
|
@@ -4653,11 +4804,14 @@ var InputValidator = class {
|
|
|
4653
4804
|
]);
|
|
4654
4805
|
}
|
|
4655
4806
|
makeArrayFilterSchema(type) {
|
|
4807
|
+
return this.internalMakeArrayFilterSchema(this.makeScalarSchema(type));
|
|
4808
|
+
}
|
|
4809
|
+
internalMakeArrayFilterSchema(elementSchema) {
|
|
4656
4810
|
return z3.strictObject({
|
|
4657
|
-
equals:
|
|
4658
|
-
has:
|
|
4659
|
-
hasEvery:
|
|
4660
|
-
hasSome:
|
|
4811
|
+
equals: elementSchema.array().optional(),
|
|
4812
|
+
has: elementSchema.optional(),
|
|
4813
|
+
hasEvery: elementSchema.array().optional(),
|
|
4814
|
+
hasSome: elementSchema.array().optional(),
|
|
4661
4815
|
isEmpty: z3.boolean().optional()
|
|
4662
4816
|
});
|
|
4663
4817
|
}
|
|
@@ -5717,14 +5871,16 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5717
5871
|
return super.transformSelectQuery(node);
|
|
5718
5872
|
}
|
|
5719
5873
|
const processedFroms = node.from.froms.map((from) => this.processSelectTable(from));
|
|
5720
|
-
const processedJoins =
|
|
5874
|
+
const processedJoins = [];
|
|
5875
|
+
const cumulativeScopes = [
|
|
5721
5876
|
...processedFroms.map(({ scope }) => scope)
|
|
5722
|
-
], () => (node.joins ?? []).map((join) => this.processSelectTable(join.table)));
|
|
5723
|
-
const scopes = [
|
|
5724
|
-
...processedFroms.map(({ scope }) => scope),
|
|
5725
|
-
...processedJoins.map(({ scope }) => scope)
|
|
5726
5877
|
];
|
|
5727
|
-
|
|
5878
|
+
for (const join of node.joins ?? []) {
|
|
5879
|
+
const processedJoin = this.withScopes(cumulativeScopes, () => this.processSelectTable(join.table));
|
|
5880
|
+
processedJoins.push(processedJoin);
|
|
5881
|
+
cumulativeScopes.push(processedJoin.scope);
|
|
5882
|
+
}
|
|
5883
|
+
return this.withScopes(cumulativeScopes, () => {
|
|
5728
5884
|
const joins = node.joins ? node.joins.map((join, i) => ({
|
|
5729
5885
|
...join,
|
|
5730
5886
|
table: processedJoins[i].node,
|
|
@@ -6059,9 +6215,9 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
6059
6215
|
let schema = this.schema.provider.defaultSchema ?? "public";
|
|
6060
6216
|
const schemaAttr = this.schema.models[model]?.attributes?.find((attr) => attr.name === "@@schema");
|
|
6061
6217
|
if (schemaAttr) {
|
|
6062
|
-
const
|
|
6063
|
-
if (
|
|
6064
|
-
schema =
|
|
6218
|
+
const mapArg = schemaAttr.args?.find((arg) => arg.name === "map");
|
|
6219
|
+
if (mapArg && mapArg.value.kind === "literal") {
|
|
6220
|
+
schema = mapArg.value.value;
|
|
6065
6221
|
}
|
|
6066
6222
|
}
|
|
6067
6223
|
return schema;
|
|
@@ -6312,7 +6468,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
6312
6468
|
if (err instanceof ORMError) {
|
|
6313
6469
|
throw err;
|
|
6314
6470
|
} else {
|
|
6315
|
-
throw createDBQueryError(
|
|
6471
|
+
throw createDBQueryError(`Failed to execute query: ${err}`, err, compiledQuery.sql, compiledQuery.parameters);
|
|
6316
6472
|
}
|
|
6317
6473
|
}
|
|
6318
6474
|
});
|
|
@@ -6552,7 +6708,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
6552
6708
|
try {
|
|
6553
6709
|
return await connection.executeQuery(compiledQuery);
|
|
6554
6710
|
} catch (err) {
|
|
6555
|
-
throw createDBQueryError(
|
|
6711
|
+
throw createDBQueryError(`Failed to execute query: ${err}`, err, compiledQuery.sql, compiledQuery.parameters);
|
|
6556
6712
|
}
|
|
6557
6713
|
}
|
|
6558
6714
|
};
|
|
@@ -7010,7 +7166,7 @@ var ResultProcessor = class {
|
|
|
7010
7166
|
}
|
|
7011
7167
|
if (key.startsWith(DELEGATE_JOINED_FIELD_PREFIX)) {
|
|
7012
7168
|
if (value) {
|
|
7013
|
-
const subRow = this.dialect.transformOutput(value, "Json");
|
|
7169
|
+
const subRow = this.dialect.transformOutput(value, "Json", false);
|
|
7014
7170
|
const subModel = key.slice(DELEGATE_JOINED_FIELD_PREFIX.length);
|
|
7015
7171
|
const idValues = getIdValues(this.schema, subModel, subRow);
|
|
7016
7172
|
if (Object.values(idValues).some((v) => v === null || v === void 0)) {
|
|
@@ -7044,10 +7200,10 @@ var ResultProcessor = class {
|
|
|
7044
7200
|
processFieldValue(value, fieldDef) {
|
|
7045
7201
|
const type = fieldDef.type;
|
|
7046
7202
|
if (Array.isArray(value)) {
|
|
7047
|
-
value.forEach((v, i) => value[i] = this.dialect.transformOutput(v, type));
|
|
7203
|
+
value.forEach((v, i) => value[i] = this.dialect.transformOutput(v, type, false));
|
|
7048
7204
|
return value;
|
|
7049
7205
|
} else {
|
|
7050
|
-
return this.dialect.transformOutput(value, type);
|
|
7206
|
+
return this.dialect.transformOutput(value, type, !!fieldDef.array);
|
|
7051
7207
|
}
|
|
7052
7208
|
}
|
|
7053
7209
|
processRelation(value, fieldDef) {
|
|
@@ -7205,24 +7361,54 @@ var ClientImpl = class _ClientImpl {
|
|
|
7205
7361
|
return txBuilder.execute((tx) => execute(tx));
|
|
7206
7362
|
}
|
|
7207
7363
|
}
|
|
7208
|
-
get $
|
|
7364
|
+
get $procs() {
|
|
7209
7365
|
return Object.keys(this.$schema.procedures ?? {}).reduce((acc, name) => {
|
|
7210
|
-
acc[name] = (
|
|
7366
|
+
acc[name] = (input) => this.handleProc(name, input);
|
|
7211
7367
|
return acc;
|
|
7212
7368
|
}, {});
|
|
7213
7369
|
}
|
|
7214
|
-
async handleProc(name,
|
|
7370
|
+
async handleProc(name, input) {
|
|
7215
7371
|
if (!("procedures" in this.$options) || !this.$options || typeof this.$options.procedures !== "object") {
|
|
7216
7372
|
throw createConfigError("Procedures are not configured for the client.");
|
|
7217
7373
|
}
|
|
7374
|
+
const procDef = (this.$schema.procedures ?? {})[name];
|
|
7375
|
+
if (!procDef) {
|
|
7376
|
+
throw createConfigError(`Procedure "${name}" is not defined in schema.`);
|
|
7377
|
+
}
|
|
7218
7378
|
const procOptions = this.$options.procedures;
|
|
7219
7379
|
if (!procOptions[name] || typeof procOptions[name] !== "function") {
|
|
7220
|
-
throw
|
|
7221
|
-
}
|
|
7222
|
-
|
|
7223
|
-
|
|
7224
|
-
|
|
7225
|
-
|
|
7380
|
+
throw createConfigError(`Procedure "${name}" does not have a handler configured.`);
|
|
7381
|
+
}
|
|
7382
|
+
const inputValidator = new InputValidator(this);
|
|
7383
|
+
const validatedInput = inputValidator.validateProcedureInput(name, input);
|
|
7384
|
+
const handler = procOptions[name];
|
|
7385
|
+
const invokeWithClient = /* @__PURE__ */ __name(async (client, _input) => {
|
|
7386
|
+
let proceed = /* @__PURE__ */ __name(async (nextInput) => {
|
|
7387
|
+
const sanitizedNextInput = nextInput && typeof nextInput === "object" && !Array.isArray(nextInput) ? nextInput : {};
|
|
7388
|
+
return handler({
|
|
7389
|
+
client,
|
|
7390
|
+
...sanitizedNextInput
|
|
7391
|
+
});
|
|
7392
|
+
}, "proceed");
|
|
7393
|
+
const plugins = [
|
|
7394
|
+
...client.$options?.plugins ?? []
|
|
7395
|
+
];
|
|
7396
|
+
for (const plugin of plugins) {
|
|
7397
|
+
const onProcedure = plugin.onProcedure;
|
|
7398
|
+
if (onProcedure) {
|
|
7399
|
+
const _proceed = proceed;
|
|
7400
|
+
proceed = /* @__PURE__ */ __name((nextInput) => onProcedure({
|
|
7401
|
+
client,
|
|
7402
|
+
name,
|
|
7403
|
+
mutation: !!procDef.mutation,
|
|
7404
|
+
input: nextInput,
|
|
7405
|
+
proceed: /* @__PURE__ */ __name((finalInput) => _proceed(finalInput), "proceed")
|
|
7406
|
+
}), "proceed");
|
|
7407
|
+
}
|
|
7408
|
+
}
|
|
7409
|
+
return proceed(_input);
|
|
7410
|
+
}, "invokeWithClient");
|
|
7411
|
+
return invokeWithClient(this, validatedInput);
|
|
7226
7412
|
}
|
|
7227
7413
|
async $connect() {
|
|
7228
7414
|
await this.kysely.connection().execute(async (conn) => {
|
|
@@ -7429,7 +7615,10 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
|
|
|
7429
7615
|
}, "aggregate"),
|
|
7430
7616
|
groupBy: /* @__PURE__ */ __name((args) => {
|
|
7431
7617
|
return createPromise("groupBy", "groupBy", args, new GroupByOperationHandler(client, model, inputValidator), true);
|
|
7432
|
-
}, "groupBy")
|
|
7618
|
+
}, "groupBy"),
|
|
7619
|
+
exists: /* @__PURE__ */ __name((args) => {
|
|
7620
|
+
return createPromise("exists", "exists", args, new ExistsOperationHandler(client, model, inputValidator), false);
|
|
7621
|
+
}, "exists")
|
|
7433
7622
|
};
|
|
7434
7623
|
}
|
|
7435
7624
|
__name(createModelCrudHandler, "createModelCrudHandler");
|
|
@@ -7860,6 +8049,7 @@ export {
|
|
|
7860
8049
|
CRUD_EXT,
|
|
7861
8050
|
DbNull,
|
|
7862
8051
|
DbNullClass,
|
|
8052
|
+
InputValidator,
|
|
7863
8053
|
JsonNull,
|
|
7864
8054
|
JsonNullClass,
|
|
7865
8055
|
kysely_utils_exports as KyselyUtils,
|