faire-auth 1.0.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/LICENSE +661 -0
- package/README.md +34 -0
- package/dist/access-B5bBI4uT.mjs +39 -0
- package/dist/access-Crt-IJ2P.cjs +51 -0
- package/dist/adapter-factory-BXei1QRX.mjs +881 -0
- package/dist/adapter-factory-DT-_2aHJ.cjs +893 -0
- package/dist/adapters/drizzle-adapter/index.cjs +208 -0
- package/dist/adapters/drizzle-adapter/index.d.cts +48 -0
- package/dist/adapters/drizzle-adapter/index.d.mts +48 -0
- package/dist/adapters/drizzle-adapter/index.mjs +207 -0
- package/dist/adapters/index.cjs +23 -0
- package/dist/adapters/index.d.cts +157 -0
- package/dist/adapters/index.d.mts +157 -0
- package/dist/adapters/index.mjs +17 -0
- package/dist/adapters/kysely-adapter/index.cjs +11 -0
- package/dist/adapters/kysely-adapter/index.d.cts +52 -0
- package/dist/adapters/kysely-adapter/index.d.mts +52 -0
- package/dist/adapters/kysely-adapter/index.mjs +9 -0
- package/dist/adapters/memory-adapter/index.cjs +9 -0
- package/dist/adapters/memory-adapter/index.d.cts +15 -0
- package/dist/adapters/memory-adapter/index.d.mts +15 -0
- package/dist/adapters/memory-adapter/index.mjs +9 -0
- package/dist/adapters/mongodb-adapter/index.cjs +9 -0
- package/dist/adapters/mongodb-adapter/index.d.cts +4 -0
- package/dist/adapters/mongodb-adapter/index.d.mts +4 -0
- package/dist/adapters/mongodb-adapter/index.mjs +9 -0
- package/dist/adapters/prisma-adapter/index.cjs +156 -0
- package/dist/adapters/prisma-adapter/index.d.cts +35 -0
- package/dist/adapters/prisma-adapter/index.d.mts +35 -0
- package/dist/adapters/prisma-adapter/index.mjs +155 -0
- package/dist/adapters/test.cjs +797 -0
- package/dist/adapters/test.d.cts +82 -0
- package/dist/adapters/test.d.mts +82 -0
- package/dist/adapters/test.mjs +793 -0
- package/dist/api/index.cjs +20 -0
- package/dist/api/index.d.cts +3 -0
- package/dist/api/index.d.mts +3 -0
- package/dist/api/index.mjs +20 -0
- package/dist/api/middleware/index.cjs +16 -0
- package/dist/api/middleware/index.d.cts +16 -0
- package/dist/api/middleware/index.d.mts +16 -0
- package/dist/api/middleware/index.mjs +11 -0
- package/dist/api-Bm89BLgN.cjs +1899 -0
- package/dist/api-_B7qO28y.mjs +1892 -0
- package/dist/auth-Bo9PDt5j.cjs +739 -0
- package/dist/auth-Ds4AG4h4.mjs +709 -0
- package/dist/bearer-0SeEmaS4.mjs +54 -0
- package/dist/bearer-CHeREcGT.cjs +60 -0
- package/dist/bun-sqlite-dialect-CfkBo2v5.mjs +155 -0
- package/dist/bun-sqlite-dialect-wuaC2-gh.cjs +156 -0
- package/dist/chunk-Ceigfy4P.mjs +39 -0
- package/dist/chunk-oakRccI5.cjs +60 -0
- package/dist/client/index.cjs +22 -0
- package/dist/client/index.d.cts +8 -0
- package/dist/client/index.d.mts +8 -0
- package/dist/client/index.mjs +19 -0
- package/dist/client/lynx/index.cjs +79 -0
- package/dist/client/lynx/index.d.cts +156 -0
- package/dist/client/lynx/index.d.mts +156 -0
- package/dist/client/lynx/index.mjs +76 -0
- package/dist/client/plugins/index.cjs +11 -0
- package/dist/client/plugins/index.d.cts +17 -0
- package/dist/client/plugins/index.d.mts +17 -0
- package/dist/client/plugins/index.mjs +10 -0
- package/dist/client/react/index.cjs +79 -0
- package/dist/client/react/index.d.cts +156 -0
- package/dist/client/react/index.d.mts +156 -0
- package/dist/client/react/index.mjs +76 -0
- package/dist/client/solid/index.cjs +51 -0
- package/dist/client/solid/index.d.cts +90 -0
- package/dist/client/solid/index.d.mts +90 -0
- package/dist/client/solid/index.mjs +50 -0
- package/dist/client/svelte/index.cjs +28 -0
- package/dist/client/svelte/index.d.cts +110 -0
- package/dist/client/svelte/index.d.mts +110 -0
- package/dist/client/svelte/index.mjs +28 -0
- package/dist/client/vue/index.cjs +68 -0
- package/dist/client/vue/index.d.cts +123 -0
- package/dist/client/vue/index.d.mts +123 -0
- package/dist/client/vue/index.mjs +67 -0
- package/dist/cookies-B5xeA6i8.cjs +456 -0
- package/dist/cookies-Ch1JfQIz.mjs +367 -0
- package/dist/crypto/index.cjs +11 -0
- package/dist/crypto/index.d.cts +22 -0
- package/dist/crypto/index.d.mts +22 -0
- package/dist/crypto/index.mjs +5 -0
- package/dist/crypto-CcTxYH82.cjs +57 -0
- package/dist/crypto-CyQRFtkq.mjs +41 -0
- package/dist/db/index.cjs +40 -0
- package/dist/db/index.d.cts +4 -0
- package/dist/db/index.d.mts +4 -0
- package/dist/db/index.mjs +16 -0
- package/dist/db-BTKyYu-_.cjs +1180 -0
- package/dist/db-xiJtoDgz.mjs +1119 -0
- package/dist/defu-B0Svx_x4.cjs +34 -0
- package/dist/defu-SZkpHhbR.mjs +28 -0
- package/dist/email-verification-C-AsfJsb.cjs +247 -0
- package/dist/email-verification-CWhj2qfb.mjs +228 -0
- package/dist/hono-BEq1NYVX.cjs +526 -0
- package/dist/hono-Cdukwu5K.cjs +13 -0
- package/dist/hono-CoKYWnXA.mjs +7 -0
- package/dist/hono-Covz10jU.mjs +508 -0
- package/dist/hono-DOM31WQz.d.cts +18 -0
- package/dist/hono-DuE14mmj.d.mts +18 -0
- package/dist/index-B-WeOMzg.d.cts +13350 -0
- package/dist/index-B09P9p5_.d.cts +33 -0
- package/dist/index-B5_0QTLg.d.mts +3 -0
- package/dist/index-B7SKyR_a.d.cts +3 -0
- package/dist/index-BVHscLQl.d.cts +992 -0
- package/dist/index-C2-2QDyS.d.cts +35 -0
- package/dist/index-CTwrVluH.d.mts +41 -0
- package/dist/index-CWAqWfoZ.d.cts +27 -0
- package/dist/index-CYSShrFx.d.mts +27 -0
- package/dist/index-Qu-iJGNr.d.mts +992 -0
- package/dist/index-cs4Sh0En.d.cts +41 -0
- package/dist/index-jlessPI0.d.mts +13350 -0
- package/dist/index-ppgcFe12.d.mts +33 -0
- package/dist/index-to0RO38U.d.mts +35 -0
- package/dist/index.cjs +85 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +27 -0
- package/dist/integrations/next-js.cjs +50 -0
- package/dist/integrations/next-js.d.cts +23 -0
- package/dist/integrations/next-js.d.mts +23 -0
- package/dist/integrations/next-js.mjs +49 -0
- package/dist/integrations/node.cjs +139 -0
- package/dist/integrations/node.d.cts +22 -0
- package/dist/integrations/node.d.mts +22 -0
- package/dist/integrations/node.mjs +134 -0
- package/dist/integrations/react-start.cjs +37 -0
- package/dist/integrations/react-start.d.cts +17 -0
- package/dist/integrations/react-start.d.mts +17 -0
- package/dist/integrations/react-start.mjs +37 -0
- package/dist/integrations/solid-start.cjs +14 -0
- package/dist/integrations/solid-start.d.cts +13 -0
- package/dist/integrations/solid-start.d.mts +13 -0
- package/dist/integrations/solid-start.mjs +13 -0
- package/dist/integrations/svelte-kit.cjs +52 -0
- package/dist/integrations/svelte-kit.d.cts +42 -0
- package/dist/integrations/svelte-kit.d.mts +42 -0
- package/dist/integrations/svelte-kit.mjs +49 -0
- package/dist/ip-B5SwXaMB.cjs +54 -0
- package/dist/ip-BjNYspCH.mjs +36 -0
- package/dist/json-B1v2qRTQ.mjs +24 -0
- package/dist/json-CcJ2HjGo.cjs +30 -0
- package/dist/kysely-adapter-Bxi7sPdG.cjs +288 -0
- package/dist/kysely-adapter-DRuoT_X8.mjs +270 -0
- package/dist/logged-CyGfp8_H.mjs +187 -0
- package/dist/logged-DQK3C64b.cjs +193 -0
- package/dist/memory-adapter-BlnDIAAX.cjs +154 -0
- package/dist/memory-adapter-Db3tsJTn.mjs +148 -0
- package/dist/middleware-CE6OhDkq.mjs +29 -0
- package/dist/middleware-CWvRH4EW.cjs +47 -0
- package/dist/mongodb-adapter-C_qGQED5.mjs +291 -0
- package/dist/mongodb-adapter-C_qfK87x.cjs +297 -0
- package/dist/node-sqlite-dialect-BfBzyOAg.mjs +155 -0
- package/dist/node-sqlite-dialect-mJWC8LWQ.cjs +156 -0
- package/dist/oauth2/index.cjs +25 -0
- package/dist/oauth2/index.d.cts +42 -0
- package/dist/oauth2/index.d.mts +42 -0
- package/dist/oauth2/index.mjs +16 -0
- package/dist/oauth2-B8M0lW9l.mjs +159 -0
- package/dist/oauth2-BLeOEi7Y.cjs +177 -0
- package/dist/origin-check-8Gajklvh.mjs +228 -0
- package/dist/origin-check-id9lTnqZ.cjs +246 -0
- package/dist/parser-CdncAl9i.cjs +83 -0
- package/dist/parser-CeAer-RM.mjs +71 -0
- package/dist/passkey-B6N20LSS.cjs +512 -0
- package/dist/passkey-DJsR_GX3.mjs +505 -0
- package/dist/plugins/access/index.cjs +4 -0
- package/dist/plugins/access/index.d.cts +2 -0
- package/dist/plugins/access/index.d.mts +2 -0
- package/dist/plugins/access/index.mjs +3 -0
- package/dist/plugins/bearer/index.cjs +7 -0
- package/dist/plugins/bearer/index.d.cts +4 -0
- package/dist/plugins/bearer/index.d.mts +4 -0
- package/dist/plugins/bearer/index.mjs +7 -0
- package/dist/plugins/index.cjs +49 -0
- package/dist/plugins/index.d.cts +9326 -0
- package/dist/plugins/index.d.mts +9326 -0
- package/dist/plugins/index.mjs +26 -0
- package/dist/plugins/passkey/index.cjs +15 -0
- package/dist/plugins/passkey/index.d.cts +4 -0
- package/dist/plugins/passkey/index.d.mts +4 -0
- package/dist/plugins/passkey/index.mjs +15 -0
- package/dist/plugins-C1lm2Wj3.cjs +5254 -0
- package/dist/plugins-Dai1QkRU.mjs +5199 -0
- package/dist/request-1NL_1lyJ.mjs +58 -0
- package/dist/request-CPNlp-DK.cjs +100 -0
- package/dist/schema-D-qjygfE.cjs +179 -0
- package/dist/schema-DTIwLLmf.mjs +125 -0
- package/dist/session-De9POJTW.cjs +641 -0
- package/dist/session-rH8ougDl.mjs +538 -0
- package/dist/social-providers/index.cjs +10 -0
- package/dist/social-providers/index.d.cts +2 -0
- package/dist/social-providers/index.d.mts +2 -0
- package/dist/social-providers/index.mjs +5 -0
- package/dist/social-providers-DcFNUT0S.cjs +13 -0
- package/dist/social-providers-DoYJhBe3.mjs +9 -0
- package/dist/state--f7fwIGh.cjs +99 -0
- package/dist/state-CqJfCIl9.mjs +86 -0
- package/dist/test-utils/index.cjs +27492 -0
- package/dist/test-utils/index.d.cts +4584 -0
- package/dist/test-utils/index.d.mts +4584 -0
- package/dist/test-utils/index.mjs +27490 -0
- package/dist/types/index.cjs +0 -0
- package/dist/types/index.d.cts +3 -0
- package/dist/types/index.d.mts +3 -0
- package/dist/types/index.mjs +1 -0
- package/dist/url-BoUuoIwZ.mjs +88 -0
- package/dist/url-D2yvKaM7.cjs +118 -0
- package/dist/utils/cookies.cjs +51 -0
- package/dist/utils/cookies.d.cts +179 -0
- package/dist/utils/cookies.d.mts +179 -0
- package/dist/utils/cookies.mjs +7 -0
- package/dist/utils-44aDDP8f.cjs +2 -0
- package/dist/utils-D4vZd1-9.mjs +4 -0
- package/dist/vanilla-Bu2PmFzE.cjs +30 -0
- package/dist/vanilla-TfHqeiC7.mjs +25 -0
- package/package.json +470 -0
|
@@ -0,0 +1,893 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-oakRccI5.cjs');
|
|
2
|
+
const require_json = require('./json-CcJ2HjGo.cjs');
|
|
3
|
+
let __faire_auth_core_env = require("@faire-auth/core/env");
|
|
4
|
+
let __faire_auth_core_error = require("@faire-auth/core/error");
|
|
5
|
+
let __faire_auth_core_static = require("@faire-auth/core/static");
|
|
6
|
+
let __faire_auth_core_crypto = require("@faire-auth/core/crypto");
|
|
7
|
+
|
|
8
|
+
//#region src/adapters/utils.ts
|
|
9
|
+
function withApplyDefault(value, field, action) {
|
|
10
|
+
if (action === "update") {
|
|
11
|
+
if (value === void 0 && field.onUpdate !== void 0) {
|
|
12
|
+
if (typeof field.onUpdate === "function") return field.onUpdate();
|
|
13
|
+
return field.onUpdate;
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
if (value === void 0 || value === null) {
|
|
18
|
+
if (field.defaultValue !== void 0) {
|
|
19
|
+
if (typeof field.defaultValue === "function") return field.defaultValue();
|
|
20
|
+
return field.defaultValue;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/db/get-tables.ts
|
|
28
|
+
const getAuthTables = (options) => {
|
|
29
|
+
const pluginSchema = (options.plugins ?? []).reduce((acc, plugin) => {
|
|
30
|
+
const schema = plugin.schema;
|
|
31
|
+
if (!schema) return acc;
|
|
32
|
+
for (const [key, value] of Object.entries(schema)) acc[key] = {
|
|
33
|
+
fields: {
|
|
34
|
+
...acc[key]?.fields,
|
|
35
|
+
...value.fields
|
|
36
|
+
},
|
|
37
|
+
modelName: value.modelName || key
|
|
38
|
+
};
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
const shouldAddRateLimitTable = options.rateLimit?.storage === "database";
|
|
42
|
+
const rateLimitTable = { rateLimit: {
|
|
43
|
+
modelName: options.rateLimit?.modelName || "rateLimit",
|
|
44
|
+
fields: {
|
|
45
|
+
key: {
|
|
46
|
+
type: "string",
|
|
47
|
+
fieldName: options.rateLimit?.fields?.key || "key"
|
|
48
|
+
},
|
|
49
|
+
count: {
|
|
50
|
+
type: "number",
|
|
51
|
+
fieldName: options.rateLimit?.fields?.count || "count"
|
|
52
|
+
},
|
|
53
|
+
lastRequest: {
|
|
54
|
+
type: "number",
|
|
55
|
+
bigint: true,
|
|
56
|
+
fieldName: options.rateLimit?.fields?.lastRequest || "lastRequest"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} };
|
|
60
|
+
const { user, session, account,...pluginTables } = pluginSchema;
|
|
61
|
+
const sessionTable = { session: {
|
|
62
|
+
modelName: options.session?.modelName || "session",
|
|
63
|
+
fields: {
|
|
64
|
+
expiresAt: {
|
|
65
|
+
type: "date",
|
|
66
|
+
required: true,
|
|
67
|
+
fieldName: options.session?.fields?.expiresAt || "expiresAt"
|
|
68
|
+
},
|
|
69
|
+
token: {
|
|
70
|
+
type: "string",
|
|
71
|
+
required: true,
|
|
72
|
+
fieldName: options.session?.fields?.token || "token",
|
|
73
|
+
unique: true
|
|
74
|
+
},
|
|
75
|
+
createdAt: {
|
|
76
|
+
type: "date",
|
|
77
|
+
required: true,
|
|
78
|
+
fieldName: options.session?.fields?.createdAt || "createdAt",
|
|
79
|
+
defaultValue: () => /* @__PURE__ */ new Date()
|
|
80
|
+
},
|
|
81
|
+
updatedAt: {
|
|
82
|
+
type: "date",
|
|
83
|
+
required: true,
|
|
84
|
+
fieldName: options.session?.fields?.updatedAt || "updatedAt",
|
|
85
|
+
onUpdate: () => /* @__PURE__ */ new Date()
|
|
86
|
+
},
|
|
87
|
+
ipAddress: {
|
|
88
|
+
type: "string",
|
|
89
|
+
required: false,
|
|
90
|
+
fieldName: options.session?.fields?.ipAddress || "ipAddress"
|
|
91
|
+
},
|
|
92
|
+
userAgent: {
|
|
93
|
+
type: "string",
|
|
94
|
+
required: false,
|
|
95
|
+
fieldName: options.session?.fields?.userAgent || "userAgent"
|
|
96
|
+
},
|
|
97
|
+
userId: {
|
|
98
|
+
type: "string",
|
|
99
|
+
fieldName: options.session?.fields?.userId || "userId",
|
|
100
|
+
references: {
|
|
101
|
+
model: options.user?.modelName || "user",
|
|
102
|
+
field: "id",
|
|
103
|
+
onDelete: "cascade"
|
|
104
|
+
},
|
|
105
|
+
required: true
|
|
106
|
+
},
|
|
107
|
+
...session?.fields,
|
|
108
|
+
...options.session?.additionalFields
|
|
109
|
+
},
|
|
110
|
+
order: 2
|
|
111
|
+
} };
|
|
112
|
+
return {
|
|
113
|
+
user: {
|
|
114
|
+
modelName: options.user?.modelName || "user",
|
|
115
|
+
fields: {
|
|
116
|
+
name: {
|
|
117
|
+
type: "string",
|
|
118
|
+
required: true,
|
|
119
|
+
fieldName: options.user?.fields?.name || "name",
|
|
120
|
+
sortable: true
|
|
121
|
+
},
|
|
122
|
+
email: {
|
|
123
|
+
type: "string",
|
|
124
|
+
unique: true,
|
|
125
|
+
required: true,
|
|
126
|
+
fieldName: options.user?.fields?.email || "email",
|
|
127
|
+
sortable: true
|
|
128
|
+
},
|
|
129
|
+
emailVerified: {
|
|
130
|
+
type: "boolean",
|
|
131
|
+
defaultValue: false,
|
|
132
|
+
required: true,
|
|
133
|
+
fieldName: options.user?.fields?.emailVerified || "emailVerified"
|
|
134
|
+
},
|
|
135
|
+
image: {
|
|
136
|
+
type: "string",
|
|
137
|
+
required: false,
|
|
138
|
+
fieldName: options.user?.fields?.image || "image"
|
|
139
|
+
},
|
|
140
|
+
createdAt: {
|
|
141
|
+
type: "date",
|
|
142
|
+
defaultValue: () => /* @__PURE__ */ new Date(),
|
|
143
|
+
required: true,
|
|
144
|
+
fieldName: options.user?.fields?.createdAt || "createdAt"
|
|
145
|
+
},
|
|
146
|
+
updatedAt: {
|
|
147
|
+
type: "date",
|
|
148
|
+
defaultValue: () => /* @__PURE__ */ new Date(),
|
|
149
|
+
onUpdate: () => /* @__PURE__ */ new Date(),
|
|
150
|
+
required: true,
|
|
151
|
+
fieldName: options.user?.fields?.updatedAt || "updatedAt"
|
|
152
|
+
},
|
|
153
|
+
...user?.fields,
|
|
154
|
+
...options.user?.additionalFields
|
|
155
|
+
},
|
|
156
|
+
order: 1
|
|
157
|
+
},
|
|
158
|
+
...!options.secondaryStorage || options.session?.storeSessionInDatabase ? sessionTable : {},
|
|
159
|
+
account: {
|
|
160
|
+
modelName: options.account?.modelName || "account",
|
|
161
|
+
fields: {
|
|
162
|
+
accountId: {
|
|
163
|
+
type: "string",
|
|
164
|
+
required: true,
|
|
165
|
+
fieldName: options.account?.fields?.accountId || "accountId"
|
|
166
|
+
},
|
|
167
|
+
providerId: {
|
|
168
|
+
type: "string",
|
|
169
|
+
required: true,
|
|
170
|
+
fieldName: options.account?.fields?.providerId || "providerId"
|
|
171
|
+
},
|
|
172
|
+
userId: {
|
|
173
|
+
type: "string",
|
|
174
|
+
references: {
|
|
175
|
+
model: options.user?.modelName || "user",
|
|
176
|
+
field: "id",
|
|
177
|
+
onDelete: "cascade"
|
|
178
|
+
},
|
|
179
|
+
required: true,
|
|
180
|
+
fieldName: options.account?.fields?.userId || "userId"
|
|
181
|
+
},
|
|
182
|
+
accessToken: {
|
|
183
|
+
type: "string",
|
|
184
|
+
required: false,
|
|
185
|
+
fieldName: options.account?.fields?.accessToken || "accessToken"
|
|
186
|
+
},
|
|
187
|
+
refreshToken: {
|
|
188
|
+
type: "string",
|
|
189
|
+
required: false,
|
|
190
|
+
fieldName: options.account?.fields?.refreshToken || "refreshToken"
|
|
191
|
+
},
|
|
192
|
+
idToken: {
|
|
193
|
+
type: "string",
|
|
194
|
+
required: false,
|
|
195
|
+
fieldName: options.account?.fields?.idToken || "idToken"
|
|
196
|
+
},
|
|
197
|
+
accessTokenExpiresAt: {
|
|
198
|
+
type: "date",
|
|
199
|
+
required: false,
|
|
200
|
+
fieldName: options.account?.fields?.accessTokenExpiresAt || "accessTokenExpiresAt"
|
|
201
|
+
},
|
|
202
|
+
refreshTokenExpiresAt: {
|
|
203
|
+
type: "date",
|
|
204
|
+
required: false,
|
|
205
|
+
fieldName: options.account?.fields?.refreshTokenExpiresAt || "refreshTokenExpiresAt"
|
|
206
|
+
},
|
|
207
|
+
scope: {
|
|
208
|
+
type: "string",
|
|
209
|
+
required: false,
|
|
210
|
+
fieldName: options.account?.fields?.scope || "scope"
|
|
211
|
+
},
|
|
212
|
+
password: {
|
|
213
|
+
type: "string",
|
|
214
|
+
required: false,
|
|
215
|
+
fieldName: options.account?.fields?.password || "password"
|
|
216
|
+
},
|
|
217
|
+
createdAt: {
|
|
218
|
+
type: "date",
|
|
219
|
+
required: true,
|
|
220
|
+
fieldName: options.account?.fields?.createdAt || "createdAt",
|
|
221
|
+
defaultValue: () => /* @__PURE__ */ new Date()
|
|
222
|
+
},
|
|
223
|
+
updatedAt: {
|
|
224
|
+
type: "date",
|
|
225
|
+
required: true,
|
|
226
|
+
fieldName: options.account?.fields?.updatedAt || "updatedAt",
|
|
227
|
+
onUpdate: () => /* @__PURE__ */ new Date()
|
|
228
|
+
},
|
|
229
|
+
...account?.fields,
|
|
230
|
+
...options.account?.additionalFields
|
|
231
|
+
},
|
|
232
|
+
order: 3
|
|
233
|
+
},
|
|
234
|
+
verification: {
|
|
235
|
+
modelName: options.verification?.modelName || "verification",
|
|
236
|
+
fields: {
|
|
237
|
+
identifier: {
|
|
238
|
+
type: "string",
|
|
239
|
+
required: true,
|
|
240
|
+
fieldName: options.verification?.fields?.identifier || "identifier"
|
|
241
|
+
},
|
|
242
|
+
value: {
|
|
243
|
+
type: "string",
|
|
244
|
+
required: true,
|
|
245
|
+
fieldName: options.verification?.fields?.value || "value"
|
|
246
|
+
},
|
|
247
|
+
expiresAt: {
|
|
248
|
+
type: "date",
|
|
249
|
+
required: true,
|
|
250
|
+
fieldName: options.verification?.fields?.expiresAt || "expiresAt"
|
|
251
|
+
},
|
|
252
|
+
createdAt: {
|
|
253
|
+
type: "date",
|
|
254
|
+
required: true,
|
|
255
|
+
defaultValue: () => /* @__PURE__ */ new Date(),
|
|
256
|
+
fieldName: options.verification?.fields?.createdAt || "createdAt"
|
|
257
|
+
},
|
|
258
|
+
updatedAt: {
|
|
259
|
+
type: "date",
|
|
260
|
+
required: true,
|
|
261
|
+
defaultValue: () => /* @__PURE__ */ new Date(),
|
|
262
|
+
onUpdate: () => /* @__PURE__ */ new Date(),
|
|
263
|
+
fieldName: options.verification?.fields?.updatedAt || "updatedAt"
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
order: 4
|
|
267
|
+
},
|
|
268
|
+
...pluginTables,
|
|
269
|
+
...shouldAddRateLimitTable ? rateLimitTable : {}
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
//#endregion
|
|
274
|
+
//#region src/adapters/adapter-factory/index.ts
|
|
275
|
+
let debugLogs = [];
|
|
276
|
+
let transactionId = -1;
|
|
277
|
+
const createAsIsTransaction = (adapter) => (fn) => {
|
|
278
|
+
const res = fn(adapter);
|
|
279
|
+
return (0, __faire_auth_core_static.isPromise)(res) ? res : Promise.resolve(res);
|
|
280
|
+
};
|
|
281
|
+
const createAdapterFactory = ({ adapter: customAdapter, config: cfg }) => (options) => {
|
|
282
|
+
const uniqueAdapterFactoryInstanceId = Math.random().toString(36).substring(2, 15);
|
|
283
|
+
const config = {
|
|
284
|
+
...cfg,
|
|
285
|
+
supportsBooleans: cfg.supportsBooleans ?? true,
|
|
286
|
+
supportsDates: cfg.supportsDates ?? true,
|
|
287
|
+
supportsJSON: cfg.supportsJSON ?? false,
|
|
288
|
+
adapterName: cfg.adapterName ?? cfg.adapterId,
|
|
289
|
+
supportsNumericIds: cfg.supportsNumericIds ?? true,
|
|
290
|
+
transaction: cfg.transaction ?? false,
|
|
291
|
+
disableTransformInput: cfg.disableTransformInput ?? false,
|
|
292
|
+
disableTransformOutput: cfg.disableTransformOutput ?? false
|
|
293
|
+
};
|
|
294
|
+
if (options.advanced?.database?.useNumberId === true && config.supportsNumericIds === false) throw new __faire_auth_core_error.FaireAuthError(`[${config.adapterName}] Your database or database adapter does not support numeric ids. Please disable "useNumberId" in your config.`);
|
|
295
|
+
const schema = getAuthTables(options);
|
|
296
|
+
/**
|
|
297
|
+
* This function helps us get the default field name from the schema defined by devs.
|
|
298
|
+
* Often times, the user will be using the `fieldName` which could had been customized by the users.
|
|
299
|
+
* This function helps us get the actual field name useful to match against the schema. (eg: schema[model].fields[field])
|
|
300
|
+
*
|
|
301
|
+
* If it's still unclear what this does:
|
|
302
|
+
*
|
|
303
|
+
* 1. User can define a custom fieldName.
|
|
304
|
+
* 2. When using a custom fieldName, doing something like `schema[model].fields[field]` will not work.
|
|
305
|
+
*/
|
|
306
|
+
const getDefaultFieldName = ({ field, model: unsafe_model }) => {
|
|
307
|
+
if (field === "id" || field === "_id") return "id";
|
|
308
|
+
const model = getDefaultModelName(unsafe_model);
|
|
309
|
+
let f = schema[model]?.fields[field];
|
|
310
|
+
if (!f) {
|
|
311
|
+
const result = Object.entries(schema[model].fields).find(([_, f$1]) => f$1.fieldName === field);
|
|
312
|
+
if (result) {
|
|
313
|
+
f = result[1];
|
|
314
|
+
field = result[0];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (!f) {
|
|
318
|
+
debugLog(`Field ${field} not found in model ${model}`);
|
|
319
|
+
debugLog(`Schema:`, schema);
|
|
320
|
+
throw new __faire_auth_core_error.FaireAuthError(`Field ${field} not found in model ${model}`);
|
|
321
|
+
}
|
|
322
|
+
return field;
|
|
323
|
+
};
|
|
324
|
+
/**
|
|
325
|
+
* This function helps us get the default model name from the schema defined by devs.
|
|
326
|
+
* Often times, the user will be using the `modelName` which could had been customized by the users.
|
|
327
|
+
* This function helps us get the actual model name useful to match against the schema. (eg: schema[model])
|
|
328
|
+
*
|
|
329
|
+
* If it's still unclear what this does:
|
|
330
|
+
*
|
|
331
|
+
* 1. User can define a custom modelName.
|
|
332
|
+
* 2. When using a custom modelName, doing something like `schema[model]` will not work.
|
|
333
|
+
* 3. Using this function helps us get the actual model name based on the user's defined custom modelName.
|
|
334
|
+
*/
|
|
335
|
+
const getDefaultModelName = (model) => {
|
|
336
|
+
if (config.usePlural && model.charAt(model.length - 1) === "s") {
|
|
337
|
+
let pluralessModel = model.slice(0, -1);
|
|
338
|
+
let m$1 = schema[pluralessModel] ? pluralessModel : void 0;
|
|
339
|
+
if (!m$1) m$1 = Object.entries(schema).find(([_, f]) => f.modelName === pluralessModel)?.[0];
|
|
340
|
+
if (m$1) return m$1;
|
|
341
|
+
}
|
|
342
|
+
let m = schema[model] ? model : void 0;
|
|
343
|
+
if (!m) m = Object.entries(schema).find(([_, f]) => f.modelName === model)?.[0];
|
|
344
|
+
if (!m) {
|
|
345
|
+
debugLog(`Model "${model}" not found in schema`);
|
|
346
|
+
debugLog(`Schema:`, schema);
|
|
347
|
+
throw new __faire_auth_core_error.FaireAuthError(`Model "${model}" not found in schema`);
|
|
348
|
+
}
|
|
349
|
+
return m;
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* Users can overwrite the default model of some tables. This function helps find the correct model name.
|
|
353
|
+
* Furthermore, if the user passes `usePlural` as true in their adapter config,
|
|
354
|
+
* then we should return the model name ending with an `s`.
|
|
355
|
+
*/
|
|
356
|
+
const getModelName = (model) => {
|
|
357
|
+
const defaultModelKey = getDefaultModelName(model);
|
|
358
|
+
const usePlural = config && config.usePlural;
|
|
359
|
+
if (schema && schema[defaultModelKey] && schema[defaultModelKey].modelName !== model) return usePlural ? `${schema[defaultModelKey].modelName}s` : schema[defaultModelKey].modelName;
|
|
360
|
+
return usePlural ? `${model}s` : model;
|
|
361
|
+
};
|
|
362
|
+
/**
|
|
363
|
+
* Get the field name which is expected to be saved in the database based on the user's schema.
|
|
364
|
+
*
|
|
365
|
+
* This function is useful if you need to save the field name to the database.
|
|
366
|
+
*
|
|
367
|
+
* 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.
|
|
368
|
+
*/
|
|
369
|
+
function getFieldName({ model: model_name, field: field_name }) {
|
|
370
|
+
const model = getDefaultModelName(model_name);
|
|
371
|
+
const field = getDefaultFieldName({
|
|
372
|
+
model,
|
|
373
|
+
field: field_name
|
|
374
|
+
});
|
|
375
|
+
return schema[model]?.fields[field]?.fieldName || field;
|
|
376
|
+
}
|
|
377
|
+
const debugLog = (...args) => {
|
|
378
|
+
if (config.debugLogs === true || typeof config.debugLogs === "object") {
|
|
379
|
+
if (typeof config.debugLogs === "object" && "isRunningAdapterTests" in config.debugLogs) {
|
|
380
|
+
if (config.debugLogs.isRunningAdapterTests) {
|
|
381
|
+
args.shift();
|
|
382
|
+
debugLogs.push({
|
|
383
|
+
instance: uniqueAdapterFactoryInstanceId,
|
|
384
|
+
args
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (typeof config.debugLogs === "object" && config.debugLogs.logCondition && !config.debugLogs.logCondition?.()) return;
|
|
390
|
+
if (typeof args[0] === "object" && "method" in args[0]) {
|
|
391
|
+
const method = args.shift().method;
|
|
392
|
+
if (typeof config.debugLogs === "object") {
|
|
393
|
+
if (method === "create" && !config.debugLogs.create) return;
|
|
394
|
+
else if (method === "update" && !config.debugLogs.update) return;
|
|
395
|
+
else if (method === "updateMany" && !config.debugLogs.updateMany) return;
|
|
396
|
+
else if (method === "findOne" && !config.debugLogs.findOne) return;
|
|
397
|
+
else if (method === "findMany" && !config.debugLogs.findMany) return;
|
|
398
|
+
else if (method === "delete" && !config.debugLogs.delete) return;
|
|
399
|
+
else if (method === "deleteMany" && !config.debugLogs.deleteMany) return;
|
|
400
|
+
else if (method === "count" && !config.debugLogs.count) return;
|
|
401
|
+
}
|
|
402
|
+
__faire_auth_core_env.logger.info(`[${config.adapterName}]`, ...args);
|
|
403
|
+
} else __faire_auth_core_env.logger.info(`[${config.adapterName}]`, ...args);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
const idField = ({ customModelName, forceAllowId }) => {
|
|
407
|
+
const shouldGenerateId = !config.disableIdGeneration && !options.advanced?.database?.useNumberId && !forceAllowId;
|
|
408
|
+
const model = getDefaultModelName(customModelName ?? "id");
|
|
409
|
+
return {
|
|
410
|
+
type: options.advanced?.database?.useNumberId ? "number" : "string",
|
|
411
|
+
required: shouldGenerateId ? true : false,
|
|
412
|
+
...shouldGenerateId ? { defaultValue() {
|
|
413
|
+
if (config.disableIdGeneration) return void 0;
|
|
414
|
+
const useNumberId = options.advanced?.database?.useNumberId;
|
|
415
|
+
let generateId$1 = options.advanced?.database?.generateId;
|
|
416
|
+
if (options.advanced?.generateId !== void 0) {
|
|
417
|
+
__faire_auth_core_env.logger.warn("Your Faire Auth config includes advanced.generateId which is deprecated. Please use advanced.database.generateId instead. This will be removed in future releases.");
|
|
418
|
+
generateId$1 = options.advanced?.generateId;
|
|
419
|
+
}
|
|
420
|
+
if (generateId$1 === false || useNumberId) return void 0;
|
|
421
|
+
if (generateId$1) return generateId$1({ model });
|
|
422
|
+
if (config.customIdGenerator) return config.customIdGenerator({ model });
|
|
423
|
+
return (0, __faire_auth_core_crypto.generateId)();
|
|
424
|
+
} } : {}
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
const getFieldAttributes = ({ model, field }) => {
|
|
428
|
+
const defaultModelName = getDefaultModelName(model);
|
|
429
|
+
const defaultFieldName = getDefaultFieldName({
|
|
430
|
+
field,
|
|
431
|
+
model: defaultModelName
|
|
432
|
+
});
|
|
433
|
+
const fields = schema[defaultModelName].fields;
|
|
434
|
+
fields.id = idField({ customModelName: defaultModelName });
|
|
435
|
+
const fieldAttributes = fields[defaultFieldName];
|
|
436
|
+
if (!fieldAttributes) throw new __faire_auth_core_error.FaireAuthError(`Field ${field} not found in model ${model}`);
|
|
437
|
+
return fieldAttributes;
|
|
438
|
+
};
|
|
439
|
+
const transformInput = async (data, defaultModelName, action, forceAllowId) => {
|
|
440
|
+
const transformedData = {};
|
|
441
|
+
const fields = schema[defaultModelName].fields;
|
|
442
|
+
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
443
|
+
if (!config.disableIdGeneration && !options.advanced?.database?.useNumberId) fields.id = idField({
|
|
444
|
+
customModelName: defaultModelName,
|
|
445
|
+
forceAllowId: !!forceAllowId && "id" in data
|
|
446
|
+
});
|
|
447
|
+
for (const field in fields) {
|
|
448
|
+
const value = data[field];
|
|
449
|
+
const fieldAttributes = fields[field];
|
|
450
|
+
let newFieldName = newMappedKeys[field] || fields[field].fieldName || field;
|
|
451
|
+
if (value === void 0 && (fieldAttributes.defaultValue === void 0 && !fieldAttributes.transform?.input && !(action === "update" && fieldAttributes.onUpdate) || action === "update" && !fieldAttributes.onUpdate)) continue;
|
|
452
|
+
let newValue = withApplyDefault(value, fieldAttributes, action);
|
|
453
|
+
if (fieldAttributes.transform?.input) newValue = await fieldAttributes.transform.input(newValue);
|
|
454
|
+
if (fieldAttributes.references?.field === "id" && options.advanced?.database?.useNumberId) if (Array.isArray(newValue)) newValue = newValue.map((x) => x !== null ? Number(x) : null);
|
|
455
|
+
else newValue = newValue !== null ? Number(newValue) : null;
|
|
456
|
+
else if (config.supportsJSON === false && typeof newValue === "object" && fieldAttributes.type === "json") newValue = JSON.stringify(newValue);
|
|
457
|
+
else if (config.supportsDates === false && newValue instanceof Date && fieldAttributes.type === "date") newValue = newValue.toISOString();
|
|
458
|
+
else if (config.supportsBooleans === false && typeof newValue === "boolean") newValue = newValue ? 1 : 0;
|
|
459
|
+
if (config.customTransformInput) newValue = config.customTransformInput({
|
|
460
|
+
data: newValue,
|
|
461
|
+
action,
|
|
462
|
+
field: newFieldName,
|
|
463
|
+
fieldAttributes,
|
|
464
|
+
model: defaultModelName,
|
|
465
|
+
schema,
|
|
466
|
+
options
|
|
467
|
+
});
|
|
468
|
+
if (newValue !== void 0) transformedData[newFieldName] = newValue;
|
|
469
|
+
}
|
|
470
|
+
return transformedData;
|
|
471
|
+
};
|
|
472
|
+
const transformOutput = async (data, unsafe_model, select = []) => {
|
|
473
|
+
if (!data) return null;
|
|
474
|
+
const newMappedKeys = config.mapKeysTransformOutput ?? {};
|
|
475
|
+
const transformedData = {};
|
|
476
|
+
const tableSchema = schema[unsafe_model].fields;
|
|
477
|
+
const idKey = Object.entries(newMappedKeys).find(([_, v]) => v === "id")?.[0];
|
|
478
|
+
tableSchema[idKey ?? "id"] = { type: options.advanced?.database?.useNumberId ? "number" : "string" };
|
|
479
|
+
for (const key in tableSchema) {
|
|
480
|
+
if (select.length && !select.includes(key)) continue;
|
|
481
|
+
const field = tableSchema[key];
|
|
482
|
+
if (field) {
|
|
483
|
+
const originalKey = field.fieldName || key;
|
|
484
|
+
let newValue = data[Object.entries(newMappedKeys).find(([_, v]) => v === originalKey)?.[0] || originalKey];
|
|
485
|
+
if (field.transform?.output) newValue = await field.transform.output(newValue);
|
|
486
|
+
let newFieldName = newMappedKeys[key] || key;
|
|
487
|
+
if (originalKey === "id" || field.references?.field === "id") {
|
|
488
|
+
if (typeof newValue !== "undefined" && newValue !== null) newValue = String(newValue);
|
|
489
|
+
} else if (config.supportsJSON === false && typeof newValue === "string" && field.type === "json") newValue = require_json.safeJSONParse(newValue);
|
|
490
|
+
else if (config.supportsDates === false && typeof newValue === "string" && field.type === "date") newValue = new Date(newValue);
|
|
491
|
+
else if (config.supportsBooleans === false && typeof newValue === "number" && field.type === "boolean") newValue = newValue === 1;
|
|
492
|
+
if (config.customTransformOutput) newValue = config.customTransformOutput({
|
|
493
|
+
data: newValue,
|
|
494
|
+
field: newFieldName,
|
|
495
|
+
fieldAttributes: field,
|
|
496
|
+
select,
|
|
497
|
+
model: unsafe_model,
|
|
498
|
+
schema,
|
|
499
|
+
options
|
|
500
|
+
});
|
|
501
|
+
transformedData[newFieldName] = newValue;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return transformedData;
|
|
505
|
+
};
|
|
506
|
+
const transformWhereClause = ({ model, where }) => {
|
|
507
|
+
if (!where) return void 0;
|
|
508
|
+
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
509
|
+
return where.map((w) => {
|
|
510
|
+
const { field: unsafe_field, value, operator = "eq", connector = "AND" } = w;
|
|
511
|
+
if (operator === "in") {
|
|
512
|
+
if (!Array.isArray(value)) throw new __faire_auth_core_error.FaireAuthError("Value must be an array");
|
|
513
|
+
}
|
|
514
|
+
let newValue = value;
|
|
515
|
+
const defaultModelName = getDefaultModelName(model);
|
|
516
|
+
const defaultFieldName = getDefaultFieldName({
|
|
517
|
+
field: unsafe_field,
|
|
518
|
+
model
|
|
519
|
+
});
|
|
520
|
+
const fieldName = newMappedKeys[defaultFieldName] || getFieldName({
|
|
521
|
+
field: defaultFieldName,
|
|
522
|
+
model: defaultModelName
|
|
523
|
+
});
|
|
524
|
+
const fieldAttr = getFieldAttributes({
|
|
525
|
+
field: defaultFieldName,
|
|
526
|
+
model: defaultModelName
|
|
527
|
+
});
|
|
528
|
+
if (defaultFieldName === "id" || fieldAttr.references?.field === "id") {
|
|
529
|
+
if (options.advanced?.database?.useNumberId) if (Array.isArray(value)) newValue = value.map(Number);
|
|
530
|
+
else newValue = Number(value);
|
|
531
|
+
}
|
|
532
|
+
if (fieldAttr.type === "date" && value instanceof Date && !config.supportsDates) newValue = value.toISOString();
|
|
533
|
+
if (fieldAttr.type === "boolean" && typeof value === "boolean" && !config.supportsBooleans) newValue = value ? 1 : 0;
|
|
534
|
+
if (fieldAttr.type === "json" && typeof value === "object" && !config.supportsJSON) try {
|
|
535
|
+
newValue = JSON.stringify(value);
|
|
536
|
+
} catch (error) {
|
|
537
|
+
throw new Error(`Failed to stringify JSON value for field ${fieldName}`, { cause: error });
|
|
538
|
+
}
|
|
539
|
+
return {
|
|
540
|
+
operator,
|
|
541
|
+
connector,
|
|
542
|
+
field: fieldName,
|
|
543
|
+
value: newValue
|
|
544
|
+
};
|
|
545
|
+
});
|
|
546
|
+
};
|
|
547
|
+
const adapterInstance = customAdapter({
|
|
548
|
+
options,
|
|
549
|
+
schema,
|
|
550
|
+
debugLog,
|
|
551
|
+
getFieldName,
|
|
552
|
+
getModelName,
|
|
553
|
+
getDefaultModelName,
|
|
554
|
+
getDefaultFieldName,
|
|
555
|
+
getFieldAttributes,
|
|
556
|
+
transformInput,
|
|
557
|
+
transformOutput,
|
|
558
|
+
transformWhereClause
|
|
559
|
+
});
|
|
560
|
+
let lazyLoadTransaction = null;
|
|
561
|
+
const adapter = {
|
|
562
|
+
transaction: (cb) => {
|
|
563
|
+
if (!lazyLoadTransaction) if (!config.transaction) {
|
|
564
|
+
if (typeof config.debugLogs === "object" && "isRunningAdapterTests" in config.debugLogs && config.debugLogs.isRunningAdapterTests) {} else __faire_auth_core_env.logger.warn(`[${config.adapterName}] - Transactions are not supported. Executing operations sequentially.`);
|
|
565
|
+
lazyLoadTransaction = createAsIsTransaction(adapter);
|
|
566
|
+
} else {
|
|
567
|
+
__faire_auth_core_env.logger.debug(`[${config.adapterName}] - Using provided transaction implementation.`);
|
|
568
|
+
lazyLoadTransaction = config.transaction;
|
|
569
|
+
}
|
|
570
|
+
return lazyLoadTransaction(cb);
|
|
571
|
+
},
|
|
572
|
+
create: async ({ data: unsafeData, model: unsafeModel, select, forceAllowId = false }) => {
|
|
573
|
+
transactionId++;
|
|
574
|
+
let thisTransactionId = transactionId;
|
|
575
|
+
const model = getModelName(unsafeModel);
|
|
576
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
577
|
+
if ("id" in unsafeData && !forceAllowId) {
|
|
578
|
+
__faire_auth_core_env.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.`);
|
|
579
|
+
const stack = (/* @__PURE__ */ new Error()).stack?.split("\n").filter((_, i) => i !== 1).join("\n").replace("Error:", "Create method with `id` being called at:");
|
|
580
|
+
console.log(stack);
|
|
581
|
+
unsafeData.id = void 0;
|
|
582
|
+
}
|
|
583
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("create")} ${formatAction("Unsafe Input")}:`, {
|
|
584
|
+
model,
|
|
585
|
+
data: unsafeData
|
|
586
|
+
});
|
|
587
|
+
let data = unsafeData;
|
|
588
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "create", forceAllowId);
|
|
589
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("create")} ${formatAction("Parsed Input")}:`, {
|
|
590
|
+
model,
|
|
591
|
+
data
|
|
592
|
+
});
|
|
593
|
+
const res = await adapterInstance.create({
|
|
594
|
+
data,
|
|
595
|
+
model
|
|
596
|
+
});
|
|
597
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("create")} ${formatAction("DB Result")}:`, {
|
|
598
|
+
model,
|
|
599
|
+
res
|
|
600
|
+
});
|
|
601
|
+
let transformed = res;
|
|
602
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select);
|
|
603
|
+
debugLog({ method: "create" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("create")} ${formatAction("Parsed Result")}:`, {
|
|
604
|
+
model,
|
|
605
|
+
data: transformed
|
|
606
|
+
});
|
|
607
|
+
return transformed;
|
|
608
|
+
},
|
|
609
|
+
update: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
610
|
+
transactionId++;
|
|
611
|
+
let thisTransactionId = transactionId;
|
|
612
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
613
|
+
const model = getModelName(unsafeModel);
|
|
614
|
+
const where = transformWhereClause({
|
|
615
|
+
model: unsafeModel,
|
|
616
|
+
where: unsafeWhere
|
|
617
|
+
});
|
|
618
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("update")} ${formatAction("Unsafe Input")}:`, {
|
|
619
|
+
model,
|
|
620
|
+
data: unsafeData
|
|
621
|
+
});
|
|
622
|
+
let data = unsafeData;
|
|
623
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
624
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("update")} ${formatAction("Parsed Input")}:`, {
|
|
625
|
+
model,
|
|
626
|
+
data
|
|
627
|
+
});
|
|
628
|
+
const res = await adapterInstance.update({
|
|
629
|
+
model,
|
|
630
|
+
where,
|
|
631
|
+
update: data
|
|
632
|
+
});
|
|
633
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("update")} ${formatAction("DB Result")}:`, {
|
|
634
|
+
model,
|
|
635
|
+
data: res
|
|
636
|
+
});
|
|
637
|
+
let transformed = res;
|
|
638
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel);
|
|
639
|
+
debugLog({ method: "update" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("update")} ${formatAction("Parsed Result")}:`, {
|
|
640
|
+
model,
|
|
641
|
+
data: transformed
|
|
642
|
+
});
|
|
643
|
+
return transformed;
|
|
644
|
+
},
|
|
645
|
+
updateMany: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
646
|
+
transactionId++;
|
|
647
|
+
let thisTransactionId = transactionId;
|
|
648
|
+
const model = getModelName(unsafeModel);
|
|
649
|
+
const where = transformWhereClause({
|
|
650
|
+
model: unsafeModel,
|
|
651
|
+
where: unsafeWhere
|
|
652
|
+
});
|
|
653
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
654
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 4)}`, `${formatMethod("updateMany")} ${formatAction("Unsafe Input")}:`, {
|
|
655
|
+
model,
|
|
656
|
+
data: unsafeData
|
|
657
|
+
});
|
|
658
|
+
let data = unsafeData;
|
|
659
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
660
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 4)}`, `${formatMethod("updateMany")} ${formatAction("Parsed Input")}:`, {
|
|
661
|
+
model,
|
|
662
|
+
data
|
|
663
|
+
});
|
|
664
|
+
const updatedCount = await adapterInstance.updateMany({
|
|
665
|
+
model,
|
|
666
|
+
where,
|
|
667
|
+
update: data
|
|
668
|
+
});
|
|
669
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 4)}`, `${formatMethod("updateMany")} ${formatAction("DB Result")}:`, {
|
|
670
|
+
model,
|
|
671
|
+
data: updatedCount
|
|
672
|
+
});
|
|
673
|
+
debugLog({ method: "updateMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(4, 4)}`, `${formatMethod("updateMany")} ${formatAction("Parsed Result")}:`, {
|
|
674
|
+
model,
|
|
675
|
+
data: updatedCount
|
|
676
|
+
});
|
|
677
|
+
return updatedCount;
|
|
678
|
+
},
|
|
679
|
+
findOne: async ({ model: unsafeModel, where: unsafeWhere, select }) => {
|
|
680
|
+
transactionId++;
|
|
681
|
+
let thisTransactionId = transactionId;
|
|
682
|
+
const model = getModelName(unsafeModel);
|
|
683
|
+
const where = transformWhereClause({
|
|
684
|
+
model: unsafeModel,
|
|
685
|
+
where: unsafeWhere
|
|
686
|
+
});
|
|
687
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
688
|
+
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("findOne")}:`, {
|
|
689
|
+
model,
|
|
690
|
+
where,
|
|
691
|
+
select
|
|
692
|
+
});
|
|
693
|
+
const res = await adapterInstance.findOne({
|
|
694
|
+
model,
|
|
695
|
+
where,
|
|
696
|
+
...select && { select }
|
|
697
|
+
});
|
|
698
|
+
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("findOne")} ${formatAction("DB Result")}:`, {
|
|
699
|
+
model,
|
|
700
|
+
data: res
|
|
701
|
+
});
|
|
702
|
+
let transformed = res;
|
|
703
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select);
|
|
704
|
+
debugLog({ method: "findOne" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("findOne")} ${formatAction("Parsed Result")}:`, {
|
|
705
|
+
model,
|
|
706
|
+
data: transformed
|
|
707
|
+
});
|
|
708
|
+
return transformed;
|
|
709
|
+
},
|
|
710
|
+
findMany: async ({ model: unsafeModel, where: unsafeWhere, limit: unsafeLimit, sortBy, offset }) => {
|
|
711
|
+
transactionId++;
|
|
712
|
+
let thisTransactionId = transactionId;
|
|
713
|
+
const limit = unsafeLimit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
714
|
+
const model = getModelName(unsafeModel);
|
|
715
|
+
const where = transformWhereClause({
|
|
716
|
+
model: unsafeModel,
|
|
717
|
+
where: unsafeWhere
|
|
718
|
+
});
|
|
719
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
720
|
+
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 3)}`, `${formatMethod("findMany")}:`, {
|
|
721
|
+
model,
|
|
722
|
+
where,
|
|
723
|
+
limit,
|
|
724
|
+
sortBy,
|
|
725
|
+
offset
|
|
726
|
+
});
|
|
727
|
+
const res = await adapterInstance.findMany({
|
|
728
|
+
model,
|
|
729
|
+
...where && { where },
|
|
730
|
+
limit,
|
|
731
|
+
...sortBy && { sortBy },
|
|
732
|
+
...offset && { offset }
|
|
733
|
+
});
|
|
734
|
+
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 3)}`, `${formatMethod("findMany")} ${formatAction("DB Result")}:`, {
|
|
735
|
+
model,
|
|
736
|
+
data: res
|
|
737
|
+
});
|
|
738
|
+
let transformed = res;
|
|
739
|
+
if (!config.disableTransformOutput) transformed = await Promise.all(res.map(async (r) => await transformOutput(r, unsafeModel)));
|
|
740
|
+
debugLog({ method: "findMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(3, 3)}`, `${formatMethod("findMany")} ${formatAction("Parsed Result")}:`, {
|
|
741
|
+
model,
|
|
742
|
+
data: transformed
|
|
743
|
+
});
|
|
744
|
+
return transformed;
|
|
745
|
+
},
|
|
746
|
+
delete: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
747
|
+
transactionId++;
|
|
748
|
+
let thisTransactionId = transactionId;
|
|
749
|
+
const model = getModelName(unsafeModel);
|
|
750
|
+
const where = transformWhereClause({
|
|
751
|
+
model: unsafeModel,
|
|
752
|
+
where: unsafeWhere
|
|
753
|
+
});
|
|
754
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
755
|
+
debugLog({ method: "delete" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("delete")}:`, {
|
|
756
|
+
model,
|
|
757
|
+
where
|
|
758
|
+
});
|
|
759
|
+
await adapterInstance.delete({
|
|
760
|
+
model,
|
|
761
|
+
where
|
|
762
|
+
});
|
|
763
|
+
debugLog({ method: "delete" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("delete")} ${formatAction("DB Result")}:`, { model });
|
|
764
|
+
},
|
|
765
|
+
deleteMany: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
766
|
+
transactionId++;
|
|
767
|
+
let thisTransactionId = transactionId;
|
|
768
|
+
const model = getModelName(unsafeModel);
|
|
769
|
+
const where = transformWhereClause({
|
|
770
|
+
model: unsafeModel,
|
|
771
|
+
where: unsafeWhere
|
|
772
|
+
});
|
|
773
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
774
|
+
debugLog({ method: "deleteMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("deleteMany")} ${formatAction("DeleteMany")}:`, {
|
|
775
|
+
model,
|
|
776
|
+
where
|
|
777
|
+
});
|
|
778
|
+
const res = await adapterInstance.deleteMany({
|
|
779
|
+
model,
|
|
780
|
+
where
|
|
781
|
+
});
|
|
782
|
+
debugLog({ method: "deleteMany" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("deleteMany")} ${formatAction("DB Result")}:`, {
|
|
783
|
+
model,
|
|
784
|
+
data: res
|
|
785
|
+
});
|
|
786
|
+
return res;
|
|
787
|
+
},
|
|
788
|
+
count: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
789
|
+
transactionId++;
|
|
790
|
+
let thisTransactionId = transactionId;
|
|
791
|
+
const model = getModelName(unsafeModel);
|
|
792
|
+
const where = transformWhereClause({
|
|
793
|
+
model: unsafeModel,
|
|
794
|
+
where: unsafeWhere
|
|
795
|
+
});
|
|
796
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
797
|
+
debugLog({ method: "count" }, `${formatTransactionId(thisTransactionId)} ${formatStep(1, 2)}`, `${formatMethod("count")}:`, {
|
|
798
|
+
model,
|
|
799
|
+
where
|
|
800
|
+
});
|
|
801
|
+
const res = await adapterInstance.count({
|
|
802
|
+
model,
|
|
803
|
+
...where && { where }
|
|
804
|
+
});
|
|
805
|
+
debugLog({ method: "count" }, `${formatTransactionId(thisTransactionId)} ${formatStep(2, 2)}`, `${formatMethod("count")}:`, {
|
|
806
|
+
model,
|
|
807
|
+
data: res
|
|
808
|
+
});
|
|
809
|
+
return res;
|
|
810
|
+
},
|
|
811
|
+
createSchema: adapterInstance.createSchema ? async (_, file) => {
|
|
812
|
+
const tables = getAuthTables(options);
|
|
813
|
+
if (options.secondaryStorage && !options.session?.storeSessionInDatabase) delete tables.session;
|
|
814
|
+
if (options.rateLimit && options.rateLimit.storage === "database" && (typeof options.rateLimit.enabled === "undefined" || options.rateLimit.enabled === true)) tables.ratelimit = {
|
|
815
|
+
modelName: options.rateLimit.modelName ?? "ratelimit",
|
|
816
|
+
fields: {
|
|
817
|
+
key: {
|
|
818
|
+
type: "string",
|
|
819
|
+
unique: true,
|
|
820
|
+
required: true,
|
|
821
|
+
fieldName: options.rateLimit.fields?.key ?? "key"
|
|
822
|
+
},
|
|
823
|
+
count: {
|
|
824
|
+
type: "number",
|
|
825
|
+
required: true,
|
|
826
|
+
fieldName: options.rateLimit.fields?.count ?? "count"
|
|
827
|
+
},
|
|
828
|
+
lastRequest: {
|
|
829
|
+
type: "number",
|
|
830
|
+
required: true,
|
|
831
|
+
bigint: true,
|
|
832
|
+
defaultValue: () => Date.now(),
|
|
833
|
+
fieldName: options.rateLimit.fields?.lastRequest ?? "lastRequest"
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
return adapterInstance.createSchema({
|
|
838
|
+
...file && { file },
|
|
839
|
+
tables
|
|
840
|
+
});
|
|
841
|
+
} : void 0,
|
|
842
|
+
options: {
|
|
843
|
+
adapterConfig: config,
|
|
844
|
+
...adapterInstance.options ?? {}
|
|
845
|
+
},
|
|
846
|
+
id: config.adapterId,
|
|
847
|
+
...config.debugLogs?.isRunningAdapterTests ? { adapterTestDebugLogs: {
|
|
848
|
+
resetDebugLogs() {
|
|
849
|
+
debugLogs = debugLogs.filter((log) => log.instance !== uniqueAdapterFactoryInstanceId);
|
|
850
|
+
},
|
|
851
|
+
printDebugLogs() {
|
|
852
|
+
const separator = `─`.repeat(80);
|
|
853
|
+
const logs = debugLogs.filter((log$1) => log$1.instance === uniqueAdapterFactoryInstanceId);
|
|
854
|
+
if (logs.length === 0) return;
|
|
855
|
+
let log = logs.reverse().map((log$1) => {
|
|
856
|
+
log$1.args[0] = `\n${log$1.args[0]}`;
|
|
857
|
+
return [...log$1.args, "\n"];
|
|
858
|
+
}).reduce((prev, curr) => {
|
|
859
|
+
return [...curr, ...prev];
|
|
860
|
+
}, [`\n${separator}`]);
|
|
861
|
+
console.log(...log);
|
|
862
|
+
}
|
|
863
|
+
} } : {}
|
|
864
|
+
};
|
|
865
|
+
return adapter;
|
|
866
|
+
};
|
|
867
|
+
function formatTransactionId(transactionId$1) {
|
|
868
|
+
if ((0, __faire_auth_core_env.getColorDepth)() < 8) return `#${transactionId$1}`;
|
|
869
|
+
return `${__faire_auth_core_env.TTY_COLORS.fg.magenta}#${transactionId$1}${__faire_auth_core_env.TTY_COLORS.reset}`;
|
|
870
|
+
}
|
|
871
|
+
function formatStep(step, total) {
|
|
872
|
+
return `${__faire_auth_core_env.TTY_COLORS.bg.black}${__faire_auth_core_env.TTY_COLORS.fg.yellow}[${step}/${total}]${__faire_auth_core_env.TTY_COLORS.reset}`;
|
|
873
|
+
}
|
|
874
|
+
function formatMethod(method) {
|
|
875
|
+
return `${__faire_auth_core_env.TTY_COLORS.bright}${method}${__faire_auth_core_env.TTY_COLORS.reset}`;
|
|
876
|
+
}
|
|
877
|
+
function formatAction(action) {
|
|
878
|
+
return `${__faire_auth_core_env.TTY_COLORS.dim}(${action})${__faire_auth_core_env.TTY_COLORS.reset}`;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
//#endregion
|
|
882
|
+
Object.defineProperty(exports, 'createAdapterFactory', {
|
|
883
|
+
enumerable: true,
|
|
884
|
+
get: function () {
|
|
885
|
+
return createAdapterFactory;
|
|
886
|
+
}
|
|
887
|
+
});
|
|
888
|
+
Object.defineProperty(exports, 'getAuthTables', {
|
|
889
|
+
enumerable: true,
|
|
890
|
+
get: function () {
|
|
891
|
+
return getAuthTables;
|
|
892
|
+
}
|
|
893
|
+
});
|