mirror of
https://github.com/immich-app/immich.git
synced 2025-12-17 17:23:20 +03:00
34 lines
865 B
TypeScript
34 lines
865 B
TypeScript
|
|
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;
|
||
|
|
};
|