@cheetah.js/orm 0.1.28 → 0.1.30
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/SqlBuilder.js +76 -23
- package/dist/SqlBuilder.js.map +1 -1
- package/dist/bun/index.js +1898 -1537
- package/dist/bun/index.js.map +26 -18
- package/dist/decorators/one-many.decorator.js +9 -2
- package/dist/decorators/one-many.decorator.js.map +1 -1
- package/dist/decorators/property.decorator.d.ts +1 -0
- package/dist/decorators/property.decorator.js +2 -1
- package/dist/decorators/property.decorator.js.map +1 -1
- package/dist/domain/entities.d.ts +1 -0
- package/dist/domain/entities.js +12 -5
- package/dist/domain/entities.js.map +1 -1
- package/dist/driver/driver.interface.d.ts +1 -0
- package/dist/driver/pg-driver.js +1 -1
- package/dist/driver/pg-driver.js.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +4 -0
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/test/domain/base-entity.spec.ts +0 -548
- package/test/domain/enum.spec.ts +0 -55
- package/test/domain/hooks.spec.ts +0 -160
- package/test/domain/relationship.spec.ts +0 -173
- package/test/domain/serialize.spec.ts +0 -44
- package/test/migration/cheetah.config.ts +0 -13
- package/test/migration/migrator.spec.ts +0 -412
- package/test/node-database.ts +0 -30
|
@@ -1,412 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata'
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
3
|
-
import { Migrator } from '../../src/migration/migrator';
|
|
4
|
-
import config from './cheetah.config';
|
|
5
|
-
import { BaseEntity, Entity, ManyToOne, PrimaryKey, Property } from '../../src';
|
|
6
|
-
import { execute, mockLogger, purgeDatabase, startDatabase } from '../node-database';
|
|
7
|
-
import * as path from 'path';
|
|
8
|
-
import * as fs from 'fs';
|
|
9
|
-
import { Metadata } from '@cheetah.js/core';
|
|
10
|
-
import { ENTITIES } from '../../src/constants';
|
|
11
|
-
import { Index } from '../../src/decorators/index.decorator';
|
|
12
|
-
import { Email } from '../../src/common/email.vo';
|
|
13
|
-
import { Enum } from '../../src/decorators/enum.decorator';
|
|
14
|
-
|
|
15
|
-
describe('Migration', () => {
|
|
16
|
-
|
|
17
|
-
beforeEach(async () => {
|
|
18
|
-
await startDatabase();
|
|
19
|
-
Metadata.delete(ENTITIES, Reflect);
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
afterEach(async () => {
|
|
23
|
-
await purgeDatabase();
|
|
24
|
-
(mockLogger as jest.Mock).mockClear();
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('When search de config file', async () => {
|
|
28
|
-
const migrator = new Migrator();
|
|
29
|
-
await migrator.initConfigFile();
|
|
30
|
-
|
|
31
|
-
expect(migrator.config).toEqual(config)
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('When not search de config file', async () => {
|
|
35
|
-
const migrator = new Migrator();
|
|
36
|
-
expect(async () => {
|
|
37
|
-
await migrator.initConfigFile('src')
|
|
38
|
-
}).toThrow("Config file not found");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should snapshot database', async () => {
|
|
42
|
-
class User extends BaseEntity {
|
|
43
|
-
@PrimaryKey()
|
|
44
|
-
id: number;
|
|
45
|
-
|
|
46
|
-
@Property({ unique: true })
|
|
47
|
-
email: string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
Entity()(User);
|
|
51
|
-
const migrator = new Migrator();
|
|
52
|
-
await migrator.initConfigFile();
|
|
53
|
-
await migrator.createMigration( 'test');
|
|
54
|
-
|
|
55
|
-
// Caminho para o arquivo gerado
|
|
56
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
57
|
-
|
|
58
|
-
// Leitura do conteúdo do arquivo
|
|
59
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
60
|
-
|
|
61
|
-
// Verificação do conteúdo conforme necessário
|
|
62
|
-
expect(migrationContent).toContain('CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" character varying(255) NOT NULL UNIQUE);');
|
|
63
|
-
await execute(migrationContent);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should modify database column', async () => {
|
|
67
|
-
await execute("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) PRIMARY KEY UNIQUE,\"email\" character varying(255) UNIQUE);")
|
|
68
|
-
class User extends BaseEntity {
|
|
69
|
-
@PrimaryKey()
|
|
70
|
-
id: number;
|
|
71
|
-
|
|
72
|
-
@Property()
|
|
73
|
-
email: string;
|
|
74
|
-
|
|
75
|
-
@Property({length: 10})
|
|
76
|
-
password: string;
|
|
77
|
-
|
|
78
|
-
@Property({nullable: true})
|
|
79
|
-
token?: string;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
Entity()(User);
|
|
83
|
-
|
|
84
|
-
const migrator = new Migrator();
|
|
85
|
-
await migrator.initConfigFile();
|
|
86
|
-
await migrator.createMigration( 'test');
|
|
87
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
88
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
89
|
-
|
|
90
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" DROP CONSTRAINT \"user_email_key\";");
|
|
91
|
-
expect(migrationContent).toContain('ALTER TABLE \"public\".\"user\" ADD COLUMN \"password\" character varying(10) NOT NULL')
|
|
92
|
-
expect(migrationContent).toContain('ALTER TABLE \"public\".\"user\" ADD COLUMN \"token\" character varying(255);')
|
|
93
|
-
await execute(migrationContent);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('should modify a column unique', async () => {
|
|
97
|
-
await execute("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" character varying(255) NOT NULL unique, \"password\" character varying(10) NOT NULL);")
|
|
98
|
-
class User extends BaseEntity {
|
|
99
|
-
@PrimaryKey()
|
|
100
|
-
id: number;
|
|
101
|
-
|
|
102
|
-
@Property()
|
|
103
|
-
email: string;
|
|
104
|
-
|
|
105
|
-
@Property({length: 10, unique: true})
|
|
106
|
-
password: string;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
Entity()(User);
|
|
110
|
-
|
|
111
|
-
const migrator = new Migrator();
|
|
112
|
-
await migrator.initConfigFile();
|
|
113
|
-
await migrator.createMigration( 'test');
|
|
114
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
115
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
116
|
-
|
|
117
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" DROP CONSTRAINT \"user_email_key\";");
|
|
118
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" ADD UNIQUE (\"password\");")
|
|
119
|
-
await execute(migrationContent);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should add a relation property', async () => {
|
|
123
|
-
await execute("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" character varying(255) NOT NULL unique);")
|
|
124
|
-
await execute("CREATE TABLE \"public\".\"address\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE);")
|
|
125
|
-
class User extends BaseEntity {
|
|
126
|
-
@PrimaryKey()
|
|
127
|
-
id: number;
|
|
128
|
-
|
|
129
|
-
@Property()
|
|
130
|
-
email: string;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
class Address extends BaseEntity {
|
|
134
|
-
@PrimaryKey()
|
|
135
|
-
id: number;
|
|
136
|
-
|
|
137
|
-
@ManyToOne(() => User)
|
|
138
|
-
user: User;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
Entity()(User);
|
|
142
|
-
Entity()(Address);
|
|
143
|
-
|
|
144
|
-
const migrator = new Migrator();
|
|
145
|
-
await migrator.initConfigFile();
|
|
146
|
-
await migrator.createMigration( 'test');
|
|
147
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
148
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
149
|
-
|
|
150
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" DROP CONSTRAINT \"user_email_key\";");
|
|
151
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"address\" ADD COLUMN \"user\" numeric(11) NOT NULL;");
|
|
152
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"address\" ADD CONSTRAINT \"address_user_fk\" FOREIGN KEY (\"user\") REFERENCES \"user\" (\"id\");")
|
|
153
|
-
await execute(migrationContent);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it('should add a relation property', async () => {
|
|
157
|
-
await execute("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" character varying(255) NOT NULL);")
|
|
158
|
-
await execute("CREATE TABLE \"public\".\"address\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE, \"user\" numeric(11) NOT NULL);")
|
|
159
|
-
await execute("ALTER TABLE \"public\".\"address\" ADD CONSTRAINT \"address_user_fk\" FOREIGN KEY (\"user\") REFERENCES \"public\".\"user\" (\"id\");")
|
|
160
|
-
|
|
161
|
-
class User extends BaseEntity {
|
|
162
|
-
@PrimaryKey()
|
|
163
|
-
id: number;
|
|
164
|
-
|
|
165
|
-
@Property()
|
|
166
|
-
email: string;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
class Address extends BaseEntity {
|
|
170
|
-
@PrimaryKey()
|
|
171
|
-
id: number;
|
|
172
|
-
|
|
173
|
-
@Property({length: 11, nullable: true})
|
|
174
|
-
user: number;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
Entity()(User);
|
|
178
|
-
Entity()(Address);
|
|
179
|
-
|
|
180
|
-
const migrator = new Migrator();
|
|
181
|
-
await migrator.initConfigFile();
|
|
182
|
-
await migrator.createMigration( 'test');
|
|
183
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
184
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
185
|
-
|
|
186
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"address\" DROP CONSTRAINT \"address_user_fk\";")
|
|
187
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"address\" ALTER COLUMN \"user\" DROP NOT NULL;")
|
|
188
|
-
await execute(migrationContent);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it('should add a index property with multiple indexes', async () => {
|
|
192
|
-
await execute("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" character varying(255) NOT NULL);")
|
|
193
|
-
|
|
194
|
-
class User extends BaseEntity {
|
|
195
|
-
@PrimaryKey()
|
|
196
|
-
@Index({properties: ['id', 'email']})
|
|
197
|
-
id: number;
|
|
198
|
-
|
|
199
|
-
@Property()
|
|
200
|
-
@Index()
|
|
201
|
-
email: string;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
Entity()(User);
|
|
205
|
-
|
|
206
|
-
const migrator = new Migrator();
|
|
207
|
-
await migrator.initConfigFile();
|
|
208
|
-
await migrator.createMigration( 'test');
|
|
209
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
210
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
211
|
-
|
|
212
|
-
expect(migrationContent).toContain("CREATE INDEX \"id_email_index\" ON \"public\".\"user\" (\"id\", \"email\");")
|
|
213
|
-
expect(migrationContent).toContain("CREATE INDEX \"email_index\" ON \"public\".\"user\" (\"email\");")
|
|
214
|
-
await execute(migrationContent);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it('should add a create with relations', async () => {
|
|
218
|
-
class User extends BaseEntity {
|
|
219
|
-
@PrimaryKey()
|
|
220
|
-
@Index({properties: ['id', 'email']})
|
|
221
|
-
id: number;
|
|
222
|
-
|
|
223
|
-
@Property()
|
|
224
|
-
@Index()
|
|
225
|
-
email: string;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
class Address extends BaseEntity {
|
|
229
|
-
@PrimaryKey()
|
|
230
|
-
id: number;
|
|
231
|
-
|
|
232
|
-
@ManyToOne(() => User)
|
|
233
|
-
user: User;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
Entity()(User);
|
|
237
|
-
Entity()(Address);
|
|
238
|
-
|
|
239
|
-
const migrator = new Migrator();
|
|
240
|
-
await migrator.initConfigFile();
|
|
241
|
-
await migrator.createMigration( 'test');
|
|
242
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
243
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
244
|
-
|
|
245
|
-
expect(migrationContent).toContain("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" character varying(255) NOT NULL);")
|
|
246
|
-
expect(migrationContent).toContain("CREATE INDEX \"id_email_index\" ON \"public\".\"user\" (\"id\", \"email\");")
|
|
247
|
-
expect(migrationContent).toContain("CREATE INDEX \"email_index\" ON \"public\".\"user\" (\"email\");\n")
|
|
248
|
-
expect(migrationContent).toContain("CREATE TABLE \"public\".\"address\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"user\" numeric(11) NOT NULL);")
|
|
249
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"address\" ADD CONSTRAINT \"address_user_fk\" FOREIGN KEY (\"user\") REFERENCES \"user\" (\"id\");")
|
|
250
|
-
expect(migrationContent.split('\n').length).toEqual(5)
|
|
251
|
-
await execute(migrationContent);
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
it('should create with value-objects', async () => {
|
|
255
|
-
class User extends BaseEntity {
|
|
256
|
-
@PrimaryKey()
|
|
257
|
-
id: number;
|
|
258
|
-
|
|
259
|
-
@Property({ dbType: 'text' })
|
|
260
|
-
email: Email;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
Entity()(User);
|
|
264
|
-
|
|
265
|
-
const migrator = new Migrator();
|
|
266
|
-
await migrator.initConfigFile();
|
|
267
|
-
await migrator.createMigration( 'test');
|
|
268
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
269
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
270
|
-
|
|
271
|
-
expect(migrationContent).toContain("CREATE TABLE \"public\".\"user\" (\"id\" numeric(11) NOT NULL PRIMARY KEY UNIQUE,\"email\" text NOT NULL);")
|
|
272
|
-
expect(migrationContent.split('\n').length).toEqual(1)
|
|
273
|
-
await execute(migrationContent);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
it('should create with auto-increment', async () => {
|
|
277
|
-
class User extends BaseEntity {
|
|
278
|
-
@PrimaryKey({ autoIncrement: true })
|
|
279
|
-
id: number;
|
|
280
|
-
|
|
281
|
-
@Property({ dbType: 'text' })
|
|
282
|
-
email: Email;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
Entity()(User);
|
|
286
|
-
|
|
287
|
-
const migrator = new Migrator();
|
|
288
|
-
await migrator.initConfigFile();
|
|
289
|
-
await migrator.createMigration( 'test');
|
|
290
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
291
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
292
|
-
|
|
293
|
-
expect(migrationContent).toContain("CREATE TABLE \"public\".\"user\" (\"id\" SERIAL PRIMARY KEY UNIQUE,\"email\" text NOT NULL);")
|
|
294
|
-
expect(migrationContent.split('\n').length).toEqual(1)
|
|
295
|
-
await execute(migrationContent);
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
it('should create with enum property', async () => {
|
|
299
|
-
enum Role {
|
|
300
|
-
ADMIN = 'admin',
|
|
301
|
-
USER = 'user',
|
|
302
|
-
}
|
|
303
|
-
class User extends BaseEntity {
|
|
304
|
-
@PrimaryKey({ autoIncrement: true })
|
|
305
|
-
id: number;
|
|
306
|
-
|
|
307
|
-
@Enum(() => Role)
|
|
308
|
-
role: Role;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
Entity()(User);
|
|
312
|
-
|
|
313
|
-
const migrator = new Migrator();
|
|
314
|
-
await migrator.initConfigFile();
|
|
315
|
-
await migrator.createMigration( 'test');
|
|
316
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
317
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
318
|
-
|
|
319
|
-
expect(migrationContent).toContain("CREATE TYPE \"public_user_role_enum\" AS ENUM ('admin', 'user');CREATE TABLE \"public\".\"user\" (\"id\" SERIAL PRIMARY KEY UNIQUE,\"role\" \"public_user_role_enum\" NOT NULL);")
|
|
320
|
-
expect(migrationContent.split('\n').length).toEqual(1)
|
|
321
|
-
await execute(migrationContent);
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
it('should alter property to enum property', async () => {
|
|
325
|
-
const DDL = `
|
|
326
|
-
CREATE TABLE "public"."user" ("id" SERIAL PRIMARY KEY UNIQUE,"role" character varying(255) NOT NULL UNIQUE);
|
|
327
|
-
`;
|
|
328
|
-
await execute(DDL);
|
|
329
|
-
enum Role {
|
|
330
|
-
ADMIN = 'admin',
|
|
331
|
-
USER = 'user',
|
|
332
|
-
}
|
|
333
|
-
class User extends BaseEntity {
|
|
334
|
-
@PrimaryKey({ autoIncrement: true })
|
|
335
|
-
id: number;
|
|
336
|
-
|
|
337
|
-
@Enum(() => Role)
|
|
338
|
-
role: Role;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
Entity()(User);
|
|
342
|
-
|
|
343
|
-
const migrator = new Migrator();
|
|
344
|
-
await migrator.initConfigFile();
|
|
345
|
-
await migrator.createMigration( 'test');
|
|
346
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
347
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
348
|
-
|
|
349
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" DROP CONSTRAINT \"user_role_key\";\nALTER TABLE \"public\".\"user\" DROP COLUMN IF EXISTS \"role\";\nCREATE TYPE \"public_user_role_enum\" AS ENUM ('admin', 'user');\nALTER TABLE \"public\".\"user\" ADD COLUMN \"role\" \"public_user_role_enum\" NOT NULL;")
|
|
350
|
-
expect(migrationContent.split('\n').length).toEqual(4)
|
|
351
|
-
await execute(migrationContent);
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
it('should alter property enum values', async () => {
|
|
355
|
-
const DDL = `
|
|
356
|
-
CREATE TYPE "public_user_role_enum" AS ENUM ('admin', 'user');
|
|
357
|
-
CREATE TABLE "public"."user" ("id" SERIAL PRIMARY KEY UNIQUE,"role" public_user_role_enum NOT NULL);
|
|
358
|
-
`;
|
|
359
|
-
await execute(DDL);
|
|
360
|
-
enum Role {
|
|
361
|
-
ADMIN = 'admin',
|
|
362
|
-
USER = 'user',
|
|
363
|
-
MODERATOR = 'moderator',
|
|
364
|
-
}
|
|
365
|
-
class User extends BaseEntity {
|
|
366
|
-
@PrimaryKey({ autoIncrement: true })
|
|
367
|
-
id: number;
|
|
368
|
-
|
|
369
|
-
@Enum(() => Role)
|
|
370
|
-
role: Role;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
Entity()(User);
|
|
374
|
-
|
|
375
|
-
const migrator = new Migrator();
|
|
376
|
-
await migrator.initConfigFile();
|
|
377
|
-
await migrator.createMigration( 'test');
|
|
378
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
379
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
380
|
-
|
|
381
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" ALTER COLUMN \"role\" TYPE varchar(255);DROP TYPE IF EXISTS \"public_user_role_enum\";CREATE TYPE \"public_user_role_enum\" AS ENUM ('admin', 'user', 'moderator');ALTER TABLE \"public\".\"user\" ALTER COLUMN \"role\" TYPE \"public_user_role_enum\" USING \"role\"::text::\"public_user_role_enum\"")
|
|
382
|
-
expect(migrationContent.split('\n').length).toEqual(1)
|
|
383
|
-
await execute(migrationContent);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
it('should alter property enum to string', async () => {
|
|
387
|
-
const DDL = `
|
|
388
|
-
CREATE TYPE "public_user_role_enum" AS ENUM ('admin', 'user');
|
|
389
|
-
CREATE TABLE "public"."user" ("id" SERIAL PRIMARY KEY UNIQUE,"role" public_user_role_enum NOT NULL);
|
|
390
|
-
`;
|
|
391
|
-
await execute(DDL);
|
|
392
|
-
class User extends BaseEntity {
|
|
393
|
-
@PrimaryKey({ autoIncrement: true })
|
|
394
|
-
id: number;
|
|
395
|
-
|
|
396
|
-
@Property()
|
|
397
|
-
role: string;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
Entity()(User);
|
|
401
|
-
|
|
402
|
-
const migrator = new Migrator();
|
|
403
|
-
await migrator.initConfigFile();
|
|
404
|
-
await migrator.createMigration( 'test');
|
|
405
|
-
const migrationFilePath = path.join(__dirname, '/test.sql');
|
|
406
|
-
const migrationContent = fs.readFileSync(migrationFilePath, {encoding: 'utf-8'});
|
|
407
|
-
|
|
408
|
-
expect(migrationContent).toContain("ALTER TABLE \"public\".\"user\" ALTER COLUMN \"role\" TYPE character varying(255);\nDROP TYPE IF EXISTS \"public_user_role_enum\";")
|
|
409
|
-
expect(migrationContent.split('\n').length).toEqual(2)
|
|
410
|
-
await execute(migrationContent);
|
|
411
|
-
});
|
|
412
|
-
})
|
package/test/node-database.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { Orm, OrmService, PgDriver } from '@cheetah.js/orm';
|
|
3
|
-
import { EntityStorage } from 'packages/orm/src/domain/entities';
|
|
4
|
-
import { LoggerService } from '@cheetah.js/core/services/logger.service';
|
|
5
|
-
import { spyOn } from 'bun:test';
|
|
6
|
-
|
|
7
|
-
const loggerInstance = new LoggerService({applicationConfig: {logger: { level: 'info'}}} as any)
|
|
8
|
-
export let app: Orm<PgDriver>
|
|
9
|
-
export const mockLogger = spyOn(loggerInstance, 'debug')
|
|
10
|
-
|
|
11
|
-
export async function startDatabase(entityFile: string | undefined = undefined, logger: LoggerService = loggerInstance) {
|
|
12
|
-
app = new Orm(logger)
|
|
13
|
-
const service = new OrmService(app, new EntityStorage(), entityFile)
|
|
14
|
-
await service.onInit({
|
|
15
|
-
host: 'localhost',
|
|
16
|
-
port: 5432,
|
|
17
|
-
database: 'postgres',
|
|
18
|
-
username: 'postgres',
|
|
19
|
-
password: 'postgres',
|
|
20
|
-
driver: PgDriver,
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export async function purgeDatabase(schema: string = 'public') {
|
|
25
|
-
await app.driverInstance.executeSql(`DROP SCHEMA IF EXISTS ${schema} CASCADE; CREATE SCHEMA ${schema};`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function execute(sql: string) {
|
|
29
|
-
return await app.driverInstance.executeSql(sql);
|
|
30
|
-
}
|