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