@better-auth/core 1.4.12-beta.2 → 1.4.13
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/.turbo/turbo-build.log +172 -35
- package/dist/api/index.d.mts +178 -1
- package/dist/api/index.mjs +2 -1
- package/dist/context/endpoint-context.d.mts +19 -0
- package/dist/context/endpoint-context.mjs +31 -0
- package/dist/context/global.d.mts +7 -0
- package/dist/context/global.mjs +37 -0
- package/dist/context/index.d.mts +5 -53
- package/dist/context/index.mjs +5 -2
- package/dist/context/request-state.d.mts +27 -0
- package/dist/context/request-state.mjs +49 -0
- package/dist/context/transaction.d.mts +16 -0
- package/dist/context/transaction.mjs +52 -0
- package/dist/db/adapter/factory.d.mts +27 -0
- package/dist/db/adapter/factory.mjs +738 -0
- package/dist/db/adapter/get-default-field-name.d.mts +18 -0
- package/dist/db/adapter/get-default-field-name.mjs +38 -0
- package/dist/db/adapter/get-default-model-name.d.mts +12 -0
- package/dist/db/adapter/get-default-model-name.mjs +32 -0
- package/dist/db/adapter/get-field-attributes.d.mts +29 -0
- package/dist/db/adapter/get-field-attributes.mjs +39 -0
- package/dist/db/adapter/get-field-name.d.mts +18 -0
- package/dist/db/adapter/get-field-name.mjs +33 -0
- package/dist/db/adapter/get-id-field.d.mts +39 -0
- package/dist/db/adapter/get-id-field.mjs +68 -0
- package/dist/db/adapter/get-model-name.d.mts +12 -0
- package/dist/db/adapter/get-model-name.mjs +23 -0
- package/dist/db/adapter/index.d.mts +513 -1
- package/dist/db/adapter/index.mjs +8 -970
- package/dist/db/adapter/types.d.mts +139 -0
- package/dist/db/adapter/utils.d.mts +7 -0
- package/dist/db/adapter/utils.mjs +38 -0
- package/dist/db/get-tables.d.mts +8 -0
- package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
- package/dist/db/index.d.mts +10 -2
- package/dist/db/index.mjs +7 -60
- package/dist/db/plugin.d.mts +12 -0
- package/dist/db/schema/account.d.mts +26 -0
- package/dist/db/schema/account.mjs +19 -0
- package/dist/db/schema/rate-limit.d.mts +14 -0
- package/dist/db/schema/rate-limit.mjs +11 -0
- package/dist/db/schema/session.d.mts +21 -0
- package/dist/db/schema/session.mjs +14 -0
- package/dist/db/schema/shared.d.mts +10 -0
- package/dist/db/schema/shared.mjs +11 -0
- package/dist/db/schema/user.d.mts +20 -0
- package/dist/db/schema/user.mjs +13 -0
- package/dist/db/schema/verification.d.mts +19 -0
- package/dist/db/schema/verification.mjs +12 -0
- package/dist/db/type.d.mts +143 -0
- package/dist/env/color-depth.d.mts +4 -0
- package/dist/env/color-depth.mjs +88 -0
- package/dist/env/env-impl.d.mts +32 -0
- package/dist/env/env-impl.mjs +82 -0
- package/dist/env/index.d.mts +4 -2
- package/dist/env/index.mjs +3 -1
- package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
- package/dist/env/logger.mjs +81 -0
- package/dist/error/codes.d.mts +48 -0
- package/dist/{error-DP1xOn7P.mjs → error/codes.mjs} +3 -14
- package/dist/error/index.d.mts +5 -48
- package/dist/error/index.mjs +12 -3
- package/dist/index.d.mts +8 -2
- package/dist/oauth2/client-credentials-token.d.mts +36 -0
- package/dist/oauth2/client-credentials-token.mjs +54 -0
- package/dist/oauth2/create-authorization-url.d.mts +45 -0
- package/dist/oauth2/create-authorization-url.mjs +42 -0
- package/dist/oauth2/index.d.mts +8 -2
- package/dist/oauth2/index.mjs +6 -2
- package/dist/oauth2/oauth-provider.d.mts +194 -0
- package/dist/oauth2/refresh-access-token.d.mts +36 -0
- package/dist/oauth2/refresh-access-token.mjs +58 -0
- package/dist/oauth2/utils.d.mts +7 -0
- package/dist/oauth2/utils.mjs +27 -0
- package/dist/oauth2/validate-authorization-code.d.mts +55 -0
- package/dist/oauth2/validate-authorization-code.mjs +71 -0
- package/dist/oauth2/verify.d.mts +49 -0
- package/dist/oauth2/verify.mjs +95 -0
- package/dist/social-providers/apple.d.mts +119 -0
- package/dist/social-providers/apple.mjs +102 -0
- package/dist/social-providers/atlassian.d.mts +72 -0
- package/dist/social-providers/atlassian.mjs +83 -0
- package/dist/social-providers/cognito.d.mts +87 -0
- package/dist/social-providers/cognito.mjs +166 -0
- package/dist/social-providers/discord.d.mts +126 -0
- package/dist/social-providers/discord.mjs +64 -0
- package/dist/social-providers/dropbox.d.mts +71 -0
- package/dist/social-providers/dropbox.mjs +75 -0
- package/dist/social-providers/facebook.d.mts +81 -0
- package/dist/social-providers/facebook.mjs +120 -0
- package/dist/social-providers/figma.d.mts +63 -0
- package/dist/social-providers/figma.mjs +84 -0
- package/dist/social-providers/github.d.mts +104 -0
- package/dist/social-providers/github.mjs +80 -0
- package/dist/social-providers/gitlab.d.mts +125 -0
- package/dist/social-providers/gitlab.mjs +82 -0
- package/dist/social-providers/google.d.mts +99 -0
- package/dist/social-providers/google.mjs +109 -0
- package/dist/social-providers/huggingface.d.mts +85 -0
- package/dist/social-providers/huggingface.mjs +75 -0
- package/dist/social-providers/index.d.mts +1723 -1
- package/dist/social-providers/index.mjs +33 -2570
- package/dist/social-providers/kakao.d.mts +163 -0
- package/dist/social-providers/kakao.mjs +72 -0
- package/dist/social-providers/kick.d.mts +75 -0
- package/dist/social-providers/kick.mjs +71 -0
- package/dist/social-providers/line.d.mts +107 -0
- package/dist/social-providers/line.mjs +113 -0
- package/dist/social-providers/linear.d.mts +70 -0
- package/dist/social-providers/linear.mjs +88 -0
- package/dist/social-providers/linkedin.d.mts +69 -0
- package/dist/social-providers/linkedin.mjs +76 -0
- package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
- package/dist/social-providers/microsoft-entra-id.mjs +106 -0
- package/dist/social-providers/naver.d.mts +104 -0
- package/dist/social-providers/naver.mjs +67 -0
- package/dist/social-providers/notion.d.mts +66 -0
- package/dist/social-providers/notion.mjs +75 -0
- package/dist/social-providers/paybin.d.mts +73 -0
- package/dist/social-providers/paybin.mjs +85 -0
- package/dist/social-providers/paypal.d.mts +131 -0
- package/dist/social-providers/paypal.mjs +144 -0
- package/dist/social-providers/polar.d.mts +76 -0
- package/dist/social-providers/polar.mjs +73 -0
- package/dist/social-providers/reddit.d.mts +64 -0
- package/dist/social-providers/reddit.mjs +83 -0
- package/dist/social-providers/roblox.d.mts +72 -0
- package/dist/social-providers/roblox.mjs +59 -0
- package/dist/social-providers/salesforce.d.mts +81 -0
- package/dist/social-providers/salesforce.mjs +91 -0
- package/dist/social-providers/slack.d.mts +85 -0
- package/dist/social-providers/slack.mjs +68 -0
- package/dist/social-providers/spotify.d.mts +65 -0
- package/dist/social-providers/spotify.mjs +71 -0
- package/dist/social-providers/tiktok.d.mts +171 -0
- package/dist/social-providers/tiktok.mjs +62 -0
- package/dist/social-providers/twitch.d.mts +81 -0
- package/dist/social-providers/twitch.mjs +78 -0
- package/dist/social-providers/twitter.d.mts +140 -0
- package/dist/social-providers/twitter.mjs +87 -0
- package/dist/social-providers/vercel.d.mts +64 -0
- package/dist/social-providers/vercel.mjs +61 -0
- package/dist/social-providers/vk.d.mts +72 -0
- package/dist/social-providers/vk.mjs +83 -0
- package/dist/social-providers/zoom.d.mts +173 -0
- package/dist/social-providers/zoom.mjs +72 -0
- package/dist/types/context.d.mts +215 -0
- package/dist/types/cookie.d.mts +15 -0
- package/dist/types/helper.d.mts +8 -0
- package/dist/types/index.d.mts +8 -0
- package/dist/types/init-options.d.mts +1266 -0
- package/dist/types/plugin-client.d.mts +103 -0
- package/dist/types/plugin.d.mts +121 -0
- package/dist/utils/deprecate.d.mts +10 -0
- package/dist/utils/deprecate.mjs +17 -0
- package/dist/utils/error-codes.d.mts +9 -0
- package/dist/utils/error-codes.mjs +7 -0
- package/dist/utils/id.d.mts +4 -0
- package/dist/utils/id.mjs +9 -0
- package/dist/utils/index.d.mts +5 -26
- package/dist/utils/index.mjs +5 -2
- package/dist/utils/json.d.mts +4 -0
- package/dist/utils/json.mjs +25 -0
- package/dist/utils/string.d.mts +4 -0
- package/dist/utils/string.mjs +7 -0
- package/package.json +1 -1
- package/src/context/endpoint-context.ts +7 -15
- package/src/context/global.ts +57 -0
- package/src/context/index.ts +1 -0
- package/src/context/request-state.ts +7 -12
- package/src/context/transaction.ts +7 -16
- package/src/db/adapter/factory.ts +13 -13
- package/src/db/adapter/get-default-model-name.ts +1 -1
- package/src/db/adapter/get-id-field.ts +2 -2
- package/src/error/index.ts +2 -3
- package/src/social-providers/gitlab.ts +1 -1
- package/src/types/context.ts +137 -131
- package/src/types/cookie.ts +6 -4
- package/src/types/index.ts +2 -1
- package/tsdown.config.ts +9 -0
- package/dist/context-BGZ8V6DD.mjs +0 -126
- package/dist/env-DbssmzoK.mjs +0 -245
- package/dist/index-zgYuzZ7O.d.mts +0 -8020
- package/dist/oauth2-COJkghlT.mjs +0 -326
- package/dist/utils-U2L7n92V.mjs +0 -59
|
@@ -1,972 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { initGetDefaultModelName } from "./get-default-model-name.mjs";
|
|
2
|
+
import { initGetDefaultFieldName } from "./get-default-field-name.mjs";
|
|
3
|
+
import { initGetIdField } from "./get-id-field.mjs";
|
|
4
|
+
import { initGetFieldAttributes } from "./get-field-attributes.mjs";
|
|
5
|
+
import { initGetFieldName } from "./get-field-name.mjs";
|
|
6
|
+
import { initGetModelName } from "./get-model-name.mjs";
|
|
7
|
+
import { deepmerge, withApplyDefault } from "./utils.mjs";
|
|
8
|
+
import { createAdapter, createAdapterFactory } from "./factory.mjs";
|
|
5
9
|
|
|
6
|
-
//#region src/db/adapter/get-default-model-name.ts
|
|
7
|
-
const initGetDefaultModelName = ({ usePlural, schema }) => {
|
|
8
|
-
/**
|
|
9
|
-
* This function helps us get the default model name from the schema defined by devs.
|
|
10
|
-
* Often times, the user will be using the `modelName` which could had been customized by the users.
|
|
11
|
-
* This function helps us get the actual model name useful to match against the schema. (eg: schema[model])
|
|
12
|
-
*
|
|
13
|
-
* If it's still unclear what this does:
|
|
14
|
-
*
|
|
15
|
-
* 1. User can define a custom modelName.
|
|
16
|
-
* 2. When using a custom modelName, doing something like `schema[model]` will not work.
|
|
17
|
-
* 3. Using this function helps us get the actual model name based on the user's defined custom modelName.
|
|
18
|
-
*/
|
|
19
|
-
const getDefaultModelName = (model) => {
|
|
20
|
-
if (usePlural && model.charAt(model.length - 1) === "s") {
|
|
21
|
-
let pluralessModel = model.slice(0, -1);
|
|
22
|
-
let m$1 = schema[pluralessModel] ? pluralessModel : void 0;
|
|
23
|
-
if (!m$1) m$1 = Object.entries(schema).find(([_, f]) => f.modelName === pluralessModel)?.[0];
|
|
24
|
-
if (m$1) return m$1;
|
|
25
|
-
}
|
|
26
|
-
let m = schema[model] ? model : void 0;
|
|
27
|
-
if (!m) m = Object.entries(schema).find(([_, f]) => f.modelName === model)?.[0];
|
|
28
|
-
if (!m) throw new BetterAuthError(`Model "${model}" not found in schema`);
|
|
29
|
-
return m;
|
|
30
|
-
};
|
|
31
|
-
return getDefaultModelName;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region src/db/adapter/get-default-field-name.ts
|
|
36
|
-
const initGetDefaultFieldName = ({ schema, usePlural }) => {
|
|
37
|
-
const getDefaultModelName = initGetDefaultModelName({
|
|
38
|
-
schema,
|
|
39
|
-
usePlural
|
|
40
|
-
});
|
|
41
|
-
/**
|
|
42
|
-
* This function helps us get the default field name from the schema defined by devs.
|
|
43
|
-
* Often times, the user will be using the `fieldName` which could had been customized by the users.
|
|
44
|
-
* This function helps us get the actual field name useful to match against the schema. (eg: schema[model].fields[field])
|
|
45
|
-
*
|
|
46
|
-
* If it's still unclear what this does:
|
|
47
|
-
*
|
|
48
|
-
* 1. User can define a custom fieldName.
|
|
49
|
-
* 2. When using a custom fieldName, doing something like `schema[model].fields[field]` will not work.
|
|
50
|
-
*/
|
|
51
|
-
const getDefaultFieldName = ({ field, model: unsafeModel }) => {
|
|
52
|
-
if (field === "id" || field === "_id") return "id";
|
|
53
|
-
const model = getDefaultModelName(unsafeModel);
|
|
54
|
-
let f = schema[model]?.fields[field];
|
|
55
|
-
if (!f) {
|
|
56
|
-
const result = Object.entries(schema[model].fields).find(([_, f$1]) => f$1.fieldName === field);
|
|
57
|
-
if (result) {
|
|
58
|
-
f = result[1];
|
|
59
|
-
field = result[0];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (!f) throw new BetterAuthError(`Field ${field} not found in model ${model}`);
|
|
63
|
-
return field;
|
|
64
|
-
};
|
|
65
|
-
return getDefaultFieldName;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
//#endregion
|
|
69
|
-
//#region src/db/adapter/get-id-field.ts
|
|
70
|
-
const initGetIdField = ({ usePlural, schema, disableIdGeneration, options, customIdGenerator, supportsUUIDs }) => {
|
|
71
|
-
const getDefaultModelName = initGetDefaultModelName({
|
|
72
|
-
usePlural,
|
|
73
|
-
schema
|
|
74
|
-
});
|
|
75
|
-
const idField = ({ customModelName, forceAllowId }) => {
|
|
76
|
-
const useNumberId = options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial";
|
|
77
|
-
const useUUIDs = options.advanced?.database?.generateId === "uuid";
|
|
78
|
-
let shouldGenerateId = (() => {
|
|
79
|
-
if (disableIdGeneration) return false;
|
|
80
|
-
else if (useNumberId && !forceAllowId) return false;
|
|
81
|
-
else if (useUUIDs) return !supportsUUIDs;
|
|
82
|
-
else return true;
|
|
83
|
-
})();
|
|
84
|
-
const model = getDefaultModelName(customModelName ?? "id");
|
|
85
|
-
return {
|
|
86
|
-
type: useNumberId ? "number" : "string",
|
|
87
|
-
required: shouldGenerateId ? true : false,
|
|
88
|
-
...shouldGenerateId ? { defaultValue() {
|
|
89
|
-
if (disableIdGeneration) return void 0;
|
|
90
|
-
let generateId$1 = options.advanced?.database?.generateId;
|
|
91
|
-
if (generateId$1 === false || useNumberId) return void 0;
|
|
92
|
-
if (typeof generateId$1 === "function") return generateId$1({ model });
|
|
93
|
-
if (customIdGenerator) return customIdGenerator({ model });
|
|
94
|
-
if (generateId$1 === "uuid") return crypto.randomUUID();
|
|
95
|
-
return generateId();
|
|
96
|
-
} } : {},
|
|
97
|
-
transform: {
|
|
98
|
-
input: (value) => {
|
|
99
|
-
if (!value) return void 0;
|
|
100
|
-
if (useNumberId) {
|
|
101
|
-
const numberValue = Number(value);
|
|
102
|
-
if (isNaN(numberValue)) return;
|
|
103
|
-
return numberValue;
|
|
104
|
-
}
|
|
105
|
-
if (useUUIDs) {
|
|
106
|
-
if (shouldGenerateId && !forceAllowId) return value;
|
|
107
|
-
if (disableIdGeneration) return void 0;
|
|
108
|
-
if (supportsUUIDs) return void 0;
|
|
109
|
-
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;
|
|
110
|
-
else {
|
|
111
|
-
const stack = (/* @__PURE__ */ new Error()).stack?.split("\n").filter((_, i) => i !== 1).join("\n").replace("Error:", "");
|
|
112
|
-
logger.warn("[Adapter Factory] - Invalid UUID value for field `id` provided when `forceAllowId` is true. Generating a new UUID.", stack);
|
|
113
|
-
}
|
|
114
|
-
if (typeof value !== "string" && !supportsUUIDs) return crypto.randomUUID();
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
return value;
|
|
118
|
-
},
|
|
119
|
-
output: (value) => {
|
|
120
|
-
if (!value) return void 0;
|
|
121
|
-
return String(value);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
return idField;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
//#endregion
|
|
130
|
-
//#region src/db/adapter/get-field-attributes.ts
|
|
131
|
-
const initGetFieldAttributes = ({ usePlural, schema, options, customIdGenerator, disableIdGeneration }) => {
|
|
132
|
-
const getDefaultModelName = initGetDefaultModelName({
|
|
133
|
-
usePlural,
|
|
134
|
-
schema
|
|
135
|
-
});
|
|
136
|
-
const getDefaultFieldName = initGetDefaultFieldName({
|
|
137
|
-
usePlural,
|
|
138
|
-
schema
|
|
139
|
-
});
|
|
140
|
-
const idField = initGetIdField({
|
|
141
|
-
usePlural,
|
|
142
|
-
schema,
|
|
143
|
-
options,
|
|
144
|
-
customIdGenerator,
|
|
145
|
-
disableIdGeneration
|
|
146
|
-
});
|
|
147
|
-
const getFieldAttributes = ({ model, field }) => {
|
|
148
|
-
const defaultModelName = getDefaultModelName(model);
|
|
149
|
-
const defaultFieldName = getDefaultFieldName({
|
|
150
|
-
field,
|
|
151
|
-
model: defaultModelName
|
|
152
|
-
});
|
|
153
|
-
const fields = schema[defaultModelName].fields;
|
|
154
|
-
fields.id = idField({ customModelName: defaultModelName });
|
|
155
|
-
const fieldAttributes = fields[defaultFieldName];
|
|
156
|
-
if (!fieldAttributes) throw new BetterAuthError(`Field ${field} not found in model ${model}`);
|
|
157
|
-
return fieldAttributes;
|
|
158
|
-
};
|
|
159
|
-
return getFieldAttributes;
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
//#endregion
|
|
163
|
-
//#region src/db/adapter/get-field-name.ts
|
|
164
|
-
const initGetFieldName = ({ schema, usePlural }) => {
|
|
165
|
-
const getDefaultModelName = initGetDefaultModelName({
|
|
166
|
-
schema,
|
|
167
|
-
usePlural
|
|
168
|
-
});
|
|
169
|
-
const getDefaultFieldName = initGetDefaultFieldName({
|
|
170
|
-
schema,
|
|
171
|
-
usePlural
|
|
172
|
-
});
|
|
173
|
-
/**
|
|
174
|
-
* Get the field name which is expected to be saved in the database based on the user's schema.
|
|
175
|
-
*
|
|
176
|
-
* This function is useful if you need to save the field name to the database.
|
|
177
|
-
*
|
|
178
|
-
* For example, if the user has defined a custom field name for the `user` model, then you can use this function to get the actual field name from the schema.
|
|
179
|
-
*/
|
|
180
|
-
function getFieldName({ model: modelName, field: fieldName }) {
|
|
181
|
-
const model = getDefaultModelName(modelName);
|
|
182
|
-
const field = getDefaultFieldName({
|
|
183
|
-
model,
|
|
184
|
-
field: fieldName
|
|
185
|
-
});
|
|
186
|
-
return schema[model]?.fields[field]?.fieldName || field;
|
|
187
|
-
}
|
|
188
|
-
return getFieldName;
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
//#endregion
|
|
192
|
-
//#region src/db/adapter/get-model-name.ts
|
|
193
|
-
const initGetModelName = ({ usePlural, schema }) => {
|
|
194
|
-
const getDefaultModelName = initGetDefaultModelName({
|
|
195
|
-
schema,
|
|
196
|
-
usePlural
|
|
197
|
-
});
|
|
198
|
-
/**
|
|
199
|
-
* Users can overwrite the default model of some tables. This function helps find the correct model name.
|
|
200
|
-
* Furthermore, if the user passes `usePlural` as true in their adapter config,
|
|
201
|
-
* then we should return the model name ending with an `s`.
|
|
202
|
-
*/
|
|
203
|
-
const getModelName = (model) => {
|
|
204
|
-
const defaultModelKey = getDefaultModelName(model);
|
|
205
|
-
if (schema && schema[defaultModelKey] && schema[defaultModelKey].modelName !== model) return usePlural ? `${schema[defaultModelKey].modelName}s` : schema[defaultModelKey].modelName;
|
|
206
|
-
return usePlural ? `${model}s` : model;
|
|
207
|
-
};
|
|
208
|
-
return getModelName;
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
//#endregion
|
|
212
|
-
//#region src/db/adapter/utils.ts
|
|
213
|
-
function withApplyDefault(value, field, action) {
|
|
214
|
-
if (action === "update") {
|
|
215
|
-
if (value === void 0 && field.onUpdate !== void 0) {
|
|
216
|
-
if (typeof field.onUpdate === "function") return field.onUpdate();
|
|
217
|
-
return field.onUpdate;
|
|
218
|
-
}
|
|
219
|
-
return value;
|
|
220
|
-
}
|
|
221
|
-
if (action === "create") {
|
|
222
|
-
if (value === void 0 || field.required === true && value === null) {
|
|
223
|
-
if (field.defaultValue !== void 0) {
|
|
224
|
-
if (typeof field.defaultValue === "function") return field.defaultValue();
|
|
225
|
-
return field.defaultValue;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return value;
|
|
230
|
-
}
|
|
231
|
-
function isObject(item) {
|
|
232
|
-
return item !== null && typeof item === "object" && !Array.isArray(item);
|
|
233
|
-
}
|
|
234
|
-
function deepmerge(target, source) {
|
|
235
|
-
if (Array.isArray(target) && Array.isArray(source)) return [...target, ...source];
|
|
236
|
-
else if (isObject(target) && isObject(source)) {
|
|
237
|
-
const result = { ...target };
|
|
238
|
-
for (const [key, value] of Object.entries(source)) {
|
|
239
|
-
if (value === void 0) continue;
|
|
240
|
-
if (key in target) result[key] = deepmerge(target[key], value);
|
|
241
|
-
else result[key] = value;
|
|
242
|
-
}
|
|
243
|
-
return result;
|
|
244
|
-
}
|
|
245
|
-
return source;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
//#endregion
|
|
249
|
-
//#region src/db/adapter/factory.ts
|
|
250
|
-
let debugLogs = [];
|
|
251
|
-
let transactionId = -1;
|
|
252
|
-
const createAsIsTransaction = (adapter) => (fn) => fn(adapter);
|
|
253
|
-
const createAdapterFactory = ({ adapter: customAdapter, config: cfg }) => (options) => {
|
|
254
|
-
const uniqueAdapterFactoryInstanceId = Math.random().toString(36).substring(2, 15);
|
|
255
|
-
const config = {
|
|
256
|
-
...cfg,
|
|
257
|
-
supportsBooleans: cfg.supportsBooleans ?? true,
|
|
258
|
-
supportsDates: cfg.supportsDates ?? true,
|
|
259
|
-
supportsJSON: cfg.supportsJSON ?? false,
|
|
260
|
-
adapterName: cfg.adapterName ?? cfg.adapterId,
|
|
261
|
-
supportsNumericIds: cfg.supportsNumericIds ?? true,
|
|
262
|
-
supportsUUIDs: cfg.supportsUUIDs ?? false,
|
|
263
|
-
supportsArrays: cfg.supportsArrays ?? false,
|
|
264
|
-
transaction: cfg.transaction ?? false,
|
|
265
|
-
disableTransformInput: cfg.disableTransformInput ?? false,
|
|
266
|
-
disableTransformOutput: cfg.disableTransformOutput ?? false,
|
|
267
|
-
disableTransformJoin: cfg.disableTransformJoin ?? false
|
|
268
|
-
};
|
|
269
|
-
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.`);
|
|
270
|
-
const schema = getAuthTables(options);
|
|
271
|
-
const debugLog = (...args) => {
|
|
272
|
-
if (config.debugLogs === true || typeof config.debugLogs === "object") {
|
|
273
|
-
const logger$2 = createLogger({ level: "info" });
|
|
274
|
-
if (typeof config.debugLogs === "object" && "isRunningAdapterTests" in config.debugLogs) {
|
|
275
|
-
if (config.debugLogs.isRunningAdapterTests) {
|
|
276
|
-
args.shift();
|
|
277
|
-
debugLogs.push({
|
|
278
|
-
instance: uniqueAdapterFactoryInstanceId,
|
|
279
|
-
args
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
if (typeof config.debugLogs === "object" && config.debugLogs.logCondition && !config.debugLogs.logCondition?.()) return;
|
|
285
|
-
if (typeof args[0] === "object" && "method" in args[0]) {
|
|
286
|
-
const method = args.shift().method;
|
|
287
|
-
if (typeof config.debugLogs === "object") {
|
|
288
|
-
if (method === "create" && !config.debugLogs.create) return;
|
|
289
|
-
else if (method === "update" && !config.debugLogs.update) return;
|
|
290
|
-
else if (method === "updateMany" && !config.debugLogs.updateMany) return;
|
|
291
|
-
else if (method === "findOne" && !config.debugLogs.findOne) return;
|
|
292
|
-
else if (method === "findMany" && !config.debugLogs.findMany) return;
|
|
293
|
-
else if (method === "delete" && !config.debugLogs.delete) return;
|
|
294
|
-
else if (method === "deleteMany" && !config.debugLogs.deleteMany) return;
|
|
295
|
-
else if (method === "count" && !config.debugLogs.count) return;
|
|
296
|
-
}
|
|
297
|
-
logger$2.info(`[${config.adapterName}]`, ...args);
|
|
298
|
-
} else logger$2.info(`[${config.adapterName}]`, ...args);
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
const logger$1 = createLogger(options.logger);
|
|
302
|
-
const getDefaultModelName = initGetDefaultModelName({
|
|
303
|
-
usePlural: config.usePlural,
|
|
304
|
-
schema
|
|
305
|
-
});
|
|
306
|
-
const getDefaultFieldName = initGetDefaultFieldName({
|
|
307
|
-
usePlural: config.usePlural,
|
|
308
|
-
schema
|
|
309
|
-
});
|
|
310
|
-
const getModelName = initGetModelName({
|
|
311
|
-
usePlural: config.usePlural,
|
|
312
|
-
schema
|
|
313
|
-
});
|
|
314
|
-
const getFieldName = initGetFieldName({
|
|
315
|
-
schema,
|
|
316
|
-
usePlural: config.usePlural
|
|
317
|
-
});
|
|
318
|
-
const idField = initGetIdField({
|
|
319
|
-
schema,
|
|
320
|
-
options,
|
|
321
|
-
usePlural: config.usePlural,
|
|
322
|
-
disableIdGeneration: config.disableIdGeneration,
|
|
323
|
-
customIdGenerator: config.customIdGenerator,
|
|
324
|
-
supportsUUIDs: config.supportsUUIDs
|
|
325
|
-
});
|
|
326
|
-
const getFieldAttributes = initGetFieldAttributes({
|
|
327
|
-
schema,
|
|
328
|
-
options,
|
|
329
|
-
usePlural: config.usePlural,
|
|
330
|
-
disableIdGeneration: config.disableIdGeneration,
|
|
331
|
-
customIdGenerator: config.customIdGenerator
|
|
332
|
-
});
|
|
333
|
-
const transformInput = async (data, defaultModelName, action, forceAllowId) => {
|
|
334
|
-
const transformedData = {};
|
|
335
|
-
const fields = schema[defaultModelName].fields;
|
|
336
|
-
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
337
|
-
const useNumberId = options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial";
|
|
338
|
-
fields.id = idField({
|
|
339
|
-
customModelName: defaultModelName,
|
|
340
|
-
forceAllowId: forceAllowId && "id" in data
|
|
341
|
-
});
|
|
342
|
-
for (const field in fields) {
|
|
343
|
-
let value = data[field];
|
|
344
|
-
const fieldAttributes = fields[field];
|
|
345
|
-
let newFieldName = newMappedKeys[field] || fields[field].fieldName || field;
|
|
346
|
-
if (value === void 0 && (fieldAttributes.defaultValue === void 0 && !fieldAttributes.transform?.input && !(action === "update" && fieldAttributes.onUpdate) || action === "update" && !fieldAttributes.onUpdate)) continue;
|
|
347
|
-
if (fieldAttributes && fieldAttributes.type === "date" && !(value instanceof Date) && typeof value === "string") try {
|
|
348
|
-
value = new Date(value);
|
|
349
|
-
} catch {
|
|
350
|
-
logger$1.error("[Adapter Factory] Failed to convert string to date", {
|
|
351
|
-
value,
|
|
352
|
-
field
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
let newValue = withApplyDefault(value, fieldAttributes, action);
|
|
356
|
-
if (fieldAttributes.transform?.input) newValue = await fieldAttributes.transform.input(newValue);
|
|
357
|
-
if (fieldAttributes.references?.field === "id" && useNumberId) if (Array.isArray(newValue)) newValue = newValue.map((x) => x !== null ? Number(x) : null);
|
|
358
|
-
else newValue = newValue !== null ? Number(newValue) : null;
|
|
359
|
-
else if (config.supportsJSON === false && typeof newValue === "object" && fieldAttributes.type === "json") newValue = JSON.stringify(newValue);
|
|
360
|
-
else if (config.supportsArrays === false && Array.isArray(newValue) && (fieldAttributes.type === "string[]" || fieldAttributes.type === "number[]")) newValue = JSON.stringify(newValue);
|
|
361
|
-
else if (config.supportsDates === false && newValue instanceof Date && fieldAttributes.type === "date") newValue = newValue.toISOString();
|
|
362
|
-
else if (config.supportsBooleans === false && typeof newValue === "boolean") newValue = newValue ? 1 : 0;
|
|
363
|
-
if (config.customTransformInput) newValue = config.customTransformInput({
|
|
364
|
-
data: newValue,
|
|
365
|
-
action,
|
|
366
|
-
field: newFieldName,
|
|
367
|
-
fieldAttributes,
|
|
368
|
-
model: getModelName(defaultModelName),
|
|
369
|
-
schema,
|
|
370
|
-
options
|
|
371
|
-
});
|
|
372
|
-
if (newValue !== void 0) transformedData[newFieldName] = newValue;
|
|
373
|
-
}
|
|
374
|
-
return transformedData;
|
|
375
|
-
};
|
|
376
|
-
const transformOutput = async (data, unsafe_model, select = [], join) => {
|
|
377
|
-
const transformSingleOutput = async (data$1, unsafe_model$1, select$1 = []) => {
|
|
378
|
-
if (!data$1) return null;
|
|
379
|
-
const newMappedKeys = config.mapKeysTransformOutput ?? {};
|
|
380
|
-
const transformedData$1 = {};
|
|
381
|
-
const tableSchema = schema[getDefaultModelName(unsafe_model$1)].fields;
|
|
382
|
-
const idKey = Object.entries(newMappedKeys).find(([_, v]) => v === "id")?.[0];
|
|
383
|
-
tableSchema[idKey ?? "id"] = { type: options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial" ? "number" : "string" };
|
|
384
|
-
for (const key in tableSchema) {
|
|
385
|
-
if (select$1.length && !select$1.includes(key)) continue;
|
|
386
|
-
const field = tableSchema[key];
|
|
387
|
-
if (field) {
|
|
388
|
-
const originalKey = field.fieldName || key;
|
|
389
|
-
let newValue = data$1[Object.entries(newMappedKeys).find(([_, v]) => v === originalKey)?.[0] || originalKey];
|
|
390
|
-
if (field.transform?.output) newValue = await field.transform.output(newValue);
|
|
391
|
-
let newFieldName = newMappedKeys[key] || key;
|
|
392
|
-
if (originalKey === "id" || field.references?.field === "id") {
|
|
393
|
-
if (typeof newValue !== "undefined" && newValue !== null) newValue = String(newValue);
|
|
394
|
-
} else if (config.supportsJSON === false && typeof newValue === "string" && field.type === "json") newValue = safeJSONParse(newValue);
|
|
395
|
-
else if (config.supportsArrays === false && typeof newValue === "string" && (field.type === "string[]" || field.type === "number[]")) newValue = safeJSONParse(newValue);
|
|
396
|
-
else if (config.supportsDates === false && typeof newValue === "string" && field.type === "date") newValue = new Date(newValue);
|
|
397
|
-
else if (config.supportsBooleans === false && typeof newValue === "number" && field.type === "boolean") newValue = newValue === 1;
|
|
398
|
-
if (config.customTransformOutput) newValue = config.customTransformOutput({
|
|
399
|
-
data: newValue,
|
|
400
|
-
field: newFieldName,
|
|
401
|
-
fieldAttributes: field,
|
|
402
|
-
select: select$1,
|
|
403
|
-
model: getModelName(unsafe_model$1),
|
|
404
|
-
schema,
|
|
405
|
-
options
|
|
406
|
-
});
|
|
407
|
-
transformedData$1[newFieldName] = newValue;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return transformedData$1;
|
|
411
|
-
};
|
|
412
|
-
if (!join || Object.keys(join).length === 0) return await transformSingleOutput(data, unsafe_model, select);
|
|
413
|
-
unsafe_model = getDefaultModelName(unsafe_model);
|
|
414
|
-
let transformedData = await transformSingleOutput(data, unsafe_model, select);
|
|
415
|
-
const requiredModels = Object.entries(join).map(([model, joinConfig]) => ({
|
|
416
|
-
modelName: getModelName(model),
|
|
417
|
-
defaultModelName: getDefaultModelName(model),
|
|
418
|
-
joinConfig
|
|
419
|
-
}));
|
|
420
|
-
if (!data) return null;
|
|
421
|
-
for (const { modelName, defaultModelName, joinConfig } of requiredModels) {
|
|
422
|
-
let joinedData = await (async () => {
|
|
423
|
-
if (options.experimental?.joins) return data[modelName];
|
|
424
|
-
else return await handleFallbackJoin({
|
|
425
|
-
baseModel: unsafe_model,
|
|
426
|
-
baseData: transformedData,
|
|
427
|
-
joinModel: modelName,
|
|
428
|
-
specificJoinConfig: joinConfig
|
|
429
|
-
});
|
|
430
|
-
})();
|
|
431
|
-
if (joinedData === void 0 || joinedData === null) joinedData = joinConfig.relation === "one-to-one" ? null : [];
|
|
432
|
-
if (joinConfig.relation === "one-to-many" && !Array.isArray(joinedData)) joinedData = [joinedData];
|
|
433
|
-
let transformed = [];
|
|
434
|
-
if (Array.isArray(joinedData)) for (const item of joinedData) {
|
|
435
|
-
const transformedItem = await transformSingleOutput(item, modelName, []);
|
|
436
|
-
transformed.push(transformedItem);
|
|
437
|
-
}
|
|
438
|
-
else {
|
|
439
|
-
const transformedItem = await transformSingleOutput(joinedData, modelName, []);
|
|
440
|
-
transformed.push(transformedItem);
|
|
441
|
-
}
|
|
442
|
-
transformedData[defaultModelName] = (joinConfig.relation === "one-to-one" ? transformed[0] : transformed) ?? null;
|
|
443
|
-
}
|
|
444
|
-
return transformedData;
|
|
445
|
-
};
|
|
446
|
-
const transformWhereClause = ({ model, where, action }) => {
|
|
447
|
-
if (!where) return void 0;
|
|
448
|
-
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
449
|
-
return where.map((w) => {
|
|
450
|
-
const { field: unsafe_field, value, operator = "eq", connector = "AND" } = w;
|
|
451
|
-
if (operator === "in") {
|
|
452
|
-
if (!Array.isArray(value)) throw new BetterAuthError("Value must be an array");
|
|
453
|
-
}
|
|
454
|
-
let newValue = value;
|
|
455
|
-
const defaultModelName = getDefaultModelName(model);
|
|
456
|
-
const defaultFieldName = getDefaultFieldName({
|
|
457
|
-
field: unsafe_field,
|
|
458
|
-
model
|
|
459
|
-
});
|
|
460
|
-
const fieldName = newMappedKeys[defaultFieldName] || getFieldName({
|
|
461
|
-
field: defaultFieldName,
|
|
462
|
-
model: defaultModelName
|
|
463
|
-
});
|
|
464
|
-
const fieldAttr = getFieldAttributes({
|
|
465
|
-
field: defaultFieldName,
|
|
466
|
-
model: defaultModelName
|
|
467
|
-
});
|
|
468
|
-
const useNumberId = options.advanced?.database?.useNumberId || options.advanced?.database?.generateId === "serial";
|
|
469
|
-
if (defaultFieldName === "id" || fieldAttr.references?.field === "id") {
|
|
470
|
-
if (useNumberId) if (Array.isArray(value)) newValue = value.map(Number);
|
|
471
|
-
else newValue = Number(value);
|
|
472
|
-
}
|
|
473
|
-
if (fieldAttr.type === "date" && value instanceof Date && !config.supportsDates) newValue = value.toISOString();
|
|
474
|
-
if (fieldAttr.type === "boolean" && typeof value === "boolean" && !config.supportsBooleans) newValue = value ? 1 : 0;
|
|
475
|
-
if (fieldAttr.type === "json" && typeof value === "object" && !config.supportsJSON) try {
|
|
476
|
-
newValue = JSON.stringify(value);
|
|
477
|
-
} catch (error) {
|
|
478
|
-
throw new Error(`Failed to stringify JSON value for field ${fieldName}`, { cause: error });
|
|
479
|
-
}
|
|
480
|
-
if (config.customTransformInput) newValue = config.customTransformInput({
|
|
481
|
-
data: newValue,
|
|
482
|
-
fieldAttributes: fieldAttr,
|
|
483
|
-
field: fieldName,
|
|
484
|
-
model: getModelName(model),
|
|
485
|
-
schema,
|
|
486
|
-
options,
|
|
487
|
-
action
|
|
488
|
-
});
|
|
489
|
-
return {
|
|
490
|
-
operator,
|
|
491
|
-
connector,
|
|
492
|
-
field: fieldName,
|
|
493
|
-
value: newValue
|
|
494
|
-
};
|
|
495
|
-
});
|
|
496
|
-
};
|
|
497
|
-
const transformJoinClause = (baseModel, unsanitizedJoin, select) => {
|
|
498
|
-
if (!unsanitizedJoin) return void 0;
|
|
499
|
-
if (Object.keys(unsanitizedJoin).length === 0) return void 0;
|
|
500
|
-
const transformedJoin = {};
|
|
501
|
-
for (const [model, join] of Object.entries(unsanitizedJoin)) {
|
|
502
|
-
if (!join) continue;
|
|
503
|
-
const defaultModelName = getDefaultModelName(model);
|
|
504
|
-
const defaultBaseModelName = getDefaultModelName(baseModel);
|
|
505
|
-
let foreignKeys = Object.entries(schema[defaultModelName].fields).filter(([field, fieldAttributes]) => fieldAttributes.references && getDefaultModelName(fieldAttributes.references.model) === defaultBaseModelName);
|
|
506
|
-
let isForwardJoin = true;
|
|
507
|
-
if (!foreignKeys.length) {
|
|
508
|
-
foreignKeys = Object.entries(schema[defaultBaseModelName].fields).filter(([field, fieldAttributes]) => fieldAttributes.references && getDefaultModelName(fieldAttributes.references.model) === defaultModelName);
|
|
509
|
-
isForwardJoin = false;
|
|
510
|
-
}
|
|
511
|
-
if (!foreignKeys.length) throw new BetterAuthError(`No foreign key found for model ${model} and base model ${baseModel} while performing join operation.`);
|
|
512
|
-
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.`);
|
|
513
|
-
const [foreignKey, foreignKeyAttributes] = foreignKeys[0];
|
|
514
|
-
if (!foreignKeyAttributes.references) throw new BetterAuthError(`No references found for foreign key ${foreignKey} on model ${model} while performing join operation.`);
|
|
515
|
-
let from;
|
|
516
|
-
let to;
|
|
517
|
-
let requiredSelectField;
|
|
518
|
-
if (isForwardJoin) {
|
|
519
|
-
requiredSelectField = foreignKeyAttributes.references.field;
|
|
520
|
-
from = getFieldName({
|
|
521
|
-
model: baseModel,
|
|
522
|
-
field: requiredSelectField
|
|
523
|
-
});
|
|
524
|
-
to = getFieldName({
|
|
525
|
-
model,
|
|
526
|
-
field: foreignKey
|
|
527
|
-
});
|
|
528
|
-
} else {
|
|
529
|
-
requiredSelectField = foreignKey;
|
|
530
|
-
from = getFieldName({
|
|
531
|
-
model: baseModel,
|
|
532
|
-
field: requiredSelectField
|
|
533
|
-
});
|
|
534
|
-
to = getFieldName({
|
|
535
|
-
model,
|
|
536
|
-
field: foreignKeyAttributes.references.field
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
if (select && !select.includes(requiredSelectField)) select.push(requiredSelectField);
|
|
540
|
-
const isUnique = to === "id" ? true : foreignKeyAttributes.unique ?? false;
|
|
541
|
-
let limit = options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
542
|
-
if (isUnique) limit = 1;
|
|
543
|
-
else if (typeof join === "object" && typeof join.limit === "number") limit = join.limit;
|
|
544
|
-
transformedJoin[getModelName(model)] = {
|
|
545
|
-
on: {
|
|
546
|
-
from,
|
|
547
|
-
to
|
|
548
|
-
},
|
|
549
|
-
limit,
|
|
550
|
-
relation: isUnique ? "one-to-one" : "one-to-many"
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
return {
|
|
554
|
-
join: transformedJoin,
|
|
555
|
-
select
|
|
556
|
-
};
|
|
557
|
-
};
|
|
558
|
-
/**
|
|
559
|
-
* Handle joins by making separate queries and combining results (fallback for adapters that don't support native joins).
|
|
560
|
-
*/
|
|
561
|
-
const handleFallbackJoin = async ({ baseModel, baseData, joinModel, specificJoinConfig: joinConfig }) => {
|
|
562
|
-
if (!baseData) return baseData;
|
|
563
|
-
const modelName = getModelName(joinModel);
|
|
564
|
-
const field = joinConfig.on.to;
|
|
565
|
-
const value = baseData[getDefaultFieldName({
|
|
566
|
-
field: joinConfig.on.from,
|
|
567
|
-
model: baseModel
|
|
568
|
-
})];
|
|
569
|
-
if (value === null || value === void 0) return joinConfig.relation === "one-to-one" ? null : [];
|
|
570
|
-
let result;
|
|
571
|
-
const where = transformWhereClause({
|
|
572
|
-
model: modelName,
|
|
573
|
-
where: [{
|
|
574
|
-
field,
|
|
575
|
-
value,
|
|
576
|
-
operator: "eq",
|
|
577
|
-
connector: "AND"
|
|
578
|
-
}],
|
|
579
|
-
action: "findOne"
|
|
580
|
-
});
|
|
581
|
-
try {
|
|
582
|
-
if (joinConfig.relation === "one-to-one") result = await adapterInstance.findOne({
|
|
583
|
-
model: modelName,
|
|
584
|
-
where
|
|
585
|
-
});
|
|
586
|
-
else {
|
|
587
|
-
const limit = joinConfig.limit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
588
|
-
result = await adapterInstance.findMany({
|
|
589
|
-
model: modelName,
|
|
590
|
-
where,
|
|
591
|
-
limit
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
|
-
} catch (error) {
|
|
595
|
-
logger$1.error(`Failed to query fallback join for model ${modelName}:`, {
|
|
596
|
-
where,
|
|
597
|
-
limit: joinConfig.limit
|
|
598
|
-
});
|
|
599
|
-
console.error(error);
|
|
600
|
-
throw error;
|
|
601
|
-
}
|
|
602
|
-
return result;
|
|
603
|
-
};
|
|
604
|
-
const adapterInstance = customAdapter({
|
|
605
|
-
options,
|
|
606
|
-
schema,
|
|
607
|
-
debugLog,
|
|
608
|
-
getFieldName,
|
|
609
|
-
getModelName,
|
|
610
|
-
getDefaultModelName,
|
|
611
|
-
getDefaultFieldName,
|
|
612
|
-
getFieldAttributes,
|
|
613
|
-
transformInput,
|
|
614
|
-
transformOutput,
|
|
615
|
-
transformWhereClause
|
|
616
|
-
});
|
|
617
|
-
let lazyLoadTransaction = null;
|
|
618
|
-
const adapter = {
|
|
619
|
-
transaction: async (cb) => {
|
|
620
|
-
if (!lazyLoadTransaction) if (!config.transaction) lazyLoadTransaction = createAsIsTransaction(adapter);
|
|
621
|
-
else {
|
|
622
|
-
logger$1.debug(`[${config.adapterName}] - Using provided transaction implementation.`);
|
|
623
|
-
lazyLoadTransaction = config.transaction;
|
|
624
|
-
}
|
|
625
|
-
return lazyLoadTransaction(cb);
|
|
626
|
-
},
|
|
627
|
-
create: async ({ data: unsafeData, model: unsafeModel, select, forceAllowId = false }) => {
|
|
628
|
-
transactionId++;
|
|
629
|
-
let thisTransactionId = transactionId;
|
|
630
|
-
const model = getModelName(unsafeModel);
|
|
631
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
632
|
-
if ("id" in unsafeData && typeof unsafeData.id !== "undefined" && !forceAllowId) {
|
|
633
|
-
logger$1.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.`);
|
|
634
|
-
const stack = (/* @__PURE__ */ new Error()).stack?.split("\n").filter((_, i) => i !== 1).join("\n").replace("Error:", "Create method with `id` being called at:");
|
|
635
|
-
console.log(stack);
|
|
636
|
-
unsafeData.id = void 0;
|
|
637
|
-
}
|
|
638
|
-
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("create")} ${formatAction("Unsafe Input")}:`, {
|
|
639
|
-
model,
|
|
640
|
-
data: unsafeData
|
|
641
|
-
});
|
|
642
|
-
let data = unsafeData;
|
|
643
|
-
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "create", forceAllowId);
|
|
644
|
-
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("create")} ${formatAction("Parsed Input")}:`, {
|
|
645
|
-
model,
|
|
646
|
-
data
|
|
647
|
-
});
|
|
648
|
-
const res = await adapterInstance.create({
|
|
649
|
-
data,
|
|
650
|
-
model
|
|
651
|
-
});
|
|
652
|
-
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("create")} ${formatAction("DB Result")}:`, {
|
|
653
|
-
model,
|
|
654
|
-
res
|
|
655
|
-
});
|
|
656
|
-
let transformed = res;
|
|
657
|
-
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select, void 0);
|
|
658
|
-
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("create")} ${formatAction("Parsed Result")}:`, {
|
|
659
|
-
model,
|
|
660
|
-
data: transformed
|
|
661
|
-
});
|
|
662
|
-
return transformed;
|
|
663
|
-
},
|
|
664
|
-
update: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
665
|
-
transactionId++;
|
|
666
|
-
let thisTransactionId = transactionId;
|
|
667
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
668
|
-
const model = getModelName(unsafeModel);
|
|
669
|
-
const where = transformWhereClause({
|
|
670
|
-
model: unsafeModel,
|
|
671
|
-
where: unsafeWhere,
|
|
672
|
-
action: "update"
|
|
673
|
-
});
|
|
674
|
-
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("update")} ${formatAction("Unsafe Input")}:`, {
|
|
675
|
-
model,
|
|
676
|
-
data: unsafeData
|
|
677
|
-
});
|
|
678
|
-
let data = unsafeData;
|
|
679
|
-
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
680
|
-
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("update")} ${formatAction("Parsed Input")}:`, {
|
|
681
|
-
model,
|
|
682
|
-
data
|
|
683
|
-
});
|
|
684
|
-
const res = await adapterInstance.update({
|
|
685
|
-
model,
|
|
686
|
-
where,
|
|
687
|
-
update: data
|
|
688
|
-
});
|
|
689
|
-
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("update")} ${formatAction("DB Result")}:`, {
|
|
690
|
-
model,
|
|
691
|
-
data: res
|
|
692
|
-
});
|
|
693
|
-
let transformed = res;
|
|
694
|
-
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, void 0, void 0);
|
|
695
|
-
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("update")} ${formatAction("Parsed Result")}:`, {
|
|
696
|
-
model,
|
|
697
|
-
data: transformed
|
|
698
|
-
});
|
|
699
|
-
return transformed;
|
|
700
|
-
},
|
|
701
|
-
updateMany: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
702
|
-
transactionId++;
|
|
703
|
-
let thisTransactionId = transactionId;
|
|
704
|
-
const model = getModelName(unsafeModel);
|
|
705
|
-
const where = transformWhereClause({
|
|
706
|
-
model: unsafeModel,
|
|
707
|
-
where: unsafeWhere,
|
|
708
|
-
action: "updateMany"
|
|
709
|
-
});
|
|
710
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
711
|
-
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("updateMany")} ${formatAction("Unsafe Input")}:`, {
|
|
712
|
-
model,
|
|
713
|
-
data: unsafeData
|
|
714
|
-
});
|
|
715
|
-
let data = unsafeData;
|
|
716
|
-
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
717
|
-
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("updateMany")} ${formatAction("Parsed Input")}:`, {
|
|
718
|
-
model,
|
|
719
|
-
data
|
|
720
|
-
});
|
|
721
|
-
const updatedCount = await adapterInstance.updateMany({
|
|
722
|
-
model,
|
|
723
|
-
where,
|
|
724
|
-
update: data
|
|
725
|
-
});
|
|
726
|
-
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("updateMany")} ${formatAction("DB Result")}:`, {
|
|
727
|
-
model,
|
|
728
|
-
data: updatedCount
|
|
729
|
-
});
|
|
730
|
-
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("updateMany")} ${formatAction("Parsed Result")}:`, {
|
|
731
|
-
model,
|
|
732
|
-
data: updatedCount
|
|
733
|
-
});
|
|
734
|
-
return updatedCount;
|
|
735
|
-
},
|
|
736
|
-
findOne: async ({ model: unsafeModel, where: unsafeWhere, select, join: unsafeJoin }) => {
|
|
737
|
-
transactionId++;
|
|
738
|
-
let thisTransactionId = transactionId;
|
|
739
|
-
const model = getModelName(unsafeModel);
|
|
740
|
-
const where = transformWhereClause({
|
|
741
|
-
model: unsafeModel,
|
|
742
|
-
where: unsafeWhere,
|
|
743
|
-
action: "findOne"
|
|
744
|
-
});
|
|
745
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
746
|
-
let join;
|
|
747
|
-
let passJoinToAdapter = true;
|
|
748
|
-
if (!config.disableTransformJoin) {
|
|
749
|
-
const result = transformJoinClause(unsafeModel, unsafeJoin, select);
|
|
750
|
-
if (result) {
|
|
751
|
-
join = result.join;
|
|
752
|
-
select = result.select;
|
|
753
|
-
}
|
|
754
|
-
if (!options.experimental?.joins && join && Object.keys(join).length > 0) passJoinToAdapter = false;
|
|
755
|
-
} else join = unsafeJoin;
|
|
756
|
-
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("findOne")}:`, {
|
|
757
|
-
model,
|
|
758
|
-
where,
|
|
759
|
-
select,
|
|
760
|
-
join
|
|
761
|
-
});
|
|
762
|
-
const res = await adapterInstance.findOne({
|
|
763
|
-
model,
|
|
764
|
-
where,
|
|
765
|
-
select,
|
|
766
|
-
join: passJoinToAdapter ? join : void 0
|
|
767
|
-
});
|
|
768
|
-
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("findOne")} ${formatAction("DB Result")}:`, {
|
|
769
|
-
model,
|
|
770
|
-
data: res
|
|
771
|
-
});
|
|
772
|
-
let transformed = res;
|
|
773
|
-
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select, join);
|
|
774
|
-
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("findOne")} ${formatAction("Parsed Result")}:`, {
|
|
775
|
-
model,
|
|
776
|
-
data: transformed
|
|
777
|
-
});
|
|
778
|
-
return transformed;
|
|
779
|
-
},
|
|
780
|
-
findMany: async ({ model: unsafeModel, where: unsafeWhere, limit: unsafeLimit, sortBy, offset, join: unsafeJoin }) => {
|
|
781
|
-
transactionId++;
|
|
782
|
-
let thisTransactionId = transactionId;
|
|
783
|
-
const limit = unsafeLimit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
784
|
-
const model = getModelName(unsafeModel);
|
|
785
|
-
const where = transformWhereClause({
|
|
786
|
-
model: unsafeModel,
|
|
787
|
-
where: unsafeWhere,
|
|
788
|
-
action: "findMany"
|
|
789
|
-
});
|
|
790
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
791
|
-
let join;
|
|
792
|
-
let passJoinToAdapter = true;
|
|
793
|
-
if (!config.disableTransformJoin) {
|
|
794
|
-
const result = transformJoinClause(unsafeModel, unsafeJoin, void 0);
|
|
795
|
-
if (result) join = result.join;
|
|
796
|
-
if (!options.experimental?.joins && join && Object.keys(join).length > 0) passJoinToAdapter = false;
|
|
797
|
-
} else join = unsafeJoin;
|
|
798
|
-
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("findMany")}:`, {
|
|
799
|
-
model,
|
|
800
|
-
where,
|
|
801
|
-
limit,
|
|
802
|
-
sortBy,
|
|
803
|
-
offset,
|
|
804
|
-
join
|
|
805
|
-
});
|
|
806
|
-
const res = await adapterInstance.findMany({
|
|
807
|
-
model,
|
|
808
|
-
where,
|
|
809
|
-
limit,
|
|
810
|
-
sortBy,
|
|
811
|
-
offset,
|
|
812
|
-
join: passJoinToAdapter ? join : void 0
|
|
813
|
-
});
|
|
814
|
-
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("findMany")} ${formatAction("DB Result")}:`, {
|
|
815
|
-
model,
|
|
816
|
-
data: res
|
|
817
|
-
});
|
|
818
|
-
let transformed = res;
|
|
819
|
-
if (!config.disableTransformOutput) transformed = await Promise.all(res.map(async (r) => {
|
|
820
|
-
return await transformOutput(r, unsafeModel, void 0, join);
|
|
821
|
-
}));
|
|
822
|
-
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("findMany")} ${formatAction("Parsed Result")}:`, {
|
|
823
|
-
model,
|
|
824
|
-
data: transformed
|
|
825
|
-
});
|
|
826
|
-
return transformed;
|
|
827
|
-
},
|
|
828
|
-
delete: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
829
|
-
transactionId++;
|
|
830
|
-
let thisTransactionId = transactionId;
|
|
831
|
-
const model = getModelName(unsafeModel);
|
|
832
|
-
const where = transformWhereClause({
|
|
833
|
-
model: unsafeModel,
|
|
834
|
-
where: unsafeWhere,
|
|
835
|
-
action: "delete"
|
|
836
|
-
});
|
|
837
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
838
|
-
debugLog({ method: "delete" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("delete")}:`, {
|
|
839
|
-
model,
|
|
840
|
-
where
|
|
841
|
-
});
|
|
842
|
-
await adapterInstance.delete({
|
|
843
|
-
model,
|
|
844
|
-
where
|
|
845
|
-
});
|
|
846
|
-
debugLog({ method: "delete" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("delete")} ${formatAction("DB Result")}:`, { model });
|
|
847
|
-
},
|
|
848
|
-
deleteMany: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
849
|
-
transactionId++;
|
|
850
|
-
let thisTransactionId = transactionId;
|
|
851
|
-
const model = getModelName(unsafeModel);
|
|
852
|
-
const where = transformWhereClause({
|
|
853
|
-
model: unsafeModel,
|
|
854
|
-
where: unsafeWhere,
|
|
855
|
-
action: "deleteMany"
|
|
856
|
-
});
|
|
857
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
858
|
-
debugLog({ method: "deleteMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("deleteMany")} ${formatAction("DeleteMany")}:`, {
|
|
859
|
-
model,
|
|
860
|
-
where
|
|
861
|
-
});
|
|
862
|
-
const res = await adapterInstance.deleteMany({
|
|
863
|
-
model,
|
|
864
|
-
where
|
|
865
|
-
});
|
|
866
|
-
debugLog({ method: "deleteMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("deleteMany")} ${formatAction("DB Result")}:`, {
|
|
867
|
-
model,
|
|
868
|
-
data: res
|
|
869
|
-
});
|
|
870
|
-
return res;
|
|
871
|
-
},
|
|
872
|
-
count: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
873
|
-
transactionId++;
|
|
874
|
-
let thisTransactionId = transactionId;
|
|
875
|
-
const model = getModelName(unsafeModel);
|
|
876
|
-
const where = transformWhereClause({
|
|
877
|
-
model: unsafeModel,
|
|
878
|
-
where: unsafeWhere,
|
|
879
|
-
action: "count"
|
|
880
|
-
});
|
|
881
|
-
unsafeModel = getDefaultModelName(unsafeModel);
|
|
882
|
-
debugLog({ method: "count" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("count")}:`, {
|
|
883
|
-
model,
|
|
884
|
-
where
|
|
885
|
-
});
|
|
886
|
-
const res = await adapterInstance.count({
|
|
887
|
-
model,
|
|
888
|
-
where
|
|
889
|
-
});
|
|
890
|
-
debugLog({ method: "count" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("count")}:`, {
|
|
891
|
-
model,
|
|
892
|
-
data: res
|
|
893
|
-
});
|
|
894
|
-
return res;
|
|
895
|
-
},
|
|
896
|
-
createSchema: adapterInstance.createSchema ? async (_, file) => {
|
|
897
|
-
const tables = getAuthTables(options);
|
|
898
|
-
if (options.secondaryStorage && !options.session?.storeSessionInDatabase) delete tables.session;
|
|
899
|
-
if (options.rateLimit && options.rateLimit.storage === "database" && (typeof options.rateLimit.enabled === "undefined" || options.rateLimit.enabled === true)) tables.ratelimit = {
|
|
900
|
-
modelName: options.rateLimit.modelName ?? "ratelimit",
|
|
901
|
-
fields: {
|
|
902
|
-
key: {
|
|
903
|
-
type: "string",
|
|
904
|
-
unique: true,
|
|
905
|
-
required: true,
|
|
906
|
-
fieldName: options.rateLimit.fields?.key ?? "key"
|
|
907
|
-
},
|
|
908
|
-
count: {
|
|
909
|
-
type: "number",
|
|
910
|
-
required: true,
|
|
911
|
-
fieldName: options.rateLimit.fields?.count ?? "count"
|
|
912
|
-
},
|
|
913
|
-
lastRequest: {
|
|
914
|
-
type: "number",
|
|
915
|
-
required: true,
|
|
916
|
-
bigint: true,
|
|
917
|
-
defaultValue: () => Date.now(),
|
|
918
|
-
fieldName: options.rateLimit.fields?.lastRequest ?? "lastRequest"
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
};
|
|
922
|
-
return adapterInstance.createSchema({
|
|
923
|
-
file,
|
|
924
|
-
tables
|
|
925
|
-
});
|
|
926
|
-
} : void 0,
|
|
927
|
-
options: {
|
|
928
|
-
adapterConfig: config,
|
|
929
|
-
...adapterInstance.options ?? {}
|
|
930
|
-
},
|
|
931
|
-
id: config.adapterId,
|
|
932
|
-
...config.debugLogs?.isRunningAdapterTests ? { adapterTestDebugLogs: {
|
|
933
|
-
resetDebugLogs() {
|
|
934
|
-
debugLogs = debugLogs.filter((log) => log.instance !== uniqueAdapterFactoryInstanceId);
|
|
935
|
-
},
|
|
936
|
-
printDebugLogs() {
|
|
937
|
-
const separator = `─`.repeat(80);
|
|
938
|
-
const logs = debugLogs.filter((log$1) => log$1.instance === uniqueAdapterFactoryInstanceId);
|
|
939
|
-
if (logs.length === 0) return;
|
|
940
|
-
let log = logs.reverse().map((log$1) => {
|
|
941
|
-
log$1.args[0] = `\n${log$1.args[0]}`;
|
|
942
|
-
return [...log$1.args, "\n"];
|
|
943
|
-
}).reduce((prev, curr) => {
|
|
944
|
-
return [...curr, ...prev];
|
|
945
|
-
}, [`\n${separator}`]);
|
|
946
|
-
console.log(...log);
|
|
947
|
-
}
|
|
948
|
-
} } : {}
|
|
949
|
-
};
|
|
950
|
-
return adapter;
|
|
951
|
-
};
|
|
952
|
-
function formatTransactionId(transactionId$1) {
|
|
953
|
-
if (getColorDepth() < 8) return `#${transactionId$1}`;
|
|
954
|
-
return `${TTY_COLORS.fg.magenta}#${transactionId$1}${TTY_COLORS.reset}`;
|
|
955
|
-
}
|
|
956
|
-
function formatStep(step, total) {
|
|
957
|
-
return `${TTY_COLORS.bg.black}${TTY_COLORS.fg.yellow}[${step}/${total}]${TTY_COLORS.reset}`;
|
|
958
|
-
}
|
|
959
|
-
function formatMethod(method) {
|
|
960
|
-
return `${TTY_COLORS.bright}${method}${TTY_COLORS.reset}`;
|
|
961
|
-
}
|
|
962
|
-
function formatAction(action) {
|
|
963
|
-
return `${TTY_COLORS.dim}(${action})${TTY_COLORS.reset}`;
|
|
964
|
-
}
|
|
965
|
-
/**
|
|
966
|
-
* @deprecated Use `createAdapterFactory` instead. This export will be removed in a future version.
|
|
967
|
-
* @alias
|
|
968
|
-
*/
|
|
969
|
-
const createAdapter = createAdapterFactory;
|
|
970
|
-
|
|
971
|
-
//#endregion
|
|
972
10
|
export { createAdapter, createAdapterFactory, deepmerge, initGetDefaultFieldName, initGetDefaultModelName, initGetFieldAttributes, initGetFieldName, initGetIdField, initGetModelName, withApplyDefault };
|