@@ -4,8 +4,8 @@ process.env.DB_URL = 'postgres://postgres:postgres@localhost:5432/immich';
4
4
import { writeFileSync } from 'node:fs' ;
5
5
import postgres from 'postgres' ;
6
6
import { ConfigRepository } from 'src/repositories/config.repository' ;
7
+ import 'src/schema/tables' ;
7
8
import { DatabaseTable , schemaDiff , schemaFromDatabase , schemaFromDecorators } from 'src/sql-tools' ;
8
- import 'src/tables' ;
9
9
10
10
const main = async ( ) => {
11
11
const command = process . argv [ 2 ] ;
@@ -54,9 +54,10 @@ const generate = async (name: string) => {
54
54
} ;
55
55
56
56
const create = ( name : string , up : string [ ] , down : string [ ] ) => {
57
- const { filename, code } = asMigration ( name , up , down ) ;
57
+ const timestamp = Date . now ( ) ;
58
+ const filename = `${ timestamp } -${ name } .ts` ;
58
59
const fullPath = `./src/${ filename } ` ;
59
- writeFileSync ( fullPath , code ) ;
60
+ writeFileSync ( fullPath , asMigration ( 'kysely' , { name , timestamp , up , down } ) ) ;
60
61
console . log ( `Wrote ${ fullPath } ` ) ;
61
62
} ;
62
63
@@ -79,14 +80,21 @@ const compare = async () => {
79
80
return { up, down } ;
80
81
} ;
81
82
82
- const asMigration = ( name : string , up : string [ ] , down : string [ ] ) => {
83
- const timestamp = Date . now ( ) ;
83
+ type MigrationProps = {
84
+ name : string ;
85
+ timestamp: number ;
86
+ up: string [ ] ;
87
+ down: string [ ] ;
88
+ } ;
89
+
90
+ const asMigration = ( type : 'kysely' | 'typeorm' , options : MigrationProps ) =>
91
+ type === 'typeorm' ? asTypeOrmMigration ( options ) : asKyselyMigration ( options ) ;
84
92
93
+ const asTypeOrmMigration = ( { timestamp, name, up, down } : MigrationProps ) => {
85
94
const upSql = up . map ( ( sql ) => ` await queryRunner.query(\`${ sql } \`);` ) . join ( '\n' ) ;
86
95
const downSql = down . map ( ( sql ) => ` await queryRunner.query(\`${ sql } \`);` ) . join ( '\n' ) ;
87
- return {
88
- filename : `${ timestamp } -${ name } .ts` ,
89
- code : `import { MigrationInterface, QueryRunner } from 'typeorm';
96
+
97
+ return `import { MigrationInterface, QueryRunner } from 'typeorm';
90
98
91
99
export class ${ name } ${ timestamp } implements MigrationInterface {
92
100
public async up(queryRunner: QueryRunner): Promise<void> {
@@ -97,8 +105,23 @@ ${upSql}
97
105
${ downSql }
98
106
}
99
107
}
100
- ` ,
101
- } ;
108
+ ` ;
109
+ } ;
110
+
111
+ const asKyselyMigration = ( { up, down } : MigrationProps ) => {
112
+ const upSql = up . map ( ( sql ) => ` await sql\`${ sql } \`.execute(db);` ) . join ( '\n' ) ;
113
+ const downSql = down . map ( ( sql ) => ` await sql\`${ sql } \`.execute(db);` ) . join ( '\n' ) ;
114
+
115
+ return `import { Kysely, sql } from 'kysely';
116
+
117
+ export async function up(db: Kysely<any>): Promise<void> {
118
+ ${ upSql }
119
+ }
120
+
121
+ export async function down(db: Kysely<any>): Promise<void> {
122
+ ${ downSql }
123
+ }
124
+ ` ;
102
125
} ;
103
126
104
127
main ( )
0 commit comments