-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathobjectql-adapter.ts
More file actions
475 lines (427 loc) · 20.7 KB
/
Copy pathobjectql-adapter.ts
File metadata and controls
475 lines (427 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type { IDataEngine } from '@objectstack/core';
import { createAdapterFactory } from 'better-auth/adapters';
import type { CleanedWhere } from 'better-auth/adapters';
import { SystemObjectName } from '@objectstack/spec/system';
/**
* Mapping from better-auth model names to ObjectStack protocol object names.
*
* better-auth uses hardcoded model names ('user', 'session', 'account', 'verification')
* while ObjectStack's protocol layer uses `sys_` prefixed names. This map bridges the two.
*/
export const AUTH_MODEL_TO_PROTOCOL: Record<string, string> = {
user: SystemObjectName.USER,
session: SystemObjectName.SESSION,
account: SystemObjectName.ACCOUNT,
verification: SystemObjectName.VERIFICATION,
// Plugin models. `@better-auth/sso` and `@better-auth/scim` both hardcode
// their model name and accept NO `schema` option (verified vs 1.6.2x — no
// mergeSchema, runtime never reads options.schema), so the table name is
// bridged here and `createObjectQLAdapterFactory` (below) auto-maps their
// camelCase fields to snake_case (oidcConfig→oidc_config, scimToken→
// scim_token, …) on every CRUD op via resolveProtocolName. Off by default
// (OS_SSO_ENABLED / OS_SCIM_ENABLED). See ADR-0024 / ADR-0071.
ssoProvider: 'sys_sso_provider',
scimProvider: 'sys_scim_provider',
};
/**
* Resolve a better-auth model name to the ObjectStack protocol object name.
* Falls back to the original model name for custom / non-core models.
*/
export function resolveProtocolName(model: string): string {
return AUTH_MODEL_TO_PROTOCOL[model] ?? model;
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
/**
* better-auth datetime columns (snake_case) per model.
*
* When the underlying driver stored these as JavaScript `Date` objects
* (legacy behaviour), the libsql HTTP transport coerces the value to a REAL
* column and round-trips it as a string like `"1779497911249.0"`. That
* string is not a valid Date string (it has a trailing `.0`), so
* `new Date(...)` produces `Invalid Date` and better-auth's client treats
* the session as expired — causing a login/redirect loop.
*
* We normalise these legacy values back to ISO strings on **read** so the
* factory's `supportsDates: false` parser can turn them into real Date
* objects. New writes always go through better-auth's own
* `Date → ISO string` conversion (because we declare `supportsDates: false`
* below), so no further `.0`-suffixed values will ever be created.
*/
const LEGACY_DATETIME_FIELDS_BY_MODEL: Record<string, string[]> = {
user: ['created_at', 'updated_at'],
session: ['expires_at', 'created_at', 'updated_at'],
account: [
'access_token_expires_at',
'refresh_token_expires_at',
'created_at',
'updated_at',
],
verification: ['expires_at', 'created_at', 'updated_at'],
};
const NUMERIC_STRING_RE = /^-?\d+(\.\d+)?$/;
/**
* If `value` looks like a stringified epoch-ms (optionally with `.0`),
* convert it to an ISO 8601 string. Otherwise return it unchanged.
*/
function normaliseLegacyDate(value: unknown): unknown {
if (typeof value !== 'string') return value;
if (!NUMERIC_STRING_RE.test(value)) return value;
const n = parseFloat(value);
if (!Number.isFinite(n)) return value;
// Heuristic: epoch milliseconds are at least 10 digits (year 2001+).
if (Math.abs(n) < 1e10) return value;
const d = new Date(n);
if (Number.isNaN(d.getTime())) return value;
return d.toISOString();
}
/**
* Walk a record and rewrite any legacy `.0`-suffixed datetime values
* into ISO strings. Mutates and returns the record.
*/
function normaliseLegacyDates<T extends Record<string, any> | null | undefined>(
model: string,
record: T,
): T {
if (!record) return record;
const cols = LEGACY_DATETIME_FIELDS_BY_MODEL[model];
if (!cols) return record;
for (const col of cols) {
if (col in record) {
(record as Record<string, unknown>)[col] = normaliseLegacyDate(
(record as Record<string, unknown>)[col],
);
}
}
return record;
}
/**
* Convert better-auth where clause to ObjectQL query format.
*
* Field names in the incoming {@link CleanedWhere} are expected to already be
* in snake_case (transformed by `createAdapterFactory`).
*/
function convertWhere(where: CleanedWhere[]): Record<string, any> {
const filter: Record<string, any> = {};
for (const condition of where) {
const fieldName = condition.field;
if (condition.operator === 'eq') {
filter[fieldName] = condition.value;
} else if (condition.operator === 'ne') {
filter[fieldName] = { $ne: condition.value };
} else if (condition.operator === 'in') {
filter[fieldName] = { $in: condition.value };
} else if (condition.operator === 'gt') {
filter[fieldName] = { $gt: condition.value };
} else if (condition.operator === 'gte') {
filter[fieldName] = { $gte: condition.value };
} else if (condition.operator === 'lt') {
filter[fieldName] = { $lt: condition.value };
} else if (condition.operator === 'lte') {
filter[fieldName] = { $lte: condition.value };
} else if (condition.operator === 'contains') {
filter[fieldName] = { $regex: condition.value };
}
}
return filter;
}
// ---------------------------------------------------------------------------
// Adapter factory
// ---------------------------------------------------------------------------
/**
* Wrap a data engine so its READ operations (find / findOne / count) run as
* SYSTEM reads — injecting `context.isSystem: true` (merged; any caller-supplied
* context still wins on other keys). better-auth has already authenticated the
* session and scopes every query by its OWN where-clauses (e.g. member.userId =
* session.user). A deployment's control-plane org-scope read hook, however, keys
* off the CALLER's user id, and these adapter reads carry no caller context — so
* without isSystem that hook filters sys_member / sys_organization reads down to
* zero and `organization.list()` returns no orgs for a real member. Writes pass
* through untouched (org-scope is a read-only hook).
*/
export function withSystemReadContext(engine: IDataEngine): IDataEngine {
const e = engine as any;
const asSystem = (q: any) => ({ ...(q ?? {}), context: { isSystem: true, ...(q?.context ?? {}) } });
return {
insert: (m: string, d: any) => e.insert(m, d),
update: (m: string, d: any) => e.update(m, d),
delete: (m: string, q?: any) => e.delete(m, q),
find: (m: string, q?: any) => e.find(m, asSystem(q)),
findOne: (m: string, q?: any) => e.findOne(m, asSystem(q)),
count: (m: string, q?: any) => e.count(m, asSystem(q)),
} as unknown as IDataEngine;
}
/**
* Create an ObjectQL adapter **factory** for better-auth.
*
* Uses better-auth's official `createAdapterFactory` so that model-name and
* field-name transformations (declared via `modelName` / `fields` in the
* betterAuth config) are applied **automatically** before any data reaches
* ObjectQL. This eliminates the need for manual camelCase ↔ snake_case
* conversion inside the adapter.
*
* The returned value is an `AdapterFactory` – a function of type
* `(options: BetterAuthOptions) => DBAdapter` – which is the shape expected
* by `betterAuth({ database: … })`.
*
* @param dataEngine - ObjectQL data engine instance
* @returns better-auth AdapterFactory
*/
export function createObjectQLAdapterFactory(rawDataEngine: IDataEngine) {
const dataEngine = withSystemReadContext(rawDataEngine);
// Field-name bridging for better-auth plugins that expose NO `schema` option
// (e.g. @better-auth/sso): when a model is remapped via AUTH_MODEL_TO_PROTOCOL,
// its camelCase model fields are also converted to snake_case columns on the
// way in and back to camelCase on the way out. SCOPED by `objectName !== model`
// so core / schema-declared models are byte-for-byte untouched.
const camelToSnake = (s: string): string => s.replace(/[A-Z]/g, (c) => '_' + c.toLowerCase());
const snakeToCamel = (s: string): string => s.replace(/_([a-z])/g, (_m, c) => c.toUpperCase());
const remapKeys = (obj: Record<string, any>, fn: (k: string) => string): Record<string, any> => {
const out: Record<string, any> = {};
for (const k of Object.keys(obj)) out[fn(k)] = obj[k];
return out;
};
const remapWhere = (where: CleanedWhere[]): CleanedWhere[] =>
where.map((c) => ({ ...c, field: camelToSnake(c.field) }));
return createAdapterFactory({
config: {
adapterId: 'objectql',
// We let better-auth handle Date↔string and boolean↔0/1 conversion so
// that values land in the underlying SQL driver as primitive strings
// and integers. Some drivers (e.g. libsql over the HTTP transport)
// otherwise mangle `Date` objects into `"<epoch>.0"` strings that
// break the client-side session parser.
supportsBooleans: false,
supportsDates: false,
supportsJSON: true,
},
adapter: () => ({
create: async <T extends Record<string, any>>(
{ model, data, select: _select }: { model: string; data: T; select?: string[] },
): Promise<T> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const result = await dataEngine.insert(objectName, bridged ? remapKeys(data, camelToSnake) : data);
const norm = normaliseLegacyDates(model, result);
return (bridged ? remapKeys(norm, snakeToCamel) : norm) as T;
},
findOne: async <T>(
{ model, where, select, join: _join }: { model: string; where: CleanedWhere[]; select?: string[]; join?: any },
): Promise<T | null> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
const fields = bridged && select ? select.map(camelToSnake) : select;
const result = await dataEngine.findOne(objectName, { where: filter, fields });
if (!result) return null;
const norm = normaliseLegacyDates(model, result);
return (bridged ? remapKeys(norm, snakeToCamel) : norm) as T;
},
findMany: async <T>(
{ model, where, limit, offset, sortBy, join: _join }: {
model: string; where?: CleanedWhere[]; limit: number;
offset?: number; sortBy?: { field: string; direction: 'asc' | 'desc' }; join?: any;
},
): Promise<T[]> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = where ? convertWhere(bridged ? remapWhere(where) : where) : {};
const orderBy = sortBy
? [{ field: bridged ? camelToSnake(sortBy.field) : sortBy.field, order: sortBy.direction as 'asc' | 'desc' }]
: undefined;
const results = await dataEngine.find(objectName, {
where: filter,
limit: limit || 100,
offset,
orderBy,
});
return results.map((r) => {
const norm = normaliseLegacyDates(model, r as Record<string, any>);
return bridged ? remapKeys(norm, snakeToCamel) : norm;
}) as T[];
},
count: async (
{ model, where }: { model: string; where?: CleanedWhere[] },
): Promise<number> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = where ? convertWhere(bridged ? remapWhere(where) : where) : {};
return await dataEngine.count(objectName, { where: filter });
},
update: async <T>(
{ model, where, update }: { model: string; where: CleanedWhere[]; update: T },
): Promise<T | null> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
// ObjectQL requires an ID for updates – find the record first
const record = await dataEngine.findOne(objectName, { where: filter });
if (!record) return null;
const patch = bridged ? remapKeys(update as any, camelToSnake) : (update as any);
const result = await dataEngine.update(objectName, { ...patch, id: record.id });
if (!result) return null;
const norm = normaliseLegacyDates(model, result);
return (bridged ? remapKeys(norm, snakeToCamel) : norm) as T;
},
updateMany: async (
{ model, where, update }: { model: string; where: CleanedWhere[]; update: Record<string, any> },
): Promise<number> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
// Sequential updates: ObjectQL requires an ID per update
const records = await dataEngine.find(objectName, { where: filter });
const patch = bridged ? remapKeys(update, camelToSnake) : update;
for (const record of records) {
await dataEngine.update(objectName, { ...patch, id: record.id });
}
return records.length;
},
delete: async (
{ model, where }: { model: string; where: CleanedWhere[] },
): Promise<void> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
const record = await dataEngine.findOne(objectName, { where: filter });
if (!record) return;
await dataEngine.delete(objectName, { where: { id: record.id } });
},
deleteMany: async (
{ model, where }: { model: string; where: CleanedWhere[] },
): Promise<number> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
const records = await dataEngine.find(objectName, { where: filter });
for (const record of records) {
await dataEngine.delete(objectName, { where: { id: record.id } });
}
return records.length;
},
// Atomic single-row consume (better-auth 1.7+). ObjectQL has no native
// `DELETE ... RETURNING`, so we find the single guarded row, delete it,
// and return the consumed record — a find-then-write mirror of `delete`.
consumeOne: async <T>(
{ model, where }: { model: string; where: CleanedWhere[] },
): Promise<T | null> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
const record = await dataEngine.findOne(objectName, { where: filter });
if (!record) return null;
await dataEngine.delete(objectName, { where: { id: record.id } });
const norm = normaliseLegacyDates(model, record);
return (bridged ? remapKeys(norm, snakeToCamel) : norm) as T;
},
// Guarded counter mutation (better-auth 1.7+). ObjectQL has no native
// `SET n = n + $delta ... RETURNING`, so we read the guarded row, apply
// `field = field + delta` for each `increment` entry (negative deltas
// decrement) plus any absolute `set` values, and write it back. `where`
// is both selector and guard, so a non-matching guard returns null.
incrementOne: async <T>(
{ model, where, increment, set }: {
model: string; where: CleanedWhere[];
increment: Record<string, number>; set?: Record<string, unknown>;
},
): Promise<T | null> => {
const objectName = resolveProtocolName(model);
const bridged = objectName !== model;
const filter = convertWhere(bridged ? remapWhere(where) : where);
const record = await dataEngine.findOne(objectName, { where: filter });
if (!record) return null;
const patch: Record<string, any> = {};
for (const [field, delta] of Object.entries(increment)) {
const col = bridged ? camelToSnake(field) : field;
const current = Number((record as Record<string, any>)[col] ?? 0);
patch[col] = current + delta;
}
if (set) Object.assign(patch, bridged ? remapKeys(set, camelToSnake) : set);
const result = await dataEngine.update(objectName, { ...patch, id: record.id });
if (!result) return null;
const norm = normaliseLegacyDates(model, result);
return (bridged ? remapKeys(norm, snakeToCamel) : norm) as T;
},
}),
});
}
// ---------------------------------------------------------------------------
// Legacy adapter (kept for backward compatibility)
// ---------------------------------------------------------------------------
/**
* Create a raw ObjectQL adapter for better-auth (without factory wrapping).
*
* > **Prefer {@link createObjectQLAdapterFactory}** for production use.
* > The factory version leverages `createAdapterFactory` and automatically
* > handles model-name + field-name transformations declared in the
* > better-auth config.
*
* This function is retained for direct / low-level usage where callers
* manage field-name conversion themselves.
*
* @param dataEngine - ObjectQL data engine instance
* @returns better-auth CustomAdapter (raw, without factory wrapping)
*/
export function createObjectQLAdapter(rawDataEngine: IDataEngine) {
const dataEngine = withSystemReadContext(rawDataEngine);
return {
create: async <T extends Record<string, any>>({ model, data, select: _select }: { model: string; data: T; select?: string[] }): Promise<T> => {
const objectName = resolveProtocolName(model);
const result = await dataEngine.insert(objectName, data);
return result as T;
},
findOne: async <T>({ model, where, select, join: _join }: { model: string; where: CleanedWhere[]; select?: string[]; join?: any }): Promise<T | null> => {
const objectName = resolveProtocolName(model);
const filter = convertWhere(where);
const result = await dataEngine.findOne(objectName, { where: filter, fields: select });
return result ? result as T : null;
},
findMany: async <T>({ model, where, limit, offset, sortBy, join: _join }: { model: string; where?: CleanedWhere[]; limit: number; offset?: number; sortBy?: { field: string; direction: 'asc' | 'desc' }; join?: any }): Promise<T[]> => {
const objectName = resolveProtocolName(model);
const filter = where ? convertWhere(where) : {};
const orderBy = sortBy ? [{ field: sortBy.field, order: sortBy.direction as 'asc' | 'desc' }] : undefined;
const results = await dataEngine.find(objectName, { where: filter, limit: limit || 100, offset, orderBy });
return results as T[];
},
count: async ({ model, where }: { model: string; where?: CleanedWhere[] }): Promise<number> => {
const objectName = resolveProtocolName(model);
const filter = where ? convertWhere(where) : {};
return await dataEngine.count(objectName, { where: filter });
},
update: async <T>({ model, where, update }: { model: string; where: CleanedWhere[]; update: Record<string, any> }): Promise<T | null> => {
const objectName = resolveProtocolName(model);
const filter = convertWhere(where);
const record = await dataEngine.findOne(objectName, { where: filter });
if (!record) return null;
const result = await dataEngine.update(objectName, { ...update, id: record.id });
return result ? result as T : null;
},
updateMany: async ({ model, where, update }: { model: string; where: CleanedWhere[]; update: Record<string, any> }): Promise<number> => {
const objectName = resolveProtocolName(model);
const filter = convertWhere(where);
const records = await dataEngine.find(objectName, { where: filter });
for (const record of records) {
await dataEngine.update(objectName, { ...update, id: record.id });
}
return records.length;
},
delete: async ({ model, where }: { model: string; where: CleanedWhere[] }): Promise<void> => {
const objectName = resolveProtocolName(model);
const filter = convertWhere(where);
const record = await dataEngine.findOne(objectName, { where: filter });
if (!record) return;
await dataEngine.delete(objectName, { where: { id: record.id } });
},
deleteMany: async ({ model, where }: { model: string; where: CleanedWhere[] }): Promise<number> => {
const objectName = resolveProtocolName(model);
const filter = convertWhere(where);
const records = await dataEngine.find(objectName, { where: filter });
for (const record of records) {
await dataEngine.delete(objectName, { where: { id: record.id } });
}
return records.length;
},
};
}