@delmaredigital/payload-better-auth 0.1.4 → 0.2.2
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 +21 -73
- package/README.md +165 -290
- package/dist/adapter/collections.d.ts +52 -0
- package/dist/adapter/collections.d.ts.map +1 -0
- package/dist/adapter/collections.js +150 -0
- package/dist/adapter/collections.js.map +1 -0
- package/dist/adapter/index.d.ts +6 -9
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/adapter/index.js +399 -350
- package/dist/adapter/index.js.map +1 -1
- package/dist/components/BeforeLogin.d.ts +11 -0
- package/dist/components/BeforeLogin.d.ts.map +1 -0
- package/dist/components/BeforeLogin.js +25 -0
- package/dist/components/BeforeLogin.js.map +1 -0
- package/dist/components/LoginView.d.ts +21 -0
- package/dist/components/LoginView.d.ts.map +1 -0
- package/dist/components/LoginView.js +214 -0
- package/dist/components/LoginView.js.map +1 -0
- package/dist/components/LogoutButton.d.ts +7 -0
- package/dist/components/LogoutButton.d.ts.map +1 -0
- package/dist/components/LogoutButton.js +43 -0
- package/dist/components/LogoutButton.js.map +1 -0
- package/dist/exports/client.d.ts +6 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +6 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/components.d.ts +12 -0
- package/dist/exports/components.d.ts.map +1 -0
- package/dist/exports/components.js +10 -0
- package/dist/exports/components.js.map +1 -0
- package/dist/index.d.ts +14 -115
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -610
- package/dist/index.js.map +1 -1
- package/dist/plugin/index.d.ts +68 -16
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/index.js +268 -76
- package/dist/plugin/index.js.map +1 -1
- package/dist/utils/detectAuthConfig.d.ts +18 -0
- package/dist/utils/detectAuthConfig.d.ts.map +1 -0
- package/dist/utils/detectAuthConfig.js +31 -0
- package/dist/utils/detectAuthConfig.js.map +1 -0
- package/dist/utils/session.d.ts +63 -0
- package/dist/utils/session.d.ts.map +1 -0
- package/dist/utils/session.js +65 -0
- package/dist/utils/session.js.map +1 -0
- package/package.json +22 -23
- package/dist/adapter/index.d.mts +0 -70
- package/dist/adapter/index.mjs +0 -366
- package/dist/adapter/index.mjs.map +0 -1
- package/dist/client.d.mts +0 -1
- package/dist/client.d.ts +0 -1
- package/dist/client.js +0 -12
- package/dist/client.js.map +0 -1
- package/dist/client.mjs +0 -3
- package/dist/client.mjs.map +0 -1
- package/dist/index.d.mts +0 -120
- package/dist/index.mjs +0 -603
- package/dist/index.mjs.map +0 -1
- package/dist/plugin/index.d.mts +0 -78
- package/dist/plugin/index.mjs +0 -82
- package/dist/plugin/index.mjs.map +0 -1
package/dist/adapter/index.js
CHANGED
|
@@ -1,368 +1,417 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Payload CMS Adapter for Better Auth
|
|
3
|
+
*
|
|
4
|
+
* A clean adapter that bridges Better Auth to Payload collections.
|
|
5
|
+
* Follows the same factory pattern as payload-auth for compatibility.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_COLLECTIONS = {
|
|
10
|
+
user: 'users',
|
|
11
|
+
session: 'sessions',
|
|
12
|
+
account: 'accounts',
|
|
13
|
+
verification: 'verifications',
|
|
9
14
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
result.or = orConditions.map((w) => ({
|
|
49
|
-
[transformFieldName(w.field)]: convertOperator(w.operator, w.value)
|
|
50
|
-
}));
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
function convertOperator(operator, value) {
|
|
55
|
-
switch (operator) {
|
|
56
|
-
case "eq":
|
|
57
|
-
return { equals: value };
|
|
58
|
-
case "ne":
|
|
59
|
-
return { not_equals: value };
|
|
60
|
-
case "gt":
|
|
61
|
-
return { greater_than: value };
|
|
62
|
-
case "gte":
|
|
63
|
-
return { greater_than_equal: value };
|
|
64
|
-
case "lt":
|
|
65
|
-
return { less_than: value };
|
|
66
|
-
case "lte":
|
|
67
|
-
return { less_than_equal: value };
|
|
68
|
-
case "in":
|
|
69
|
-
return { in: value };
|
|
70
|
-
case "contains":
|
|
71
|
-
return { contains: value };
|
|
72
|
-
case "starts_with":
|
|
73
|
-
return { like: `${value}%` };
|
|
74
|
-
case "ends_with":
|
|
75
|
-
return { like: `%${value}` };
|
|
76
|
-
default:
|
|
77
|
-
return { equals: value };
|
|
15
|
+
/**
|
|
16
|
+
* Creates a Better Auth adapter that uses Payload CMS as the database.
|
|
17
|
+
*
|
|
18
|
+
* Returns a factory function that Better Auth calls with its options.
|
|
19
|
+
* This matches the pattern used by other Better Auth adapters.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { payloadAdapter } from '@delmare/payload-better-auth/adapter'
|
|
24
|
+
*
|
|
25
|
+
* const auth = betterAuth({
|
|
26
|
+
* database: payloadAdapter({
|
|
27
|
+
* payloadClient: payload,
|
|
28
|
+
* adapterConfig: {
|
|
29
|
+
* idType: 'number', // Use Payload's default SERIAL IDs
|
|
30
|
+
* collections: { user: 'users' },
|
|
31
|
+
* },
|
|
32
|
+
* }),
|
|
33
|
+
* // Required when using serial/integer IDs
|
|
34
|
+
* advanced: {
|
|
35
|
+
* database: {
|
|
36
|
+
* generateId: 'serial',
|
|
37
|
+
* },
|
|
38
|
+
* },
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function payloadAdapter({ payloadClient, adapterConfig, }) {
|
|
43
|
+
const { collections = {}, enableDebugLogs = false, idType } = adapterConfig;
|
|
44
|
+
const finalCollections = { ...DEFAULT_COLLECTIONS, ...collections };
|
|
45
|
+
const log = (...args) => {
|
|
46
|
+
if (enableDebugLogs)
|
|
47
|
+
console.log('[payload-adapter]', ...args);
|
|
48
|
+
};
|
|
49
|
+
async function resolvePayloadClient() {
|
|
50
|
+
return typeof payloadClient === 'function'
|
|
51
|
+
? await payloadClient()
|
|
52
|
+
: payloadClient;
|
|
78
53
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if ("and" in where || "or" in where) return null;
|
|
82
|
-
const idCondition = where.id;
|
|
83
|
-
if (idCondition && typeof idCondition === "object" && "equals" in idCondition) {
|
|
84
|
-
const value = idCondition.equals;
|
|
85
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
86
|
-
return value;
|
|
87
|
-
}
|
|
54
|
+
function getCollection(model) {
|
|
55
|
+
return (finalCollections[model] ?? model);
|
|
88
56
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
} else {
|
|
100
|
-
result[fieldName] = value;
|
|
57
|
+
/**
|
|
58
|
+
* Transform field name for where clause (Better Auth → Payload)
|
|
59
|
+
* Converts relationship fields like `userId` to `user`
|
|
60
|
+
*/
|
|
61
|
+
function transformFieldName(fieldName) {
|
|
62
|
+
if (fieldName.endsWith('Id') &&
|
|
63
|
+
fieldName !== 'id' &&
|
|
64
|
+
fieldName !== 'accountId' &&
|
|
65
|
+
fieldName !== 'providerId') {
|
|
66
|
+
return fieldName.slice(0, -2);
|
|
101
67
|
}
|
|
102
|
-
|
|
103
|
-
result[key] = value;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
function transformOutput(doc) {
|
|
109
|
-
if (!doc || typeof doc !== "object") return doc;
|
|
110
|
-
const result = { ...doc };
|
|
111
|
-
if ("id" in result && result.id !== void 0) {
|
|
112
|
-
result.id = String(result.id);
|
|
68
|
+
return fieldName;
|
|
113
69
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Convert Better Auth where clause to Payload where clause
|
|
72
|
+
*/
|
|
73
|
+
function convertWhere(where) {
|
|
74
|
+
if (!where || where.length === 0)
|
|
75
|
+
return {};
|
|
76
|
+
if (where.length === 1) {
|
|
77
|
+
const w = where[0];
|
|
78
|
+
return {
|
|
79
|
+
[transformFieldName(w.field)]: convertOperator(w.operator, w.value),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const andConditions = where.filter((w) => w.connector !== 'OR');
|
|
83
|
+
const orConditions = where.filter((w) => w.connector === 'OR');
|
|
84
|
+
const result = {};
|
|
85
|
+
if (andConditions.length > 0) {
|
|
86
|
+
result.and = andConditions.map((w) => ({
|
|
87
|
+
[transformFieldName(w.field)]: convertOperator(w.operator, w.value),
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
if (orConditions.length > 0) {
|
|
91
|
+
result.or = orConditions.map((w) => ({
|
|
92
|
+
[transformFieldName(w.field)]: convertOperator(w.operator, w.value),
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
128
96
|
}
|
|
129
|
-
|
|
130
|
-
|
|
97
|
+
function convertOperator(operator, value) {
|
|
98
|
+
switch (operator) {
|
|
99
|
+
case 'eq':
|
|
100
|
+
return { equals: value };
|
|
101
|
+
case 'ne':
|
|
102
|
+
return { not_equals: value };
|
|
103
|
+
case 'gt':
|
|
104
|
+
return { greater_than: value };
|
|
105
|
+
case 'gte':
|
|
106
|
+
return { greater_than_equal: value };
|
|
107
|
+
case 'lt':
|
|
108
|
+
return { less_than: value };
|
|
109
|
+
case 'lte':
|
|
110
|
+
return { less_than_equal: value };
|
|
111
|
+
case 'in':
|
|
112
|
+
return { in: value };
|
|
113
|
+
case 'contains':
|
|
114
|
+
return { contains: value };
|
|
115
|
+
case 'starts_with':
|
|
116
|
+
return { like: `${value}%` };
|
|
117
|
+
case 'ends_with':
|
|
118
|
+
return { like: `%${value}` };
|
|
119
|
+
default:
|
|
120
|
+
return { equals: value };
|
|
121
|
+
}
|
|
131
122
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
where: { user: { equals: doc.id } },
|
|
144
|
-
limit: limit ?? 100,
|
|
145
|
-
depth: 0
|
|
146
|
-
});
|
|
147
|
-
result[joinKey] = accounts.docs.map((a) => transformOutput(a));
|
|
148
|
-
} else if (model === "session" && joinKey === "user") {
|
|
149
|
-
const userId = doc.userId;
|
|
150
|
-
if (userId) {
|
|
151
|
-
try {
|
|
152
|
-
const user = await payload.findByID({
|
|
153
|
-
collection: getCollection("user"),
|
|
154
|
-
id: userId,
|
|
155
|
-
depth: 0
|
|
156
|
-
});
|
|
157
|
-
result[joinKey] = transformOutput(user);
|
|
158
|
-
} catch {
|
|
159
|
-
result[joinKey] = null;
|
|
160
|
-
}
|
|
123
|
+
function extractSingleId(where) {
|
|
124
|
+
if ('and' in where || 'or' in where)
|
|
125
|
+
return null;
|
|
126
|
+
const idCondition = where.id;
|
|
127
|
+
if (idCondition &&
|
|
128
|
+
typeof idCondition === 'object' &&
|
|
129
|
+
'equals' in idCondition) {
|
|
130
|
+
const value = idCondition.equals;
|
|
131
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
161
134
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Transform input data (Better Auth → Payload field names)
|
|
139
|
+
* Also converts relationship IDs to the correct type based on idType
|
|
140
|
+
*/
|
|
141
|
+
function transformInput(data) {
|
|
142
|
+
const result = {};
|
|
143
|
+
for (const [key, value] of Object.entries(data)) {
|
|
144
|
+
if (key.endsWith('Id') &&
|
|
145
|
+
key !== 'id' &&
|
|
146
|
+
key !== 'accountId' &&
|
|
147
|
+
key !== 'providerId') {
|
|
148
|
+
// Transform userId -> user and convert to correct ID type for relationships
|
|
149
|
+
const fieldName = key.slice(0, -2);
|
|
150
|
+
if (idType === 'number' && typeof value === 'string') {
|
|
151
|
+
const num = parseInt(value, 10);
|
|
152
|
+
result[fieldName] = isNaN(num) ? value : num;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
result[fieldName] = value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
result[key] = value;
|
|
160
|
+
}
|
|
175
161
|
}
|
|
176
|
-
|
|
162
|
+
return result;
|
|
177
163
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}) {
|
|
188
|
-
const payload = await resolvePayloadClient();
|
|
189
|
-
const collection = getCollection(model);
|
|
190
|
-
const transformedData = transformInput(data);
|
|
191
|
-
log("create", { collection, data: transformedData });
|
|
192
|
-
try {
|
|
193
|
-
const result = await payload.create({
|
|
194
|
-
collection,
|
|
195
|
-
data: transformedData,
|
|
196
|
-
depth: 0
|
|
197
|
-
});
|
|
198
|
-
return transformOutput(result);
|
|
199
|
-
} catch (error) {
|
|
200
|
-
console.error("[payload-adapter] create failed:", {
|
|
201
|
-
collection,
|
|
202
|
-
model,
|
|
203
|
-
error: error instanceof Error ? error.message : error
|
|
204
|
-
});
|
|
205
|
-
throw error;
|
|
164
|
+
/**
|
|
165
|
+
* Transform output data (Payload → Better Auth field names)
|
|
166
|
+
*/
|
|
167
|
+
function transformOutput(doc) {
|
|
168
|
+
if (!doc || typeof doc !== 'object')
|
|
169
|
+
return doc;
|
|
170
|
+
const result = { ...doc };
|
|
171
|
+
if ('id' in result && result.id !== undefined) {
|
|
172
|
+
result.id = String(result.id);
|
|
206
173
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const payload = await resolvePayloadClient();
|
|
216
|
-
const collection = getCollection(model);
|
|
217
|
-
const payloadWhere = convertWhere(where);
|
|
218
|
-
const id = extractSingleId(payloadWhere);
|
|
219
|
-
if (id) {
|
|
220
|
-
try {
|
|
221
|
-
const result2 = await payload.findByID({
|
|
222
|
-
collection,
|
|
223
|
-
id: convertId(id),
|
|
224
|
-
depth: 1
|
|
225
|
-
});
|
|
226
|
-
let doc2 = transformOutput(result2);
|
|
227
|
-
if (join) {
|
|
228
|
-
doc2 = await handleJoins(payload, doc2, model, join);
|
|
229
|
-
}
|
|
230
|
-
return doc2;
|
|
231
|
-
} catch (error) {
|
|
232
|
-
if (error instanceof Error && "status" in error && error.status === 404) {
|
|
233
|
-
return null;
|
|
234
|
-
}
|
|
235
|
-
throw error;
|
|
174
|
+
for (const [key, value] of Object.entries(result)) {
|
|
175
|
+
if (value && typeof value === 'object' && 'id' in value) {
|
|
176
|
+
result[`${key}Id`] = String(value.id);
|
|
177
|
+
delete result[key];
|
|
178
|
+
}
|
|
179
|
+
else if (typeof value === 'string' &&
|
|
180
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(value)) {
|
|
181
|
+
result[key] = new Date(value);
|
|
236
182
|
}
|
|
237
|
-
}
|
|
238
|
-
const result = await payload.find({
|
|
239
|
-
collection,
|
|
240
|
-
where: payloadWhere,
|
|
241
|
-
limit: 1,
|
|
242
|
-
depth: 1
|
|
243
|
-
});
|
|
244
|
-
let doc = result.docs[0] ? transformOutput(result.docs[0]) : null;
|
|
245
|
-
if (doc && join) {
|
|
246
|
-
doc = await handleJoins(payload, doc, model, join);
|
|
247
|
-
}
|
|
248
|
-
return doc;
|
|
249
|
-
} catch (error) {
|
|
250
|
-
console.error("[payload-adapter] findOne FAILED", {
|
|
251
|
-
model,
|
|
252
|
-
where,
|
|
253
|
-
error
|
|
254
|
-
});
|
|
255
|
-
throw error;
|
|
256
183
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
sortBy
|
|
264
|
-
}) {
|
|
265
|
-
const payload = await resolvePayloadClient();
|
|
266
|
-
const collection = getCollection(model);
|
|
267
|
-
const payloadWhere = convertWhere(where);
|
|
268
|
-
const result = await payload.find({
|
|
269
|
-
collection,
|
|
270
|
-
where: payloadWhere,
|
|
271
|
-
limit: limit ?? 100,
|
|
272
|
-
page: offset ? Math.floor(offset / (limit ?? 100)) + 1 : 1,
|
|
273
|
-
sort: sortBy ? `${sortBy.direction === "desc" ? "-" : ""}${sortBy.field}` : void 0,
|
|
274
|
-
depth: 1
|
|
275
|
-
});
|
|
276
|
-
return result.docs.map((doc) => transformOutput(doc));
|
|
277
|
-
},
|
|
278
|
-
async update({
|
|
279
|
-
model,
|
|
280
|
-
where,
|
|
281
|
-
update: data
|
|
282
|
-
}) {
|
|
283
|
-
const payload = await resolvePayloadClient();
|
|
284
|
-
const collection = getCollection(model);
|
|
285
|
-
const payloadWhere = convertWhere(where);
|
|
286
|
-
const transformedData = transformInput(data);
|
|
287
|
-
const id = extractSingleId(payloadWhere);
|
|
288
|
-
if (id) {
|
|
289
|
-
const result2 = await payload.update({
|
|
290
|
-
collection,
|
|
291
|
-
id: convertId(id),
|
|
292
|
-
data: transformedData,
|
|
293
|
-
depth: 1
|
|
294
|
-
});
|
|
295
|
-
return transformOutput(result2);
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
function convertId(id) {
|
|
187
|
+
if (idType === 'number' && typeof id === 'string') {
|
|
188
|
+
const num = parseInt(id, 10);
|
|
189
|
+
return isNaN(num) ? id : num;
|
|
296
190
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
where: payloadWhere,
|
|
300
|
-
data: transformedData,
|
|
301
|
-
depth: 1
|
|
302
|
-
});
|
|
303
|
-
return result.docs[0] ? transformOutput(result.docs[0]) : null;
|
|
304
|
-
},
|
|
305
|
-
async updateMany({
|
|
306
|
-
model,
|
|
307
|
-
where,
|
|
308
|
-
update: data
|
|
309
|
-
}) {
|
|
310
|
-
const payload = await resolvePayloadClient();
|
|
311
|
-
const collection = getCollection(model);
|
|
312
|
-
const payloadWhere = convertWhere(where);
|
|
313
|
-
const transformedData = transformInput(data);
|
|
314
|
-
const result = await payload.update({
|
|
315
|
-
collection,
|
|
316
|
-
where: payloadWhere,
|
|
317
|
-
data: transformedData,
|
|
318
|
-
depth: 0
|
|
319
|
-
});
|
|
320
|
-
return result.docs.length;
|
|
321
|
-
},
|
|
322
|
-
async delete({ model, where }) {
|
|
323
|
-
const payload = await resolvePayloadClient();
|
|
324
|
-
const collection = getCollection(model);
|
|
325
|
-
const payloadWhere = convertWhere(where);
|
|
326
|
-
const id = extractSingleId(payloadWhere);
|
|
327
|
-
if (id) {
|
|
328
|
-
await payload.delete({ collection, id: convertId(id) });
|
|
329
|
-
return;
|
|
191
|
+
if (idType === 'text' && typeof id === 'number') {
|
|
192
|
+
return String(id);
|
|
330
193
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
const
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
194
|
+
return id;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Handle join requests - fetch related data
|
|
198
|
+
*/
|
|
199
|
+
async function handleJoins(payload, doc, model, join) {
|
|
200
|
+
const result = { ...doc };
|
|
201
|
+
for (const [joinKey, joinConfig] of Object.entries(join)) {
|
|
202
|
+
if (!joinConfig)
|
|
203
|
+
continue;
|
|
204
|
+
const limit = typeof joinConfig === 'object' ? joinConfig.limit : undefined;
|
|
205
|
+
log('handleJoins', { model, joinKey, docId: doc.id });
|
|
206
|
+
if (model === 'user' && joinKey === 'account') {
|
|
207
|
+
const accounts = await payload.find({
|
|
208
|
+
collection: getCollection('account'),
|
|
209
|
+
where: { user: { equals: doc.id } },
|
|
210
|
+
limit: limit ?? 100,
|
|
211
|
+
depth: 0,
|
|
212
|
+
});
|
|
213
|
+
result[joinKey] = accounts.docs.map((a) => transformOutput(a));
|
|
214
|
+
}
|
|
215
|
+
else if (model === 'session' && joinKey === 'user') {
|
|
216
|
+
const userId = doc.userId;
|
|
217
|
+
if (userId) {
|
|
218
|
+
try {
|
|
219
|
+
const user = await payload.findByID({
|
|
220
|
+
collection: getCollection('user'),
|
|
221
|
+
id: userId,
|
|
222
|
+
depth: 0,
|
|
223
|
+
});
|
|
224
|
+
result[joinKey] = transformOutput(user);
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
result[joinKey] = null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else if (model === 'account' && joinKey === 'user') {
|
|
232
|
+
const userId = doc.userId;
|
|
233
|
+
if (userId) {
|
|
234
|
+
try {
|
|
235
|
+
const user = await payload.findByID({
|
|
236
|
+
collection: getCollection('user'),
|
|
237
|
+
id: userId,
|
|
238
|
+
depth: 0,
|
|
239
|
+
});
|
|
240
|
+
result[joinKey] = transformOutput(user);
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
result[joinKey] = null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
// Extensibility: Add more join patterns here for plugins
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
return (_options) => {
|
|
252
|
+
log('Adapter initialized', { collections: finalCollections });
|
|
253
|
+
return {
|
|
254
|
+
id: 'payload-adapter',
|
|
255
|
+
async create({ model, data, }) {
|
|
256
|
+
const payload = await resolvePayloadClient();
|
|
257
|
+
const collection = getCollection(model);
|
|
258
|
+
const transformedData = transformInput(data);
|
|
259
|
+
log('create', { collection, data: transformedData });
|
|
260
|
+
try {
|
|
261
|
+
const result = await payload.create({
|
|
262
|
+
collection,
|
|
263
|
+
data: transformedData,
|
|
264
|
+
depth: 0,
|
|
265
|
+
});
|
|
266
|
+
return transformOutput(result);
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
console.error('[payload-adapter] create failed:', {
|
|
270
|
+
collection,
|
|
271
|
+
model,
|
|
272
|
+
error: error instanceof Error ? error.message : error,
|
|
273
|
+
});
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
async findOne({ model, where, select: _select, join, }) {
|
|
278
|
+
try {
|
|
279
|
+
const payload = await resolvePayloadClient();
|
|
280
|
+
const collection = getCollection(model);
|
|
281
|
+
const payloadWhere = convertWhere(where);
|
|
282
|
+
const id = extractSingleId(payloadWhere);
|
|
283
|
+
if (id) {
|
|
284
|
+
try {
|
|
285
|
+
const result = await payload.findByID({
|
|
286
|
+
collection,
|
|
287
|
+
id: convertId(id),
|
|
288
|
+
depth: 1,
|
|
289
|
+
});
|
|
290
|
+
let doc = transformOutput(result);
|
|
291
|
+
if (join) {
|
|
292
|
+
doc = await handleJoins(payload, doc, model, join);
|
|
293
|
+
}
|
|
294
|
+
return doc;
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
if (error instanceof Error &&
|
|
298
|
+
'status' in error &&
|
|
299
|
+
error.status === 404) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const result = await payload.find({
|
|
306
|
+
collection,
|
|
307
|
+
where: payloadWhere,
|
|
308
|
+
limit: 1,
|
|
309
|
+
depth: 1,
|
|
310
|
+
});
|
|
311
|
+
let doc = result.docs[0]
|
|
312
|
+
? transformOutput(result.docs[0])
|
|
313
|
+
: null;
|
|
314
|
+
if (doc && join) {
|
|
315
|
+
doc = await handleJoins(payload, doc, model, join);
|
|
316
|
+
}
|
|
317
|
+
return doc;
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
console.error('[payload-adapter] findOne FAILED', {
|
|
321
|
+
model,
|
|
322
|
+
where,
|
|
323
|
+
error,
|
|
324
|
+
});
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
async findMany({ model, where, limit, offset, sortBy, }) {
|
|
329
|
+
const payload = await resolvePayloadClient();
|
|
330
|
+
const collection = getCollection(model);
|
|
331
|
+
const payloadWhere = convertWhere(where);
|
|
332
|
+
const result = await payload.find({
|
|
333
|
+
collection,
|
|
334
|
+
where: payloadWhere,
|
|
335
|
+
limit: limit ?? 100,
|
|
336
|
+
page: offset ? Math.floor(offset / (limit ?? 100)) + 1 : 1,
|
|
337
|
+
sort: sortBy
|
|
338
|
+
? `${sortBy.direction === 'desc' ? '-' : ''}${sortBy.field}`
|
|
339
|
+
: undefined,
|
|
340
|
+
depth: 1,
|
|
341
|
+
});
|
|
342
|
+
return result.docs.map((doc) => transformOutput(doc));
|
|
343
|
+
},
|
|
344
|
+
async update({ model, where, update: data, }) {
|
|
345
|
+
const payload = await resolvePayloadClient();
|
|
346
|
+
const collection = getCollection(model);
|
|
347
|
+
const payloadWhere = convertWhere(where);
|
|
348
|
+
const transformedData = transformInput(data);
|
|
349
|
+
const id = extractSingleId(payloadWhere);
|
|
350
|
+
if (id) {
|
|
351
|
+
const result = await payload.update({
|
|
352
|
+
collection,
|
|
353
|
+
id: convertId(id),
|
|
354
|
+
data: transformedData,
|
|
355
|
+
depth: 1,
|
|
356
|
+
});
|
|
357
|
+
return transformOutput(result);
|
|
358
|
+
}
|
|
359
|
+
const result = await payload.update({
|
|
360
|
+
collection,
|
|
361
|
+
where: payloadWhere,
|
|
362
|
+
data: transformedData,
|
|
363
|
+
depth: 1,
|
|
364
|
+
});
|
|
365
|
+
return result.docs[0] ? transformOutput(result.docs[0]) : null;
|
|
366
|
+
},
|
|
367
|
+
async updateMany({ model, where, update: data, }) {
|
|
368
|
+
const payload = await resolvePayloadClient();
|
|
369
|
+
const collection = getCollection(model);
|
|
370
|
+
const payloadWhere = convertWhere(where);
|
|
371
|
+
const transformedData = transformInput(data);
|
|
372
|
+
const result = await payload.update({
|
|
373
|
+
collection,
|
|
374
|
+
where: payloadWhere,
|
|
375
|
+
data: transformedData,
|
|
376
|
+
depth: 0,
|
|
377
|
+
});
|
|
378
|
+
return result.docs.length;
|
|
379
|
+
},
|
|
380
|
+
async delete({ model, where }) {
|
|
381
|
+
const payload = await resolvePayloadClient();
|
|
382
|
+
const collection = getCollection(model);
|
|
383
|
+
const payloadWhere = convertWhere(where);
|
|
384
|
+
const id = extractSingleId(payloadWhere);
|
|
385
|
+
if (id) {
|
|
386
|
+
await payload.delete({ collection, id: convertId(id) });
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
await payload.delete({ collection, where: payloadWhere });
|
|
390
|
+
},
|
|
391
|
+
async deleteMany({ model, where, }) {
|
|
392
|
+
const payload = await resolvePayloadClient();
|
|
393
|
+
const collection = getCollection(model);
|
|
394
|
+
const payloadWhere = convertWhere(where);
|
|
395
|
+
const result = await payload.delete({
|
|
396
|
+
collection,
|
|
397
|
+
where: payloadWhere,
|
|
398
|
+
});
|
|
399
|
+
return result.docs.length;
|
|
400
|
+
},
|
|
401
|
+
async count({ model, where, }) {
|
|
402
|
+
const payload = await resolvePayloadClient();
|
|
403
|
+
const collection = getCollection(model);
|
|
404
|
+
const payloadWhere = convertWhere(where);
|
|
405
|
+
const result = await payload.count({
|
|
406
|
+
collection,
|
|
407
|
+
where: payloadWhere,
|
|
408
|
+
});
|
|
409
|
+
return result.totalDocs;
|
|
410
|
+
},
|
|
411
|
+
async transaction(callback) {
|
|
412
|
+
return callback(this);
|
|
413
|
+
},
|
|
414
|
+
};
|
|
362
415
|
};
|
|
363
|
-
};
|
|
364
416
|
}
|
|
365
|
-
|
|
366
|
-
exports.payloadAdapter = payloadAdapter;
|
|
367
|
-
//# sourceMappingURL=index.js.map
|
|
368
417
|
//# sourceMappingURL=index.js.map
|