@better-auth/mongo-adapter 1.5.0-beta.18 → 1.5.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/package.json +23 -6
- package/.turbo/turbo-build.log +0 -16
- package/src/index.ts +0 -1
- package/src/mongodb-adapter.test.ts +0 -12
- package/src/mongodb-adapter.ts +0 -723
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -8
- package/vitest.config.ts +0 -8
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Better Auth MongoDB Adapter
|
|
2
|
+
|
|
3
|
+
MongoDB adapter for [Better Auth](https://www.better-auth.com).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @better-auth/mongo-adapter
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
For full documentation, visit [better-auth.com/docs/adapters/mongodb](https://www.better-auth.com/docs/adapters/mongodb).
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/mongo-adapter",
|
|
3
|
-
"version": "1.5.0-beta.
|
|
3
|
+
"version": "1.5.0-beta.20",
|
|
4
4
|
"description": "Mongo adapter for Better Auth",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://www.better-auth.com/docs/adapters/mongodb",
|
|
6
8
|
"repository": {
|
|
7
9
|
"type": "git",
|
|
8
10
|
"url": "git+https://github.com/better-auth/better-auth.git",
|
|
9
11
|
"directory": "packages/mongo-adapter"
|
|
10
12
|
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"auth",
|
|
15
|
+
"mongodb",
|
|
16
|
+
"adapter",
|
|
17
|
+
"typescript",
|
|
18
|
+
"better-auth"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
11
26
|
"main": "./dist/index.mjs",
|
|
12
27
|
"module": "./dist/index.mjs",
|
|
13
28
|
"types": "./dist/index.d.mts",
|
|
@@ -21,19 +36,21 @@
|
|
|
21
36
|
"peerDependencies": {
|
|
22
37
|
"@better-auth/utils": "^0.3.0",
|
|
23
38
|
"mongodb": "^6.0.0 || ^7.0.0",
|
|
24
|
-
"@better-auth/core": "1.5.0-beta.
|
|
39
|
+
"@better-auth/core": "1.5.0-beta.20"
|
|
25
40
|
},
|
|
26
41
|
"devDependencies": {
|
|
27
42
|
"@better-auth/utils": "^0.3.1",
|
|
28
|
-
"mongodb": "^7.
|
|
43
|
+
"mongodb": "^7.1.0",
|
|
29
44
|
"tsdown": "^0.20.3",
|
|
30
45
|
"typescript": "^5.9.3",
|
|
31
|
-
"@better-auth/core": "1.5.0-beta.
|
|
46
|
+
"@better-auth/core": "1.5.0-beta.20"
|
|
32
47
|
},
|
|
33
48
|
"scripts": {
|
|
34
49
|
"build": "tsdown",
|
|
35
50
|
"dev": "tsdown --watch",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
51
|
+
"lint:package": "publint run --strict",
|
|
52
|
+
"lint:types": "attw --profile esm-only --pack .",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"test": "vitest"
|
|
38
55
|
}
|
|
39
56
|
}
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @better-auth/mongo-adapter@1.5.0-beta.18 build /home/runner/work/better-auth/better-auth/packages/mongo-adapter
|
|
3
|
-
> tsdown
|
|
4
|
-
|
|
5
|
-
[34mℹ[39m tsdown [2mv0.20.3[22m powered by rolldown [2mv1.0.0-rc.3[22m
|
|
6
|
-
[34mℹ[39m config file: [4m/home/runner/work/better-auth/better-auth/packages/mongo-adapter/tsdown.config.ts[24m
|
|
7
|
-
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
8
|
-
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
9
|
-
[34mℹ[39m Build start
|
|
10
|
-
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m13.17 kB[22m [2m│ gzip: 3.13 kB[22m
|
|
11
|
-
[34mℹ[39m [2mdist/[22mindex.mjs.map [2m31.74 kB[22m [2m│ gzip: 6.76 kB[22m
|
|
12
|
-
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 1.23 kB[22m [2m│ gzip: 0.57 kB[22m
|
|
13
|
-
[34mℹ[39m 3 files, total: 46.14 kB
|
|
14
|
-
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
15
|
-
|
|
16
|
-
[32m✔[39m Build complete in [32m9531ms[39m
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./mongodb-adapter";
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { mongodbAdapter } from "./mongodb-adapter";
|
|
3
|
-
|
|
4
|
-
describe("mongodb-adapter", () => {
|
|
5
|
-
it("should create mongodb adapter", () => {
|
|
6
|
-
const db = {
|
|
7
|
-
collection: vi.fn(),
|
|
8
|
-
} as any;
|
|
9
|
-
const adapter = mongodbAdapter(db);
|
|
10
|
-
expect(adapter).toBeDefined();
|
|
11
|
-
});
|
|
12
|
-
});
|
package/src/mongodb-adapter.ts
DELETED
|
@@ -1,723 +0,0 @@
|
|
|
1
|
-
import type { BetterAuthOptions } from "@better-auth/core";
|
|
2
|
-
import type {
|
|
3
|
-
AdapterFactoryCustomizeAdapterCreator,
|
|
4
|
-
AdapterFactoryOptions,
|
|
5
|
-
DBAdapter,
|
|
6
|
-
DBAdapterDebugLogOption,
|
|
7
|
-
Where,
|
|
8
|
-
} from "@better-auth/core/db/adapter";
|
|
9
|
-
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
10
|
-
import type { ClientSession, Db, MongoClient } from "mongodb";
|
|
11
|
-
import { ObjectId } from "mongodb";
|
|
12
|
-
|
|
13
|
-
class MongoAdapterError extends Error {
|
|
14
|
-
constructor(
|
|
15
|
-
public code: "INVALID_ID" | "UNSUPPORTED_OPERATOR",
|
|
16
|
-
message: string,
|
|
17
|
-
) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.name = "MongoAdapterError";
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface MongoDBAdapterConfig {
|
|
24
|
-
/**
|
|
25
|
-
* MongoDB client instance
|
|
26
|
-
* If not provided, Database transactions won't be enabled.
|
|
27
|
-
*/
|
|
28
|
-
client?: MongoClient | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Enable debug logs for the adapter
|
|
31
|
-
*
|
|
32
|
-
* @default false
|
|
33
|
-
*/
|
|
34
|
-
debugLogs?: DBAdapterDebugLogOption | undefined;
|
|
35
|
-
/**
|
|
36
|
-
* Use plural table names
|
|
37
|
-
*
|
|
38
|
-
* @default false
|
|
39
|
-
*/
|
|
40
|
-
usePlural?: boolean | undefined;
|
|
41
|
-
/**
|
|
42
|
-
* Whether to execute multiple operations in a transaction.
|
|
43
|
-
*
|
|
44
|
-
* ⚠️ Important:
|
|
45
|
-
* - Defaults to `true` when a MongoDB client is provided.
|
|
46
|
-
* - If your MongoDB instance does not support transactions
|
|
47
|
-
* (e.g. standalone server without a replica set),
|
|
48
|
-
* you must explicitly set `transaction: false`.
|
|
49
|
-
*/
|
|
50
|
-
transaction?: boolean | undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const mongodbAdapter = (
|
|
54
|
-
db: Db,
|
|
55
|
-
config?: MongoDBAdapterConfig | undefined,
|
|
56
|
-
) => {
|
|
57
|
-
let lazyOptions: BetterAuthOptions | null;
|
|
58
|
-
|
|
59
|
-
const getCustomIdGenerator = (options: BetterAuthOptions) => {
|
|
60
|
-
const generator = options.advanced?.database?.generateId;
|
|
61
|
-
if (typeof generator === "function") {
|
|
62
|
-
return generator;
|
|
63
|
-
}
|
|
64
|
-
return undefined;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const createCustomAdapter =
|
|
68
|
-
(
|
|
69
|
-
db: Db,
|
|
70
|
-
session?: ClientSession | undefined,
|
|
71
|
-
): AdapterFactoryCustomizeAdapterCreator =>
|
|
72
|
-
({
|
|
73
|
-
getFieldAttributes,
|
|
74
|
-
getFieldName,
|
|
75
|
-
schema,
|
|
76
|
-
getDefaultModelName,
|
|
77
|
-
options,
|
|
78
|
-
}) => {
|
|
79
|
-
const customIdGen = getCustomIdGenerator(options);
|
|
80
|
-
|
|
81
|
-
function serializeID({
|
|
82
|
-
field,
|
|
83
|
-
value,
|
|
84
|
-
model,
|
|
85
|
-
}: {
|
|
86
|
-
field: string;
|
|
87
|
-
value: any;
|
|
88
|
-
model: string;
|
|
89
|
-
}) {
|
|
90
|
-
if (customIdGen) {
|
|
91
|
-
return value;
|
|
92
|
-
}
|
|
93
|
-
model = getDefaultModelName(model);
|
|
94
|
-
if (
|
|
95
|
-
field === "id" ||
|
|
96
|
-
field === "_id" ||
|
|
97
|
-
schema[model]!.fields[field]?.references?.field === "id"
|
|
98
|
-
) {
|
|
99
|
-
if (value === null || value === undefined) {
|
|
100
|
-
return value;
|
|
101
|
-
}
|
|
102
|
-
if (typeof value !== "string") {
|
|
103
|
-
if (value instanceof ObjectId) {
|
|
104
|
-
return value;
|
|
105
|
-
}
|
|
106
|
-
if (Array.isArray(value)) {
|
|
107
|
-
return value.map((v) => {
|
|
108
|
-
if (v === null || v === undefined) {
|
|
109
|
-
return v;
|
|
110
|
-
}
|
|
111
|
-
if (typeof v === "string") {
|
|
112
|
-
try {
|
|
113
|
-
return new ObjectId(v);
|
|
114
|
-
} catch {
|
|
115
|
-
return v;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (v instanceof ObjectId) {
|
|
119
|
-
return v;
|
|
120
|
-
}
|
|
121
|
-
throw new MongoAdapterError("INVALID_ID", "Invalid id value");
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
throw new MongoAdapterError("INVALID_ID", "Invalid id value");
|
|
125
|
-
}
|
|
126
|
-
try {
|
|
127
|
-
return new ObjectId(value);
|
|
128
|
-
} catch {
|
|
129
|
-
return value;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return value;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function convertWhereClause({
|
|
136
|
-
where,
|
|
137
|
-
model,
|
|
138
|
-
}: {
|
|
139
|
-
where: Where[];
|
|
140
|
-
model: string;
|
|
141
|
-
}) {
|
|
142
|
-
if (!where.length) return {};
|
|
143
|
-
const conditions = where.map((w) => {
|
|
144
|
-
const {
|
|
145
|
-
field: field_,
|
|
146
|
-
value,
|
|
147
|
-
operator = "eq",
|
|
148
|
-
connector = "AND",
|
|
149
|
-
} = w;
|
|
150
|
-
let condition: any;
|
|
151
|
-
let field = getFieldName({ model, field: field_ });
|
|
152
|
-
if (field === "id") field = "_id";
|
|
153
|
-
switch (operator.toLowerCase()) {
|
|
154
|
-
case "eq":
|
|
155
|
-
condition = {
|
|
156
|
-
[field]: serializeID({
|
|
157
|
-
field,
|
|
158
|
-
value,
|
|
159
|
-
model,
|
|
160
|
-
}),
|
|
161
|
-
};
|
|
162
|
-
break;
|
|
163
|
-
case "in":
|
|
164
|
-
condition = {
|
|
165
|
-
[field]: {
|
|
166
|
-
$in: Array.isArray(value)
|
|
167
|
-
? value.map((v) => serializeID({ field, value: v, model }))
|
|
168
|
-
: [serializeID({ field, value, model })],
|
|
169
|
-
},
|
|
170
|
-
};
|
|
171
|
-
break;
|
|
172
|
-
case "not_in":
|
|
173
|
-
condition = {
|
|
174
|
-
[field]: {
|
|
175
|
-
$nin: Array.isArray(value)
|
|
176
|
-
? value.map((v) => serializeID({ field, value: v, model }))
|
|
177
|
-
: [serializeID({ field, value, model })],
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
break;
|
|
181
|
-
case "gt":
|
|
182
|
-
condition = {
|
|
183
|
-
[field]: {
|
|
184
|
-
$gt: serializeID({
|
|
185
|
-
field,
|
|
186
|
-
value,
|
|
187
|
-
model,
|
|
188
|
-
}),
|
|
189
|
-
},
|
|
190
|
-
};
|
|
191
|
-
break;
|
|
192
|
-
case "gte":
|
|
193
|
-
condition = {
|
|
194
|
-
[field]: {
|
|
195
|
-
$gte: serializeID({
|
|
196
|
-
field,
|
|
197
|
-
value,
|
|
198
|
-
model,
|
|
199
|
-
}),
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
break;
|
|
203
|
-
case "lt":
|
|
204
|
-
condition = {
|
|
205
|
-
[field]: {
|
|
206
|
-
$lt: serializeID({
|
|
207
|
-
field,
|
|
208
|
-
value,
|
|
209
|
-
model,
|
|
210
|
-
}),
|
|
211
|
-
},
|
|
212
|
-
};
|
|
213
|
-
break;
|
|
214
|
-
case "lte":
|
|
215
|
-
condition = {
|
|
216
|
-
[field]: {
|
|
217
|
-
$lte: serializeID({
|
|
218
|
-
field,
|
|
219
|
-
value,
|
|
220
|
-
model,
|
|
221
|
-
}),
|
|
222
|
-
},
|
|
223
|
-
};
|
|
224
|
-
break;
|
|
225
|
-
case "ne":
|
|
226
|
-
condition = {
|
|
227
|
-
[field]: {
|
|
228
|
-
$ne: serializeID({
|
|
229
|
-
field,
|
|
230
|
-
value,
|
|
231
|
-
model,
|
|
232
|
-
}),
|
|
233
|
-
},
|
|
234
|
-
};
|
|
235
|
-
break;
|
|
236
|
-
case "contains":
|
|
237
|
-
condition = {
|
|
238
|
-
[field]: {
|
|
239
|
-
$regex: `.*${escapeForMongoRegex(value as string)}.*`,
|
|
240
|
-
},
|
|
241
|
-
};
|
|
242
|
-
break;
|
|
243
|
-
case "starts_with":
|
|
244
|
-
condition = {
|
|
245
|
-
[field]: { $regex: `^${escapeForMongoRegex(value as string)}` },
|
|
246
|
-
};
|
|
247
|
-
break;
|
|
248
|
-
case "ends_with":
|
|
249
|
-
condition = {
|
|
250
|
-
[field]: { $regex: `${escapeForMongoRegex(value as string)}$` },
|
|
251
|
-
};
|
|
252
|
-
break;
|
|
253
|
-
default:
|
|
254
|
-
throw new MongoAdapterError(
|
|
255
|
-
"UNSUPPORTED_OPERATOR",
|
|
256
|
-
`Unsupported operator: ${operator}`,
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
return { condition, connector };
|
|
260
|
-
});
|
|
261
|
-
if (conditions.length === 1) {
|
|
262
|
-
return conditions[0]!.condition;
|
|
263
|
-
}
|
|
264
|
-
const andConditions = conditions
|
|
265
|
-
.filter((c) => c.connector === "AND")
|
|
266
|
-
.map((c) => c.condition);
|
|
267
|
-
const orConditions = conditions
|
|
268
|
-
.filter((c) => c.connector === "OR")
|
|
269
|
-
.map((c) => c.condition);
|
|
270
|
-
|
|
271
|
-
let clause = {};
|
|
272
|
-
if (andConditions.length) {
|
|
273
|
-
clause = { ...clause, $and: andConditions };
|
|
274
|
-
}
|
|
275
|
-
if (orConditions.length) {
|
|
276
|
-
clause = { ...clause, $or: orConditions };
|
|
277
|
-
}
|
|
278
|
-
return clause;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return {
|
|
282
|
-
async create({ model, data: values }) {
|
|
283
|
-
const res = await db.collection(model).insertOne(values, { session });
|
|
284
|
-
const insertedData = { _id: res.insertedId.toString(), ...values };
|
|
285
|
-
return insertedData as any;
|
|
286
|
-
},
|
|
287
|
-
async findOne({ model, where, select, join }) {
|
|
288
|
-
const matchStage = where
|
|
289
|
-
? { $match: convertWhereClause({ where, model }) }
|
|
290
|
-
: { $match: {} };
|
|
291
|
-
const pipeline: any[] = [matchStage];
|
|
292
|
-
|
|
293
|
-
if (join) {
|
|
294
|
-
for (const [joinedModel, joinConfig] of Object.entries(join)) {
|
|
295
|
-
const localField = getFieldName({
|
|
296
|
-
field: joinConfig.on.from,
|
|
297
|
-
model,
|
|
298
|
-
});
|
|
299
|
-
const foreignField = getFieldName({
|
|
300
|
-
field: joinConfig.on.to,
|
|
301
|
-
model: joinedModel,
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
const localFieldName = localField === "id" ? "_id" : localField;
|
|
305
|
-
const foreignFieldName =
|
|
306
|
-
foreignField === "id" ? "_id" : foreignField;
|
|
307
|
-
|
|
308
|
-
// Only unwind if the foreign field has a unique constraint (one-to-one relationship)
|
|
309
|
-
const joinedModelSchema =
|
|
310
|
-
schema[getDefaultModelName(joinedModel)];
|
|
311
|
-
const foreignFieldAttribute =
|
|
312
|
-
joinedModelSchema?.fields[joinConfig.on.to];
|
|
313
|
-
const isUnique = foreignFieldAttribute?.unique === true;
|
|
314
|
-
|
|
315
|
-
// For unique relationships, limit is ignored (as per JoinConfig type)
|
|
316
|
-
// For non-unique relationships, apply limit if specified
|
|
317
|
-
const shouldLimit = !isUnique && joinConfig.limit !== undefined;
|
|
318
|
-
const limit =
|
|
319
|
-
joinConfig.limit ??
|
|
320
|
-
options.advanced?.database?.defaultFindManyLimit ??
|
|
321
|
-
100;
|
|
322
|
-
if (shouldLimit && limit > 0) {
|
|
323
|
-
// Use pipeline syntax to support limit
|
|
324
|
-
// Construct the field reference string for the foreign field
|
|
325
|
-
const foreignFieldRef = `$${foreignFieldName}`;
|
|
326
|
-
pipeline.push({
|
|
327
|
-
$lookup: {
|
|
328
|
-
from: joinedModel,
|
|
329
|
-
let: { localFieldValue: `$${localFieldName}` },
|
|
330
|
-
pipeline: [
|
|
331
|
-
{
|
|
332
|
-
$match: {
|
|
333
|
-
$expr: {
|
|
334
|
-
$eq: [foreignFieldRef, "$$localFieldValue"],
|
|
335
|
-
},
|
|
336
|
-
},
|
|
337
|
-
},
|
|
338
|
-
{ $limit: limit },
|
|
339
|
-
],
|
|
340
|
-
as: joinedModel,
|
|
341
|
-
},
|
|
342
|
-
});
|
|
343
|
-
} else {
|
|
344
|
-
// Use simple syntax when no limit is needed
|
|
345
|
-
pipeline.push({
|
|
346
|
-
$lookup: {
|
|
347
|
-
from: joinedModel,
|
|
348
|
-
localField: localFieldName,
|
|
349
|
-
foreignField: foreignFieldName,
|
|
350
|
-
as: joinedModel,
|
|
351
|
-
},
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (isUnique) {
|
|
356
|
-
// For one-to-one relationships, unwind to flatten to a single object
|
|
357
|
-
pipeline.push({
|
|
358
|
-
$unwind: {
|
|
359
|
-
path: `$${joinedModel}`,
|
|
360
|
-
preserveNullAndEmptyArrays: true,
|
|
361
|
-
},
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
// For one-to-many, keep as array - no unwind
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (select) {
|
|
369
|
-
const projection: any = {};
|
|
370
|
-
select.forEach((field) => {
|
|
371
|
-
projection[getFieldName({ field, model })] = 1;
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
// Include joined collections in projection
|
|
375
|
-
if (join) {
|
|
376
|
-
for (const joinedModel of Object.keys(join)) {
|
|
377
|
-
projection[joinedModel] = 1;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
pipeline.push({ $project: projection });
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
pipeline.push({ $limit: 1 });
|
|
385
|
-
|
|
386
|
-
const res = await db
|
|
387
|
-
.collection(model)
|
|
388
|
-
.aggregate(pipeline, { session })
|
|
389
|
-
.toArray();
|
|
390
|
-
|
|
391
|
-
if (!res || res.length === 0) return null;
|
|
392
|
-
return res[0] as any;
|
|
393
|
-
},
|
|
394
|
-
async findMany({ model, where, limit, select, offset, sortBy, join }) {
|
|
395
|
-
const matchStage = where
|
|
396
|
-
? { $match: convertWhereClause({ where, model }) }
|
|
397
|
-
: { $match: {} };
|
|
398
|
-
const pipeline: any[] = [matchStage];
|
|
399
|
-
|
|
400
|
-
if (join) {
|
|
401
|
-
for (const [joinedModel, joinConfig] of Object.entries(join)) {
|
|
402
|
-
const localField = getFieldName({
|
|
403
|
-
field: joinConfig.on.from,
|
|
404
|
-
model,
|
|
405
|
-
});
|
|
406
|
-
const foreignField = getFieldName({
|
|
407
|
-
field: joinConfig.on.to,
|
|
408
|
-
model: joinedModel,
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
const localFieldName = localField === "id" ? "_id" : localField;
|
|
412
|
-
const foreignFieldName =
|
|
413
|
-
foreignField === "id" ? "_id" : foreignField;
|
|
414
|
-
|
|
415
|
-
// Only unwind if the foreign field has a unique constraint (one-to-one relationship)
|
|
416
|
-
const foreignFieldAttribute = getFieldAttributes({
|
|
417
|
-
model: joinedModel,
|
|
418
|
-
field: joinConfig.on.to,
|
|
419
|
-
});
|
|
420
|
-
const isUnique = foreignFieldAttribute?.unique === true;
|
|
421
|
-
|
|
422
|
-
// For unique relationships, limit is ignored (as per JoinConfig type)
|
|
423
|
-
// For non-unique relationships, apply limit if specified
|
|
424
|
-
const shouldLimit =
|
|
425
|
-
joinConfig.relation !== "one-to-one" &&
|
|
426
|
-
joinConfig.limit !== undefined;
|
|
427
|
-
|
|
428
|
-
const limit =
|
|
429
|
-
joinConfig.limit ??
|
|
430
|
-
options.advanced?.database?.defaultFindManyLimit ??
|
|
431
|
-
100;
|
|
432
|
-
if (shouldLimit && limit > 0) {
|
|
433
|
-
// Use pipeline syntax to support limit
|
|
434
|
-
// Construct the field reference string for the foreign field
|
|
435
|
-
const foreignFieldRef = `$${foreignFieldName}`;
|
|
436
|
-
pipeline.push({
|
|
437
|
-
$lookup: {
|
|
438
|
-
from: joinedModel,
|
|
439
|
-
let: { localFieldValue: `$${localFieldName}` },
|
|
440
|
-
pipeline: [
|
|
441
|
-
{
|
|
442
|
-
$match: {
|
|
443
|
-
$expr: {
|
|
444
|
-
$eq: [foreignFieldRef, "$$localFieldValue"],
|
|
445
|
-
},
|
|
446
|
-
},
|
|
447
|
-
},
|
|
448
|
-
{ $limit: limit },
|
|
449
|
-
],
|
|
450
|
-
as: joinedModel,
|
|
451
|
-
},
|
|
452
|
-
});
|
|
453
|
-
} else {
|
|
454
|
-
// Use simple syntax when no limit is needed
|
|
455
|
-
pipeline.push({
|
|
456
|
-
$lookup: {
|
|
457
|
-
from: joinedModel,
|
|
458
|
-
localField: localFieldName,
|
|
459
|
-
foreignField: foreignFieldName,
|
|
460
|
-
as: joinedModel,
|
|
461
|
-
},
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if (isUnique) {
|
|
466
|
-
// For one-to-one relationships, unwind to flatten to a single object
|
|
467
|
-
pipeline.push({
|
|
468
|
-
$unwind: {
|
|
469
|
-
path: `$${joinedModel}`,
|
|
470
|
-
preserveNullAndEmptyArrays: true,
|
|
471
|
-
},
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
// For one-to-many, keep as array - no unwind
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
if (select?.length && select.length > 0) {
|
|
479
|
-
const projection: any = {};
|
|
480
|
-
select.forEach((field) => {
|
|
481
|
-
projection[getFieldName({ field, model })] = 1;
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
// Include joined collections in projection
|
|
485
|
-
if (join) {
|
|
486
|
-
for (const joinedModel of Object.keys(join)) {
|
|
487
|
-
projection[joinedModel] = 1;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
pipeline.push({ $project: projection });
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if (sortBy) {
|
|
495
|
-
pipeline.push({
|
|
496
|
-
$sort: {
|
|
497
|
-
[getFieldName({ field: sortBy.field, model })]:
|
|
498
|
-
sortBy.direction === "desc" ? -1 : 1,
|
|
499
|
-
},
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
if (offset) {
|
|
504
|
-
pipeline.push({ $skip: offset });
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
if (limit) {
|
|
508
|
-
pipeline.push({ $limit: limit });
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
const res = await db
|
|
512
|
-
.collection(model)
|
|
513
|
-
.aggregate(pipeline, { session })
|
|
514
|
-
.toArray();
|
|
515
|
-
|
|
516
|
-
return res as any;
|
|
517
|
-
},
|
|
518
|
-
async count({ model, where }) {
|
|
519
|
-
const matchStage = where
|
|
520
|
-
? { $match: convertWhereClause({ where, model }) }
|
|
521
|
-
: { $match: {} };
|
|
522
|
-
const pipeline: any[] = [matchStage, { $count: "total" }];
|
|
523
|
-
|
|
524
|
-
const res = await db
|
|
525
|
-
.collection(model)
|
|
526
|
-
.aggregate(pipeline, { session })
|
|
527
|
-
.toArray();
|
|
528
|
-
|
|
529
|
-
if (!res || res.length === 0) return 0;
|
|
530
|
-
return res[0]?.total ?? 0;
|
|
531
|
-
},
|
|
532
|
-
async update({ model, where, update: values }) {
|
|
533
|
-
const clause = convertWhereClause({ where, model });
|
|
534
|
-
|
|
535
|
-
const res = await db.collection(model).findOneAndUpdate(
|
|
536
|
-
clause,
|
|
537
|
-
{ $set: values as any },
|
|
538
|
-
{
|
|
539
|
-
session,
|
|
540
|
-
returnDocument: "after",
|
|
541
|
-
includeResultMetadata: true,
|
|
542
|
-
},
|
|
543
|
-
);
|
|
544
|
-
const doc = (res as any)?.value ?? null;
|
|
545
|
-
if (!doc) return null;
|
|
546
|
-
return doc as any;
|
|
547
|
-
},
|
|
548
|
-
async updateMany({ model, where, update: values }) {
|
|
549
|
-
const clause = convertWhereClause({ where, model });
|
|
550
|
-
|
|
551
|
-
const res = await db.collection(model).updateMany(
|
|
552
|
-
clause,
|
|
553
|
-
{
|
|
554
|
-
$set: values as any,
|
|
555
|
-
},
|
|
556
|
-
{ session },
|
|
557
|
-
);
|
|
558
|
-
return res.modifiedCount;
|
|
559
|
-
},
|
|
560
|
-
async delete({ model, where }) {
|
|
561
|
-
const clause = convertWhereClause({ where, model });
|
|
562
|
-
await db.collection(model).deleteOne(clause, { session });
|
|
563
|
-
},
|
|
564
|
-
async deleteMany({ model, where }) {
|
|
565
|
-
const clause = convertWhereClause({ where, model });
|
|
566
|
-
const res = await db
|
|
567
|
-
.collection(model)
|
|
568
|
-
.deleteMany(clause, { session });
|
|
569
|
-
return res.deletedCount;
|
|
570
|
-
},
|
|
571
|
-
};
|
|
572
|
-
};
|
|
573
|
-
|
|
574
|
-
let lazyAdapter:
|
|
575
|
-
| ((options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>)
|
|
576
|
-
| null = null;
|
|
577
|
-
let adapterOptions: AdapterFactoryOptions | null = null;
|
|
578
|
-
adapterOptions = {
|
|
579
|
-
config: {
|
|
580
|
-
adapterId: "mongodb-adapter",
|
|
581
|
-
adapterName: "MongoDB Adapter",
|
|
582
|
-
usePlural: config?.usePlural ?? false,
|
|
583
|
-
debugLogs: config?.debugLogs ?? false,
|
|
584
|
-
mapKeysTransformInput: {
|
|
585
|
-
id: "_id",
|
|
586
|
-
},
|
|
587
|
-
mapKeysTransformOutput: {
|
|
588
|
-
_id: "id",
|
|
589
|
-
},
|
|
590
|
-
supportsArrays: true,
|
|
591
|
-
supportsNumericIds: false,
|
|
592
|
-
transaction:
|
|
593
|
-
config?.client && (config?.transaction ?? true)
|
|
594
|
-
? async (cb) => {
|
|
595
|
-
if (!config.client) {
|
|
596
|
-
return cb(lazyAdapter!(lazyOptions!));
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
const session = config.client.startSession();
|
|
600
|
-
|
|
601
|
-
try {
|
|
602
|
-
session.startTransaction();
|
|
603
|
-
|
|
604
|
-
const adapter = createAdapterFactory({
|
|
605
|
-
config: adapterOptions!.config,
|
|
606
|
-
adapter: createCustomAdapter(db, session),
|
|
607
|
-
})(lazyOptions!);
|
|
608
|
-
|
|
609
|
-
const result = await cb(adapter);
|
|
610
|
-
|
|
611
|
-
await session.commitTransaction();
|
|
612
|
-
return result;
|
|
613
|
-
} catch (err) {
|
|
614
|
-
await session.abortTransaction();
|
|
615
|
-
throw err;
|
|
616
|
-
} finally {
|
|
617
|
-
await session.endSession();
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
: false,
|
|
621
|
-
customTransformInput({
|
|
622
|
-
action,
|
|
623
|
-
data,
|
|
624
|
-
field,
|
|
625
|
-
fieldAttributes,
|
|
626
|
-
schema,
|
|
627
|
-
model,
|
|
628
|
-
options,
|
|
629
|
-
}) {
|
|
630
|
-
const customIdGen = getCustomIdGenerator(options);
|
|
631
|
-
if (field === "_id" || fieldAttributes.references?.field === "id") {
|
|
632
|
-
if (customIdGen) {
|
|
633
|
-
return data;
|
|
634
|
-
}
|
|
635
|
-
if (action !== "create" && action !== "update") {
|
|
636
|
-
return data;
|
|
637
|
-
}
|
|
638
|
-
if (data instanceof ObjectId) {
|
|
639
|
-
return data;
|
|
640
|
-
}
|
|
641
|
-
if (Array.isArray(data)) {
|
|
642
|
-
return data.map((v) => {
|
|
643
|
-
if (typeof v === "string") {
|
|
644
|
-
try {
|
|
645
|
-
const oid = new ObjectId(v);
|
|
646
|
-
return oid;
|
|
647
|
-
} catch {
|
|
648
|
-
return v;
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
return v;
|
|
652
|
-
});
|
|
653
|
-
}
|
|
654
|
-
if (typeof data === "string") {
|
|
655
|
-
try {
|
|
656
|
-
const oid = new ObjectId(data);
|
|
657
|
-
return oid;
|
|
658
|
-
} catch {
|
|
659
|
-
return data;
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
if (
|
|
663
|
-
fieldAttributes?.references?.field === "id" &&
|
|
664
|
-
!fieldAttributes?.required &&
|
|
665
|
-
data === null
|
|
666
|
-
) {
|
|
667
|
-
return null;
|
|
668
|
-
}
|
|
669
|
-
if (action === "update") {
|
|
670
|
-
return data;
|
|
671
|
-
}
|
|
672
|
-
const oid = new ObjectId();
|
|
673
|
-
return oid;
|
|
674
|
-
}
|
|
675
|
-
return data;
|
|
676
|
-
},
|
|
677
|
-
customTransformOutput({ data, field, fieldAttributes }) {
|
|
678
|
-
if (field === "id" || fieldAttributes.references?.field === "id") {
|
|
679
|
-
if (data instanceof ObjectId) {
|
|
680
|
-
return data.toHexString();
|
|
681
|
-
}
|
|
682
|
-
if (Array.isArray(data)) {
|
|
683
|
-
return data.map((v) => {
|
|
684
|
-
if (v instanceof ObjectId) {
|
|
685
|
-
return v.toHexString();
|
|
686
|
-
}
|
|
687
|
-
return v;
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
return data;
|
|
691
|
-
}
|
|
692
|
-
return data;
|
|
693
|
-
},
|
|
694
|
-
customIdGenerator() {
|
|
695
|
-
return new ObjectId().toString();
|
|
696
|
-
},
|
|
697
|
-
},
|
|
698
|
-
adapter: createCustomAdapter(db),
|
|
699
|
-
};
|
|
700
|
-
lazyAdapter = createAdapterFactory(adapterOptions);
|
|
701
|
-
|
|
702
|
-
return (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {
|
|
703
|
-
lazyOptions = options;
|
|
704
|
-
return lazyAdapter(options);
|
|
705
|
-
};
|
|
706
|
-
};
|
|
707
|
-
|
|
708
|
-
/**
|
|
709
|
-
* Safely escape user input for use in a MongoDB regex.
|
|
710
|
-
* This ensures the resulting pattern is treated as literal text,
|
|
711
|
-
* and not as a regex with special syntax.
|
|
712
|
-
*
|
|
713
|
-
* @param input - The input string to escape. Any type that isn't a string will be converted to an empty string.
|
|
714
|
-
* @param maxLength - The maximum length of the input string to escape. Defaults to 256. This is to prevent DOS attacks.
|
|
715
|
-
* @returns The escaped string.
|
|
716
|
-
*/
|
|
717
|
-
function escapeForMongoRegex(input: string, maxLength = 256): string {
|
|
718
|
-
if (typeof input !== "string") return "";
|
|
719
|
-
|
|
720
|
-
// Escape all PCRE special characters
|
|
721
|
-
// Source: PCRE docs — https://www.pcre.org/original/doc/html/pcrepattern.html
|
|
722
|
-
return input.slice(0, maxLength).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
723
|
-
}
|
package/tsconfig.json
DELETED
package/tsdown.config.ts
DELETED