@rio.js/enterprise 1.4.0
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/README.md +89 -0
- package/dist/adapter-factory-BTRALCLD-kJBwe70v.mjs +836 -0
- package/dist/better-auth-CStoaWiq.d.mts +10 -0
- package/dist/better-auth-CqfhQJYE.mjs +558 -0
- package/dist/better-auth.d.mts +2 -0
- package/dist/better-auth.mjs +17 -0
- package/dist/bun-sqlite-dialect-2R9nCsVF-DFs6tpGr.mjs +155 -0
- package/dist/client--1_AEBPu-8Ae9icC9.mjs +125 -0
- package/dist/client.d.mts +17 -0
- package/dist/client.mjs +381 -0
- package/dist/db-BVXTgOd3.mjs +681 -0
- package/dist/db-BadqSwVl.d.mts +9542 -0
- package/dist/db-schema.final-DWleoQm0.mjs +785 -0
- package/dist/db.d.mts +2 -0
- package/dist/db.mjs +3 -0
- package/dist/dialect-C6_pK3V9-CPJHWkYR.mjs +72 -0
- package/dist/dist-CygcgJYk.mjs +422 -0
- package/dist/env-DwlNAN_D-C1zHd0cf-Cdlw8sNp.mjs +289 -0
- package/dist/esm-C5TuvtGn.mjs +15816 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +17 -0
- package/dist/init-D8lwWc90.mjs +27 -0
- package/dist/json-oFuWgANh-O1U6k3bL.mjs +3811 -0
- package/dist/kysely-adapter-D_seG51p.mjs +297 -0
- package/dist/memory-adapter-CY-oDozb.mjs +215 -0
- package/dist/misc-CbURQDlR-sLtUwwQY.mjs +7 -0
- package/dist/node-sqlite-dialect-CdC7L-ji-QLbJGmDc.mjs +155 -0
- package/dist/parser-bL7W2mQ0-YdTgjtji.mjs +140 -0
- package/dist/plugins-BNFht2HW.mjs +23358 -0
- package/dist/plugins.d.mts +1 -0
- package/dist/plugins.mjs +13 -0
- package/dist/react--VZQu7s1.mjs +560 -0
- package/dist/react.d.mts +1 -0
- package/dist/react.mjs +6 -0
- package/dist/server.d.mts +10 -0
- package/dist/server.mjs +45 -0
- package/dist/social-providers-DNfE9Ak7-Be5zMAEe.mjs +2920 -0
- package/dist/social-providers.d.mts +1 -0
- package/dist/social-providers.mjs +6 -0
- package/dist/verify-CN5Qc0e-.mjs +1183 -0
- package/package.json +98 -0
|
@@ -0,0 +1,836 @@
|
|
|
1
|
+
import { a as initGetFieldName, c as generateId, i as initGetDefaultModelName, n as getAuthTables, o as initGetModelName, r as initGetDefaultFieldName, t as safeJSONParse } from "./json-oFuWgANh-O1U6k3bL.mjs";
|
|
2
|
+
import { f as logger, g as BetterAuthError, n as TTY_COLORS, o as getColorDepth } from "./env-DwlNAN_D-C1zHd0cf-Cdlw8sNp.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../better-auth/dist/adapter-factory-BTRALCLD.mjs
|
|
5
|
+
function withApplyDefault(value, field, action) {
|
|
6
|
+
if (action === "update") {
|
|
7
|
+
if (value === void 0 && field.onUpdate !== void 0) {
|
|
8
|
+
if (typeof field.onUpdate === "function") return field.onUpdate();
|
|
9
|
+
return field.onUpdate;
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
if (action === "create") {
|
|
14
|
+
if (value === void 0 || field.required === true && value === null) {
|
|
15
|
+
if (field.defaultValue !== void 0) {
|
|
16
|
+
if (typeof field.defaultValue === "function") return field.defaultValue();
|
|
17
|
+
return field.defaultValue;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
const initGetIdField = ({ usePlural, schema, disableIdGeneration, options, customIdGenerator, supportsUUIDs }) => {
|
|
24
|
+
const getDefaultModelName = initGetDefaultModelName({
|
|
25
|
+
usePlural,
|
|
26
|
+
schema
|
|
27
|
+
});
|
|
28
|
+
const idField = ({ customModelName, forceAllowId }) => {
|
|
29
|
+
const useNumberId = options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial";
|
|
30
|
+
const useUUIDs = options.advanced?.database?.generateId === "uuid";
|
|
31
|
+
let shouldGenerateId = (() => {
|
|
32
|
+
if (disableIdGeneration) return false;
|
|
33
|
+
else if (useNumberId && !forceAllowId) return false;
|
|
34
|
+
else if (useUUIDs) return !supportsUUIDs;
|
|
35
|
+
else return true;
|
|
36
|
+
})();
|
|
37
|
+
const model = getDefaultModelName(customModelName ?? "id");
|
|
38
|
+
return {
|
|
39
|
+
type: useNumberId ? "number" : "string",
|
|
40
|
+
required: shouldGenerateId ? true : false,
|
|
41
|
+
...shouldGenerateId ? { defaultValue() {
|
|
42
|
+
if (disableIdGeneration) return void 0;
|
|
43
|
+
let generateId$1 = options.advanced?.database?.generateId;
|
|
44
|
+
if (generateId$1 === false || useNumberId) return void 0;
|
|
45
|
+
if (typeof generateId$1 === "function") return generateId$1({ model });
|
|
46
|
+
if (customIdGenerator) return customIdGenerator({ model });
|
|
47
|
+
if (generateId$1 === "uuid") return crypto.randomUUID();
|
|
48
|
+
return generateId();
|
|
49
|
+
} } : {},
|
|
50
|
+
transform: {
|
|
51
|
+
input: (value) => {
|
|
52
|
+
if (!value) return void 0;
|
|
53
|
+
if (useNumberId) {
|
|
54
|
+
const numberValue = Number(value);
|
|
55
|
+
if (isNaN(numberValue)) return;
|
|
56
|
+
return numberValue;
|
|
57
|
+
}
|
|
58
|
+
if (useUUIDs) {
|
|
59
|
+
if (shouldGenerateId && !forceAllowId) return value;
|
|
60
|
+
if (disableIdGeneration) return void 0;
|
|
61
|
+
if (supportsUUIDs) return void 0;
|
|
62
|
+
if (forceAllowId && typeof value === "string") if (/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)) return value;
|
|
63
|
+
else {
|
|
64
|
+
const stack = (/* @__PURE__ */ new Error()).stack?.split("\n").filter((_, i) => i !== 1).join("\n").replace("Error:", "");
|
|
65
|
+
logger.warn("[Adapter Factory] - Invalid UUID value for field `id` provided when `forceAllowId` is true. Generating a new UUID.", stack);
|
|
66
|
+
}
|
|
67
|
+
if (typeof value !== "string" && !supportsUUIDs) return crypto.randomUUID();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
},
|
|
72
|
+
output: (value) => {
|
|
73
|
+
if (!value) return void 0;
|
|
74
|
+
return String(value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
return idField;
|
|
80
|
+
};
|
|
81
|
+
const initGetFieldAttributes = ({ usePlural, schema, options, customIdGenerator, disableIdGeneration }) => {
|
|
82
|
+
const getDefaultModelName = initGetDefaultModelName({
|
|
83
|
+
usePlural,
|
|
84
|
+
schema
|
|
85
|
+
});
|
|
86
|
+
const getDefaultFieldName = initGetDefaultFieldName({
|
|
87
|
+
usePlural,
|
|
88
|
+
schema
|
|
89
|
+
});
|
|
90
|
+
const idField = initGetIdField({
|
|
91
|
+
usePlural,
|
|
92
|
+
schema,
|
|
93
|
+
options,
|
|
94
|
+
customIdGenerator,
|
|
95
|
+
disableIdGeneration
|
|
96
|
+
});
|
|
97
|
+
const getFieldAttributes = ({ model, field }) => {
|
|
98
|
+
const defaultModelName = getDefaultModelName(model);
|
|
99
|
+
const defaultFieldName = getDefaultFieldName({
|
|
100
|
+
field,
|
|
101
|
+
model: defaultModelName
|
|
102
|
+
});
|
|
103
|
+
const fields = schema[defaultModelName].fields;
|
|
104
|
+
fields.id = idField({ customModelName: defaultModelName });
|
|
105
|
+
const fieldAttributes = fields[defaultFieldName];
|
|
106
|
+
if (!fieldAttributes) throw new BetterAuthError(`Field ${field} not found in model ${model}`);
|
|
107
|
+
return fieldAttributes;
|
|
108
|
+
};
|
|
109
|
+
return getFieldAttributes;
|
|
110
|
+
};
|
|
111
|
+
let debugLogs = [];
|
|
112
|
+
let transactionId = -1;
|
|
113
|
+
const createAsIsTransaction = (adapter) => (fn) => fn(adapter);
|
|
114
|
+
const createAdapterFactory = ({ adapter: customAdapter, config: cfg }) => (options) => {
|
|
115
|
+
const uniqueAdapterFactoryInstanceId = Math.random().toString(36).substring(2, 15);
|
|
116
|
+
const config = {
|
|
117
|
+
...cfg,
|
|
118
|
+
supportsBooleans: cfg.supportsBooleans ?? true,
|
|
119
|
+
supportsDates: cfg.supportsDates ?? true,
|
|
120
|
+
supportsJSON: cfg.supportsJSON ?? false,
|
|
121
|
+
adapterName: cfg.adapterName ?? cfg.adapterId,
|
|
122
|
+
supportsNumericIds: cfg.supportsNumericIds ?? true,
|
|
123
|
+
supportsUUIDs: cfg.supportsUUIDs ?? false,
|
|
124
|
+
transaction: cfg.transaction ?? false,
|
|
125
|
+
disableTransformInput: cfg.disableTransformInput ?? false,
|
|
126
|
+
disableTransformOutput: cfg.disableTransformOutput ?? false,
|
|
127
|
+
disableTransformJoin: cfg.disableTransformJoin ?? false
|
|
128
|
+
};
|
|
129
|
+
if ((options.advanced?.database?.useNumberId === true || options.advanced?.database?.generateId === "serial") && config.supportsNumericIds === false) throw new BetterAuthError(`[${config.adapterName}] Your database or database adapter does not support numeric ids. Please disable "useNumberId" in your config.`);
|
|
130
|
+
const schema = getAuthTables(options);
|
|
131
|
+
const debugLog = (...args) => {
|
|
132
|
+
if (config.debugLogs === true || typeof config.debugLogs === "object") {
|
|
133
|
+
if (typeof config.debugLogs === "object" && "isRunningAdapterTests" in config.debugLogs) {
|
|
134
|
+
if (config.debugLogs.isRunningAdapterTests) {
|
|
135
|
+
args.shift();
|
|
136
|
+
debugLogs.push({
|
|
137
|
+
instance: uniqueAdapterFactoryInstanceId,
|
|
138
|
+
args
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (typeof config.debugLogs === "object" && config.debugLogs.logCondition && !config.debugLogs.logCondition?.()) return;
|
|
144
|
+
if (typeof args[0] === "object" && "method" in args[0]) {
|
|
145
|
+
const method = args.shift().method;
|
|
146
|
+
if (typeof config.debugLogs === "object") {
|
|
147
|
+
if (method === "create" && !config.debugLogs.create) return;
|
|
148
|
+
else if (method === "update" && !config.debugLogs.update) return;
|
|
149
|
+
else if (method === "updateMany" && !config.debugLogs.updateMany) return;
|
|
150
|
+
else if (method === "findOne" && !config.debugLogs.findOne) return;
|
|
151
|
+
else if (method === "findMany" && !config.debugLogs.findMany) return;
|
|
152
|
+
else if (method === "delete" && !config.debugLogs.delete) return;
|
|
153
|
+
else if (method === "deleteMany" && !config.debugLogs.deleteMany) return;
|
|
154
|
+
else if (method === "count" && !config.debugLogs.count) return;
|
|
155
|
+
}
|
|
156
|
+
logger.info(`[${config.adapterName}]`, ...args);
|
|
157
|
+
} else logger.info(`[${config.adapterName}]`, ...args);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
const getDefaultModelName = initGetDefaultModelName({
|
|
161
|
+
usePlural: config.usePlural,
|
|
162
|
+
schema
|
|
163
|
+
});
|
|
164
|
+
const getDefaultFieldName = initGetDefaultFieldName({
|
|
165
|
+
usePlural: config.usePlural,
|
|
166
|
+
schema
|
|
167
|
+
});
|
|
168
|
+
const getModelName = initGetModelName({
|
|
169
|
+
usePlural: config.usePlural,
|
|
170
|
+
schema
|
|
171
|
+
});
|
|
172
|
+
const getFieldName = initGetFieldName({
|
|
173
|
+
schema,
|
|
174
|
+
usePlural: config.usePlural
|
|
175
|
+
});
|
|
176
|
+
const idField = initGetIdField({
|
|
177
|
+
schema,
|
|
178
|
+
options,
|
|
179
|
+
usePlural: config.usePlural,
|
|
180
|
+
disableIdGeneration: config.disableIdGeneration,
|
|
181
|
+
customIdGenerator: config.customIdGenerator,
|
|
182
|
+
supportsUUIDs: config.supportsUUIDs
|
|
183
|
+
});
|
|
184
|
+
const getFieldAttributes = initGetFieldAttributes({
|
|
185
|
+
schema,
|
|
186
|
+
options,
|
|
187
|
+
usePlural: config.usePlural,
|
|
188
|
+
disableIdGeneration: config.disableIdGeneration,
|
|
189
|
+
customIdGenerator: config.customIdGenerator
|
|
190
|
+
});
|
|
191
|
+
const transformInput = async (data, defaultModelName, action, forceAllowId) => {
|
|
192
|
+
const transformedData = {};
|
|
193
|
+
const fields = schema[defaultModelName].fields;
|
|
194
|
+
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
195
|
+
const useNumberId = options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial";
|
|
196
|
+
fields.id = idField({
|
|
197
|
+
customModelName: defaultModelName,
|
|
198
|
+
forceAllowId: forceAllowId && "id" in data
|
|
199
|
+
});
|
|
200
|
+
for (const field in fields) {
|
|
201
|
+
let value = data[field];
|
|
202
|
+
const fieldAttributes = fields[field];
|
|
203
|
+
let newFieldName = newMappedKeys[field] || fields[field].fieldName || field;
|
|
204
|
+
if (value === void 0 && (fieldAttributes.defaultValue === void 0 && !fieldAttributes.transform?.input && !(action === "update" && fieldAttributes.onUpdate) || action === "update" && !fieldAttributes.onUpdate)) continue;
|
|
205
|
+
if (fieldAttributes && fieldAttributes.type === "date" && !(value instanceof Date) && typeof value === "string") try {
|
|
206
|
+
value = new Date(value);
|
|
207
|
+
} catch {
|
|
208
|
+
logger.error("[Adapter Factory] Failed to convert string to date", {
|
|
209
|
+
value,
|
|
210
|
+
field
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
let newValue = withApplyDefault(value, fieldAttributes, action);
|
|
214
|
+
if (fieldAttributes.transform?.input) newValue = await fieldAttributes.transform.input(newValue);
|
|
215
|
+
if (fieldAttributes.references?.field === "id" && useNumberId) if (Array.isArray(newValue)) newValue = newValue.map((x) => x !== null ? Number(x) : null);
|
|
216
|
+
else newValue = newValue !== null ? Number(newValue) : null;
|
|
217
|
+
else if (config.supportsJSON === false && typeof newValue === "object" && fieldAttributes.type === "json") newValue = JSON.stringify(newValue);
|
|
218
|
+
else if (config.supportsDates === false && newValue instanceof Date && fieldAttributes.type === "date") newValue = newValue.toISOString();
|
|
219
|
+
else if (config.supportsBooleans === false && typeof newValue === "boolean") newValue = newValue ? 1 : 0;
|
|
220
|
+
if (config.customTransformInput) newValue = config.customTransformInput({
|
|
221
|
+
data: newValue,
|
|
222
|
+
action,
|
|
223
|
+
field: newFieldName,
|
|
224
|
+
fieldAttributes,
|
|
225
|
+
model: getModelName(defaultModelName),
|
|
226
|
+
schema,
|
|
227
|
+
options
|
|
228
|
+
});
|
|
229
|
+
if (newValue !== void 0) transformedData[newFieldName] = newValue;
|
|
230
|
+
}
|
|
231
|
+
return transformedData;
|
|
232
|
+
};
|
|
233
|
+
const transformOutput = async (data, unsafe_model, select = [], join) => {
|
|
234
|
+
const transformSingleOutput = async (data$1, unsafe_model$1, select$1 = []) => {
|
|
235
|
+
if (!data$1) return null;
|
|
236
|
+
const newMappedKeys = config.mapKeysTransformOutput ?? {};
|
|
237
|
+
const transformedData$1 = {};
|
|
238
|
+
const tableSchema = schema[getDefaultModelName(unsafe_model$1)].fields;
|
|
239
|
+
const idKey = Object.entries(newMappedKeys).find(([_, v]) => v === "id")?.[0];
|
|
240
|
+
tableSchema[idKey ?? "id"] = { type: options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial" ? "number" : "string" };
|
|
241
|
+
for (const key in tableSchema) {
|
|
242
|
+
if (select$1.length && !select$1.includes(key)) continue;
|
|
243
|
+
const field = tableSchema[key];
|
|
244
|
+
if (field) {
|
|
245
|
+
const originalKey = field.fieldName || key;
|
|
246
|
+
let newValue = data$1[Object.entries(newMappedKeys).find(([_, v]) => v === originalKey)?.[0] || originalKey];
|
|
247
|
+
if (field.transform?.output) newValue = await field.transform.output(newValue);
|
|
248
|
+
let newFieldName = newMappedKeys[key] || key;
|
|
249
|
+
if (originalKey === "id" || field.references?.field === "id") {
|
|
250
|
+
if (typeof newValue !== "undefined" && newValue !== null) newValue = String(newValue);
|
|
251
|
+
} else if (config.supportsJSON === false && typeof newValue === "string" && field.type === "json") newValue = safeJSONParse(newValue);
|
|
252
|
+
else if (config.supportsDates === false && typeof newValue === "string" && field.type === "date") newValue = new Date(newValue);
|
|
253
|
+
else if (config.supportsBooleans === false && typeof newValue === "number" && field.type === "boolean") newValue = newValue === 1;
|
|
254
|
+
if (config.customTransformOutput) newValue = config.customTransformOutput({
|
|
255
|
+
data: newValue,
|
|
256
|
+
field: newFieldName,
|
|
257
|
+
fieldAttributes: field,
|
|
258
|
+
select: select$1,
|
|
259
|
+
model: getModelName(unsafe_model$1),
|
|
260
|
+
schema,
|
|
261
|
+
options
|
|
262
|
+
});
|
|
263
|
+
transformedData$1[newFieldName] = newValue;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return transformedData$1;
|
|
267
|
+
};
|
|
268
|
+
if (!join || Object.keys(join).length === 0) return await transformSingleOutput(data, unsafe_model, select);
|
|
269
|
+
unsafe_model = getDefaultModelName(unsafe_model);
|
|
270
|
+
let transformedData = await transformSingleOutput(data, unsafe_model, select);
|
|
271
|
+
const requiredModels = Object.entries(join).map(([model, joinConfig]) => ({
|
|
272
|
+
modelName: getModelName(model),
|
|
273
|
+
defaultModelName: getDefaultModelName(model),
|
|
274
|
+
joinConfig
|
|
275
|
+
}));
|
|
276
|
+
if (!data) return null;
|
|
277
|
+
for (const { modelName, defaultModelName, joinConfig } of requiredModels) {
|
|
278
|
+
let joinedData = await (async () => {
|
|
279
|
+
if (options.experimental?.joins) return data[modelName];
|
|
280
|
+
else return await handleFallbackJoin({
|
|
281
|
+
baseModel: unsafe_model,
|
|
282
|
+
baseData: transformedData,
|
|
283
|
+
joinModel: modelName,
|
|
284
|
+
specificJoinConfig: joinConfig
|
|
285
|
+
});
|
|
286
|
+
})();
|
|
287
|
+
if (joinedData === void 0 || joinedData === null) joinedData = joinConfig.relation === "one-to-one" ? null : [];
|
|
288
|
+
if (joinConfig.relation === "one-to-many" && !Array.isArray(joinedData)) joinedData = [joinedData];
|
|
289
|
+
let transformed = [];
|
|
290
|
+
if (Array.isArray(joinedData)) for (const item of joinedData) {
|
|
291
|
+
const transformedItem = await transformSingleOutput(item, modelName, []);
|
|
292
|
+
transformed.push(transformedItem);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
const transformedItem = await transformSingleOutput(joinedData, modelName, []);
|
|
296
|
+
transformed.push(transformedItem);
|
|
297
|
+
}
|
|
298
|
+
transformedData[defaultModelName] = (joinConfig.relation === "one-to-one" ? transformed[0] : transformed) ?? null;
|
|
299
|
+
}
|
|
300
|
+
return transformedData;
|
|
301
|
+
};
|
|
302
|
+
const transformWhereClause = ({ model, where }) => {
|
|
303
|
+
if (!where) return void 0;
|
|
304
|
+
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
305
|
+
return where.map((w) => {
|
|
306
|
+
const { field: unsafe_field, value, operator = "eq", connector = "AND" } = w;
|
|
307
|
+
if (operator === "in") {
|
|
308
|
+
if (!Array.isArray(value)) throw new BetterAuthError("Value must be an array");
|
|
309
|
+
}
|
|
310
|
+
let newValue = value;
|
|
311
|
+
const defaultModelName = getDefaultModelName(model);
|
|
312
|
+
const defaultFieldName = getDefaultFieldName({
|
|
313
|
+
field: unsafe_field,
|
|
314
|
+
model
|
|
315
|
+
});
|
|
316
|
+
const fieldName = newMappedKeys[defaultFieldName] || getFieldName({
|
|
317
|
+
field: defaultFieldName,
|
|
318
|
+
model: defaultModelName
|
|
319
|
+
});
|
|
320
|
+
const fieldAttr = getFieldAttributes({
|
|
321
|
+
field: defaultFieldName,
|
|
322
|
+
model: defaultModelName
|
|
323
|
+
});
|
|
324
|
+
const useNumberId = options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial";
|
|
325
|
+
if (defaultFieldName === "id" || fieldAttr.references?.field === "id") {
|
|
326
|
+
if (useNumberId) if (Array.isArray(value)) newValue = value.map(Number);
|
|
327
|
+
else newValue = Number(value);
|
|
328
|
+
}
|
|
329
|
+
if (fieldAttr.type === "date" && value instanceof Date && !config.supportsDates) newValue = value.toISOString();
|
|
330
|
+
if (fieldAttr.type === "boolean" && typeof value === "boolean" && !config.supportsBooleans) newValue = value ? 1 : 0;
|
|
331
|
+
if (fieldAttr.type === "json" && typeof value === "object" && !config.supportsJSON) try {
|
|
332
|
+
newValue = JSON.stringify(value);
|
|
333
|
+
} catch (error) {
|
|
334
|
+
throw new Error(`Failed to stringify JSON value for field ${fieldName}`, { cause: error });
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
operator,
|
|
338
|
+
connector,
|
|
339
|
+
field: fieldName,
|
|
340
|
+
value: newValue
|
|
341
|
+
};
|
|
342
|
+
});
|
|
343
|
+
};
|
|
344
|
+
const transformJoinClause = (baseModel, unsanitizedJoin, select) => {
|
|
345
|
+
if (!unsanitizedJoin) return void 0;
|
|
346
|
+
if (Object.keys(unsanitizedJoin).length === 0) return void 0;
|
|
347
|
+
const transformedJoin = {};
|
|
348
|
+
for (const [model, join] of Object.entries(unsanitizedJoin)) {
|
|
349
|
+
if (!join) continue;
|
|
350
|
+
const defaultModelName = getDefaultModelName(model);
|
|
351
|
+
const defaultBaseModelName = getDefaultModelName(baseModel);
|
|
352
|
+
let foreignKeys = Object.entries(schema[defaultModelName].fields).filter(([field, fieldAttributes]) => fieldAttributes.references && getDefaultModelName(fieldAttributes.references.model) === defaultBaseModelName);
|
|
353
|
+
let isForwardJoin = true;
|
|
354
|
+
if (!foreignKeys.length) {
|
|
355
|
+
foreignKeys = Object.entries(schema[defaultBaseModelName].fields).filter(([field, fieldAttributes]) => fieldAttributes.references && getDefaultModelName(fieldAttributes.references.model) === defaultModelName);
|
|
356
|
+
isForwardJoin = false;
|
|
357
|
+
}
|
|
358
|
+
if (!foreignKeys.length) throw new BetterAuthError(`No foreign key found for model ${model} and base model ${baseModel} while performing join operation.`);
|
|
359
|
+
else if (foreignKeys.length > 1) throw new BetterAuthError(`Multiple foreign keys found for model ${model} and base model ${baseModel} while performing join operation. Only one foreign key is supported.`);
|
|
360
|
+
const [foreignKey, foreignKeyAttributes] = foreignKeys[0];
|
|
361
|
+
if (!foreignKeyAttributes.references) throw new BetterAuthError(`No references found for foreign key ${foreignKey} on model ${model} while performing join operation.`);
|
|
362
|
+
let from;
|
|
363
|
+
let to;
|
|
364
|
+
let requiredSelectField;
|
|
365
|
+
if (isForwardJoin) {
|
|
366
|
+
requiredSelectField = foreignKeyAttributes.references.field;
|
|
367
|
+
from = getFieldName({
|
|
368
|
+
model: baseModel,
|
|
369
|
+
field: requiredSelectField
|
|
370
|
+
});
|
|
371
|
+
to = getFieldName({
|
|
372
|
+
model,
|
|
373
|
+
field: foreignKey
|
|
374
|
+
});
|
|
375
|
+
} else {
|
|
376
|
+
requiredSelectField = foreignKey;
|
|
377
|
+
from = getFieldName({
|
|
378
|
+
model: baseModel,
|
|
379
|
+
field: requiredSelectField
|
|
380
|
+
});
|
|
381
|
+
to = getFieldName({
|
|
382
|
+
model,
|
|
383
|
+
field: foreignKeyAttributes.references.field
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
if (select && !select.includes(requiredSelectField)) select.push(requiredSelectField);
|
|
387
|
+
const isUnique = to === "id" ? true : foreignKeyAttributes.unique ?? false;
|
|
388
|
+
let limit = options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
389
|
+
if (isUnique) limit = 1;
|
|
390
|
+
else if (typeof join === "object" && typeof join.limit === "number") limit = join.limit;
|
|
391
|
+
transformedJoin[getModelName(model)] = {
|
|
392
|
+
on: {
|
|
393
|
+
from,
|
|
394
|
+
to
|
|
395
|
+
},
|
|
396
|
+
limit,
|
|
397
|
+
relation: isUnique ? "one-to-one" : "one-to-many"
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
join: transformedJoin,
|
|
402
|
+
select
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* Handle joins by making separate queries and combining results (fallback for adapters that don't support native joins).
|
|
407
|
+
*/
|
|
408
|
+
const handleFallbackJoin = async ({ baseModel, baseData, joinModel, specificJoinConfig: joinConfig }) => {
|
|
409
|
+
if (!baseData) return baseData;
|
|
410
|
+
const modelName = getModelName(joinModel);
|
|
411
|
+
const field = joinConfig.on.to;
|
|
412
|
+
const value = baseData[getDefaultFieldName({
|
|
413
|
+
field: joinConfig.on.from,
|
|
414
|
+
model: baseModel
|
|
415
|
+
})];
|
|
416
|
+
if (value === null || value === void 0) return joinConfig.relation === "one-to-one" ? null : [];
|
|
417
|
+
let result;
|
|
418
|
+
const where = transformWhereClause({
|
|
419
|
+
model: modelName,
|
|
420
|
+
where: [{
|
|
421
|
+
field,
|
|
422
|
+
value,
|
|
423
|
+
operator: "eq",
|
|
424
|
+
connector: "AND"
|
|
425
|
+
}]
|
|
426
|
+
});
|
|
427
|
+
try {
|
|
428
|
+
if (joinConfig.relation === "one-to-one") result = await adapterInstance.findOne({
|
|
429
|
+
model: modelName,
|
|
430
|
+
where
|
|
431
|
+
});
|
|
432
|
+
else {
|
|
433
|
+
const limit = joinConfig.limit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
434
|
+
result = await adapterInstance.findMany({
|
|
435
|
+
model: modelName,
|
|
436
|
+
where,
|
|
437
|
+
limit
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
} catch (error) {
|
|
441
|
+
logger.error(`Failed to query fallback join for model ${modelName}:`, {
|
|
442
|
+
where,
|
|
443
|
+
limit: joinConfig.limit
|
|
444
|
+
});
|
|
445
|
+
console.error(error);
|
|
446
|
+
throw error;
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
};
|
|
450
|
+
const adapterInstance = customAdapter({
|
|
451
|
+
options,
|
|
452
|
+
schema,
|
|
453
|
+
debugLog,
|
|
454
|
+
getFieldName,
|
|
455
|
+
getModelName,
|
|
456
|
+
getDefaultModelName,
|
|
457
|
+
getDefaultFieldName,
|
|
458
|
+
getFieldAttributes,
|
|
459
|
+
transformInput,
|
|
460
|
+
transformOutput,
|
|
461
|
+
transformWhereClause
|
|
462
|
+
});
|
|
463
|
+
let lazyLoadTransaction = null;
|
|
464
|
+
const adapter = {
|
|
465
|
+
transaction: async (cb) => {
|
|
466
|
+
if (!lazyLoadTransaction) if (!config.transaction) lazyLoadTransaction = createAsIsTransaction(adapter);
|
|
467
|
+
else {
|
|
468
|
+
logger.debug(`[${config.adapterName}] - Using provided transaction implementation.`);
|
|
469
|
+
lazyLoadTransaction = config.transaction;
|
|
470
|
+
}
|
|
471
|
+
return lazyLoadTransaction(cb);
|
|
472
|
+
},
|
|
473
|
+
create: async ({ data: unsafeData, model: unsafeModel, select, forceAllowId = false }) => {
|
|
474
|
+
transactionId++;
|
|
475
|
+
let thisTransactionId = transactionId;
|
|
476
|
+
const model = getModelName(unsafeModel);
|
|
477
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
478
|
+
if ("id" in unsafeData && typeof unsafeData.id !== "undefined" && !forceAllowId) {
|
|
479
|
+
logger.warn(`[${config.adapterName}] - You are trying to create a record with an id. This is not allowed as we handle id generation for you, unless you pass in the \`forceAllowId\` parameter. The id will be ignored.`);
|
|
480
|
+
const stack = (/* @__PURE__ */ new Error()).stack?.split("\n").filter((_, i) => i !== 1).join("\n").replace("Error:", "Create method with `id` being called at:");
|
|
481
|
+
console.log(stack);
|
|
482
|
+
unsafeData.id = void 0;
|
|
483
|
+
}
|
|
484
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("create")} ${formatAction("Unsafe Input")}:`, {
|
|
485
|
+
model,
|
|
486
|
+
data: unsafeData
|
|
487
|
+
});
|
|
488
|
+
let data = unsafeData;
|
|
489
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "create", forceAllowId);
|
|
490
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("create")} ${formatAction("Parsed Input")}:`, {
|
|
491
|
+
model,
|
|
492
|
+
data
|
|
493
|
+
});
|
|
494
|
+
const res = await adapterInstance.create({
|
|
495
|
+
data,
|
|
496
|
+
model
|
|
497
|
+
});
|
|
498
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("create")} ${formatAction("DB Result")}:`, {
|
|
499
|
+
model,
|
|
500
|
+
res
|
|
501
|
+
});
|
|
502
|
+
let transformed = res;
|
|
503
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select, void 0);
|
|
504
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("create")} ${formatAction("Parsed Result")}:`, {
|
|
505
|
+
model,
|
|
506
|
+
data: transformed
|
|
507
|
+
});
|
|
508
|
+
return transformed;
|
|
509
|
+
},
|
|
510
|
+
createMany: async ({ model: unsafeModel, data: unsafeDataArray, select, forceAllowId }) => {
|
|
511
|
+
transactionId++;
|
|
512
|
+
let thisTransactionId = transactionId;
|
|
513
|
+
const model = getModelName(getDefaultModelName(unsafeModel));
|
|
514
|
+
let processedDataArray = unsafeDataArray;
|
|
515
|
+
debugLog({ method: "createMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("createMany")} ${formatAction("Unsafe Input")}:`, {
|
|
516
|
+
model,
|
|
517
|
+
data: unsafeDataArray
|
|
518
|
+
});
|
|
519
|
+
if (!config.disableTransformInput) processedDataArray = await Promise.all(unsafeDataArray.map((d) => transformInput(d, unsafeModel, "create", forceAllowId)));
|
|
520
|
+
debugLog({ method: "createMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("createMany")} ${formatAction("Parsed Input")}:`, {
|
|
521
|
+
model,
|
|
522
|
+
data: processedDataArray
|
|
523
|
+
});
|
|
524
|
+
const res = await adapterInstance.createMany({
|
|
525
|
+
model,
|
|
526
|
+
data: processedDataArray
|
|
527
|
+
});
|
|
528
|
+
debugLog({ method: "createMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("createMany")} ${formatAction("DB Result")}:`, {
|
|
529
|
+
model,
|
|
530
|
+
data: res
|
|
531
|
+
});
|
|
532
|
+
let transformed = res;
|
|
533
|
+
if (!config.disableTransformOutput) transformed = await Promise.all(res.map((r) => transformOutput(r, unsafeModel, select, void 0)));
|
|
534
|
+
debugLog({ method: "createMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("createMany")} ${formatAction("Parsed Result")}:`, {
|
|
535
|
+
model,
|
|
536
|
+
data: transformed
|
|
537
|
+
});
|
|
538
|
+
return transformed;
|
|
539
|
+
},
|
|
540
|
+
update: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
541
|
+
transactionId++;
|
|
542
|
+
let thisTransactionId = transactionId;
|
|
543
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
544
|
+
const model = getModelName(unsafeModel);
|
|
545
|
+
const where = transformWhereClause({
|
|
546
|
+
model: unsafeModel,
|
|
547
|
+
where: unsafeWhere
|
|
548
|
+
});
|
|
549
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("update")} ${formatAction("Unsafe Input")}:`, {
|
|
550
|
+
model,
|
|
551
|
+
data: unsafeData
|
|
552
|
+
});
|
|
553
|
+
let data = unsafeData;
|
|
554
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
555
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("update")} ${formatAction("Parsed Input")}:`, {
|
|
556
|
+
model,
|
|
557
|
+
data
|
|
558
|
+
});
|
|
559
|
+
const res = await adapterInstance.update({
|
|
560
|
+
model,
|
|
561
|
+
where,
|
|
562
|
+
update: data
|
|
563
|
+
});
|
|
564
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("update")} ${formatAction("DB Result")}:`, {
|
|
565
|
+
model,
|
|
566
|
+
data: res
|
|
567
|
+
});
|
|
568
|
+
let transformed = res;
|
|
569
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, void 0, void 0);
|
|
570
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("update")} ${formatAction("Parsed Result")}:`, {
|
|
571
|
+
model,
|
|
572
|
+
data: transformed
|
|
573
|
+
});
|
|
574
|
+
return transformed;
|
|
575
|
+
},
|
|
576
|
+
updateMany: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
577
|
+
transactionId++;
|
|
578
|
+
let thisTransactionId = transactionId;
|
|
579
|
+
const model = getModelName(unsafeModel);
|
|
580
|
+
const where = transformWhereClause({
|
|
581
|
+
model: unsafeModel,
|
|
582
|
+
where: unsafeWhere
|
|
583
|
+
});
|
|
584
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
585
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("updateMany")} ${formatAction("Unsafe Input")}:`, {
|
|
586
|
+
model,
|
|
587
|
+
data: unsafeData
|
|
588
|
+
});
|
|
589
|
+
let data = unsafeData;
|
|
590
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
591
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("updateMany")} ${formatAction("Parsed Input")}:`, {
|
|
592
|
+
model,
|
|
593
|
+
data
|
|
594
|
+
});
|
|
595
|
+
const updatedCount = await adapterInstance.updateMany({
|
|
596
|
+
model,
|
|
597
|
+
where,
|
|
598
|
+
update: data
|
|
599
|
+
});
|
|
600
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("updateMany")} ${formatAction("DB Result")}:`, {
|
|
601
|
+
model,
|
|
602
|
+
data: updatedCount
|
|
603
|
+
});
|
|
604
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("updateMany")} ${formatAction("Parsed Result")}:`, {
|
|
605
|
+
model,
|
|
606
|
+
data: updatedCount
|
|
607
|
+
});
|
|
608
|
+
return updatedCount;
|
|
609
|
+
},
|
|
610
|
+
findOne: async ({ model: unsafeModel, where: unsafeWhere, select, join: unsafeJoin }) => {
|
|
611
|
+
transactionId++;
|
|
612
|
+
let thisTransactionId = transactionId;
|
|
613
|
+
const model = getModelName(unsafeModel);
|
|
614
|
+
const where = transformWhereClause({
|
|
615
|
+
model: unsafeModel,
|
|
616
|
+
where: unsafeWhere
|
|
617
|
+
});
|
|
618
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
619
|
+
let join;
|
|
620
|
+
let passJoinToAdapter = true;
|
|
621
|
+
if (!config.disableTransformJoin) {
|
|
622
|
+
const result = transformJoinClause(unsafeModel, unsafeJoin, select);
|
|
623
|
+
if (result) {
|
|
624
|
+
join = result.join;
|
|
625
|
+
select = result.select;
|
|
626
|
+
}
|
|
627
|
+
if (!options.experimental?.joins && join && Object.keys(join).length > 0) passJoinToAdapter = false;
|
|
628
|
+
} else join = unsafeJoin;
|
|
629
|
+
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("findOne")}:`, {
|
|
630
|
+
model,
|
|
631
|
+
where,
|
|
632
|
+
select,
|
|
633
|
+
join
|
|
634
|
+
});
|
|
635
|
+
const res = await adapterInstance.findOne({
|
|
636
|
+
model,
|
|
637
|
+
where,
|
|
638
|
+
select,
|
|
639
|
+
join: passJoinToAdapter ? join : void 0
|
|
640
|
+
});
|
|
641
|
+
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("findOne")} ${formatAction("DB Result")}:`, {
|
|
642
|
+
model,
|
|
643
|
+
data: res
|
|
644
|
+
});
|
|
645
|
+
let transformed = res;
|
|
646
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select, join);
|
|
647
|
+
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("findOne")} ${formatAction("Parsed Result")}:`, {
|
|
648
|
+
model,
|
|
649
|
+
data: transformed
|
|
650
|
+
});
|
|
651
|
+
return transformed;
|
|
652
|
+
},
|
|
653
|
+
findMany: async ({ model: unsafeModel, where: unsafeWhere, limit: unsafeLimit, sortBy, offset, join: unsafeJoin }) => {
|
|
654
|
+
transactionId++;
|
|
655
|
+
let thisTransactionId = transactionId;
|
|
656
|
+
const limit = unsafeLimit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
657
|
+
const model = getModelName(unsafeModel);
|
|
658
|
+
const where = transformWhereClause({
|
|
659
|
+
model: unsafeModel,
|
|
660
|
+
where: unsafeWhere
|
|
661
|
+
});
|
|
662
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
663
|
+
let join;
|
|
664
|
+
let passJoinToAdapter = true;
|
|
665
|
+
if (!config.disableTransformJoin) {
|
|
666
|
+
const result = transformJoinClause(unsafeModel, unsafeJoin, void 0);
|
|
667
|
+
if (result) join = result.join;
|
|
668
|
+
if (!options.experimental?.joins && join && Object.keys(join).length > 0) passJoinToAdapter = false;
|
|
669
|
+
} else join = unsafeJoin;
|
|
670
|
+
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("findMany")}:`, {
|
|
671
|
+
model,
|
|
672
|
+
where,
|
|
673
|
+
limit,
|
|
674
|
+
sortBy,
|
|
675
|
+
offset,
|
|
676
|
+
join
|
|
677
|
+
});
|
|
678
|
+
const res = await adapterInstance.findMany({
|
|
679
|
+
model,
|
|
680
|
+
where,
|
|
681
|
+
limit,
|
|
682
|
+
sortBy,
|
|
683
|
+
offset,
|
|
684
|
+
join: passJoinToAdapter ? join : void 0
|
|
685
|
+
});
|
|
686
|
+
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("findMany")} ${formatAction("DB Result")}:`, {
|
|
687
|
+
model,
|
|
688
|
+
data: res
|
|
689
|
+
});
|
|
690
|
+
let transformed = res;
|
|
691
|
+
if (!config.disableTransformOutput) transformed = await Promise.all(res.map(async (r) => {
|
|
692
|
+
return await transformOutput(r, unsafeModel, void 0, join);
|
|
693
|
+
}));
|
|
694
|
+
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("findMany")} ${formatAction("Parsed Result")}:`, {
|
|
695
|
+
model,
|
|
696
|
+
data: transformed
|
|
697
|
+
});
|
|
698
|
+
return transformed;
|
|
699
|
+
},
|
|
700
|
+
delete: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
701
|
+
transactionId++;
|
|
702
|
+
let thisTransactionId = transactionId;
|
|
703
|
+
const model = getModelName(unsafeModel);
|
|
704
|
+
const where = transformWhereClause({
|
|
705
|
+
model: unsafeModel,
|
|
706
|
+
where: unsafeWhere
|
|
707
|
+
});
|
|
708
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
709
|
+
debugLog({ method: "delete" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("delete")}:`, {
|
|
710
|
+
model,
|
|
711
|
+
where
|
|
712
|
+
});
|
|
713
|
+
await adapterInstance.delete({
|
|
714
|
+
model,
|
|
715
|
+
where
|
|
716
|
+
});
|
|
717
|
+
debugLog({ method: "delete" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("delete")} ${formatAction("DB Result")}:`, { model });
|
|
718
|
+
},
|
|
719
|
+
deleteMany: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
720
|
+
transactionId++;
|
|
721
|
+
let thisTransactionId = transactionId;
|
|
722
|
+
const model = getModelName(unsafeModel);
|
|
723
|
+
const where = transformWhereClause({
|
|
724
|
+
model: unsafeModel,
|
|
725
|
+
where: unsafeWhere
|
|
726
|
+
});
|
|
727
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
728
|
+
debugLog({ method: "deleteMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("deleteMany")} ${formatAction("DeleteMany")}:`, {
|
|
729
|
+
model,
|
|
730
|
+
where
|
|
731
|
+
});
|
|
732
|
+
const res = await adapterInstance.deleteMany({
|
|
733
|
+
model,
|
|
734
|
+
where
|
|
735
|
+
});
|
|
736
|
+
debugLog({ method: "deleteMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("deleteMany")} ${formatAction("DB Result")}:`, {
|
|
737
|
+
model,
|
|
738
|
+
data: res
|
|
739
|
+
});
|
|
740
|
+
return res;
|
|
741
|
+
},
|
|
742
|
+
count: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
743
|
+
transactionId++;
|
|
744
|
+
let thisTransactionId = transactionId;
|
|
745
|
+
const model = getModelName(unsafeModel);
|
|
746
|
+
const where = transformWhereClause({
|
|
747
|
+
model: unsafeModel,
|
|
748
|
+
where: unsafeWhere
|
|
749
|
+
});
|
|
750
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
751
|
+
debugLog({ method: "count" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("count")}:`, {
|
|
752
|
+
model,
|
|
753
|
+
where
|
|
754
|
+
});
|
|
755
|
+
const res = await adapterInstance.count({
|
|
756
|
+
model,
|
|
757
|
+
where
|
|
758
|
+
});
|
|
759
|
+
debugLog({ method: "count" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("count")}:`, {
|
|
760
|
+
model,
|
|
761
|
+
data: res
|
|
762
|
+
});
|
|
763
|
+
return res;
|
|
764
|
+
},
|
|
765
|
+
createSchema: adapterInstance.createSchema ? async (_, file) => {
|
|
766
|
+
const tables = getAuthTables(options);
|
|
767
|
+
if (options.secondaryStorage && !options.session?.storeSessionInDatabase) delete tables.session;
|
|
768
|
+
if (options.rateLimit && options.rateLimit.storage === "database" && (typeof options.rateLimit.enabled === "undefined" || options.rateLimit.enabled === true)) tables.ratelimit = {
|
|
769
|
+
modelName: options.rateLimit.modelName ?? "ratelimit",
|
|
770
|
+
fields: {
|
|
771
|
+
key: {
|
|
772
|
+
type: "string",
|
|
773
|
+
unique: true,
|
|
774
|
+
required: true,
|
|
775
|
+
fieldName: options.rateLimit.fields?.key ?? "key"
|
|
776
|
+
},
|
|
777
|
+
count: {
|
|
778
|
+
type: "number",
|
|
779
|
+
required: true,
|
|
780
|
+
fieldName: options.rateLimit.fields?.count ?? "count"
|
|
781
|
+
},
|
|
782
|
+
lastRequest: {
|
|
783
|
+
type: "number",
|
|
784
|
+
required: true,
|
|
785
|
+
bigint: true,
|
|
786
|
+
defaultValue: () => Date.now(),
|
|
787
|
+
fieldName: options.rateLimit.fields?.lastRequest ?? "lastRequest"
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
return adapterInstance.createSchema({
|
|
792
|
+
file,
|
|
793
|
+
tables
|
|
794
|
+
});
|
|
795
|
+
} : void 0,
|
|
796
|
+
options: {
|
|
797
|
+
adapterConfig: config,
|
|
798
|
+
...adapterInstance.options ?? {}
|
|
799
|
+
},
|
|
800
|
+
id: config.adapterId,
|
|
801
|
+
...config.debugLogs?.isRunningAdapterTests ? { adapterTestDebugLogs: {
|
|
802
|
+
resetDebugLogs() {
|
|
803
|
+
debugLogs = debugLogs.filter((log) => log.instance !== uniqueAdapterFactoryInstanceId);
|
|
804
|
+
},
|
|
805
|
+
printDebugLogs() {
|
|
806
|
+
const separator = `─`.repeat(80);
|
|
807
|
+
const logs = debugLogs.filter((log$1) => log$1.instance === uniqueAdapterFactoryInstanceId);
|
|
808
|
+
if (logs.length === 0) return;
|
|
809
|
+
let log = logs.reverse().map((log$1) => {
|
|
810
|
+
log$1.args[0] = `\n${log$1.args[0]}`;
|
|
811
|
+
return [...log$1.args, "\n"];
|
|
812
|
+
}).reduce((prev, curr) => {
|
|
813
|
+
return [...curr, ...prev];
|
|
814
|
+
}, [`\n${separator}`]);
|
|
815
|
+
console.log(...log);
|
|
816
|
+
}
|
|
817
|
+
} } : {}
|
|
818
|
+
};
|
|
819
|
+
return adapter;
|
|
820
|
+
};
|
|
821
|
+
function formatTransactionId(transactionId$1) {
|
|
822
|
+
if (getColorDepth() < 8) return `#${transactionId$1}`;
|
|
823
|
+
return `${TTY_COLORS.fg.magenta}#${transactionId$1}${TTY_COLORS.reset}`;
|
|
824
|
+
}
|
|
825
|
+
function formatStep(step, total) {
|
|
826
|
+
return `${TTY_COLORS.bg.black}${TTY_COLORS.fg.yellow}[${step}/${total}]${TTY_COLORS.reset}`;
|
|
827
|
+
}
|
|
828
|
+
function formatMethod(method) {
|
|
829
|
+
return `${TTY_COLORS.bright}${method}${TTY_COLORS.reset}`;
|
|
830
|
+
}
|
|
831
|
+
function formatAction(action) {
|
|
832
|
+
return `${TTY_COLORS.dim}(${action})${TTY_COLORS.reset}`;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
//#endregion
|
|
836
|
+
export { createAdapterFactory as t };
|