@better-auth/kysely-adapter 1.7.19 → 1.7.21
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/index.d.mts +9 -0
- package/dist/index.mjs +56 -2
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -111,5 +111,14 @@ export declare function toPhysicalSchema(db: Kysely<unknown>, expected: Expected
|
|
|
111
111
|
*/
|
|
112
112
|
export declare function getPostgresSchema(db: Kysely<unknown>): Promise<string>;
|
|
113
113
|
export declare function getMssqlSchema(db: Kysely<unknown>): Promise<string>;
|
|
114
|
+
/**
|
|
115
|
+
* Reads the tables in `schemas` with the metadata Kysely's PostgreSQL
|
|
116
|
+
* introspector reports. That introspector reads every schema and resolves
|
|
117
|
+
* each one by name, so a schema another connection drops during the read
|
|
118
|
+
* fails it with "schema does not exist" or "relation does not exist".
|
|
119
|
+
*/
|
|
120
|
+
export declare function introspectPostgresTables(db: Kysely<unknown>, schemas: readonly string[]): Promise<(TableMetadata & {
|
|
121
|
+
isForeign: boolean;
|
|
122
|
+
})[]>;
|
|
114
123
|
//#endregion
|
|
115
124
|
export {};
|
package/dist/index.mjs
CHANGED
|
@@ -236,6 +236,60 @@ async function getMssqlSchema(db) {
|
|
|
236
236
|
SELECT SCHEMA_NAME() AS "schemaName"
|
|
237
237
|
`.execute(db.withoutPlugins())).rows[0]?.schemaName || "dbo";
|
|
238
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Reads the tables in `schemas` with the metadata Kysely's PostgreSQL
|
|
241
|
+
* introspector reports. That introspector reads every schema and resolves
|
|
242
|
+
* each one by name, so a schema another connection drops during the read
|
|
243
|
+
* fails it with "schema does not exist" or "relation does not exist".
|
|
244
|
+
*/
|
|
245
|
+
async function introspectPostgresTables(db, schemas) {
|
|
246
|
+
if (schemas.length === 0) return [];
|
|
247
|
+
const { rows } = await sql`
|
|
248
|
+
SELECT
|
|
249
|
+
ns.nspname AS "schema",
|
|
250
|
+
c.relname AS "table",
|
|
251
|
+
c.relkind = 'v' AS "isView",
|
|
252
|
+
c.relkind = 'f' AS "isForeign",
|
|
253
|
+
a.attname AS "column",
|
|
254
|
+
typ.typname AS "dataType",
|
|
255
|
+
NOT a.attnotnull AS "isNullable",
|
|
256
|
+
a.atthasdef AS "hasDefaultValue",
|
|
257
|
+
pg_catalog.pg_get_serial_sequence(
|
|
258
|
+
pg_catalog.quote_ident(ns.nspname) || '.' || pg_catalog.quote_ident(c.relname),
|
|
259
|
+
a.attname
|
|
260
|
+
) IS NOT NULL AS "isAutoIncrementing"
|
|
261
|
+
FROM pg_catalog.pg_attribute AS a
|
|
262
|
+
INNER JOIN pg_catalog.pg_class AS c ON c.oid = a.attrelid
|
|
263
|
+
INNER JOIN pg_catalog.pg_namespace AS ns ON ns.oid = c.relnamespace
|
|
264
|
+
INNER JOIN pg_catalog.pg_type AS typ ON typ.oid = a.atttypid
|
|
265
|
+
WHERE ns.nspname IN (${sql.join(schemas)})
|
|
266
|
+
AND ns.nspname !~ '^pg_'
|
|
267
|
+
AND pg_catalog.has_schema_privilege(ns.oid, 'USAGE')
|
|
268
|
+
AND c.relkind IN ('r', 'v', 'p', 'f')
|
|
269
|
+
AND a.attnum >= 0
|
|
270
|
+
AND NOT a.attisdropped
|
|
271
|
+
ORDER BY ns.nspname, c.relname, a.attnum
|
|
272
|
+
`.execute(db.withoutPlugins());
|
|
273
|
+
const tables = [];
|
|
274
|
+
let current;
|
|
275
|
+
for (const { schema, table, isView, isForeign, column, ...metadata } of rows) {
|
|
276
|
+
if (current?.schema !== schema || current.name !== table) {
|
|
277
|
+
current = {
|
|
278
|
+
name: table,
|
|
279
|
+
schema,
|
|
280
|
+
isView,
|
|
281
|
+
isForeign,
|
|
282
|
+
columns: []
|
|
283
|
+
};
|
|
284
|
+
tables.push(current);
|
|
285
|
+
}
|
|
286
|
+
current.columns.push({
|
|
287
|
+
name: column,
|
|
288
|
+
...metadata
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
return tables;
|
|
292
|
+
}
|
|
239
293
|
async function schemaSearchPath(db, dbType) {
|
|
240
294
|
if (dbType === "postgres") return (await sql`
|
|
241
295
|
SELECT pg_catalog.current_schemas(true)::text[] AS schemas
|
|
@@ -257,7 +311,7 @@ async function findSchemaProblems(db, dbType, expected) {
|
|
|
257
311
|
}
|
|
258
312
|
return db.connection().execute(async (connection) => {
|
|
259
313
|
const searchPath = await schemaSearchPath(connection, dbType);
|
|
260
|
-
const tables = toIntrospectedTables(await connection.introspection.getTables());
|
|
314
|
+
const tables = toIntrospectedTables(dbType === "postgres" ? await introspectPostgresTables(connection, [...searchPath ?? [], ...Object.values(physical).flatMap(({ schema }) => schema ?? [])]) : await connection.introspection.getTables());
|
|
261
315
|
for (const [name, table] of Object.entries(physical)) {
|
|
262
316
|
if (table.schema !== void 0 || !searchPath) continue;
|
|
263
317
|
table.schema = searchPath.find((schema) => tables.some((candidate) => candidate.name === name && candidate.schema === schema)) ?? "";
|
|
@@ -722,4 +776,4 @@ const kyselyAdapter = (db, config) => {
|
|
|
722
776
|
};
|
|
723
777
|
};
|
|
724
778
|
//#endregion
|
|
725
|
-
export { createKyselyAdapter, getKyselyDatabaseType, getMssqlSchema, getPostgresSchema, kyselyAdapter, toIntrospectedTables, toPhysicalSchema };
|
|
779
|
+
export { createKyselyAdapter, getKyselyDatabaseType, getMssqlSchema, getPostgresSchema, introspectPostgresTables, kyselyAdapter, toIntrospectedTables, toPhysicalSchema };
|
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.21",
|
|
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 || ^0.5.0",
|
|
44
44
|
"kysely": "^0.28.17 || ^0.29.0",
|
|
45
|
-
"@better-auth/core": "^1.7.
|
|
45
|
+
"@better-auth/core": "^1.7.21"
|
|
46
46
|
},
|
|
47
47
|
"peerDependenciesMeta": {
|
|
48
48
|
"kysely": {
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"pg": "^8.0.0",
|
|
58
58
|
"tsdown": "0.23.1",
|
|
59
59
|
"typescript": "^6.0.3",
|
|
60
|
-
"@better-auth/core": "1.7.
|
|
60
|
+
"@better-auth/core": "1.7.21"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsdown",
|