@better-auth/kysely-adapter 1.7.5 → 1.7.6
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/dist/{bun-sqlite-dialect-C7ftEUSI.mjs → bun-sqlite-dialect-DoOVo4ja.mjs} +3 -41
- package/dist/{d1-sqlite-dialect-D4qp4-wW.mjs → d1-sqlite-dialect-Cru74LbR.mjs} +13 -24
- package/dist/index.mjs +52 -50
- package/dist/node-sqlite-dialect.mjs +3 -41
- package/dist/sqlite-introspector-DSCAP82F.mjs +90 -0
- package/package.json +3 -3
- package/dist/kysely-migration-tables-DFfmKhq-.mjs +0 -17
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CompiledQuery, DefaultQueryCompiler
|
|
1
|
+
import { t as createSqliteIntrospector } from "./sqlite-introspector-DSCAP82F.mjs";
|
|
2
|
+
import { CompiledQuery, DefaultQueryCompiler } from "kysely";
|
|
3
3
|
//#region src/bun-sqlite-dialect.ts
|
|
4
4
|
var BunSqliteAdapter = class {
|
|
5
5
|
get supportsCreateIfNotExists() {
|
|
@@ -87,44 +87,6 @@ var ConnectionMutex = class {
|
|
|
87
87
|
resolve?.();
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
|
-
var BunSqliteIntrospector = class {
|
|
91
|
-
#db;
|
|
92
|
-
constructor(db) {
|
|
93
|
-
this.#db = db;
|
|
94
|
-
}
|
|
95
|
-
async getSchemas() {
|
|
96
|
-
return [];
|
|
97
|
-
}
|
|
98
|
-
async getTables(options = { withInternalKyselyTables: false }) {
|
|
99
|
-
let query = this.#db.selectFrom("sqlite_schema").where("type", "=", "table").where("name", "not like", "sqlite_%").select("name").$castTo();
|
|
100
|
-
if (!options.withInternalKyselyTables) query = query.where("name", "!=", DEFAULT_MIGRATION_TABLE).where("name", "!=", DEFAULT_MIGRATION_LOCK_TABLE);
|
|
101
|
-
const tables = await query.execute();
|
|
102
|
-
return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
|
|
103
|
-
}
|
|
104
|
-
async getMetadata(options) {
|
|
105
|
-
return { tables: await this.getTables(options) };
|
|
106
|
-
}
|
|
107
|
-
async #getTableMetadata(table) {
|
|
108
|
-
const db = this.#db;
|
|
109
|
-
const autoIncrementCol = (await db.selectFrom("sqlite_master").where("name", "=", table).select("sql").$castTo().execute())[0]?.sql?.split(/[\(\),]/)?.find((it) => it.toLowerCase().includes("autoincrement"))?.split(/\s+/)?.[0]?.replace(/["`]/g, "");
|
|
110
|
-
return {
|
|
111
|
-
name: table,
|
|
112
|
-
columns: (await db.selectFrom(sql`pragma_table_info(${table})`.as("table_info")).select([
|
|
113
|
-
"name",
|
|
114
|
-
"type",
|
|
115
|
-
"notnull",
|
|
116
|
-
"dflt_value"
|
|
117
|
-
]).execute()).map((col) => ({
|
|
118
|
-
name: col.name,
|
|
119
|
-
dataType: col.type,
|
|
120
|
-
isNullable: !col.notnull,
|
|
121
|
-
isAutoIncrementing: col.name === autoIncrementCol,
|
|
122
|
-
hasDefaultValue: col.dflt_value != null
|
|
123
|
-
})),
|
|
124
|
-
isView: false
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
90
|
var BunSqliteQueryCompiler = class extends DefaultQueryCompiler {
|
|
129
91
|
getCurrentParameterPlaceholder() {
|
|
130
92
|
return "?";
|
|
@@ -154,7 +116,7 @@ var BunSqliteDialect = class {
|
|
|
154
116
|
return new BunSqliteAdapter();
|
|
155
117
|
}
|
|
156
118
|
createIntrospector(db) {
|
|
157
|
-
return
|
|
119
|
+
return createSqliteIntrospector(db);
|
|
158
120
|
}
|
|
159
121
|
};
|
|
160
122
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as DEFAULT_MIGRATION_LOCK_TABLE, i as toSqliteTableMetadata, o as DEFAULT_MIGRATION_TABLE, r as sqliteIntegerPrimaryKeyColumn } from "./sqlite-introspector-DSCAP82F.mjs";
|
|
2
2
|
import { SqliteAdapter, SqliteQueryCompiler } from "kysely";
|
|
3
3
|
//#region src/d1-sqlite-dialect.ts
|
|
4
4
|
function quoteSqliteStringLiteral(value) {
|
|
@@ -86,35 +86,24 @@ var D1SqliteIntrospector = class {
|
|
|
86
86
|
return [];
|
|
87
87
|
}
|
|
88
88
|
async getTables(options = { withInternalKyselyTables: false }) {
|
|
89
|
-
let query = this.#db.selectFrom("sqlite_master").where("type", "in", ["table", "view"]).where("name", "not like", "sqlite_%").where("name", "not like", "_cf_%").select([
|
|
90
|
-
"name",
|
|
91
|
-
"type",
|
|
92
|
-
"sql"
|
|
93
|
-
]).$castTo();
|
|
89
|
+
let query = this.#db.selectFrom("sqlite_master").where("type", "in", ["table", "view"]).where("name", "not like", "sqlite_%").where("name", "not like", "_cf_%").select(["name", "type"]).$castTo();
|
|
94
90
|
if (!options.withInternalKyselyTables) query = query.where("name", "!=", DEFAULT_MIGRATION_TABLE).where("name", "!=", DEFAULT_MIGRATION_LOCK_TABLE);
|
|
95
91
|
const tables = await query.execute();
|
|
96
92
|
if (tables.length === 0) return [];
|
|
97
93
|
const statements = tables.map((table) => this.#d1.prepare("SELECT * FROM pragma_table_info(?)").bind(table.name));
|
|
98
94
|
const batchResults = await this.#d1.batch(statements);
|
|
95
|
+
const rowidCandidates = tables.flatMap((table, index) => {
|
|
96
|
+
const column = sqliteIntegerPrimaryKeyColumn(batchResults[index]?.results ?? []);
|
|
97
|
+
return column ? [{
|
|
98
|
+
index,
|
|
99
|
+
tableName: table.name,
|
|
100
|
+
column
|
|
101
|
+
}] : [];
|
|
102
|
+
});
|
|
103
|
+
const indexResults = rowidCandidates.length ? await this.#d1.batch(rowidCandidates.map(({ tableName }) => this.#d1.prepare(`PRAGMA index_list(${quoteSqliteStringLiteral(tableName)})`))) : [];
|
|
104
|
+
const generatedColumns = new Map(rowidCandidates.filter((_, index) => !indexResults[index]?.results?.some((row) => row.origin === "pk")).map(({ index, column }) => [index, column]));
|
|
99
105
|
return tables.map((table, index) => {
|
|
100
|
-
|
|
101
|
-
let autoIncrementCol = table.sql?.split(/[(),]/)?.find((it) => it.toLowerCase().includes("autoincrement"))?.split(/\s+/)?.filter(Boolean)?.[0]?.replace(/["`]/g, "");
|
|
102
|
-
if (!autoIncrementCol) {
|
|
103
|
-
const pkCols = columnInfo.filter((r) => r.pk > 0);
|
|
104
|
-
const singlePk = pkCols.length === 1 ? pkCols[0] : void 0;
|
|
105
|
-
if (singlePk && singlePk.type.toLowerCase() === "integer") autoIncrementCol = singlePk.name;
|
|
106
|
-
}
|
|
107
|
-
return {
|
|
108
|
-
name: table.name,
|
|
109
|
-
isView: table.type === "view",
|
|
110
|
-
columns: columnInfo.map((col) => ({
|
|
111
|
-
name: col.name,
|
|
112
|
-
dataType: col.type,
|
|
113
|
-
isNullable: !col.notnull,
|
|
114
|
-
isAutoIncrementing: col.name === autoIncrementCol,
|
|
115
|
-
hasDefaultValue: col.dflt_value != null
|
|
116
|
-
}))
|
|
117
|
-
};
|
|
106
|
+
return toSqliteTableMetadata(table, batchResults[index]?.results ?? [], generatedColumns.get(index));
|
|
118
107
|
});
|
|
119
108
|
}
|
|
120
109
|
async getMetadata(options) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { n as introspectSqliteTables } from "./sqlite-introspector-DSCAP82F.mjs";
|
|
1
2
|
import { BetterAuthError } from "@better-auth/core/error";
|
|
2
3
|
import { ColumnNode, Kysely, MssqlDialect, MysqlDialect, PostgresDialect, ReferenceNode, SelectQueryNode, SqliteDialect, TableNode, sql } from "kysely";
|
|
3
4
|
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
@@ -77,7 +78,7 @@ const createKyselyAdapter = async (config) => {
|
|
|
77
78
|
transaction = true;
|
|
78
79
|
}
|
|
79
80
|
if ("fileControl" in db) {
|
|
80
|
-
const { BunSqliteDialect } = await import("./bun-sqlite-dialect-
|
|
81
|
+
const { BunSqliteDialect } = await import("./bun-sqlite-dialect-DoOVo4ja.mjs");
|
|
81
82
|
dialect = new BunSqliteDialect({ database: db });
|
|
82
83
|
transaction = true;
|
|
83
84
|
}
|
|
@@ -100,7 +101,7 @@ const createKyselyAdapter = async (config) => {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
if ("batch" in db && "exec" in db && "prepare" in db) {
|
|
103
|
-
const { createD1IndexIntrospector, D1SqliteDialect } = await import("./d1-sqlite-dialect-
|
|
104
|
+
const { createD1IndexIntrospector, D1SqliteDialect } = await import("./d1-sqlite-dialect-Cru74LbR.mjs");
|
|
104
105
|
dialect = new D1SqliteDialect({ database: db });
|
|
105
106
|
introspectIndexes = createD1IndexIntrospector(db);
|
|
106
107
|
transaction = false;
|
|
@@ -249,6 +250,7 @@ async function schemaSearchPath(db, dbType) {
|
|
|
249
250
|
*/
|
|
250
251
|
async function findSchemaProblems(db, dbType, expected) {
|
|
251
252
|
const physical = toPhysicalSchema(db, expected);
|
|
253
|
+
if (dbType === "sqlite") return diffSchema(physical, toIntrospectedTables(await introspectSqliteTables(db, Object.entries(physical).flatMap(([name, table]) => table.disableMigrations ? [] : [name]))));
|
|
252
254
|
return db.connection().execute(async (connection) => {
|
|
253
255
|
const searchPath = await schemaSearchPath(connection, dbType);
|
|
254
256
|
const tables = toIntrospectedTables(await connection.introspection.getTables());
|
|
@@ -273,8 +275,8 @@ const kyselyAdapter = (db, config) => {
|
|
|
273
275
|
const selectAllJoins = (join) => {
|
|
274
276
|
const allSelects = [];
|
|
275
277
|
const allSelectsStr = [];
|
|
276
|
-
if (join) for (const [joinModel,
|
|
277
|
-
const fields = schema[getDefaultModelName(joinModel)]?.fields;
|
|
278
|
+
if (join) for (const [joinModel, joinConfig] of Object.entries(join)) {
|
|
279
|
+
const fields = schema[joinConfig.modelKey ?? getDefaultModelName(joinModel)]?.fields;
|
|
278
280
|
const [_joinModelSchema, joinModelName] = joinModel.includes(".") ? joinModel.split(".") : [void 0, joinModel];
|
|
279
281
|
if (!fields) continue;
|
|
280
282
|
fields.id = { type: "string" };
|
|
@@ -292,7 +294,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
292
294
|
allSelects
|
|
293
295
|
};
|
|
294
296
|
};
|
|
295
|
-
const withReturning = async (values, builder, model, where) => {
|
|
297
|
+
const withReturning = async (values, builder, model, modelKey, where) => {
|
|
296
298
|
if (config?.type === "mysql") {
|
|
297
299
|
if (where.length > 0) {
|
|
298
300
|
const updateResult = await builder.executeTakeFirst();
|
|
@@ -311,28 +313,28 @@ const kyselyAdapter = (db, config) => {
|
|
|
311
313
|
reselectValue = values[reselectField] !== void 0 ? values[reselectField] : where[0].value;
|
|
312
314
|
} else return null;
|
|
313
315
|
return await db.selectFrom(model).selectAll().where(getFieldName({
|
|
314
|
-
model,
|
|
316
|
+
model: modelKey,
|
|
315
317
|
field: reselectField
|
|
316
318
|
}), reselectValue === null ? "is" : "=", reselectValue).limit(1).executeTakeFirst();
|
|
317
319
|
}
|
|
318
320
|
await builder.execute();
|
|
319
321
|
const fetchInserted = async (trx) => {
|
|
320
322
|
if (values.id) return await trx.selectFrom(model).selectAll().where(getFieldName({
|
|
321
|
-
model,
|
|
323
|
+
model: modelKey,
|
|
322
324
|
field: "id"
|
|
323
325
|
}), "=", values.id).limit(1).executeTakeFirst();
|
|
324
326
|
if (options.advanced?.database?.generateId === "serial") {
|
|
325
327
|
const lastId = (await sql`SELECT LAST_INSERT_ID() as id`.execute(trx)).rows[0]?.id;
|
|
326
328
|
if (lastId) return await trx.selectFrom(model).selectAll().where(getFieldName({
|
|
327
|
-
model,
|
|
329
|
+
model: modelKey,
|
|
328
330
|
field: "id"
|
|
329
331
|
}), "=", lastId).limit(1).executeTakeFirst();
|
|
330
332
|
}
|
|
331
|
-
const modelSchema = schema[
|
|
333
|
+
const modelSchema = schema[modelKey]?.fields;
|
|
332
334
|
if (modelSchema) for (const [fieldKey, fieldAttr] of Object.entries(modelSchema)) {
|
|
333
335
|
if (!fieldAttr.unique) continue;
|
|
334
336
|
const dbFieldName = getFieldName({
|
|
335
|
-
model,
|
|
337
|
+
model: modelKey,
|
|
336
338
|
field: fieldKey
|
|
337
339
|
});
|
|
338
340
|
const val = values[dbFieldName];
|
|
@@ -359,7 +361,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
359
361
|
if (config?.type === "mssql") return await builder.outputAll("inserted").executeTakeFirst();
|
|
360
362
|
return await builder.returningAll().executeTakeFirst();
|
|
361
363
|
};
|
|
362
|
-
function convertWhereClause(model, w) {
|
|
364
|
+
function convertWhereClause(model, modelKey, w) {
|
|
363
365
|
if (!w) return {
|
|
364
366
|
and: null,
|
|
365
367
|
or: null
|
|
@@ -372,7 +374,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
372
374
|
const { field: _field, value: _value, operator = "eq", connector = "AND", mode = "sensitive" } = condition;
|
|
373
375
|
const value = _value;
|
|
374
376
|
const field = getFieldName({
|
|
375
|
-
model,
|
|
377
|
+
model: modelKey,
|
|
376
378
|
field: _field
|
|
377
379
|
});
|
|
378
380
|
const isInsensitive = mode === "insensitive" && (typeof value === "string" || Array.isArray(value) && value.every((v) => typeof v === "string"));
|
|
@@ -440,13 +442,13 @@ const kyselyAdapter = (db, config) => {
|
|
|
440
442
|
for (const currentRow of rows) {
|
|
441
443
|
const mainModelFields = {};
|
|
442
444
|
const joinedModelFields = {};
|
|
443
|
-
for (const [joinModel] of Object.entries(joinConfig)) joinedModelFields[
|
|
445
|
+
for (const [joinModel] of Object.entries(joinConfig)) joinedModelFields[joinModel] = {};
|
|
444
446
|
for (const [key, value] of Object.entries(currentRow)) {
|
|
445
447
|
const keyStr = String(key);
|
|
446
448
|
let assigned = false;
|
|
447
449
|
for (const { joinModel, fieldName, joinModelRef } of allSelectsStr) if (keyStr === `_joined_${joinModelRef}_${fieldName}` || keyStr === `_Joined${capitalizeFirstLetter(joinModelRef)}${capitalizeFirstLetter(fieldName)}`) {
|
|
448
|
-
joinedModelFields[
|
|
449
|
-
model: joinModel,
|
|
450
|
+
joinedModelFields[joinModel][getFieldName({
|
|
451
|
+
model: joinConfig[joinModel]?.modelKey ?? joinModel,
|
|
450
452
|
field: fieldName
|
|
451
453
|
})] = value;
|
|
452
454
|
assigned = true;
|
|
@@ -458,22 +460,22 @@ const kyselyAdapter = (db, config) => {
|
|
|
458
460
|
if (!mainId) continue;
|
|
459
461
|
if (!groupedByMainId.has(mainId)) {
|
|
460
462
|
const entry = { ...mainModelFields };
|
|
461
|
-
for (const [joinModel, joinAttr] of Object.entries(joinConfig)) entry[
|
|
463
|
+
for (const [joinModel, joinAttr] of Object.entries(joinConfig)) entry[joinModel] = joinAttr.relation === "one-to-one" ? null : [];
|
|
462
464
|
groupedByMainId.set(mainId, entry);
|
|
463
465
|
}
|
|
464
466
|
const entry = groupedByMainId.get(mainId);
|
|
465
467
|
for (const [joinModel, joinAttr] of Object.entries(joinConfig)) {
|
|
466
468
|
const isUnique = joinAttr.relation === "one-to-one";
|
|
467
469
|
const limit = joinAttr.limit ?? 100;
|
|
468
|
-
const joinedObj = joinedModelFields[
|
|
470
|
+
const joinedObj = joinedModelFields[joinModel];
|
|
469
471
|
const hasData = joinedObj && Object.keys(joinedObj).length > 0 && Object.values(joinedObj).some((value) => value !== null && value !== void 0);
|
|
470
|
-
if (isUnique) entry[
|
|
472
|
+
if (isUnique) entry[joinModel] = hasData ? joinedObj : null;
|
|
471
473
|
else {
|
|
472
|
-
const joinModelName =
|
|
474
|
+
const joinModelName = joinModel;
|
|
473
475
|
if (Array.isArray(entry[joinModelName]) && hasData) {
|
|
474
476
|
if (entry[joinModelName].length >= limit) continue;
|
|
475
477
|
const idFieldName = getFieldName({
|
|
476
|
-
model: joinModel,
|
|
478
|
+
model: joinAttr.modelKey ?? joinModel,
|
|
477
479
|
field: "id"
|
|
478
480
|
});
|
|
479
481
|
const joinedId = joinedObj[idFieldName];
|
|
@@ -486,7 +488,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
486
488
|
}
|
|
487
489
|
const result = Array.from(groupedByMainId.values());
|
|
488
490
|
for (const entry of result) for (const [joinModel, joinAttr] of Object.entries(joinConfig)) if (joinAttr.relation !== "one-to-one") {
|
|
489
|
-
const joinModelName =
|
|
491
|
+
const joinModelName = joinModel;
|
|
490
492
|
if (Array.isArray(entry[joinModelName])) {
|
|
491
493
|
const limit = joinAttr.limit ?? 100;
|
|
492
494
|
if (entry[joinModelName].length > limit) entry[joinModelName] = entry[joinModelName].slice(0, limit);
|
|
@@ -495,17 +497,17 @@ const kyselyAdapter = (db, config) => {
|
|
|
495
497
|
return result;
|
|
496
498
|
}
|
|
497
499
|
return {
|
|
498
|
-
async create({ data, model }) {
|
|
499
|
-
return await withReturning(data, db.insertInto(model).values(data), model, []);
|
|
500
|
+
async create({ data, model, modelKey = model }) {
|
|
501
|
+
return await withReturning(data, db.insertInto(model).values(data), model, modelKey, []);
|
|
500
502
|
},
|
|
501
|
-
async findOne({ model, where, select, join }) {
|
|
502
|
-
const { and, or } = convertWhereClause(model, where);
|
|
503
|
+
async findOne({ model, modelKey = model, where, select, join }) {
|
|
504
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
503
505
|
let query = db.selectFrom((eb) => {
|
|
504
506
|
let b = eb.selectFrom(model);
|
|
505
507
|
if (and) b = b.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
506
508
|
if (or) b = b.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
507
509
|
if (select?.length && select.length > 0) b = b.select(select.map((field) => getFieldName({
|
|
508
|
-
model,
|
|
510
|
+
model: modelKey,
|
|
509
511
|
field
|
|
510
512
|
})));
|
|
511
513
|
else b = b.selectAll();
|
|
@@ -523,14 +525,14 @@ const kyselyAdapter = (db, config) => {
|
|
|
523
525
|
if (join) return processJoinedResults(res, join, allSelectsStr)[0];
|
|
524
526
|
return row;
|
|
525
527
|
},
|
|
526
|
-
async findMany({ model, where, limit, select, offset, sortBy, join }) {
|
|
527
|
-
const { and, or } = convertWhereClause(model, where);
|
|
528
|
+
async findMany({ model, modelKey = model, where, limit, select, offset, sortBy, join }) {
|
|
529
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
528
530
|
let query = db.selectFrom((eb) => {
|
|
529
531
|
let b = eb.selectFrom(model);
|
|
530
532
|
if (config?.type === "mssql") {
|
|
531
533
|
if (offset !== void 0) {
|
|
532
534
|
if (!sortBy) b = b.orderBy(getFieldName({
|
|
533
|
-
model,
|
|
535
|
+
model: modelKey,
|
|
534
536
|
field: "id"
|
|
535
537
|
}));
|
|
536
538
|
b = b.offset(offset).fetch(limit || 100);
|
|
@@ -540,13 +542,13 @@ const kyselyAdapter = (db, config) => {
|
|
|
540
542
|
if (offset !== void 0) b = b.offset(offset);
|
|
541
543
|
}
|
|
542
544
|
if (sortBy?.field) b = b.orderBy(`${getFieldName({
|
|
543
|
-
model,
|
|
545
|
+
model: modelKey,
|
|
544
546
|
field: sortBy.field
|
|
545
547
|
})}`, sortBy.direction);
|
|
546
548
|
if (and) b = b.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
547
549
|
if (or) b = b.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
548
550
|
if (select?.length && select.length > 0) b = b.select(select.map((field) => getFieldName({
|
|
549
|
-
model,
|
|
551
|
+
model: modelKey,
|
|
550
552
|
field
|
|
551
553
|
})));
|
|
552
554
|
else b = b.selectAll();
|
|
@@ -559,7 +561,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
559
561
|
const { allSelectsStr, allSelects } = selectAllJoins(join);
|
|
560
562
|
query = query.select(allSelects);
|
|
561
563
|
if (sortBy?.field) query = query.orderBy(`${getFieldName({
|
|
562
|
-
model,
|
|
564
|
+
model: modelKey,
|
|
563
565
|
field: sortBy.field
|
|
564
566
|
})}`, sortBy.direction);
|
|
565
567
|
const res = await query.execute();
|
|
@@ -567,24 +569,24 @@ const kyselyAdapter = (db, config) => {
|
|
|
567
569
|
if (join) return processJoinedResults(res, join, allSelectsStr);
|
|
568
570
|
return res;
|
|
569
571
|
},
|
|
570
|
-
async update({ model, where, update: values }) {
|
|
572
|
+
async update({ model, modelKey = model, where, update: values }) {
|
|
571
573
|
if (where.length === 0) return null;
|
|
572
|
-
const { and, or } = convertWhereClause(model, where);
|
|
574
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
573
575
|
let query = db.updateTable(model).set(values);
|
|
574
576
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
575
577
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
576
|
-
return await withReturning(values, query, model, where);
|
|
578
|
+
return await withReturning(values, query, model, modelKey, where);
|
|
577
579
|
},
|
|
578
|
-
async updateMany({ model, where, update: values }) {
|
|
579
|
-
const { and, or } = convertWhereClause(model, where);
|
|
580
|
+
async updateMany({ model, modelKey = model, where, update: values }) {
|
|
581
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
580
582
|
let query = db.updateTable(model).set(values);
|
|
581
583
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
582
584
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
583
585
|
const res = (await query.executeTakeFirst()).numUpdatedRows;
|
|
584
586
|
return res > Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : Number(res);
|
|
585
587
|
},
|
|
586
|
-
async count({ model, where }) {
|
|
587
|
-
const { and, or } = convertWhereClause(model, where);
|
|
588
|
+
async count({ model, modelKey = model, where }) {
|
|
589
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
588
590
|
let query = db.selectFrom(model).select(db.fn.count("id").as("count"));
|
|
589
591
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
590
592
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
@@ -593,30 +595,30 @@ const kyselyAdapter = (db, config) => {
|
|
|
593
595
|
if (typeof res[0].count === "bigint") return Number(res[0].count);
|
|
594
596
|
return parseInt(res[0].count);
|
|
595
597
|
},
|
|
596
|
-
async delete({ model, where }) {
|
|
597
|
-
const { and, or } = convertWhereClause(model, where);
|
|
598
|
+
async delete({ model, modelKey = model, where }) {
|
|
599
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
598
600
|
let query = db.deleteFrom(model);
|
|
599
601
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
600
602
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
601
603
|
await query.execute();
|
|
602
604
|
},
|
|
603
|
-
async deleteMany({ model, where }) {
|
|
604
|
-
const { and, or } = convertWhereClause(model, where);
|
|
605
|
+
async deleteMany({ model, modelKey = model, where }) {
|
|
606
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
605
607
|
let query = db.deleteFrom(model);
|
|
606
608
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
607
609
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
608
610
|
const res = (await query.executeTakeFirst()).numDeletedRows;
|
|
609
611
|
return res > Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : Number(res);
|
|
610
612
|
},
|
|
611
|
-
async consumeOne({ model, where }) {
|
|
612
|
-
const { and, or } = convertWhereClause(model, where);
|
|
613
|
+
async consumeOne({ model, modelKey = model, where }) {
|
|
614
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
613
615
|
const applyWhere = (query) => {
|
|
614
616
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
615
617
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
616
618
|
return query;
|
|
617
619
|
};
|
|
618
620
|
const idField = getFieldName({
|
|
619
|
-
model,
|
|
621
|
+
model: modelKey,
|
|
620
622
|
field: "id"
|
|
621
623
|
});
|
|
622
624
|
const deleteSelectedRow = async (db, row) => {
|
|
@@ -646,8 +648,8 @@ const kyselyAdapter = (db, config) => {
|
|
|
646
648
|
const targetIds = config?.type === "mssql" ? selectIds.top(1) : selectIds.limit(1);
|
|
647
649
|
return deleteWithReturning(db.deleteFrom(model).where(`${model}.${idField}`, "in", targetIds));
|
|
648
650
|
},
|
|
649
|
-
async incrementOne({ model, where, increment, set }) {
|
|
650
|
-
const { and, or } = convertWhereClause(model, where);
|
|
651
|
+
async incrementOne({ model, modelKey = model, where, increment, set }) {
|
|
652
|
+
const { and, or } = convertWhereClause(model, modelKey, where);
|
|
651
653
|
const applyWhere = (query) => {
|
|
652
654
|
if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
|
|
653
655
|
if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
|
|
@@ -656,7 +658,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
656
658
|
const assignments = { ...set ?? {} };
|
|
657
659
|
for (const [field, delta] of Object.entries(increment)) assignments[field] = sql`${sql.ref(field)} + ${delta}`;
|
|
658
660
|
const idField = getFieldName({
|
|
659
|
-
model,
|
|
661
|
+
model: modelKey,
|
|
660
662
|
field: "id"
|
|
661
663
|
});
|
|
662
664
|
if (config?.type === "mysql") {
|
|
@@ -709,7 +711,7 @@ const kyselyAdapter = (db, config) => {
|
|
|
709
711
|
return (options) => {
|
|
710
712
|
lazyOptions = options;
|
|
711
713
|
const instance = adapter(options);
|
|
712
|
-
|
|
714
|
+
registerSchemaCheck(instance, createSchemaCheck(() => findSchemaProblems(db, config?.type, getExpectedSchema(options, { usePlural: config?.usePlural })), "database", options.database), { runtimeEnabled: checksSchema(options) });
|
|
713
715
|
return instance;
|
|
714
716
|
};
|
|
715
717
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CompiledQuery, DefaultQueryCompiler
|
|
1
|
+
import { t as createSqliteIntrospector } from "./sqlite-introspector-DSCAP82F.mjs";
|
|
2
|
+
import { CompiledQuery, DefaultQueryCompiler } from "kysely";
|
|
3
3
|
//#region src/node-sqlite-dialect.ts
|
|
4
4
|
var NodeSqliteAdapter = class {
|
|
5
5
|
get supportsCreateIfNotExists() {
|
|
@@ -87,44 +87,6 @@ var ConnectionMutex = class {
|
|
|
87
87
|
resolve?.();
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
|
-
var NodeSqliteIntrospector = class {
|
|
91
|
-
#db;
|
|
92
|
-
constructor(db) {
|
|
93
|
-
this.#db = db;
|
|
94
|
-
}
|
|
95
|
-
async getSchemas() {
|
|
96
|
-
return [];
|
|
97
|
-
}
|
|
98
|
-
async getTables(options = { withInternalKyselyTables: false }) {
|
|
99
|
-
let query = this.#db.selectFrom("sqlite_schema").where("type", "=", "table").where("name", "not like", "sqlite_%").select("name").$castTo();
|
|
100
|
-
if (!options.withInternalKyselyTables) query = query.where("name", "!=", DEFAULT_MIGRATION_TABLE).where("name", "!=", DEFAULT_MIGRATION_LOCK_TABLE);
|
|
101
|
-
const tables = await query.execute();
|
|
102
|
-
return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
|
|
103
|
-
}
|
|
104
|
-
async getMetadata(options) {
|
|
105
|
-
return { tables: await this.getTables(options) };
|
|
106
|
-
}
|
|
107
|
-
async #getTableMetadata(table) {
|
|
108
|
-
const db = this.#db;
|
|
109
|
-
const autoIncrementCol = (await db.selectFrom("sqlite_master").where("name", "=", table).select("sql").$castTo().execute())[0]?.sql?.split(/[\(\),]/)?.find((it) => it.toLowerCase().includes("autoincrement"))?.split(/\s+/)?.[0]?.replace(/["`]/g, "");
|
|
110
|
-
return {
|
|
111
|
-
name: table,
|
|
112
|
-
columns: (await db.selectFrom(sql`pragma_table_info(${table})`.as("table_info")).select([
|
|
113
|
-
"name",
|
|
114
|
-
"type",
|
|
115
|
-
"notnull",
|
|
116
|
-
"dflt_value"
|
|
117
|
-
]).execute()).map((col) => ({
|
|
118
|
-
name: col.name,
|
|
119
|
-
dataType: col.type,
|
|
120
|
-
isNullable: !col.notnull,
|
|
121
|
-
isAutoIncrementing: col.name === autoIncrementCol,
|
|
122
|
-
hasDefaultValue: col.dflt_value != null
|
|
123
|
-
})),
|
|
124
|
-
isView: false
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
90
|
var NodeSqliteQueryCompiler = class extends DefaultQueryCompiler {
|
|
129
91
|
getCurrentParameterPlaceholder() {
|
|
130
92
|
return "?";
|
|
@@ -154,7 +116,7 @@ var NodeSqliteDialect = class {
|
|
|
154
116
|
return new NodeSqliteAdapter();
|
|
155
117
|
}
|
|
156
118
|
createIntrospector(db) {
|
|
157
|
-
return
|
|
119
|
+
return createSqliteIntrospector(db);
|
|
158
120
|
}
|
|
159
121
|
};
|
|
160
122
|
//#endregion
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
//#region src/kysely-migration-tables.ts
|
|
3
|
+
/**
|
|
4
|
+
* Kysely's internal migration table names, mirrored as local constants.
|
|
5
|
+
*
|
|
6
|
+
* Kysely 0.29 moved these from its main entry to the `kysely/migration`
|
|
7
|
+
* subpath (which 0.28 lacks), and the main entry now exports only type stubs
|
|
8
|
+
* with no runtime value, which breaks strict ESM bundlers.
|
|
9
|
+
*
|
|
10
|
+
* The values are stable parts of Kysely's public migration contract. Mirroring
|
|
11
|
+
* them allows the SQLite dialects to support both Kysely 0.28 and 0.29.
|
|
12
|
+
*
|
|
13
|
+
* TODO: Import these from `kysely/migration` after dropping Kysely 0.28.
|
|
14
|
+
*/
|
|
15
|
+
const DEFAULT_MIGRATION_TABLE = "kysely_migration";
|
|
16
|
+
const DEFAULT_MIGRATION_LOCK_TABLE = "kysely_migration_lock";
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/sqlite-introspector.ts
|
|
19
|
+
function sqliteIntegerPrimaryKeyColumn(columns) {
|
|
20
|
+
const primaryKeys = columns.filter((column) => column.pk > 0);
|
|
21
|
+
const primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : void 0;
|
|
22
|
+
return primaryKey?.type.toLowerCase() === "integer" ? primaryKey.name : void 0;
|
|
23
|
+
}
|
|
24
|
+
async function sqliteGeneratedPrimaryKeyColumn(db, name, columns) {
|
|
25
|
+
const column = sqliteIntegerPrimaryKeyColumn(columns);
|
|
26
|
+
if (!column) return;
|
|
27
|
+
const { rows } = await sql`
|
|
28
|
+
PRAGMA index_list(${sql.id(name)})
|
|
29
|
+
`.execute(db.withoutPlugins());
|
|
30
|
+
return rows.some((index) => index.origin === "pk") ? void 0 : column;
|
|
31
|
+
}
|
|
32
|
+
function toSqliteTableMetadata(table, columns, autoIncrementColumn) {
|
|
33
|
+
return {
|
|
34
|
+
name: table.name,
|
|
35
|
+
isView: table.type === "view",
|
|
36
|
+
columns: columns.map((column) => ({
|
|
37
|
+
name: column.name,
|
|
38
|
+
dataType: column.type,
|
|
39
|
+
isNullable: column.notnull === 0,
|
|
40
|
+
isAutoIncrementing: column.name === autoIncrementColumn,
|
|
41
|
+
hasDefaultValue: column.dflt_value != null
|
|
42
|
+
}))
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async function readTableMetadata(db, name) {
|
|
46
|
+
const { rows: columns } = await sql`
|
|
47
|
+
PRAGMA table_info(${sql.id(name)})
|
|
48
|
+
`.execute(db.withoutPlugins());
|
|
49
|
+
if (columns.length === 0) return;
|
|
50
|
+
const generatedColumn = await sqliteGeneratedPrimaryKeyColumn(db, name, columns);
|
|
51
|
+
return toSqliteTableMetadata({ name }, columns, generatedColumn);
|
|
52
|
+
}
|
|
53
|
+
async function introspectSqliteTables(db, tableNames) {
|
|
54
|
+
if (tableNames.length === 0) return [];
|
|
55
|
+
try {
|
|
56
|
+
return await db.introspection.getTables();
|
|
57
|
+
} catch {
|
|
58
|
+
return (await Promise.all(tableNames.map((name) => readTableMetadata(db, name)))).filter((table) => table !== void 0);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function createSqliteIntrospector(db) {
|
|
62
|
+
const systemDb = db;
|
|
63
|
+
async function getSchemas() {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
async function getTables(options = { withInternalKyselyTables: false }) {
|
|
67
|
+
let query = systemDb.selectFrom("sqlite_schema").where("type", "=", "table").where("name", "not like", "sqlite_%").select("name");
|
|
68
|
+
if (!options.withInternalKyselyTables) query = query.where("name", "!=", DEFAULT_MIGRATION_TABLE).where("name", "!=", DEFAULT_MIGRATION_LOCK_TABLE);
|
|
69
|
+
const tables = await query.execute();
|
|
70
|
+
return Promise.all(tables.map(async ({ name }) => {
|
|
71
|
+
const columns = await db.selectFrom(sql`pragma_table_info(${name})`.as("table_info")).select([
|
|
72
|
+
"cid",
|
|
73
|
+
"name",
|
|
74
|
+
"type",
|
|
75
|
+
"notnull",
|
|
76
|
+
"dflt_value",
|
|
77
|
+
"pk"
|
|
78
|
+
]).execute();
|
|
79
|
+
const generatedColumn = await sqliteGeneratedPrimaryKeyColumn(db, name, columns);
|
|
80
|
+
return toSqliteTableMetadata({ name }, columns, generatedColumn);
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
getSchemas,
|
|
85
|
+
getTables,
|
|
86
|
+
getMetadata: async (options) => ({ tables: await getTables(options) })
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { DEFAULT_MIGRATION_LOCK_TABLE as a, toSqliteTableMetadata as i, introspectSqliteTables as n, DEFAULT_MIGRATION_TABLE as o, sqliteIntegerPrimaryKeyColumn as r, createSqliteIntrospector as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/kysely-adapter",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.6",
|
|
4
4
|
"description": "Kysely adapter for Better Auth",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"@better-auth/utils": "0.4.2",
|
|
44
44
|
"kysely": "^0.28.17 || ^0.29.0",
|
|
45
|
-
"@better-auth/core": "^1.7.
|
|
45
|
+
"@better-auth/core": "^1.7.6"
|
|
46
46
|
},
|
|
47
47
|
"peerDependenciesMeta": {
|
|
48
48
|
"kysely": {
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"pg": "^8.0.0",
|
|
58
58
|
"tsdown": "0.21.10",
|
|
59
59
|
"typescript": "^6.0.3",
|
|
60
|
-
"@better-auth/core": "1.7.
|
|
60
|
+
"@better-auth/core": "1.7.6"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsdown",
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
//#region src/kysely-migration-tables.ts
|
|
2
|
-
/**
|
|
3
|
-
* Kysely's internal migration table names, mirrored as local constants.
|
|
4
|
-
*
|
|
5
|
-
* Kysely 0.29 moved these from its main entry to the `kysely/migration`
|
|
6
|
-
* subpath (which 0.28 lacks), and the main entry now exports only type stubs
|
|
7
|
-
* with no runtime value, which breaks strict ESM bundlers.
|
|
8
|
-
*
|
|
9
|
-
* The values are stable parts of Kysely's public migration contract. Mirroring
|
|
10
|
-
* them allows the SQLite dialects to support both Kysely 0.28 and 0.29.
|
|
11
|
-
*
|
|
12
|
-
* TODO: Import these from `kysely/migration` after dropping Kysely 0.28.
|
|
13
|
-
*/
|
|
14
|
-
const DEFAULT_MIGRATION_TABLE = "kysely_migration";
|
|
15
|
-
const DEFAULT_MIGRATION_LOCK_TABLE = "kysely_migration_lock";
|
|
16
|
-
//#endregion
|
|
17
|
-
export { DEFAULT_MIGRATION_TABLE as n, DEFAULT_MIGRATION_LOCK_TABLE as t };
|