Files
immich/server/src/sql-tools/schema-from-database.ts

28 lines
791 B
TypeScript
Raw Normal View History

2025-07-03 10:59:17 -04:00
import { Kysely } from 'kysely';
import { PostgresJSDialect } from 'kysely-postgres-js';
import { Sql } from 'postgres';
2025-07-08 08:17:40 -04:00
import { ReaderContext } from 'src/sql-tools/contexts/reader-context';
2025-07-03 10:59:17 -04:00
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 db = new Kysely<PostgresDB>({ dialect: new PostgresJSDialect({ postgres }) });
2025-07-08 08:17:40 -04:00
const ctx = new ReaderContext(options);
2025-07-03 10:59:17 -04:00
2025-07-08 08:17:40 -04:00
try {
for (const reader of readers) {
await reader(ctx, db);
}
2025-07-03 10:59:17 -04:00
2025-07-08 08:17:40 -04:00
return ctx.build();
} finally {
await db.destroy();
}
2025-07-03 10:59:17 -04:00
};