Skip to content

Commit 1d0b83b

Browse files
committed
fix(json-api-nestjs-microorm): replace deprecated faker methods and add PGlite compatibility in MikroORM setup
add transaction for CURRENT_ENTITY_MANAGER_TOKEN transaction for atomic module
1 parent 1aa21f5 commit 1d0b83b

File tree

12 files changed

+43
-73
lines changed

12 files changed

+43
-73
lines changed

libs/json-api/json-api-nestjs-microorm/src/lib/factory/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ export function CurrentMicroOrmProvider(
5050
): FactoryProvider<MikroORM> {
5151
return {
5252
provide: CURRENT_DATA_SOURCE_TOKEN,
53-
useFactory: (mikroORM: MikroORM) => {
54-
return mikroORM;
55-
},
53+
useFactory: (mikroORM: MikroORM) => mikroORM,
5654
inject: [connectionName ? getMikroORMToken(connectionName) : MikroORM],
5755
};
5856
}
@@ -132,9 +130,10 @@ export function EntityPropsMap<E extends object>(entities: EntityClass<E>[]) {
132130
export function RunInTransactionFactory(): FactoryProvider<RunInTransaction> {
133131
return {
134132
provide: RUN_IN_TRANSACTION_FUNCTION,
135-
inject: [],
136-
useFactory() {
137-
return async (callback) => callback();
133+
inject: [CURRENT_ENTITY_MANAGER_TOKEN],
134+
useFactory(entityManager: EntityManager) {
135+
return (callback) =>
136+
entityManager.transactional(() => callback());
138137
},
139138
};
140139
}

libs/json-api/json-api-nestjs-microorm/src/lib/mock-utils/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,20 @@ import {
2626
OrmServiceFactory,
2727
EntityPropsMap,
2828
} from '../factory';
29-
import { MicroOrmUtilService } from '../service/micro-orm-util.service';
29+
import { MicroOrmUtilService } from '../service';
3030

3131
export * from './entities';
3232
export * from './utils';
3333

34-
import { sharedConnect, initMikroOrm, pullAllData } from './utils';
34+
import { initMikroOrm, pullAllData } from './utils';
3535
import { DEFAULT_ARRAY_TYPE } from '../constants';
3636

3737
export const entities = [Users, UserGroups, Roles, Comments, Addresses, Notes];
3838

3939
export function mockDbPgLiteTestModule(dbName = `test_db_${Date.now()}`) {
4040
const mikroORM = {
4141
provide: MikroORM,
42-
useFactory: async function () {
43-
const knexInst = await sharedConnect();
44-
return initMikroOrm(knexInst, dbName);
45-
},
42+
useFactory: () => initMikroOrm(dbName),
4643
};
4744
return {
4845
module: MikroOrmModule,

libs/json-api/json-api-nestjs-microorm/src/lib/mock-utils/utils/init-db.ts

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import { MikroORM } from '@mikro-orm/core';
33
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
44
import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
55

6-
import Knex from 'knex';
7-
import ClientPgLite from 'knex-pglite';
8-
96
import {
107
Addresses,
118
Comments,
@@ -14,63 +11,37 @@ import {
1411
UserGroups,
1512
Users,
1613
} from '../entities';
17-
18-
let knexInst: TypeKnex;
19-
20-
export async function sharedConnect(): Promise<TypeKnex> {
21-
if (knexInst) {
22-
return knexInst;
23-
}
24-
25-
const pgLite = await Promise.all([
26-
import('@electric-sql/pglite'),
27-
// @ts-ignore
28-
import('@electric-sql/pglite/contrib/uuid_ossp'),
29-
]).then(
30-
([{ PGlite }, { uuid_ossp }]) =>
31-
new PGlite({
32-
extensions: { uuid_ossp },
33-
})
34-
);
35-
36-
knexInst = Knex({
37-
client: ClientPgLite,
38-
dialect: 'postgres',
39-
// @ts-ignore
40-
connection: { pglite: pgLite },
14+
import { PGlite } from '@electric-sql/pglite';
15+
// @ts-ignore
16+
import { PGliteDriver, PGliteConnectionConfig } from 'mikro-orm-pglite';
17+
// @ts-ignore
18+
import { uuid_ossp } from '@electric-sql/pglite/contrib/uuid_ossp';
19+
20+
export async function initMikroOrm(testDbName: string) {
21+
const pgLite = new PGlite({
22+
extensions: { uuid_ossp },
4123
});
4224

43-
return knexInst;
44-
}
45-
46-
export async function initMikroOrm(knex: TypeKnex, testDbName: string) {
47-
const result = await knex.raw(
48-
`select 1 from pg_database where datname = '${testDbName}'`
49-
);
50-
51-
if ((result['rows'] as []).length === 0) {
52-
await knex.raw(`create database ??`, [testDbName]);
53-
}
54-
5525
const orm = await MikroORM.init<PostgreSqlDriver>({
5626
highlighter: new SqlHighlighter(),
57-
driver: PostgreSqlDriver,
27+
driver: PGliteDriver,
5828
dbName: testDbName,
59-
driverOptions: knexInst,
29+
driverOptions: {
30+
connection: {
31+
pglite: () => pgLite,
32+
} satisfies PGliteConnectionConfig,
33+
},
6034
entities: [Users, UserGroups, Roles, Comments, Addresses, Notes],
6135
allowGlobalContext: true,
6236
schema: 'public',
6337
debug:
6438
process.env['DB_LOGGING'] !== '0' ? ['query', 'query-params'] : false,
6539
});
6640

67-
if ((result['rows'] as []).length === 0) {
68-
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL();
69-
const statements = sql.split(';').filter((s) => s.trim().length > 0); // Разбиваем на отдельные команды
70-
71-
for (const statement of statements) {
72-
await orm.em.execute(statement);
73-
}
41+
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL();
42+
const statements = sql.split(';').filter((s) => s.trim().length > 0); // Разбиваем на отдельные команды
43+
for (const statement of statements) {
44+
await orm.em.execute(statement);
7445
}
7546

7647
return orm;

libs/json-api/json-api-nestjs-microorm/src/lib/mock-utils/utils/pull-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export async function pullUser() {
4747
user.firstName = faker.person.firstName();
4848
user.lastName = faker.person.lastName();
4949
user.isActive = faker.datatype.boolean();
50-
user.login = faker.internet.userName({
50+
user.login = faker.internet.username({
5151
lastName: user.lastName,
5252
firstName: user.firstName,
5353
});

libs/json-api/json-api-nestjs-microorm/src/lib/orm-methods/delete-relationship/delete-relationship.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('delete-relationship', () => {
7474
firstName: firstName,
7575
lastName: lastName,
7676
isActive: faker.datatype.boolean(),
77-
login: faker.internet.userName({
77+
login: faker.internet.username({
7878
lastName: firstName,
7979
firstName: lastName,
8080
}),

libs/json-api/json-api-nestjs-microorm/src/lib/orm-methods/get-relationship/get-relationship.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('get-relationship', () => {
7878
firstName: firstName,
7979
lastName: lastName,
8080
isActive: faker.datatype.boolean(),
81-
login: faker.internet.userName({
81+
login: faker.internet.username({
8282
lastName: firstName,
8383
firstName: lastName,
8484
}),

libs/json-api/json-api-nestjs-microorm/src/lib/orm-methods/patch-one/patch-one.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('patch-one', () => {
8686
firstName: firstName,
8787
lastName: lastName,
8888
isActive: faker.datatype.boolean(),
89-
login: faker.internet.userName({
89+
login: faker.internet.username({
9090
lastName: firstName,
9191
firstName: lastName,
9292
}),

libs/json-api/json-api-nestjs-microorm/src/lib/orm-methods/patch-relationship/patch-relationship.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('patch-relationship', () => {
6363
firstName: firstName,
6464
lastName: lastName,
6565
isActive: faker.datatype.boolean(),
66-
login: faker.internet.userName({
66+
login: faker.internet.username({
6767
lastName: firstName,
6868
firstName: lastName,
6969
}),

libs/json-api/json-api-nestjs-microorm/src/lib/orm-methods/post-one/post-one.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('post-one', () => {
8282
firstName: firstName,
8383
lastName: lastName,
8484
isActive: faker.datatype.boolean(),
85-
login: faker.internet.userName({
85+
login: faker.internet.username({
8686
lastName: firstName,
8787
firstName: lastName,
8888
}),

libs/json-api/json-api-nestjs-microorm/src/lib/orm-methods/post-relationship/post-relationship.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('post-relationshipa', () => {
6363
firstName: firstName,
6464
lastName: lastName,
6565
isActive: faker.datatype.boolean(),
66-
login: faker.internet.userName({
66+
login: faker.internet.username({
6767
lastName: firstName,
6868
firstName: lastName,
6969
}),

0 commit comments

Comments
 (0)