@zenstackhq/better-auth 3.0.0-beta.24
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/LICENSE +21 -0
- package/dist/index.cjs +696 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +661 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ZenStack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var src_exports = {};
|
|
33
|
+
__export(src_exports, {
|
|
34
|
+
zenstackAdapter: () => zenstackAdapter
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
|
|
38
|
+
// src/adapter.ts
|
|
39
|
+
var import_error = require("@better-auth/core/error");
|
|
40
|
+
var import_adapters = require("better-auth/adapters");
|
|
41
|
+
|
|
42
|
+
// src/schema-generator.ts
|
|
43
|
+
var import_common_helpers = require("@zenstackhq/common-helpers");
|
|
44
|
+
var import_language = require("@zenstackhq/language");
|
|
45
|
+
var import_ast = require("@zenstackhq/language/ast");
|
|
46
|
+
var import_utils = require("@zenstackhq/language/utils");
|
|
47
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
48
|
+
var import_ts_pattern = require("ts-pattern");
|
|
49
|
+
async function generateSchema(file, tables, config, options) {
|
|
50
|
+
let filePath = file;
|
|
51
|
+
if (!filePath) {
|
|
52
|
+
if (import_node_fs.default.existsSync("./schema.zmodel")) {
|
|
53
|
+
filePath = "./schema.zmodel";
|
|
54
|
+
} else {
|
|
55
|
+
filePath = "./zenstack/schema.zmodel";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const schemaExists = import_node_fs.default.existsSync(filePath);
|
|
59
|
+
const schema = await updateSchema(filePath, tables, config, options);
|
|
60
|
+
return {
|
|
61
|
+
code: schema ?? "",
|
|
62
|
+
path: filePath,
|
|
63
|
+
overwrite: schemaExists && !!schema
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
__name(generateSchema, "generateSchema");
|
|
67
|
+
async function updateSchema(schemaPath, tables, config, options) {
|
|
68
|
+
let zmodel;
|
|
69
|
+
if (import_node_fs.default.existsSync(schemaPath)) {
|
|
70
|
+
const loadResult = await (0, import_language.loadDocument)(schemaPath);
|
|
71
|
+
if (!loadResult.success) {
|
|
72
|
+
throw new Error(`Failed to load existing schema at ${schemaPath}: ${loadResult.errors.join(", ")}`);
|
|
73
|
+
}
|
|
74
|
+
zmodel = loadResult.model;
|
|
75
|
+
} else {
|
|
76
|
+
zmodel = initializeZmodel(config);
|
|
77
|
+
}
|
|
78
|
+
const toManyRelations = /* @__PURE__ */ new Map();
|
|
79
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
80
|
+
const fields = tables[tableName]?.fields;
|
|
81
|
+
for (const field in fields) {
|
|
82
|
+
const attr = fields[field];
|
|
83
|
+
if (attr.references) {
|
|
84
|
+
const referencedOriginalModel = attr.references.model;
|
|
85
|
+
const referencedCustomModel = tables[referencedOriginalModel]?.modelName || referencedOriginalModel;
|
|
86
|
+
const referencedModelNameCap = (0, import_common_helpers.upperCaseFirst)(referencedCustomModel);
|
|
87
|
+
if (!toManyRelations.has(referencedModelNameCap)) {
|
|
88
|
+
toManyRelations.set(referencedModelNameCap, /* @__PURE__ */ new Set());
|
|
89
|
+
}
|
|
90
|
+
const currentCustomModel = table.modelName ?? tableName;
|
|
91
|
+
const currentModelNameCap = (0, import_common_helpers.upperCaseFirst)(currentCustomModel);
|
|
92
|
+
toManyRelations.get(referencedModelNameCap).add(currentModelNameCap);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let changed = false;
|
|
97
|
+
for (const [name, table] of Object.entries(tables)) {
|
|
98
|
+
const c = addOrUpdateModel(name, table, zmodel, tables, toManyRelations, !!options.advanced?.database?.useNumberId);
|
|
99
|
+
changed = changed || c;
|
|
100
|
+
}
|
|
101
|
+
if (!changed) {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
const generator = new import_language.ZModelCodeGenerator();
|
|
105
|
+
let content = generator.generate(zmodel);
|
|
106
|
+
try {
|
|
107
|
+
content = await (0, import_language.formatDocument)(content);
|
|
108
|
+
} catch {
|
|
109
|
+
}
|
|
110
|
+
return content;
|
|
111
|
+
}
|
|
112
|
+
__name(updateSchema, "updateSchema");
|
|
113
|
+
function addDefaultNow(df) {
|
|
114
|
+
const nowArg = {
|
|
115
|
+
$type: "AttributeArg"
|
|
116
|
+
};
|
|
117
|
+
const nowExpr = {
|
|
118
|
+
$type: "InvocationExpr",
|
|
119
|
+
function: {
|
|
120
|
+
$refText: "now"
|
|
121
|
+
},
|
|
122
|
+
args: [],
|
|
123
|
+
$container: nowArg
|
|
124
|
+
};
|
|
125
|
+
nowArg.value = nowExpr;
|
|
126
|
+
addFieldAttribute(df, "@default", [
|
|
127
|
+
nowArg
|
|
128
|
+
]);
|
|
129
|
+
}
|
|
130
|
+
__name(addDefaultNow, "addDefaultNow");
|
|
131
|
+
function createDataModel(modelName, zmodel, numericId) {
|
|
132
|
+
const dataModel = {
|
|
133
|
+
$type: "DataModel",
|
|
134
|
+
name: modelName,
|
|
135
|
+
fields: [],
|
|
136
|
+
attributes: [],
|
|
137
|
+
mixins: [],
|
|
138
|
+
comments: [],
|
|
139
|
+
isView: false,
|
|
140
|
+
$container: zmodel
|
|
141
|
+
};
|
|
142
|
+
let idField;
|
|
143
|
+
if (numericId) {
|
|
144
|
+
idField = addModelField(dataModel, "id", "Int", false, false);
|
|
145
|
+
} else {
|
|
146
|
+
idField = addModelField(dataModel, "id", "String", false, false);
|
|
147
|
+
}
|
|
148
|
+
addFieldAttribute(idField, "@id");
|
|
149
|
+
return dataModel;
|
|
150
|
+
}
|
|
151
|
+
__name(createDataModel, "createDataModel");
|
|
152
|
+
function addModelField(dataModel, fieldName, fieldType, array, optional) {
|
|
153
|
+
const field = {
|
|
154
|
+
$type: "DataField",
|
|
155
|
+
name: fieldName,
|
|
156
|
+
attributes: [],
|
|
157
|
+
comments: [],
|
|
158
|
+
$container: dataModel
|
|
159
|
+
};
|
|
160
|
+
field.type = {
|
|
161
|
+
$type: "DataFieldType",
|
|
162
|
+
type: fieldType,
|
|
163
|
+
array,
|
|
164
|
+
optional,
|
|
165
|
+
$container: field
|
|
166
|
+
};
|
|
167
|
+
dataModel.fields.push(field);
|
|
168
|
+
return field;
|
|
169
|
+
}
|
|
170
|
+
__name(addModelField, "addModelField");
|
|
171
|
+
function initializeZmodel(config) {
|
|
172
|
+
const zmodel = {
|
|
173
|
+
$type: "Model",
|
|
174
|
+
declarations: [],
|
|
175
|
+
imports: []
|
|
176
|
+
};
|
|
177
|
+
const ds = {
|
|
178
|
+
$type: "DataSource",
|
|
179
|
+
name: "db",
|
|
180
|
+
fields: [],
|
|
181
|
+
$container: zmodel
|
|
182
|
+
};
|
|
183
|
+
zmodel.declarations.push(ds);
|
|
184
|
+
const providerField = {
|
|
185
|
+
$type: "ConfigField",
|
|
186
|
+
name: "provider",
|
|
187
|
+
$container: ds
|
|
188
|
+
};
|
|
189
|
+
providerField.value = {
|
|
190
|
+
$type: "StringLiteral",
|
|
191
|
+
value: config.provider,
|
|
192
|
+
$container: providerField
|
|
193
|
+
};
|
|
194
|
+
const urlField = {
|
|
195
|
+
$type: "ConfigField",
|
|
196
|
+
name: "url",
|
|
197
|
+
$container: ds
|
|
198
|
+
};
|
|
199
|
+
const envCall = {
|
|
200
|
+
$type: "InvocationExpr",
|
|
201
|
+
function: {
|
|
202
|
+
$refText: "env"
|
|
203
|
+
},
|
|
204
|
+
args: [],
|
|
205
|
+
$container: urlField
|
|
206
|
+
};
|
|
207
|
+
const dbUrlArg = {
|
|
208
|
+
$type: "Argument"
|
|
209
|
+
};
|
|
210
|
+
dbUrlArg.value = {
|
|
211
|
+
$type: "StringLiteral",
|
|
212
|
+
value: "DATABASE_URL",
|
|
213
|
+
$container: dbUrlArg
|
|
214
|
+
};
|
|
215
|
+
envCall.args = [
|
|
216
|
+
dbUrlArg
|
|
217
|
+
];
|
|
218
|
+
urlField.value = config.provider === "sqlite" ? {
|
|
219
|
+
$type: "StringLiteral",
|
|
220
|
+
value: "file:./dev.db",
|
|
221
|
+
$container: urlField
|
|
222
|
+
} : envCall;
|
|
223
|
+
ds.fields.push(providerField);
|
|
224
|
+
ds.fields.push(urlField);
|
|
225
|
+
return zmodel;
|
|
226
|
+
}
|
|
227
|
+
__name(initializeZmodel, "initializeZmodel");
|
|
228
|
+
function getMappedFieldType({ bigint, type }) {
|
|
229
|
+
return (0, import_ts_pattern.match)(type).with("string", () => ({
|
|
230
|
+
type: "String"
|
|
231
|
+
})).with("number", () => bigint ? {
|
|
232
|
+
type: "BigInt"
|
|
233
|
+
} : {
|
|
234
|
+
type: "Int"
|
|
235
|
+
}).with("boolean", () => ({
|
|
236
|
+
type: "Boolean"
|
|
237
|
+
})).with("date", () => ({
|
|
238
|
+
type: "DateTime"
|
|
239
|
+
})).with("json", () => ({
|
|
240
|
+
type: "Json"
|
|
241
|
+
})).with("string[]", () => ({
|
|
242
|
+
type: "String",
|
|
243
|
+
array: true
|
|
244
|
+
})).with("number[]", () => ({
|
|
245
|
+
type: "Int",
|
|
246
|
+
array: true
|
|
247
|
+
})).otherwise(() => {
|
|
248
|
+
throw new Error(`Unsupported field type: ${type}`);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
__name(getMappedFieldType, "getMappedFieldType");
|
|
252
|
+
function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, numericId) {
|
|
253
|
+
let changed = false;
|
|
254
|
+
const customModelName = tables[tableName]?.modelName ?? tableName;
|
|
255
|
+
const modelName = (0, import_common_helpers.upperCaseFirst)(customModelName);
|
|
256
|
+
let dataModel = zmodel.declarations.find((d) => (0, import_ast.isDataModel)(d) && d.name === modelName);
|
|
257
|
+
if (!dataModel) {
|
|
258
|
+
changed = true;
|
|
259
|
+
dataModel = createDataModel(modelName, zmodel, numericId);
|
|
260
|
+
zmodel.declarations.push(dataModel);
|
|
261
|
+
}
|
|
262
|
+
if (modelName !== tableName && !(0, import_utils.hasAttribute)(dataModel, "@@map")) {
|
|
263
|
+
addModelAttribute(dataModel, "@@map", [
|
|
264
|
+
createStringAttributeArg(tableName)
|
|
265
|
+
]);
|
|
266
|
+
}
|
|
267
|
+
for (const [fName, field] of Object.entries(table.fields)) {
|
|
268
|
+
const fieldName = field.fieldName ?? fName;
|
|
269
|
+
if (dataModel.fields.some((f) => f.name === fieldName)) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
changed = true;
|
|
273
|
+
if (!field.references) {
|
|
274
|
+
const { array, type } = getMappedFieldType(field);
|
|
275
|
+
const df = {
|
|
276
|
+
$type: "DataField",
|
|
277
|
+
name: fieldName,
|
|
278
|
+
attributes: [],
|
|
279
|
+
comments: [],
|
|
280
|
+
$container: dataModel
|
|
281
|
+
};
|
|
282
|
+
df.type = {
|
|
283
|
+
$type: "DataFieldType",
|
|
284
|
+
type,
|
|
285
|
+
array: !!array,
|
|
286
|
+
optional: !field.required,
|
|
287
|
+
$container: df
|
|
288
|
+
};
|
|
289
|
+
dataModel.fields.push(df);
|
|
290
|
+
if (fieldName === "id") {
|
|
291
|
+
addFieldAttribute(df, "@id");
|
|
292
|
+
}
|
|
293
|
+
if (field.unique) {
|
|
294
|
+
addFieldAttribute(df, "@unique");
|
|
295
|
+
}
|
|
296
|
+
if (field.defaultValue !== void 0) {
|
|
297
|
+
if (fieldName === "createdAt") {
|
|
298
|
+
addDefaultNow(df);
|
|
299
|
+
} else if (typeof field.defaultValue === "boolean") {
|
|
300
|
+
addFieldAttribute(df, "@default", [
|
|
301
|
+
createBooleanAttributeArg(field.defaultValue)
|
|
302
|
+
]);
|
|
303
|
+
} else if (typeof field.defaultValue === "function") {
|
|
304
|
+
const defaultVal = field.defaultValue();
|
|
305
|
+
if (defaultVal instanceof Date) {
|
|
306
|
+
addDefaultNow(df);
|
|
307
|
+
} else {
|
|
308
|
+
console.warn(`Warning: Unsupported default function for field ${fieldName} in model ${table.modelName}. Please adjust manually.`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (fieldName === "updatedAt" && field.onUpdate) {
|
|
313
|
+
addFieldAttribute(df, "@updatedAt");
|
|
314
|
+
} else if (field.onUpdate) {
|
|
315
|
+
console.warn(`Warning: 'onUpdate' is only supported on 'updatedAt' fields. Please adjust manually for field ${fieldName} in model ${table.modelName}.`);
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
addModelField(dataModel, fieldName, numericId ? "Int" : "String", false, !field.required);
|
|
319
|
+
const referencedOriginalModelName = field.references.model;
|
|
320
|
+
const referencedCustomModelName = tables[referencedOriginalModelName]?.modelName || referencedOriginalModelName;
|
|
321
|
+
const relationField = {
|
|
322
|
+
$type: "DataField",
|
|
323
|
+
name: (0, import_common_helpers.lowerCaseFirst)(referencedCustomModelName),
|
|
324
|
+
attributes: [],
|
|
325
|
+
comments: [],
|
|
326
|
+
$container: dataModel
|
|
327
|
+
};
|
|
328
|
+
relationField.type = {
|
|
329
|
+
$type: "DataFieldType",
|
|
330
|
+
reference: {
|
|
331
|
+
$refText: (0, import_common_helpers.upperCaseFirst)(referencedCustomModelName)
|
|
332
|
+
},
|
|
333
|
+
array: field.type.endsWith("[]"),
|
|
334
|
+
optional: !field.required,
|
|
335
|
+
$container: relationField
|
|
336
|
+
};
|
|
337
|
+
let action = "Cascade";
|
|
338
|
+
if (field.references.onDelete === "no action") action = "NoAction";
|
|
339
|
+
else if (field.references.onDelete === "set null") action = "SetNull";
|
|
340
|
+
else if (field.references.onDelete === "set default") action = "SetDefault";
|
|
341
|
+
else if (field.references.onDelete === "restrict") action = "Restrict";
|
|
342
|
+
const relationAttr = {
|
|
343
|
+
$type: "DataFieldAttribute",
|
|
344
|
+
decl: {
|
|
345
|
+
$refText: "@relation"
|
|
346
|
+
},
|
|
347
|
+
args: [],
|
|
348
|
+
$container: relationField
|
|
349
|
+
};
|
|
350
|
+
const fieldsArg = {
|
|
351
|
+
$type: "AttributeArg",
|
|
352
|
+
name: "fields",
|
|
353
|
+
$container: relationAttr
|
|
354
|
+
};
|
|
355
|
+
const fieldsExpr = {
|
|
356
|
+
$type: "ArrayExpr",
|
|
357
|
+
items: [],
|
|
358
|
+
$container: fieldsArg
|
|
359
|
+
};
|
|
360
|
+
const fkRefExpr = {
|
|
361
|
+
$type: "ReferenceExpr",
|
|
362
|
+
args: [],
|
|
363
|
+
$container: fieldsExpr,
|
|
364
|
+
target: {
|
|
365
|
+
$refText: fieldName
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
fieldsExpr.items.push(fkRefExpr);
|
|
369
|
+
fieldsArg.value = fieldsExpr;
|
|
370
|
+
const referencesArg = {
|
|
371
|
+
$type: "AttributeArg",
|
|
372
|
+
name: "references",
|
|
373
|
+
$container: relationAttr
|
|
374
|
+
};
|
|
375
|
+
const referencesExpr = {
|
|
376
|
+
$type: "ArrayExpr",
|
|
377
|
+
items: [],
|
|
378
|
+
$container: referencesArg
|
|
379
|
+
};
|
|
380
|
+
const pkRefExpr = {
|
|
381
|
+
$type: "ReferenceExpr",
|
|
382
|
+
args: [],
|
|
383
|
+
$container: referencesExpr,
|
|
384
|
+
target: {
|
|
385
|
+
$refText: field.references.field
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
referencesExpr.items.push(pkRefExpr);
|
|
389
|
+
referencesArg.value = referencesExpr;
|
|
390
|
+
const onDeleteArg = {
|
|
391
|
+
$type: "AttributeArg",
|
|
392
|
+
name: "onDelete",
|
|
393
|
+
$container: relationAttr
|
|
394
|
+
};
|
|
395
|
+
const onDeleteValueExpr = {
|
|
396
|
+
$type: "ReferenceExpr",
|
|
397
|
+
target: {
|
|
398
|
+
$refText: action
|
|
399
|
+
},
|
|
400
|
+
args: [],
|
|
401
|
+
$container: onDeleteArg
|
|
402
|
+
};
|
|
403
|
+
onDeleteArg.value = onDeleteValueExpr;
|
|
404
|
+
relationAttr.args.push(...[
|
|
405
|
+
fieldsArg,
|
|
406
|
+
referencesArg,
|
|
407
|
+
onDeleteArg
|
|
408
|
+
]);
|
|
409
|
+
relationField.attributes.push(relationAttr);
|
|
410
|
+
dataModel.fields.push(relationField);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (toManyRelations.has(modelName)) {
|
|
414
|
+
const relations = toManyRelations.get(modelName);
|
|
415
|
+
for (const relatedModel of relations) {
|
|
416
|
+
const relationName = `${(0, import_common_helpers.lowerCaseFirst)(relatedModel)}s`;
|
|
417
|
+
if (!dataModel.fields.some((f) => f.name === relationName)) {
|
|
418
|
+
const relationField = {
|
|
419
|
+
$type: "DataField",
|
|
420
|
+
name: relationName,
|
|
421
|
+
attributes: [],
|
|
422
|
+
comments: [],
|
|
423
|
+
$container: dataModel
|
|
424
|
+
};
|
|
425
|
+
const relationType = {
|
|
426
|
+
$type: "DataFieldType",
|
|
427
|
+
reference: {
|
|
428
|
+
$refText: relatedModel
|
|
429
|
+
},
|
|
430
|
+
array: true,
|
|
431
|
+
optional: false,
|
|
432
|
+
$container: relationField
|
|
433
|
+
};
|
|
434
|
+
relationField.type = relationType;
|
|
435
|
+
dataModel.fields.push(relationField);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return changed;
|
|
440
|
+
}
|
|
441
|
+
__name(addOrUpdateModel, "addOrUpdateModel");
|
|
442
|
+
function addModelAttribute(dataModel, name, args = []) {
|
|
443
|
+
const attr = {
|
|
444
|
+
$type: "DataModelAttribute",
|
|
445
|
+
decl: {
|
|
446
|
+
$refText: name
|
|
447
|
+
},
|
|
448
|
+
$container: dataModel,
|
|
449
|
+
args: []
|
|
450
|
+
};
|
|
451
|
+
const finalArgs = args.map((arg) => ({
|
|
452
|
+
...arg,
|
|
453
|
+
$container: attr
|
|
454
|
+
}));
|
|
455
|
+
attr.args.push(...finalArgs);
|
|
456
|
+
dataModel.attributes.push(attr);
|
|
457
|
+
}
|
|
458
|
+
__name(addModelAttribute, "addModelAttribute");
|
|
459
|
+
function addFieldAttribute(dataField, name, args = []) {
|
|
460
|
+
const attr = {
|
|
461
|
+
$type: "DataFieldAttribute",
|
|
462
|
+
decl: {
|
|
463
|
+
$refText: name
|
|
464
|
+
},
|
|
465
|
+
$container: dataField,
|
|
466
|
+
args: []
|
|
467
|
+
};
|
|
468
|
+
const finalArgs = args.map((arg) => ({
|
|
469
|
+
...arg,
|
|
470
|
+
$container: attr
|
|
471
|
+
}));
|
|
472
|
+
attr.args.push(...finalArgs);
|
|
473
|
+
dataField.attributes.push(attr);
|
|
474
|
+
}
|
|
475
|
+
__name(addFieldAttribute, "addFieldAttribute");
|
|
476
|
+
function createBooleanAttributeArg(value) {
|
|
477
|
+
const arg = {
|
|
478
|
+
$type: "AttributeArg"
|
|
479
|
+
};
|
|
480
|
+
const expr = {
|
|
481
|
+
$type: "BooleanLiteral",
|
|
482
|
+
value,
|
|
483
|
+
$container: arg
|
|
484
|
+
};
|
|
485
|
+
arg.value = expr;
|
|
486
|
+
return arg;
|
|
487
|
+
}
|
|
488
|
+
__name(createBooleanAttributeArg, "createBooleanAttributeArg");
|
|
489
|
+
function createStringAttributeArg(value) {
|
|
490
|
+
const arg = {
|
|
491
|
+
$type: "AttributeArg"
|
|
492
|
+
};
|
|
493
|
+
const expr = {
|
|
494
|
+
$type: "StringLiteral",
|
|
495
|
+
value,
|
|
496
|
+
$container: arg
|
|
497
|
+
};
|
|
498
|
+
arg.value = expr;
|
|
499
|
+
return arg;
|
|
500
|
+
}
|
|
501
|
+
__name(createStringAttributeArg, "createStringAttributeArg");
|
|
502
|
+
|
|
503
|
+
// src/adapter.ts
|
|
504
|
+
var zenstackAdapter = /* @__PURE__ */ __name((db, config) => {
|
|
505
|
+
let lazyOptions = null;
|
|
506
|
+
const createCustomAdapter = /* @__PURE__ */ __name((db2) => ({ getFieldName, options }) => {
|
|
507
|
+
const convertSelect = /* @__PURE__ */ __name((select, model) => {
|
|
508
|
+
if (!select || !model) return void 0;
|
|
509
|
+
return select.reduce((prev, cur) => {
|
|
510
|
+
return {
|
|
511
|
+
...prev,
|
|
512
|
+
[getFieldName({
|
|
513
|
+
model,
|
|
514
|
+
field: cur
|
|
515
|
+
})]: true
|
|
516
|
+
};
|
|
517
|
+
}, {});
|
|
518
|
+
}, "convertSelect");
|
|
519
|
+
function operatorToORMOperator(operator) {
|
|
520
|
+
switch (operator) {
|
|
521
|
+
case "starts_with":
|
|
522
|
+
return "startsWith";
|
|
523
|
+
case "ends_with":
|
|
524
|
+
return "endsWith";
|
|
525
|
+
case "ne":
|
|
526
|
+
return "not";
|
|
527
|
+
case "not_in":
|
|
528
|
+
return "notIn";
|
|
529
|
+
default:
|
|
530
|
+
return operator;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
__name(operatorToORMOperator, "operatorToORMOperator");
|
|
534
|
+
const convertWhereClause = /* @__PURE__ */ __name((model, where) => {
|
|
535
|
+
if (!where || !where.length) return {};
|
|
536
|
+
if (where.length === 1) {
|
|
537
|
+
const w = where[0];
|
|
538
|
+
if (!w) {
|
|
539
|
+
throw new import_error.BetterAuthError("Invalid where clause");
|
|
540
|
+
}
|
|
541
|
+
return {
|
|
542
|
+
[getFieldName({
|
|
543
|
+
model,
|
|
544
|
+
field: w.field
|
|
545
|
+
})]: w.operator === "eq" || !w.operator ? w.value : {
|
|
546
|
+
[operatorToORMOperator(w.operator)]: w.value
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
const and = where.filter((w) => w.connector === "AND" || !w.connector);
|
|
551
|
+
const or = where.filter((w) => w.connector === "OR");
|
|
552
|
+
const andClause = and.map((w) => {
|
|
553
|
+
return {
|
|
554
|
+
[getFieldName({
|
|
555
|
+
model,
|
|
556
|
+
field: w.field
|
|
557
|
+
})]: w.operator === "eq" || !w.operator ? w.value : {
|
|
558
|
+
[operatorToORMOperator(w.operator)]: w.value
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
});
|
|
562
|
+
const orClause = or.map((w) => {
|
|
563
|
+
return {
|
|
564
|
+
[getFieldName({
|
|
565
|
+
model,
|
|
566
|
+
field: w.field
|
|
567
|
+
})]: w.operator === "eq" || !w.operator ? w.value : {
|
|
568
|
+
[operatorToORMOperator(w.operator)]: w.value
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
});
|
|
572
|
+
return {
|
|
573
|
+
...andClause.length ? {
|
|
574
|
+
AND: andClause
|
|
575
|
+
} : {},
|
|
576
|
+
...orClause.length ? {
|
|
577
|
+
OR: orClause
|
|
578
|
+
} : {}
|
|
579
|
+
};
|
|
580
|
+
}, "convertWhereClause");
|
|
581
|
+
function requireModelDb(db3, model) {
|
|
582
|
+
const modelDb = db3[model];
|
|
583
|
+
if (!modelDb) {
|
|
584
|
+
throw new import_error.BetterAuthError(`Model ${model} does not exist in the database. If you haven't generated the ZenStack schema, you need to run 'npx zen generate'`);
|
|
585
|
+
}
|
|
586
|
+
return modelDb;
|
|
587
|
+
}
|
|
588
|
+
__name(requireModelDb, "requireModelDb");
|
|
589
|
+
return {
|
|
590
|
+
async create({ model, data: values, select }) {
|
|
591
|
+
const modelDb = requireModelDb(db2, model);
|
|
592
|
+
return await modelDb.create({
|
|
593
|
+
data: values,
|
|
594
|
+
select: convertSelect(select, model)
|
|
595
|
+
});
|
|
596
|
+
},
|
|
597
|
+
async findOne({ model, where, select }) {
|
|
598
|
+
const modelDb = requireModelDb(db2, model);
|
|
599
|
+
const whereClause = convertWhereClause(model, where);
|
|
600
|
+
return await modelDb.findFirst({
|
|
601
|
+
where: whereClause,
|
|
602
|
+
select: convertSelect(select, model)
|
|
603
|
+
});
|
|
604
|
+
},
|
|
605
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
606
|
+
const modelDb = requireModelDb(db2, model);
|
|
607
|
+
const whereClause = convertWhereClause(model, where);
|
|
608
|
+
return await modelDb.findMany({
|
|
609
|
+
where: whereClause,
|
|
610
|
+
take: limit || 100,
|
|
611
|
+
skip: offset || 0,
|
|
612
|
+
...sortBy?.field ? {
|
|
613
|
+
orderBy: {
|
|
614
|
+
[getFieldName({
|
|
615
|
+
model,
|
|
616
|
+
field: sortBy.field
|
|
617
|
+
})]: sortBy.direction === "desc" ? "desc" : "asc"
|
|
618
|
+
}
|
|
619
|
+
} : {}
|
|
620
|
+
});
|
|
621
|
+
},
|
|
622
|
+
async count({ model, where }) {
|
|
623
|
+
const modelDb = requireModelDb(db2, model);
|
|
624
|
+
const whereClause = convertWhereClause(model, where);
|
|
625
|
+
return await modelDb.count({
|
|
626
|
+
where: whereClause
|
|
627
|
+
});
|
|
628
|
+
},
|
|
629
|
+
async update({ model, where, update }) {
|
|
630
|
+
const modelDb = requireModelDb(db2, model);
|
|
631
|
+
const whereClause = convertWhereClause(model, where);
|
|
632
|
+
return await modelDb.update({
|
|
633
|
+
where: whereClause,
|
|
634
|
+
data: update
|
|
635
|
+
});
|
|
636
|
+
},
|
|
637
|
+
async updateMany({ model, where, update }) {
|
|
638
|
+
const modelDb = requireModelDb(db2, model);
|
|
639
|
+
const whereClause = convertWhereClause(model, where);
|
|
640
|
+
const result = await modelDb.updateMany({
|
|
641
|
+
where: whereClause,
|
|
642
|
+
data: update
|
|
643
|
+
});
|
|
644
|
+
return result ? result.count : 0;
|
|
645
|
+
},
|
|
646
|
+
async delete({ model, where }) {
|
|
647
|
+
const modelDb = requireModelDb(db2, model);
|
|
648
|
+
const whereClause = convertWhereClause(model, where);
|
|
649
|
+
try {
|
|
650
|
+
await modelDb.delete({
|
|
651
|
+
where: whereClause
|
|
652
|
+
});
|
|
653
|
+
} catch {
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
async deleteMany({ model, where }) {
|
|
657
|
+
const modelDb = requireModelDb(db2, model);
|
|
658
|
+
const whereClause = convertWhereClause(model, where);
|
|
659
|
+
const result = await modelDb.deleteMany({
|
|
660
|
+
where: whereClause
|
|
661
|
+
});
|
|
662
|
+
return result ? result.count : 0;
|
|
663
|
+
},
|
|
664
|
+
options: config,
|
|
665
|
+
createSchema: /* @__PURE__ */ __name(async ({ file, tables }) => {
|
|
666
|
+
return generateSchema(file, tables, config, options);
|
|
667
|
+
}, "createSchema")
|
|
668
|
+
};
|
|
669
|
+
}, "createCustomAdapter");
|
|
670
|
+
const adapterOptions = {
|
|
671
|
+
config: {
|
|
672
|
+
adapterId: "zenstack",
|
|
673
|
+
adapterName: "ZenStack Adapter",
|
|
674
|
+
usePlural: config.usePlural ?? false,
|
|
675
|
+
debugLogs: config.debugLogs ?? false,
|
|
676
|
+
transaction: /* @__PURE__ */ __name((cb) => db.$transaction((tx) => {
|
|
677
|
+
const adapter2 = (0, import_adapters.createAdapterFactory)({
|
|
678
|
+
config: adapterOptions.config,
|
|
679
|
+
adapter: createCustomAdapter(tx)
|
|
680
|
+
})(lazyOptions);
|
|
681
|
+
return cb(adapter2);
|
|
682
|
+
}), "transaction")
|
|
683
|
+
},
|
|
684
|
+
adapter: createCustomAdapter(db)
|
|
685
|
+
};
|
|
686
|
+
const adapter = (0, import_adapters.createAdapterFactory)(adapterOptions);
|
|
687
|
+
return (options) => {
|
|
688
|
+
lazyOptions = options;
|
|
689
|
+
return adapter(options);
|
|
690
|
+
};
|
|
691
|
+
}, "zenstackAdapter");
|
|
692
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
693
|
+
0 && (module.exports = {
|
|
694
|
+
zenstackAdapter
|
|
695
|
+
});
|
|
696
|
+
//# sourceMappingURL=index.cjs.map
|