mirror of
https://github.com/immich-app/immich.git
synced 2025-12-18 09:13:15 +03:00
refactor: sql-tools (#19717)
This commit is contained in:
33
server/src/sql-tools/schema-from-database.ts
Normal file
33
server/src/sql-tools/schema-from-database.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { PostgresJSDialect } from 'kysely-postgres-js';
|
||||
import { Sql } from 'postgres';
|
||||
import { readers } from 'src/sql-tools/readers';
|
||||
import { DatabaseSchema, PostgresDB, SchemaFromDatabaseOptions } from 'src/sql-tools/types';
|
||||
|
||||
/**
|
||||
* Load schema from a database url
|
||||
*/
|
||||
export const schemaFromDatabase = async (
|
||||
postgres: Sql,
|
||||
options: SchemaFromDatabaseOptions = {},
|
||||
): Promise<DatabaseSchema> => {
|
||||
const schema: DatabaseSchema = {
|
||||
databaseName: 'immich',
|
||||
schemaName: options.schemaName || 'public',
|
||||
parameters: [],
|
||||
functions: [],
|
||||
enums: [],
|
||||
extensions: [],
|
||||
tables: [],
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
const db = new Kysely<PostgresDB>({ dialect: new PostgresJSDialect({ postgres }) });
|
||||
for (const reader of readers) {
|
||||
await reader(schema, db);
|
||||
}
|
||||
|
||||
await db.destroy();
|
||||
|
||||
return schema;
|
||||
};
|
||||
Reference in New Issue
Block a user