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,47 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/api/factory/middleware.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a hook handler.
|
|
5
|
+
* @template E - The environment type.
|
|
6
|
+
* @template P - The path type.
|
|
7
|
+
* @template I - The input type.
|
|
8
|
+
* @param handler - The hook handler function.
|
|
9
|
+
* @returns The hook handler.
|
|
10
|
+
*/
|
|
11
|
+
const createHook = () => (handler) => handler;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a middleware handler.
|
|
14
|
+
* @template E - The environment type.
|
|
15
|
+
* @template P - The path type.
|
|
16
|
+
* @template I - The input type.
|
|
17
|
+
* @param handler - The middleware handler function.
|
|
18
|
+
* @returns The middleware handler.
|
|
19
|
+
*/
|
|
20
|
+
const createMiddleware = () => (handler) => handler;
|
|
21
|
+
/**
|
|
22
|
+
* Creates an error handler.
|
|
23
|
+
* @template E - The environment type.
|
|
24
|
+
* @param handler - The error handler function.
|
|
25
|
+
* @returns The error handler.
|
|
26
|
+
*/
|
|
27
|
+
const createErrorHandler = () => (handler) => handler;
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
Object.defineProperty(exports, 'createErrorHandler', {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return createErrorHandler;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, 'createHook', {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return createHook;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, 'createMiddleware', {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () {
|
|
45
|
+
return createMiddleware;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { t as createAdapterFactory } from "./adapter-factory-BXei1QRX.mjs";
|
|
2
|
+
import { ObjectId } from "mongodb";
|
|
3
|
+
|
|
4
|
+
//#region src/adapters/mongodb-adapter/mongodb-adapter.ts
|
|
5
|
+
const mongodbAdapter = (db, config) => {
|
|
6
|
+
let lazyOptions;
|
|
7
|
+
const createCustomAdapter = (db$1, session) => ({ options, getFieldName, schema, getDefaultModelName }) => {
|
|
8
|
+
function serializeID({ field, value, model }) {
|
|
9
|
+
model = getDefaultModelName(model);
|
|
10
|
+
if (field === "id" || field === "_id" || schema[model].fields[field]?.references?.field === "id") {
|
|
11
|
+
if (typeof value !== "string") {
|
|
12
|
+
if (value instanceof ObjectId) return value;
|
|
13
|
+
if (Array.isArray(value)) return value.map((v) => {
|
|
14
|
+
if (typeof v === "string") try {
|
|
15
|
+
return new ObjectId(v);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
return v;
|
|
18
|
+
}
|
|
19
|
+
if (v instanceof ObjectId) return v;
|
|
20
|
+
throw new Error("Invalid id value");
|
|
21
|
+
});
|
|
22
|
+
throw new Error("Invalid id value");
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
return new ObjectId(value);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
function convertWhereClause({ where, model }) {
|
|
33
|
+
if (!where.length) return {};
|
|
34
|
+
const conditions = where.map((w) => {
|
|
35
|
+
const { field: field_, value, operator = "eq", connector = "AND" } = w;
|
|
36
|
+
let condition;
|
|
37
|
+
let field = getFieldName({
|
|
38
|
+
model,
|
|
39
|
+
field: field_
|
|
40
|
+
});
|
|
41
|
+
if (field === "id") field = "_id";
|
|
42
|
+
switch (operator.toLowerCase()) {
|
|
43
|
+
case "eq":
|
|
44
|
+
condition = { [field]: serializeID({
|
|
45
|
+
field,
|
|
46
|
+
value,
|
|
47
|
+
model
|
|
48
|
+
}) };
|
|
49
|
+
break;
|
|
50
|
+
case "in":
|
|
51
|
+
condition = { [field]: { $in: Array.isArray(value) ? value.map((v) => serializeID({
|
|
52
|
+
field,
|
|
53
|
+
value: v,
|
|
54
|
+
model
|
|
55
|
+
})) : [serializeID({
|
|
56
|
+
field,
|
|
57
|
+
value,
|
|
58
|
+
model
|
|
59
|
+
})] } };
|
|
60
|
+
break;
|
|
61
|
+
case "not_in":
|
|
62
|
+
condition = { [field]: { $nin: Array.isArray(value) ? value.map((v) => serializeID({
|
|
63
|
+
field,
|
|
64
|
+
value: v,
|
|
65
|
+
model
|
|
66
|
+
})) : [serializeID({
|
|
67
|
+
field,
|
|
68
|
+
value,
|
|
69
|
+
model
|
|
70
|
+
})] } };
|
|
71
|
+
break;
|
|
72
|
+
case "gt":
|
|
73
|
+
condition = { [field]: { $gt: value } };
|
|
74
|
+
break;
|
|
75
|
+
case "gte":
|
|
76
|
+
condition = { [field]: { $gte: value } };
|
|
77
|
+
break;
|
|
78
|
+
case "lt":
|
|
79
|
+
condition = { [field]: { $lt: value } };
|
|
80
|
+
break;
|
|
81
|
+
case "lte":
|
|
82
|
+
condition = { [field]: { $lte: value } };
|
|
83
|
+
break;
|
|
84
|
+
case "ne":
|
|
85
|
+
condition = { [field]: { $ne: value } };
|
|
86
|
+
break;
|
|
87
|
+
case "contains":
|
|
88
|
+
condition = { [field]: { $regex: `.*${escapeForMongoRegex(value)}.*` } };
|
|
89
|
+
break;
|
|
90
|
+
case "starts_with":
|
|
91
|
+
condition = { [field]: { $regex: `^${escapeForMongoRegex(value)}` } };
|
|
92
|
+
break;
|
|
93
|
+
case "ends_with":
|
|
94
|
+
condition = { [field]: { $regex: `${escapeForMongoRegex(value)}$` } };
|
|
95
|
+
break;
|
|
96
|
+
default: throw new Error(`Unsupported operator: ${operator}`);
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
condition,
|
|
100
|
+
connector
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
if (conditions.length === 1) return conditions[0].condition;
|
|
104
|
+
const andConditions = conditions.filter((c) => c.connector === "AND").map((c) => c.condition);
|
|
105
|
+
const orConditions = conditions.filter((c) => c.connector === "OR").map((c) => c.condition);
|
|
106
|
+
let clause = {};
|
|
107
|
+
if (andConditions.length) clause = {
|
|
108
|
+
...clause,
|
|
109
|
+
$and: andConditions
|
|
110
|
+
};
|
|
111
|
+
if (orConditions.length) clause = {
|
|
112
|
+
...clause,
|
|
113
|
+
$or: orConditions
|
|
114
|
+
};
|
|
115
|
+
return clause;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
async create({ model, data: values }) {
|
|
119
|
+
return {
|
|
120
|
+
_id: (await db$1.collection(model).insertOne(values, { ...session && { session } })).insertedId.toString(),
|
|
121
|
+
...values
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
async findOne({ model, where, select }) {
|
|
125
|
+
const clause = convertWhereClause({
|
|
126
|
+
where,
|
|
127
|
+
model
|
|
128
|
+
});
|
|
129
|
+
const projection = select ? Object.fromEntries(select.map((field) => [getFieldName({
|
|
130
|
+
field,
|
|
131
|
+
model
|
|
132
|
+
}), 1])) : void 0;
|
|
133
|
+
const res = await db$1.collection(model).findOne(clause, {
|
|
134
|
+
...session && { session },
|
|
135
|
+
...projection && { projection }
|
|
136
|
+
});
|
|
137
|
+
if (!res) return null;
|
|
138
|
+
return res;
|
|
139
|
+
},
|
|
140
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
141
|
+
const clause = where ? convertWhereClause({
|
|
142
|
+
where,
|
|
143
|
+
model
|
|
144
|
+
}) : {};
|
|
145
|
+
const cursor = db$1.collection(model).find(clause, { ...session && { session } });
|
|
146
|
+
if (limit) cursor.limit(limit);
|
|
147
|
+
if (offset) cursor.skip(offset);
|
|
148
|
+
if (sortBy) cursor.sort(getFieldName({
|
|
149
|
+
field: sortBy.field,
|
|
150
|
+
model
|
|
151
|
+
}), sortBy.direction === "desc" ? -1 : 1);
|
|
152
|
+
return await cursor.toArray();
|
|
153
|
+
},
|
|
154
|
+
async count({ model, where }) {
|
|
155
|
+
const clause = where ? convertWhereClause({
|
|
156
|
+
where,
|
|
157
|
+
model
|
|
158
|
+
}) : {};
|
|
159
|
+
return await db$1.collection(model).countDocuments(clause, { ...session && { session } });
|
|
160
|
+
},
|
|
161
|
+
async update({ model, where, update: values }) {
|
|
162
|
+
const clause = convertWhereClause({
|
|
163
|
+
where,
|
|
164
|
+
model
|
|
165
|
+
});
|
|
166
|
+
const res = await db$1.collection(model).findOneAndUpdate(clause, { $set: values }, {
|
|
167
|
+
...session && { session },
|
|
168
|
+
returnDocument: "after"
|
|
169
|
+
});
|
|
170
|
+
if (!res) return null;
|
|
171
|
+
return res;
|
|
172
|
+
},
|
|
173
|
+
async updateMany({ model, where, update: values }) {
|
|
174
|
+
const clause = convertWhereClause({
|
|
175
|
+
where,
|
|
176
|
+
model
|
|
177
|
+
});
|
|
178
|
+
return (await db$1.collection(model).updateMany(clause, { $set: values }, { ...session && { session } })).modifiedCount;
|
|
179
|
+
},
|
|
180
|
+
async delete({ model, where }) {
|
|
181
|
+
const clause = convertWhereClause({
|
|
182
|
+
where,
|
|
183
|
+
model
|
|
184
|
+
});
|
|
185
|
+
await db$1.collection(model).deleteOne(clause, { ...session && { session } });
|
|
186
|
+
},
|
|
187
|
+
async deleteMany({ model, where }) {
|
|
188
|
+
const clause = convertWhereClause({
|
|
189
|
+
where,
|
|
190
|
+
model
|
|
191
|
+
});
|
|
192
|
+
return (await db$1.collection(model).deleteMany(clause, { ...session && { session } })).deletedCount;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
let lazyAdapter = null;
|
|
197
|
+
let adapterOptions = null;
|
|
198
|
+
adapterOptions = {
|
|
199
|
+
config: {
|
|
200
|
+
adapterId: "mongodb-adapter",
|
|
201
|
+
adapterName: "MongoDB Adapter",
|
|
202
|
+
usePlural: config?.usePlural ?? false,
|
|
203
|
+
debugLogs: config?.debugLogs ?? false,
|
|
204
|
+
mapKeysTransformInput: { id: "_id" },
|
|
205
|
+
mapKeysTransformOutput: { _id: "id" },
|
|
206
|
+
supportsNumericIds: false,
|
|
207
|
+
transaction: config?.client && (config?.transaction ?? true) ? async (cb) => {
|
|
208
|
+
if (!config.client) return cb(lazyAdapter(lazyOptions));
|
|
209
|
+
const session = config.client.startSession();
|
|
210
|
+
try {
|
|
211
|
+
session.startTransaction();
|
|
212
|
+
const result = await cb(createAdapterFactory({
|
|
213
|
+
config: adapterOptions.config,
|
|
214
|
+
adapter: createCustomAdapter(db, session)
|
|
215
|
+
})(lazyOptions));
|
|
216
|
+
await session.commitTransaction();
|
|
217
|
+
return result;
|
|
218
|
+
} catch (err) {
|
|
219
|
+
await session.abortTransaction();
|
|
220
|
+
throw err;
|
|
221
|
+
} finally {
|
|
222
|
+
await session.endSession();
|
|
223
|
+
}
|
|
224
|
+
} : false,
|
|
225
|
+
customTransformInput({ action, data, field, fieldAttributes, schema, model, options }) {
|
|
226
|
+
if (field === "_id" || fieldAttributes.references?.field === "id") {
|
|
227
|
+
if (action === "update") {
|
|
228
|
+
if (typeof data === "string") try {
|
|
229
|
+
return new ObjectId(data);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
return data;
|
|
232
|
+
}
|
|
233
|
+
return data;
|
|
234
|
+
}
|
|
235
|
+
if (Array.isArray(data)) return data.map((v) => {
|
|
236
|
+
if (typeof v === "string") try {
|
|
237
|
+
return new ObjectId(v);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
return v;
|
|
240
|
+
}
|
|
241
|
+
return v;
|
|
242
|
+
});
|
|
243
|
+
if (typeof data === "string") try {
|
|
244
|
+
return new ObjectId(data);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
return new ObjectId();
|
|
247
|
+
}
|
|
248
|
+
if (data === null && fieldAttributes.references?.field === "id") return null;
|
|
249
|
+
return new ObjectId();
|
|
250
|
+
}
|
|
251
|
+
return data;
|
|
252
|
+
},
|
|
253
|
+
customTransformOutput({ data, field, fieldAttributes }) {
|
|
254
|
+
if (field === "id" || fieldAttributes.references?.field === "id") {
|
|
255
|
+
if (data instanceof ObjectId) return data.toHexString();
|
|
256
|
+
if (Array.isArray(data)) return data.map((v) => {
|
|
257
|
+
if (v instanceof ObjectId) return v.toHexString();
|
|
258
|
+
return v;
|
|
259
|
+
});
|
|
260
|
+
return data;
|
|
261
|
+
}
|
|
262
|
+
return data;
|
|
263
|
+
},
|
|
264
|
+
customIdGenerator(props) {
|
|
265
|
+
return new ObjectId().toString();
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
adapter: createCustomAdapter(db)
|
|
269
|
+
};
|
|
270
|
+
lazyAdapter = createAdapterFactory(adapterOptions);
|
|
271
|
+
return (options) => {
|
|
272
|
+
lazyOptions = options;
|
|
273
|
+
return lazyAdapter(options);
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Safely escape user input for use in a MongoDB regex.
|
|
278
|
+
* This ensures the resulting pattern is treated as literal text,
|
|
279
|
+
* and not as a regex with special syntax.
|
|
280
|
+
*
|
|
281
|
+
* @param input - The input string to escape. Any type that isn't a string will be converted to an empty string.
|
|
282
|
+
* @param maxLength - The maximum length of the input string to escape. Defaults to 256. This is to prevent DOS attacks.
|
|
283
|
+
* @returns The escaped string.
|
|
284
|
+
*/
|
|
285
|
+
function escapeForMongoRegex(input, maxLength = 256) {
|
|
286
|
+
if (typeof input !== "string") return "";
|
|
287
|
+
return input.slice(0, maxLength).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
//#endregion
|
|
291
|
+
export { mongodbAdapter as t };
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-oakRccI5.cjs');
|
|
2
|
+
const require_adapter_factory = require('./adapter-factory-DT-_2aHJ.cjs');
|
|
3
|
+
let mongodb = require("mongodb");
|
|
4
|
+
|
|
5
|
+
//#region src/adapters/mongodb-adapter/mongodb-adapter.ts
|
|
6
|
+
const mongodbAdapter = (db, config) => {
|
|
7
|
+
let lazyOptions;
|
|
8
|
+
const createCustomAdapter = (db$1, session) => ({ options, getFieldName, schema, getDefaultModelName }) => {
|
|
9
|
+
function serializeID({ field, value, model }) {
|
|
10
|
+
model = getDefaultModelName(model);
|
|
11
|
+
if (field === "id" || field === "_id" || schema[model].fields[field]?.references?.field === "id") {
|
|
12
|
+
if (typeof value !== "string") {
|
|
13
|
+
if (value instanceof mongodb.ObjectId) return value;
|
|
14
|
+
if (Array.isArray(value)) return value.map((v) => {
|
|
15
|
+
if (typeof v === "string") try {
|
|
16
|
+
return new mongodb.ObjectId(v);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
return v;
|
|
19
|
+
}
|
|
20
|
+
if (v instanceof mongodb.ObjectId) return v;
|
|
21
|
+
throw new Error("Invalid id value");
|
|
22
|
+
});
|
|
23
|
+
throw new Error("Invalid id value");
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return new mongodb.ObjectId(value);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
function convertWhereClause({ where, model }) {
|
|
34
|
+
if (!where.length) return {};
|
|
35
|
+
const conditions = where.map((w) => {
|
|
36
|
+
const { field: field_, value, operator = "eq", connector = "AND" } = w;
|
|
37
|
+
let condition;
|
|
38
|
+
let field = getFieldName({
|
|
39
|
+
model,
|
|
40
|
+
field: field_
|
|
41
|
+
});
|
|
42
|
+
if (field === "id") field = "_id";
|
|
43
|
+
switch (operator.toLowerCase()) {
|
|
44
|
+
case "eq":
|
|
45
|
+
condition = { [field]: serializeID({
|
|
46
|
+
field,
|
|
47
|
+
value,
|
|
48
|
+
model
|
|
49
|
+
}) };
|
|
50
|
+
break;
|
|
51
|
+
case "in":
|
|
52
|
+
condition = { [field]: { $in: Array.isArray(value) ? value.map((v) => serializeID({
|
|
53
|
+
field,
|
|
54
|
+
value: v,
|
|
55
|
+
model
|
|
56
|
+
})) : [serializeID({
|
|
57
|
+
field,
|
|
58
|
+
value,
|
|
59
|
+
model
|
|
60
|
+
})] } };
|
|
61
|
+
break;
|
|
62
|
+
case "not_in":
|
|
63
|
+
condition = { [field]: { $nin: Array.isArray(value) ? value.map((v) => serializeID({
|
|
64
|
+
field,
|
|
65
|
+
value: v,
|
|
66
|
+
model
|
|
67
|
+
})) : [serializeID({
|
|
68
|
+
field,
|
|
69
|
+
value,
|
|
70
|
+
model
|
|
71
|
+
})] } };
|
|
72
|
+
break;
|
|
73
|
+
case "gt":
|
|
74
|
+
condition = { [field]: { $gt: value } };
|
|
75
|
+
break;
|
|
76
|
+
case "gte":
|
|
77
|
+
condition = { [field]: { $gte: value } };
|
|
78
|
+
break;
|
|
79
|
+
case "lt":
|
|
80
|
+
condition = { [field]: { $lt: value } };
|
|
81
|
+
break;
|
|
82
|
+
case "lte":
|
|
83
|
+
condition = { [field]: { $lte: value } };
|
|
84
|
+
break;
|
|
85
|
+
case "ne":
|
|
86
|
+
condition = { [field]: { $ne: value } };
|
|
87
|
+
break;
|
|
88
|
+
case "contains":
|
|
89
|
+
condition = { [field]: { $regex: `.*${escapeForMongoRegex(value)}.*` } };
|
|
90
|
+
break;
|
|
91
|
+
case "starts_with":
|
|
92
|
+
condition = { [field]: { $regex: `^${escapeForMongoRegex(value)}` } };
|
|
93
|
+
break;
|
|
94
|
+
case "ends_with":
|
|
95
|
+
condition = { [field]: { $regex: `${escapeForMongoRegex(value)}$` } };
|
|
96
|
+
break;
|
|
97
|
+
default: throw new Error(`Unsupported operator: ${operator}`);
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
condition,
|
|
101
|
+
connector
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
if (conditions.length === 1) return conditions[0].condition;
|
|
105
|
+
const andConditions = conditions.filter((c) => c.connector === "AND").map((c) => c.condition);
|
|
106
|
+
const orConditions = conditions.filter((c) => c.connector === "OR").map((c) => c.condition);
|
|
107
|
+
let clause = {};
|
|
108
|
+
if (andConditions.length) clause = {
|
|
109
|
+
...clause,
|
|
110
|
+
$and: andConditions
|
|
111
|
+
};
|
|
112
|
+
if (orConditions.length) clause = {
|
|
113
|
+
...clause,
|
|
114
|
+
$or: orConditions
|
|
115
|
+
};
|
|
116
|
+
return clause;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
async create({ model, data: values }) {
|
|
120
|
+
return {
|
|
121
|
+
_id: (await db$1.collection(model).insertOne(values, { ...session && { session } })).insertedId.toString(),
|
|
122
|
+
...values
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
async findOne({ model, where, select }) {
|
|
126
|
+
const clause = convertWhereClause({
|
|
127
|
+
where,
|
|
128
|
+
model
|
|
129
|
+
});
|
|
130
|
+
const projection = select ? Object.fromEntries(select.map((field) => [getFieldName({
|
|
131
|
+
field,
|
|
132
|
+
model
|
|
133
|
+
}), 1])) : void 0;
|
|
134
|
+
const res = await db$1.collection(model).findOne(clause, {
|
|
135
|
+
...session && { session },
|
|
136
|
+
...projection && { projection }
|
|
137
|
+
});
|
|
138
|
+
if (!res) return null;
|
|
139
|
+
return res;
|
|
140
|
+
},
|
|
141
|
+
async findMany({ model, where, limit, offset, sortBy }) {
|
|
142
|
+
const clause = where ? convertWhereClause({
|
|
143
|
+
where,
|
|
144
|
+
model
|
|
145
|
+
}) : {};
|
|
146
|
+
const cursor = db$1.collection(model).find(clause, { ...session && { session } });
|
|
147
|
+
if (limit) cursor.limit(limit);
|
|
148
|
+
if (offset) cursor.skip(offset);
|
|
149
|
+
if (sortBy) cursor.sort(getFieldName({
|
|
150
|
+
field: sortBy.field,
|
|
151
|
+
model
|
|
152
|
+
}), sortBy.direction === "desc" ? -1 : 1);
|
|
153
|
+
return await cursor.toArray();
|
|
154
|
+
},
|
|
155
|
+
async count({ model, where }) {
|
|
156
|
+
const clause = where ? convertWhereClause({
|
|
157
|
+
where,
|
|
158
|
+
model
|
|
159
|
+
}) : {};
|
|
160
|
+
return await db$1.collection(model).countDocuments(clause, { ...session && { session } });
|
|
161
|
+
},
|
|
162
|
+
async update({ model, where, update: values }) {
|
|
163
|
+
const clause = convertWhereClause({
|
|
164
|
+
where,
|
|
165
|
+
model
|
|
166
|
+
});
|
|
167
|
+
const res = await db$1.collection(model).findOneAndUpdate(clause, { $set: values }, {
|
|
168
|
+
...session && { session },
|
|
169
|
+
returnDocument: "after"
|
|
170
|
+
});
|
|
171
|
+
if (!res) return null;
|
|
172
|
+
return res;
|
|
173
|
+
},
|
|
174
|
+
async updateMany({ model, where, update: values }) {
|
|
175
|
+
const clause = convertWhereClause({
|
|
176
|
+
where,
|
|
177
|
+
model
|
|
178
|
+
});
|
|
179
|
+
return (await db$1.collection(model).updateMany(clause, { $set: values }, { ...session && { session } })).modifiedCount;
|
|
180
|
+
},
|
|
181
|
+
async delete({ model, where }) {
|
|
182
|
+
const clause = convertWhereClause({
|
|
183
|
+
where,
|
|
184
|
+
model
|
|
185
|
+
});
|
|
186
|
+
await db$1.collection(model).deleteOne(clause, { ...session && { session } });
|
|
187
|
+
},
|
|
188
|
+
async deleteMany({ model, where }) {
|
|
189
|
+
const clause = convertWhereClause({
|
|
190
|
+
where,
|
|
191
|
+
model
|
|
192
|
+
});
|
|
193
|
+
return (await db$1.collection(model).deleteMany(clause, { ...session && { session } })).deletedCount;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
let lazyAdapter = null;
|
|
198
|
+
let adapterOptions = null;
|
|
199
|
+
adapterOptions = {
|
|
200
|
+
config: {
|
|
201
|
+
adapterId: "mongodb-adapter",
|
|
202
|
+
adapterName: "MongoDB Adapter",
|
|
203
|
+
usePlural: config?.usePlural ?? false,
|
|
204
|
+
debugLogs: config?.debugLogs ?? false,
|
|
205
|
+
mapKeysTransformInput: { id: "_id" },
|
|
206
|
+
mapKeysTransformOutput: { _id: "id" },
|
|
207
|
+
supportsNumericIds: false,
|
|
208
|
+
transaction: config?.client && (config?.transaction ?? true) ? async (cb) => {
|
|
209
|
+
if (!config.client) return cb(lazyAdapter(lazyOptions));
|
|
210
|
+
const session = config.client.startSession();
|
|
211
|
+
try {
|
|
212
|
+
session.startTransaction();
|
|
213
|
+
const result = await cb(require_adapter_factory.createAdapterFactory({
|
|
214
|
+
config: adapterOptions.config,
|
|
215
|
+
adapter: createCustomAdapter(db, session)
|
|
216
|
+
})(lazyOptions));
|
|
217
|
+
await session.commitTransaction();
|
|
218
|
+
return result;
|
|
219
|
+
} catch (err) {
|
|
220
|
+
await session.abortTransaction();
|
|
221
|
+
throw err;
|
|
222
|
+
} finally {
|
|
223
|
+
await session.endSession();
|
|
224
|
+
}
|
|
225
|
+
} : false,
|
|
226
|
+
customTransformInput({ action, data, field, fieldAttributes, schema, model, options }) {
|
|
227
|
+
if (field === "_id" || fieldAttributes.references?.field === "id") {
|
|
228
|
+
if (action === "update") {
|
|
229
|
+
if (typeof data === "string") try {
|
|
230
|
+
return new mongodb.ObjectId(data);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
return data;
|
|
233
|
+
}
|
|
234
|
+
return data;
|
|
235
|
+
}
|
|
236
|
+
if (Array.isArray(data)) return data.map((v) => {
|
|
237
|
+
if (typeof v === "string") try {
|
|
238
|
+
return new mongodb.ObjectId(v);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
return v;
|
|
241
|
+
}
|
|
242
|
+
return v;
|
|
243
|
+
});
|
|
244
|
+
if (typeof data === "string") try {
|
|
245
|
+
return new mongodb.ObjectId(data);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
return new mongodb.ObjectId();
|
|
248
|
+
}
|
|
249
|
+
if (data === null && fieldAttributes.references?.field === "id") return null;
|
|
250
|
+
return new mongodb.ObjectId();
|
|
251
|
+
}
|
|
252
|
+
return data;
|
|
253
|
+
},
|
|
254
|
+
customTransformOutput({ data, field, fieldAttributes }) {
|
|
255
|
+
if (field === "id" || fieldAttributes.references?.field === "id") {
|
|
256
|
+
if (data instanceof mongodb.ObjectId) return data.toHexString();
|
|
257
|
+
if (Array.isArray(data)) return data.map((v) => {
|
|
258
|
+
if (v instanceof mongodb.ObjectId) return v.toHexString();
|
|
259
|
+
return v;
|
|
260
|
+
});
|
|
261
|
+
return data;
|
|
262
|
+
}
|
|
263
|
+
return data;
|
|
264
|
+
},
|
|
265
|
+
customIdGenerator(props) {
|
|
266
|
+
return new mongodb.ObjectId().toString();
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
adapter: createCustomAdapter(db)
|
|
270
|
+
};
|
|
271
|
+
lazyAdapter = require_adapter_factory.createAdapterFactory(adapterOptions);
|
|
272
|
+
return (options) => {
|
|
273
|
+
lazyOptions = options;
|
|
274
|
+
return lazyAdapter(options);
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Safely escape user input for use in a MongoDB regex.
|
|
279
|
+
* This ensures the resulting pattern is treated as literal text,
|
|
280
|
+
* and not as a regex with special syntax.
|
|
281
|
+
*
|
|
282
|
+
* @param input - The input string to escape. Any type that isn't a string will be converted to an empty string.
|
|
283
|
+
* @param maxLength - The maximum length of the input string to escape. Defaults to 256. This is to prevent DOS attacks.
|
|
284
|
+
* @returns The escaped string.
|
|
285
|
+
*/
|
|
286
|
+
function escapeForMongoRegex(input, maxLength = 256) {
|
|
287
|
+
if (typeof input !== "string") return "";
|
|
288
|
+
return input.slice(0, maxLength).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
//#endregion
|
|
292
|
+
Object.defineProperty(exports, 'mongodbAdapter', {
|
|
293
|
+
enumerable: true,
|
|
294
|
+
get: function () {
|
|
295
|
+
return mongodbAdapter;
|
|
296
|
+
}
|
|
297
|
+
});
|