@danielhritcu/zenstack-orm 3.5.20 → 3.5.22
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 +141 -70
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +73 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -69,7 +69,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
69
69
|
|
|
70
70
|
// src/client/client-impl.ts
|
|
71
71
|
var import_common_helpers15 = require("@zenstackhq/common-helpers");
|
|
72
|
-
var
|
|
72
|
+
var import_kysely13 = require("kysely");
|
|
73
73
|
var import_zod3 = __toESM(require("zod"), 1);
|
|
74
74
|
|
|
75
75
|
// src/utils/zod-utils.ts
|
|
@@ -7986,7 +7986,7 @@ __name(performanceNow, "performanceNow");
|
|
|
7986
7986
|
|
|
7987
7987
|
// src/client/executor/zenstack-query-executor.ts
|
|
7988
7988
|
var import_common_helpers12 = require("@zenstackhq/common-helpers");
|
|
7989
|
-
var
|
|
7989
|
+
var import_kysely10 = require("kysely");
|
|
7990
7990
|
var import_ts_pattern12 = require("ts-pattern");
|
|
7991
7991
|
|
|
7992
7992
|
// src/client/executor/name-mapper.ts
|
|
@@ -8569,9 +8569,35 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
8569
8569
|
}
|
|
8570
8570
|
};
|
|
8571
8571
|
|
|
8572
|
-
// src/client/executor/
|
|
8572
|
+
// src/client/executor/rls.ts
|
|
8573
8573
|
var import_kysely8 = require("kysely");
|
|
8574
|
-
|
|
8574
|
+
function buildRlsSetConfigSql(rlsDef, rlsValues) {
|
|
8575
|
+
const parts = [];
|
|
8576
|
+
for (const [name, keyDef] of Object.entries(rlsDef.keys)) {
|
|
8577
|
+
const rawValue = rlsValues[name];
|
|
8578
|
+
const resolved = keyDef.resolve(rawValue);
|
|
8579
|
+
const escaped = String(resolved).replace(/'/g, "''");
|
|
8580
|
+
parts.push(`set_config('${keyDef.set}', '${escaped}', true)`);
|
|
8581
|
+
}
|
|
8582
|
+
return `SELECT ${parts.join(", ")}`;
|
|
8583
|
+
}
|
|
8584
|
+
__name(buildRlsSetConfigSql, "buildRlsSetConfigSql");
|
|
8585
|
+
async function applyRlsContext(connection, rlsDef, rlsValues) {
|
|
8586
|
+
const sql9 = buildRlsSetConfigSql(rlsDef, rlsValues);
|
|
8587
|
+
await connection.executeQuery({
|
|
8588
|
+
sql: sql9,
|
|
8589
|
+
parameters: [],
|
|
8590
|
+
query: {
|
|
8591
|
+
kind: "RawNode"
|
|
8592
|
+
},
|
|
8593
|
+
queryId: (0, import_kysely8.createQueryId)()
|
|
8594
|
+
});
|
|
8595
|
+
}
|
|
8596
|
+
__name(applyRlsContext, "applyRlsContext");
|
|
8597
|
+
|
|
8598
|
+
// src/client/executor/temp-alias-transformer.ts
|
|
8599
|
+
var import_kysely9 = require("kysely");
|
|
8600
|
+
var TempAliasTransformer = class extends import_kysely9.OperationNodeTransformer {
|
|
8575
8601
|
static {
|
|
8576
8602
|
__name(this, "TempAliasTransformer");
|
|
8577
8603
|
}
|
|
@@ -8611,7 +8637,7 @@ var TempAliasTransformer = class extends import_kysely8.OperationNodeTransformer
|
|
|
8611
8637
|
mapped = `$$t${this.aliasMap.size + 1}`;
|
|
8612
8638
|
this.aliasMap.set(node.name, mapped);
|
|
8613
8639
|
}
|
|
8614
|
-
return
|
|
8640
|
+
return import_kysely9.IdentifierNode.create(mapped);
|
|
8615
8641
|
} else {
|
|
8616
8642
|
return super.transformIdentifier(node, queryId);
|
|
8617
8643
|
}
|
|
@@ -8620,7 +8646,7 @@ var TempAliasTransformer = class extends import_kysely8.OperationNodeTransformer
|
|
|
8620
8646
|
|
|
8621
8647
|
// src/client/executor/zenstack-query-executor.ts
|
|
8622
8648
|
var DEFAULT_MAX_SLOW_RECORDS = 100;
|
|
8623
|
-
var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends
|
|
8649
|
+
var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely10.DefaultQueryExecutor {
|
|
8624
8650
|
static {
|
|
8625
8651
|
__name(this, "ZenStackQueryExecutor");
|
|
8626
8652
|
}
|
|
@@ -8632,6 +8658,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely9.
|
|
|
8632
8658
|
// #region constructor, fields and props
|
|
8633
8659
|
nameMapper;
|
|
8634
8660
|
dialect;
|
|
8661
|
+
rlsInitializedConnections = /* @__PURE__ */ new WeakSet();
|
|
8635
8662
|
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
|
|
8636
8663
|
super(compiler, adapter, connectionProvider, plugins), this.client = client, this.driver = driver, this.compiler = compiler, this.connectionProvider = connectionProvider, this.suppressMutationHooks = suppressMutationHooks;
|
|
8637
8664
|
if (client.$schema.provider.type === "postgresql" || // postgres queries need to be schema-qualified
|
|
@@ -8726,17 +8753,17 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely9.
|
|
|
8726
8753
|
}
|
|
8727
8754
|
let preUpdateIds;
|
|
8728
8755
|
const mutationModel = this.getMutationModel(query);
|
|
8729
|
-
const needLoadAfterMutationEntities = (
|
|
8756
|
+
const needLoadAfterMutationEntities = (import_kysely10.InsertQueryNode.is(query) || import_kysely10.UpdateQueryNode.is(query)) && this.hasEntityMutationPluginsWithAfterMutationHooks;
|
|
8730
8757
|
if (needLoadAfterMutationEntities) {
|
|
8731
8758
|
if (this.dialect.supportsReturning) {
|
|
8732
8759
|
query = {
|
|
8733
8760
|
...query,
|
|
8734
|
-
returning:
|
|
8735
|
-
|
|
8761
|
+
returning: import_kysely10.ReturningNode.create([
|
|
8762
|
+
import_kysely10.SelectionNode.createSelectAll()
|
|
8736
8763
|
])
|
|
8737
8764
|
};
|
|
8738
8765
|
} else {
|
|
8739
|
-
if (
|
|
8766
|
+
if (import_kysely10.UpdateQueryNode.is(query)) {
|
|
8740
8767
|
preUpdateIds = await this.getPreUpdateIds(mutationModel, query, connection);
|
|
8741
8768
|
}
|
|
8742
8769
|
}
|
|
@@ -8746,7 +8773,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely9.
|
|
|
8746
8773
|
const mutationInfo = this.getMutationInfo(query);
|
|
8747
8774
|
let beforeMutationEntities;
|
|
8748
8775
|
const loadBeforeMutationEntities = /* @__PURE__ */ __name(async () => {
|
|
8749
|
-
if (beforeMutationEntities === void 0 && (
|
|
8776
|
+
if (beforeMutationEntities === void 0 && (import_kysely10.UpdateQueryNode.is(query) || import_kysely10.DeleteQueryNode.is(query))) {
|
|
8750
8777
|
beforeMutationEntities = await this.loadEntities(mutationInfo.model, mutationInfo.where, connection, void 0);
|
|
8751
8778
|
}
|
|
8752
8779
|
return beforeMutationEntities;
|
|
@@ -8850,10 +8877,10 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely9.
|
|
|
8850
8877
|
if (this.dialect.supportsReturning) {
|
|
8851
8878
|
return queryResult.rows;
|
|
8852
8879
|
} else {
|
|
8853
|
-
const mutatedIds =
|
|
8880
|
+
const mutatedIds = import_kysely10.InsertQueryNode.is(queryNode) ? this.getInsertIds(mutationInfo.model, queryNode, queryResult) : preUpdateIds;
|
|
8854
8881
|
if (mutatedIds) {
|
|
8855
8882
|
const idFields = requireIdFields(this.client.$schema, mutationInfo.model);
|
|
8856
|
-
const eb = (0,
|
|
8883
|
+
const eb = (0, import_kysely10.expressionBuilder)();
|
|
8857
8884
|
const filter = eb(
|
|
8858
8885
|
// @ts-ignore
|
|
8859
8886
|
eb.refTuple(...idFields),
|
|
@@ -8862,7 +8889,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely9.
|
|
|
8862
8889
|
...idFields.map((idField) => eb.val(idObj[idField]))
|
|
8863
8890
|
))
|
|
8864
8891
|
);
|
|
8865
|
-
const entities = await this.loadEntities(mutationInfo.model,
|
|
8892
|
+
const entities = await this.loadEntities(mutationInfo.model, import_kysely10.WhereNode.create(filter.toOperationNode()), connection, void 0);
|
|
8866
8893
|
return entities;
|
|
8867
8894
|
} else {
|
|
8868
8895
|
console.warn(`Unable to load after-mutation entities for hooks: model "${mutationInfo.model}", operation "${mutationInfo.action}".
|
|
@@ -8883,9 +8910,9 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8883
8910
|
const idFields = requireIdFields(this.client.$schema, mutationModel);
|
|
8884
8911
|
if (query.updates) {
|
|
8885
8912
|
for (const update of query.updates) {
|
|
8886
|
-
if (
|
|
8913
|
+
if (import_kysely10.ColumnUpdateNode.is(update)) {
|
|
8887
8914
|
const columnNode = update.column;
|
|
8888
|
-
if (
|
|
8915
|
+
if (import_kysely10.ColumnNode.is(columnNode)) {
|
|
8889
8916
|
const columnName = columnNode.column.name;
|
|
8890
8917
|
if (idFields.includes(columnName)) {
|
|
8891
8918
|
return void 0;
|
|
@@ -8898,7 +8925,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8898
8925
|
}
|
|
8899
8926
|
getInsertIds(mutationModel, query, queryResult) {
|
|
8900
8927
|
const idFields = requireIdFields(this.client.$schema, mutationModel);
|
|
8901
|
-
if (
|
|
8928
|
+
if (import_kysely10.InsertQueryNode.is(query) && queryResult.numAffectedRows === 1n && queryResult.insertId && idFields.length === 1) {
|
|
8902
8929
|
return [
|
|
8903
8930
|
{
|
|
8904
8931
|
[idFields[0]]: queryResult.insertId
|
|
@@ -8910,13 +8937,13 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8910
8937
|
return void 0;
|
|
8911
8938
|
}
|
|
8912
8939
|
const values = query.values;
|
|
8913
|
-
if (!values || !
|
|
8940
|
+
if (!values || !import_kysely10.ValuesNode.is(values)) {
|
|
8914
8941
|
return void 0;
|
|
8915
8942
|
}
|
|
8916
8943
|
const allIds = [];
|
|
8917
8944
|
for (const valuesItem of values.values) {
|
|
8918
8945
|
const rowIds = {};
|
|
8919
|
-
if (
|
|
8946
|
+
if (import_kysely10.PrimitiveValueListNode.is(valuesItem)) {
|
|
8920
8947
|
(0, import_common_helpers12.invariant)(valuesItem.values.length === columns.length, "Values count must match columns count");
|
|
8921
8948
|
for (const idField of idFields) {
|
|
8922
8949
|
const colIndex = columns.findIndex((col) => col.column.name === idField);
|
|
@@ -8933,7 +8960,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8933
8960
|
return void 0;
|
|
8934
8961
|
}
|
|
8935
8962
|
const valueNode = valuesItem.values[colIndex];
|
|
8936
|
-
if (!valueNode || !
|
|
8963
|
+
if (!valueNode || !import_kysely10.ValueNode.is(valueNode)) {
|
|
8937
8964
|
return void 0;
|
|
8938
8965
|
}
|
|
8939
8966
|
rowIds[idField] = valueNode.value;
|
|
@@ -8962,13 +8989,13 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8962
8989
|
// #region utilities
|
|
8963
8990
|
getMutationInfo(queryNode) {
|
|
8964
8991
|
const model = this.getMutationModel(queryNode);
|
|
8965
|
-
const { action, where } = (0, import_ts_pattern12.match)(queryNode).when(
|
|
8992
|
+
const { action, where } = (0, import_ts_pattern12.match)(queryNode).when(import_kysely10.InsertQueryNode.is, () => ({
|
|
8966
8993
|
action: "create",
|
|
8967
8994
|
where: void 0
|
|
8968
|
-
})).when(
|
|
8995
|
+
})).when(import_kysely10.UpdateQueryNode.is, (node) => ({
|
|
8969
8996
|
action: "update",
|
|
8970
8997
|
where: node.where
|
|
8971
|
-
})).when(
|
|
8998
|
+
})).when(import_kysely10.DeleteQueryNode.is, (node) => ({
|
|
8972
8999
|
action: "delete",
|
|
8973
9000
|
where: node.where
|
|
8974
9001
|
})).exhaustive();
|
|
@@ -8979,21 +9006,21 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8979
9006
|
};
|
|
8980
9007
|
}
|
|
8981
9008
|
isMutationNode(queryNode) {
|
|
8982
|
-
return
|
|
9009
|
+
return import_kysely10.InsertQueryNode.is(queryNode) || import_kysely10.UpdateQueryNode.is(queryNode) || import_kysely10.DeleteQueryNode.is(queryNode);
|
|
8983
9010
|
}
|
|
8984
9011
|
getMutationModel(queryNode) {
|
|
8985
|
-
return (0, import_ts_pattern12.match)(queryNode).when(
|
|
9012
|
+
return (0, import_ts_pattern12.match)(queryNode).when(import_kysely10.InsertQueryNode.is, (node) => {
|
|
8986
9013
|
(0, import_common_helpers12.invariant)(node.into, "InsertQueryNode must have an into clause");
|
|
8987
9014
|
return node.into.table.identifier.name;
|
|
8988
|
-
}).when(
|
|
9015
|
+
}).when(import_kysely10.UpdateQueryNode.is, (node) => {
|
|
8989
9016
|
(0, import_common_helpers12.invariant)(node.table, "UpdateQueryNode must have a table");
|
|
8990
9017
|
const { node: tableNode } = stripAlias(node.table);
|
|
8991
|
-
(0, import_common_helpers12.invariant)(
|
|
9018
|
+
(0, import_common_helpers12.invariant)(import_kysely10.TableNode.is(tableNode), "UpdateQueryNode must use a TableNode");
|
|
8992
9019
|
return tableNode.table.identifier.name;
|
|
8993
|
-
}).when(
|
|
9020
|
+
}).when(import_kysely10.DeleteQueryNode.is, (node) => {
|
|
8994
9021
|
(0, import_common_helpers12.invariant)(node.from.froms.length === 1, "Delete query must have exactly one from table");
|
|
8995
9022
|
const { node: tableNode } = stripAlias(node.from.froms[0]);
|
|
8996
|
-
(0, import_common_helpers12.invariant)(
|
|
9023
|
+
(0, import_common_helpers12.invariant)(import_kysely10.TableNode.is(tableNode), "DeleteQueryNode must use a TableNode");
|
|
8997
9024
|
return tableNode.table.identifier.name;
|
|
8998
9025
|
}).otherwise((node) => {
|
|
8999
9026
|
throw createInternalError(`Invalid query node: ${node}`);
|
|
@@ -9014,7 +9041,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
9014
9041
|
}).run(query);
|
|
9015
9042
|
}
|
|
9016
9043
|
createClientForConnection(connection, inTx) {
|
|
9017
|
-
const innerExecutor = this.withConnectionProvider(new
|
|
9044
|
+
const innerExecutor = this.withConnectionProvider(new import_kysely10.SingleConnectionProvider(connection));
|
|
9018
9045
|
innerExecutor.suppressMutationHooks = true;
|
|
9019
9046
|
const innerClient = this.client.withExecutor(innerExecutor);
|
|
9020
9047
|
if (inTx) {
|
|
@@ -9024,32 +9051,76 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
9024
9051
|
}
|
|
9025
9052
|
andNodes(condition1, condition2) {
|
|
9026
9053
|
if (condition1 && condition2) {
|
|
9027
|
-
return
|
|
9054
|
+
return import_kysely10.WhereNode.create(import_kysely10.AndNode.create(condition1, condition2));
|
|
9028
9055
|
} else if (condition1) {
|
|
9029
|
-
return
|
|
9056
|
+
return import_kysely10.WhereNode.create(condition1);
|
|
9030
9057
|
} else {
|
|
9031
9058
|
return condition2;
|
|
9032
9059
|
}
|
|
9033
9060
|
}
|
|
9034
9061
|
async internalExecuteQuery(query, connection, queryId, parameters) {
|
|
9035
9062
|
const finalQuery = this.processQueryNode(query);
|
|
9036
|
-
let compiledQuery = this.compileQuery(finalQuery, queryId ?? (0,
|
|
9063
|
+
let compiledQuery = this.compileQuery(finalQuery, queryId ?? (0, import_kysely10.createQueryId)());
|
|
9037
9064
|
if (parameters) {
|
|
9038
9065
|
compiledQuery = {
|
|
9039
9066
|
...compiledQuery,
|
|
9040
9067
|
parameters
|
|
9041
9068
|
};
|
|
9042
9069
|
}
|
|
9070
|
+
const rlsDef = this.client.$schema.rls;
|
|
9071
|
+
const rlsValues = this.client.$options.rls;
|
|
9072
|
+
const isTransaction = this.driver.isTransactionConnection(connection);
|
|
9073
|
+
const needsRls = rlsDef && rlsValues;
|
|
9074
|
+
const needsRlsInit = needsRls && (!isTransaction || !this.rlsInitializedConnections.has(connection));
|
|
9043
9075
|
const trackSlowQuery = this.options.diagnostics !== void 0;
|
|
9044
9076
|
const startTimestamp = trackSlowQuery ? performance.now() : void 0;
|
|
9045
9077
|
const startedAt = trackSlowQuery ? /* @__PURE__ */ new Date() : void 0;
|
|
9046
9078
|
try {
|
|
9079
|
+
if (needsRlsInit) {
|
|
9080
|
+
if (!isTransaction) {
|
|
9081
|
+
await connection.executeQuery({
|
|
9082
|
+
sql: "BEGIN",
|
|
9083
|
+
parameters: [],
|
|
9084
|
+
query: {
|
|
9085
|
+
kind: "RawNode"
|
|
9086
|
+
},
|
|
9087
|
+
queryId: (0, import_kysely10.createQueryId)()
|
|
9088
|
+
});
|
|
9089
|
+
}
|
|
9090
|
+
await applyRlsContext(connection, rlsDef, rlsValues);
|
|
9091
|
+
if (isTransaction) {
|
|
9092
|
+
this.rlsInitializedConnections.add(connection);
|
|
9093
|
+
}
|
|
9094
|
+
}
|
|
9047
9095
|
const result = await connection.executeQuery(compiledQuery);
|
|
9096
|
+
if (needsRlsInit && !isTransaction) {
|
|
9097
|
+
await connection.executeQuery({
|
|
9098
|
+
sql: "COMMIT",
|
|
9099
|
+
parameters: [],
|
|
9100
|
+
query: {
|
|
9101
|
+
kind: "RawNode"
|
|
9102
|
+
},
|
|
9103
|
+
queryId: (0, import_kysely10.createQueryId)()
|
|
9104
|
+
});
|
|
9105
|
+
}
|
|
9048
9106
|
if (startTimestamp !== void 0) {
|
|
9049
9107
|
this.trackSlowQuery(compiledQuery, startTimestamp, startedAt);
|
|
9050
9108
|
}
|
|
9051
9109
|
return this.ensureProperQueryResult(compiledQuery.query, result);
|
|
9052
9110
|
} catch (err) {
|
|
9111
|
+
if (needsRlsInit && !isTransaction) {
|
|
9112
|
+
try {
|
|
9113
|
+
await connection.executeQuery({
|
|
9114
|
+
sql: "ROLLBACK",
|
|
9115
|
+
parameters: [],
|
|
9116
|
+
query: {
|
|
9117
|
+
kind: "RawNode"
|
|
9118
|
+
},
|
|
9119
|
+
queryId: (0, import_kysely10.createQueryId)()
|
|
9120
|
+
});
|
|
9121
|
+
} catch {
|
|
9122
|
+
}
|
|
9123
|
+
}
|
|
9053
9124
|
throw createDBQueryError(`Failed to execute query: ${err}`, err, compiledQuery.sql, compiledQuery.parameters);
|
|
9054
9125
|
}
|
|
9055
9126
|
}
|
|
@@ -9139,7 +9210,7 @@ __export(functions_exports, {
|
|
|
9139
9210
|
startsWith: () => startsWith
|
|
9140
9211
|
});
|
|
9141
9212
|
var import_common_helpers13 = require("@zenstackhq/common-helpers");
|
|
9142
|
-
var
|
|
9213
|
+
var import_kysely11 = require("kysely");
|
|
9143
9214
|
var import_ts_pattern13 = require("ts-pattern");
|
|
9144
9215
|
var contains = /* @__PURE__ */ __name((eb, args, context) => textMatch(eb, args, context, "contains"), "contains");
|
|
9145
9216
|
var search = /* @__PURE__ */ __name((_eb, _args) => {
|
|
@@ -9177,20 +9248,20 @@ var textMatch = /* @__PURE__ */ __name((eb, args, { dialect }, method) => {
|
|
|
9177
9248
|
} else {
|
|
9178
9249
|
op = "like";
|
|
9179
9250
|
}
|
|
9180
|
-
searchExpr = eb.fn.coalesce(searchExpr,
|
|
9181
|
-
const escapedSearch =
|
|
9251
|
+
searchExpr = eb.fn.coalesce(searchExpr, import_kysely11.sql.lit(""));
|
|
9252
|
+
const escapedSearch = import_kysely11.sql`REPLACE(REPLACE(REPLACE(${dialect.castText(searchExpr)}, ${import_kysely11.sql.val("\\")}, ${import_kysely11.sql.val("\\\\")}), ${import_kysely11.sql.val("%")}, ${import_kysely11.sql.val("\\%")}), ${import_kysely11.sql.val("_")}, ${import_kysely11.sql.val("\\_")})`;
|
|
9182
9253
|
searchExpr = (0, import_ts_pattern13.match)(method).with("contains", () => eb.fn("CONCAT", [
|
|
9183
|
-
|
|
9254
|
+
import_kysely11.sql.lit("%"),
|
|
9184
9255
|
escapedSearch,
|
|
9185
|
-
|
|
9256
|
+
import_kysely11.sql.lit("%")
|
|
9186
9257
|
])).with("startsWith", () => eb.fn("CONCAT", [
|
|
9187
9258
|
escapedSearch,
|
|
9188
|
-
|
|
9259
|
+
import_kysely11.sql.lit("%")
|
|
9189
9260
|
])).with("endsWith", () => eb.fn("CONCAT", [
|
|
9190
|
-
|
|
9261
|
+
import_kysely11.sql.lit("%"),
|
|
9191
9262
|
escapedSearch
|
|
9192
9263
|
])).exhaustive();
|
|
9193
|
-
return
|
|
9264
|
+
return import_kysely11.sql`${fieldExpr} ${import_kysely11.sql.raw(op)} ${searchExpr} escape ${import_kysely11.sql.val("\\")}`;
|
|
9194
9265
|
}, "textMatch");
|
|
9195
9266
|
var has = /* @__PURE__ */ __name((_eb, args, context) => {
|
|
9196
9267
|
const [field, search2] = args;
|
|
@@ -9227,16 +9298,16 @@ var isEmpty = /* @__PURE__ */ __name((eb, args, { dialect }) => {
|
|
|
9227
9298
|
if (!field) {
|
|
9228
9299
|
throw new Error('"field" parameter is required');
|
|
9229
9300
|
}
|
|
9230
|
-
return eb(dialect.buildArrayLength(field), "=",
|
|
9301
|
+
return eb(dialect.buildArrayLength(field), "=", import_kysely11.sql.lit(0));
|
|
9231
9302
|
}, "isEmpty");
|
|
9232
|
-
var now = /* @__PURE__ */ __name((_eb, _args, context) => (0, import_ts_pattern13.match)(context.dialect.provider).with("sqlite", () =>
|
|
9303
|
+
var now = /* @__PURE__ */ __name((_eb, _args, context) => (0, import_ts_pattern13.match)(context.dialect.provider).with("sqlite", () => import_kysely11.sql.raw("strftime('%Y-%m-%dT%H:%M:%fZ')")).with("mysql", () => import_kysely11.sql.raw("CONCAT(SUBSTRING(DATE_FORMAT(UTC_TIMESTAMP(3), '%Y-%m-%dT%H:%i:%s.%f'), 1, 23), '+00:00')")).with("postgresql", () => import_kysely11.sql.raw("CURRENT_TIMESTAMP")).exhaustive(), "now");
|
|
9233
9304
|
var currentModel = /* @__PURE__ */ __name((_eb, args, { model }) => {
|
|
9234
9305
|
let result = model;
|
|
9235
9306
|
const [casing] = args;
|
|
9236
9307
|
if (casing) {
|
|
9237
9308
|
result = processCasing(casing, result, model);
|
|
9238
9309
|
}
|
|
9239
|
-
return
|
|
9310
|
+
return import_kysely11.sql.lit(result);
|
|
9240
9311
|
}, "currentModel");
|
|
9241
9312
|
var currentOperation = /* @__PURE__ */ __name((_eb, args, { operation }) => {
|
|
9242
9313
|
let result = operation;
|
|
@@ -9244,11 +9315,11 @@ var currentOperation = /* @__PURE__ */ __name((_eb, args, { operation }) => {
|
|
|
9244
9315
|
if (casing) {
|
|
9245
9316
|
result = processCasing(casing, result, operation);
|
|
9246
9317
|
}
|
|
9247
|
-
return
|
|
9318
|
+
return import_kysely11.sql.lit(result);
|
|
9248
9319
|
}, "currentOperation");
|
|
9249
9320
|
function processCasing(casing, result, model) {
|
|
9250
9321
|
const opNode = casing.toOperationNode();
|
|
9251
|
-
(0, import_common_helpers13.invariant)(
|
|
9322
|
+
(0, import_common_helpers13.invariant)(import_kysely11.ValueNode.is(opNode) && typeof opNode.value === "string", '"casting" parameter must be a string value');
|
|
9252
9323
|
result = (0, import_ts_pattern13.match)(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => (0, import_common_helpers13.upperCaseFirst)(result)).with("uncapitalize", () => (0, import_common_helpers13.lowerCaseFirst)(result)).otherwise(() => {
|
|
9253
9324
|
throw new Error(`Invalid casing value: ${opNode.value}. Must be "original", "upper", "lower", "capitalize", or "uncapitalize".`);
|
|
9254
9325
|
});
|
|
@@ -9260,14 +9331,14 @@ function readBoolean(expr, defaultValue) {
|
|
|
9260
9331
|
return defaultValue;
|
|
9261
9332
|
}
|
|
9262
9333
|
const opNode = expr.toOperationNode();
|
|
9263
|
-
(0, import_common_helpers13.invariant)(
|
|
9334
|
+
(0, import_common_helpers13.invariant)(import_kysely11.ValueNode.is(opNode), "expression must be a literal value");
|
|
9264
9335
|
return !!opNode.value;
|
|
9265
9336
|
}
|
|
9266
9337
|
__name(readBoolean, "readBoolean");
|
|
9267
9338
|
|
|
9268
9339
|
// src/client/helpers/schema-db-pusher.ts
|
|
9269
9340
|
var import_common_helpers14 = require("@zenstackhq/common-helpers");
|
|
9270
|
-
var
|
|
9341
|
+
var import_kysely12 = require("kysely");
|
|
9271
9342
|
var import_toposort = __toESM(require("toposort"), 1);
|
|
9272
9343
|
var import_ts_pattern14 = require("ts-pattern");
|
|
9273
9344
|
var SchemaDbPusher = class {
|
|
@@ -9451,7 +9522,7 @@ var SchemaDbPusher = class {
|
|
|
9451
9522
|
if (fieldDef.default !== void 0 && this.isDefaultValueSupportedForType(fieldDef.type)) {
|
|
9452
9523
|
if (typeof fieldDef.default === "object" && "kind" in fieldDef.default) {
|
|
9453
9524
|
if (schema_exports.ExpressionUtils.isCall(fieldDef.default) && fieldDef.default.function === "now") {
|
|
9454
|
-
col = this.schema.provider.type === "mysql" ? col.defaultTo(
|
|
9525
|
+
col = this.schema.provider.type === "mysql" ? col.defaultTo(import_kysely12.sql`CURRENT_TIMESTAMP(3)`) : col.defaultTo(import_kysely12.sql`CURRENT_TIMESTAMP`);
|
|
9455
9526
|
}
|
|
9456
9527
|
} else {
|
|
9457
9528
|
if (this.schema.provider.type === "mysql" && fieldDef.type === "DateTime" && typeof fieldDef.default === "string") {
|
|
@@ -9483,7 +9554,7 @@ var SchemaDbPusher = class {
|
|
|
9483
9554
|
mapFieldType(fieldDef) {
|
|
9484
9555
|
if (this.schema.enums?.[fieldDef.type]) {
|
|
9485
9556
|
if (this.schema.provider.type === "postgresql") {
|
|
9486
|
-
return
|
|
9557
|
+
return import_kysely12.sql.ref(fieldDef.type);
|
|
9487
9558
|
} else if (this.schema.provider.type === "mysql") {
|
|
9488
9559
|
const enumDef = this.schema.enums[fieldDef.type];
|
|
9489
9560
|
let enumValues;
|
|
@@ -9501,7 +9572,7 @@ var SchemaDbPusher = class {
|
|
|
9501
9572
|
} else {
|
|
9502
9573
|
enumValues = Object.values(enumDef.values);
|
|
9503
9574
|
}
|
|
9504
|
-
return
|
|
9575
|
+
return import_kysely12.sql.raw(`enum(${enumValues.map((v) => `'${v}'`).join(", ")})`);
|
|
9505
9576
|
} else {
|
|
9506
9577
|
return "text";
|
|
9507
9578
|
}
|
|
@@ -9517,7 +9588,7 @@ var SchemaDbPusher = class {
|
|
|
9517
9588
|
throw new Error(`Unsupported field type: ${type}`);
|
|
9518
9589
|
});
|
|
9519
9590
|
if (fieldDef.array) {
|
|
9520
|
-
return
|
|
9591
|
+
return import_kysely12.sql.raw(`${result}[]`);
|
|
9521
9592
|
} else {
|
|
9522
9593
|
return result;
|
|
9523
9594
|
}
|
|
@@ -9563,25 +9634,25 @@ var SchemaDbPusher = class {
|
|
|
9563
9634
|
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("postgresql", () => "bytea").with("mysql", () => "blob").otherwise(() => "blob");
|
|
9564
9635
|
}
|
|
9565
9636
|
get stringType() {
|
|
9566
|
-
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () =>
|
|
9637
|
+
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () => import_kysely12.sql.raw("varchar(255)")).otherwise(() => "text");
|
|
9567
9638
|
}
|
|
9568
9639
|
get booleanType() {
|
|
9569
|
-
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () =>
|
|
9640
|
+
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () => import_kysely12.sql.raw("tinyint(1)")).otherwise(() => "boolean");
|
|
9570
9641
|
}
|
|
9571
9642
|
get intType() {
|
|
9572
9643
|
return "integer";
|
|
9573
9644
|
}
|
|
9574
9645
|
get floatType() {
|
|
9575
|
-
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("postgresql", () => "double precision").with("mysql", () =>
|
|
9646
|
+
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("postgresql", () => "double precision").with("mysql", () => import_kysely12.sql.raw("double")).otherwise(() => "real");
|
|
9576
9647
|
}
|
|
9577
9648
|
get bigIntType() {
|
|
9578
9649
|
return "bigint";
|
|
9579
9650
|
}
|
|
9580
9651
|
get decimalType() {
|
|
9581
|
-
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () =>
|
|
9652
|
+
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () => import_kysely12.sql.raw("decimal(65, 30)")).otherwise(() => "decimal");
|
|
9582
9653
|
}
|
|
9583
9654
|
get dateTimeType() {
|
|
9584
|
-
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () =>
|
|
9655
|
+
return (0, import_ts_pattern14.match)(this.schema.provider.type).with("mysql", () => import_kysely12.sql.raw("datetime(3)")).otherwise(() => "timestamp");
|
|
9585
9656
|
}
|
|
9586
9657
|
columnSupportsAutoIncrement() {
|
|
9587
9658
|
return [
|
|
@@ -9799,16 +9870,16 @@ var ClientImpl = class _ClientImpl {
|
|
|
9799
9870
|
if (baseClient) {
|
|
9800
9871
|
this.kyselyProps = {
|
|
9801
9872
|
...baseClient.kyselyProps,
|
|
9802
|
-
executor: executor ?? new ZenStackQueryExecutor(this, baseClient.kyselyProps.driver, baseClient.kyselyProps.dialect.createQueryCompiler(), baseClient.kyselyProps.dialect.createAdapter(), new
|
|
9873
|
+
executor: executor ?? new ZenStackQueryExecutor(this, baseClient.kyselyProps.driver, baseClient.kyselyProps.dialect.createQueryCompiler(), baseClient.kyselyProps.dialect.createAdapter(), new import_kysely13.DefaultConnectionProvider(baseClient.kyselyProps.driver))
|
|
9803
9874
|
};
|
|
9804
9875
|
this.kyselyRaw = baseClient.kyselyRaw;
|
|
9805
9876
|
this.auth = baseClient.auth;
|
|
9806
9877
|
this.slowQueries = baseClient.slowQueries;
|
|
9807
9878
|
} else {
|
|
9808
|
-
const driver = new ZenStackDriver(options.dialect.createDriver(), new
|
|
9879
|
+
const driver = new ZenStackDriver(options.dialect.createDriver(), new import_kysely13.Log(this.$options.log ?? []));
|
|
9809
9880
|
const compiler = options.dialect.createQueryCompiler();
|
|
9810
9881
|
const adapter = options.dialect.createAdapter();
|
|
9811
|
-
const connectionProvider = new
|
|
9882
|
+
const connectionProvider = new import_kysely13.DefaultConnectionProvider(driver);
|
|
9812
9883
|
this.kyselyProps = {
|
|
9813
9884
|
config: {
|
|
9814
9885
|
dialect: options.dialect,
|
|
@@ -9819,15 +9890,15 @@ var ClientImpl = class _ClientImpl {
|
|
|
9819
9890
|
driver,
|
|
9820
9891
|
executor: executor ?? new ZenStackQueryExecutor(this, driver, compiler, adapter, connectionProvider)
|
|
9821
9892
|
};
|
|
9822
|
-
this.kyselyRaw = new
|
|
9893
|
+
this.kyselyRaw = new import_kysely13.Kysely({
|
|
9823
9894
|
...this.kyselyProps,
|
|
9824
|
-
executor: new
|
|
9895
|
+
executor: new import_kysely13.DefaultQueryExecutor(compiler, adapter, connectionProvider, [])
|
|
9825
9896
|
});
|
|
9826
9897
|
}
|
|
9827
9898
|
if (baseClient?.isTransaction && !executor) {
|
|
9828
9899
|
this.kysely = baseClient.$qb;
|
|
9829
9900
|
} else {
|
|
9830
|
-
this.kysely = new
|
|
9901
|
+
this.kysely = new import_kysely13.Kysely(this.kyselyProps);
|
|
9831
9902
|
}
|
|
9832
9903
|
this.inputValidator = baseClient?.inputValidator ?? new InputValidator(this, {
|
|
9833
9904
|
enabled: this.$options.validateInput !== false
|
|
@@ -9898,7 +9969,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
9898
9969
|
}
|
|
9899
9970
|
forceTransaction() {
|
|
9900
9971
|
if (!this.kysely.isTransaction) {
|
|
9901
|
-
this.kysely = new
|
|
9972
|
+
this.kysely = new import_kysely13.Transaction(this.kyselyProps);
|
|
9902
9973
|
}
|
|
9903
9974
|
}
|
|
9904
9975
|
async interactiveTransaction(callback, options) {
|
|
@@ -9989,7 +10060,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
9989
10060
|
}
|
|
9990
10061
|
async $connect() {
|
|
9991
10062
|
await this.kysely.connection().execute(async (conn) => {
|
|
9992
|
-
await conn.executeQuery(
|
|
10063
|
+
await conn.executeQuery(import_kysely13.sql`select 1`.compile(this.kysely));
|
|
9993
10064
|
});
|
|
9994
10065
|
}
|
|
9995
10066
|
async $disconnect() {
|
|
@@ -10076,7 +10147,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
10076
10147
|
}
|
|
10077
10148
|
$executeRaw(query, ...values) {
|
|
10078
10149
|
return createZenStackPromise(async () => {
|
|
10079
|
-
const result = await (0,
|
|
10150
|
+
const result = await (0, import_kysely13.sql)(query, ...values).execute(this.kysely);
|
|
10080
10151
|
return Number(result.numAffectedRows ?? 0);
|
|
10081
10152
|
});
|
|
10082
10153
|
}
|
|
@@ -10089,7 +10160,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
10089
10160
|
}
|
|
10090
10161
|
$queryRaw(query, ...values) {
|
|
10091
10162
|
return createZenStackPromise(async () => {
|
|
10092
|
-
const result = await (0,
|
|
10163
|
+
const result = await (0, import_kysely13.sql)(query, ...values).execute(this.kysely);
|
|
10093
10164
|
return result.rows;
|
|
10094
10165
|
});
|
|
10095
10166
|
}
|
|
@@ -10101,7 +10172,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
10101
10172
|
});
|
|
10102
10173
|
}
|
|
10103
10174
|
createRawCompiledQuery(query, values) {
|
|
10104
|
-
const q =
|
|
10175
|
+
const q = import_kysely13.CompiledQuery.raw(query, values);
|
|
10105
10176
|
return {
|
|
10106
10177
|
...q,
|
|
10107
10178
|
$raw: true
|
|
@@ -10837,8 +10908,8 @@ var kysely_utils_exports = {};
|
|
|
10837
10908
|
__export(kysely_utils_exports, {
|
|
10838
10909
|
DefaultOperationNodeVisitor: () => DefaultOperationNodeVisitor
|
|
10839
10910
|
});
|
|
10840
|
-
var
|
|
10841
|
-
var DefaultOperationNodeVisitor = class extends
|
|
10911
|
+
var import_kysely14 = require("kysely");
|
|
10912
|
+
var DefaultOperationNodeVisitor = class extends import_kysely14.OperationNodeVisitor {
|
|
10842
10913
|
static {
|
|
10843
10914
|
__name(this, "DefaultOperationNodeVisitor");
|
|
10844
10915
|
}
|