@koalarx/nest 3.1.14 → 3.1.16
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/core/database/repository.base.js +31 -10
- package/core/utils/generate-prisma-include-schema.js +16 -10
- package/package.json +1 -1
- package/test/utils/create-e2e-database.d.ts +6 -1
- package/test/utils/create-e2e-database.js +4 -13
- package/test/utils/e2e-database-client.d.ts +7 -0
- package/test/utils/e2e-database-client.js +12 -0
- package/tsconfig.lib.tsbuildinfo +1 -1
- package/test/utils/drop-e2e-database.d.ts +0 -2
- package/test/utils/drop-e2e-database.js +0 -33
|
@@ -17,7 +17,7 @@ class RepositoryBase {
|
|
|
17
17
|
this._modelName = modelName;
|
|
18
18
|
this._includeFindMany = (0, generate_prisma_include_schema_1.generateIncludeSchema)({
|
|
19
19
|
forList: true,
|
|
20
|
-
deepLimit:
|
|
20
|
+
deepLimit: 2,
|
|
21
21
|
entity: this._modelName,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
@@ -58,17 +58,22 @@ class RepositoryBase {
|
|
|
58
58
|
}
|
|
59
59
|
getSelectRootPrismaSchema(entity) {
|
|
60
60
|
const selectSchema = {};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
const entityProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
|
|
62
|
+
entityProps.forEach((prop) => {
|
|
63
|
+
let instance;
|
|
64
|
+
try {
|
|
65
|
+
instance = new (prop.type())();
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
instance = null;
|
|
69
|
+
}
|
|
70
|
+
if (instance instanceof entity_base_1.EntityBase) {
|
|
71
|
+
selectSchema[prop.name] = {
|
|
72
|
+
select: this.getSelectRootPrismaSchema(instance.constructor),
|
|
68
73
|
};
|
|
69
74
|
}
|
|
70
75
|
else {
|
|
71
|
-
selectSchema[
|
|
76
|
+
selectSchema[prop.name] = true;
|
|
72
77
|
}
|
|
73
78
|
});
|
|
74
79
|
return selectSchema;
|
|
@@ -183,6 +188,22 @@ class RepositoryBase {
|
|
|
183
188
|
});
|
|
184
189
|
}
|
|
185
190
|
}
|
|
191
|
+
else if (entity[key] instanceof entity_base_1.EntityBase) {
|
|
192
|
+
const entityInstance = entity[key];
|
|
193
|
+
const modelName = entity[key].constructor.name;
|
|
194
|
+
if (entity[key]._action === entity_base_1.EntityActionType.update) {
|
|
195
|
+
relationUpdates.push({
|
|
196
|
+
modelName: (0, KlString_1.toCamelCase)(modelName),
|
|
197
|
+
entityInstance,
|
|
198
|
+
schema: {
|
|
199
|
+
where: { id: entityInstance._id },
|
|
200
|
+
data: this.entityToPrisma(entityInstance),
|
|
201
|
+
select: this.getSelectRootPrismaSchema(entityInstance),
|
|
202
|
+
},
|
|
203
|
+
relations: this.listRelationEntities(entityInstance),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
186
207
|
});
|
|
187
208
|
return { relationCreates, relationUpdates, relationDeletes };
|
|
188
209
|
}
|
|
@@ -269,7 +290,7 @@ class RepositoryBase {
|
|
|
269
290
|
async loadRelationForEntity(where, entity, cache) {
|
|
270
291
|
return this._context[(0, KlString_1.toCamelCase)((0, KlString_1.toCamelCase)(entity.name))]
|
|
271
292
|
.findFirst({
|
|
272
|
-
select: this.
|
|
293
|
+
select: this.getSelectRootPrismaSchema(entity),
|
|
273
294
|
where,
|
|
274
295
|
})
|
|
275
296
|
.then((data) => this.enrichEntityWithRelations(entity, data, cache));
|
|
@@ -2,33 +2,39 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateIncludeSchema = generateIncludeSchema;
|
|
4
4
|
const entity_base_1 = require("../database/entity.base");
|
|
5
|
-
const list_1 = require("./list");
|
|
6
5
|
const auto_mapping_list_1 = require("../mapping/auto-mapping-list");
|
|
6
|
+
const list_1 = require("./list");
|
|
7
7
|
function generateIncludeSchema({ forList, entity, deepIncludeCount = 0, deepLimit, }) {
|
|
8
8
|
if (deepIncludeCount >= deepLimit) {
|
|
9
9
|
return true;
|
|
10
10
|
}
|
|
11
11
|
const includeSchema = {};
|
|
12
|
+
const props = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
|
|
12
13
|
const entityInstance = new entity();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
props.forEach((prop) => {
|
|
15
|
+
let instance;
|
|
16
|
+
try {
|
|
17
|
+
instance = new (prop.type())();
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
instance = null;
|
|
21
|
+
}
|
|
16
22
|
let includes;
|
|
17
|
-
if (
|
|
23
|
+
if (instance instanceof list_1.List) {
|
|
18
24
|
if (forList) {
|
|
19
|
-
includeSchema[
|
|
25
|
+
includeSchema[prop.name] = true;
|
|
20
26
|
}
|
|
21
27
|
else {
|
|
22
28
|
includes = generateIncludeSchema({
|
|
23
29
|
forList,
|
|
24
|
-
entity:
|
|
30
|
+
entity: instance.entityType,
|
|
25
31
|
deepLimit,
|
|
26
32
|
deepIncludeCount: deepIncludeCount > 0 ? deepIncludeCount + 1 : 1,
|
|
27
33
|
});
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
36
|
else {
|
|
31
|
-
const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(entityInstance.constructor,
|
|
37
|
+
const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(entityInstance.constructor, prop.name);
|
|
32
38
|
if (propDefinitions) {
|
|
33
39
|
const source = auto_mapping_list_1.AutoMappingList.getSourceByName(propDefinitions.type);
|
|
34
40
|
if (source?.prototype instanceof entity_base_1.EntityBase) {
|
|
@@ -43,10 +49,10 @@ function generateIncludeSchema({ forList, entity, deepIncludeCount = 0, deepLimi
|
|
|
43
49
|
}
|
|
44
50
|
if (includes) {
|
|
45
51
|
if (includes === true || Object.keys(includes).length > 0) {
|
|
46
|
-
includeSchema[
|
|
52
|
+
includeSchema[prop.name] = includes;
|
|
47
53
|
}
|
|
48
54
|
else {
|
|
49
|
-
includeSchema[
|
|
55
|
+
includeSchema[prop.name] = true;
|
|
50
56
|
}
|
|
51
57
|
}
|
|
52
58
|
});
|
package/package.json
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
1
2
|
import 'dotenv/config';
|
|
2
|
-
|
|
3
|
+
import { E2EDatabaseClient } from './e2e-database-client';
|
|
4
|
+
export declare function createE2EDatabase<T extends E2EDatabaseClient>(runtime: "node" | "bun" | undefined, clientInstance: Type<T>): Promise<{
|
|
5
|
+
client: T;
|
|
6
|
+
schemaId: `${string}-${string}-${string}-${string}-${string}`;
|
|
7
|
+
}>;
|
|
@@ -4,7 +4,6 @@ exports.createE2EDatabase = createE2EDatabase;
|
|
|
4
4
|
require("dotenv/config");
|
|
5
5
|
const node_child_process_1 = require("node:child_process");
|
|
6
6
|
const node_crypto_1 = require("node:crypto");
|
|
7
|
-
const pg_1 = require("pg");
|
|
8
7
|
function generateUniqueDatabaseURL() {
|
|
9
8
|
const schemaId = (0, node_crypto_1.randomUUID)();
|
|
10
9
|
if (!process.env.DATABASE_URL) {
|
|
@@ -17,31 +16,23 @@ function generateUniqueDatabaseURL() {
|
|
|
17
16
|
schemaId,
|
|
18
17
|
};
|
|
19
18
|
}
|
|
20
|
-
async function createE2EDatabase(runtime = 'node') {
|
|
19
|
+
async function createE2EDatabase(runtime = 'node', clientInstance) {
|
|
21
20
|
const { url, schemaId } = generateUniqueDatabaseURL();
|
|
22
21
|
process.env.DATABASE_URL = url;
|
|
23
|
-
process.env.DIRECT_URL = url;
|
|
24
22
|
process.env.PRISMA_SCHEMA_DISABLE_ADVISORY_LOCK = 'true';
|
|
25
23
|
try {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const pool = new pg_1.Pool({ connectionString: baseUrl.toString() });
|
|
29
|
-
try {
|
|
30
|
-
await pool.query(`CREATE DATABASE "${schemaId}"`);
|
|
31
|
-
}
|
|
32
|
-
finally {
|
|
33
|
-
await pool.end();
|
|
34
|
-
}
|
|
24
|
+
const client = new clientInstance(url, schemaId);
|
|
25
|
+
await client.createDatabase(schemaId);
|
|
35
26
|
const env = { ...process.env, DATABASE_URL: url, DIRECT_URL: url };
|
|
36
27
|
(0, node_child_process_1.execSync)(`${runtime}x prisma migrate deploy`, {
|
|
37
28
|
cwd: process.cwd(),
|
|
38
29
|
env,
|
|
39
30
|
stdio: 'inherit',
|
|
40
31
|
});
|
|
32
|
+
return { client, schemaId };
|
|
41
33
|
}
|
|
42
34
|
catch (error) {
|
|
43
35
|
console.error('Erro ao criar banco de dados e2e:', error);
|
|
44
36
|
throw error;
|
|
45
37
|
}
|
|
46
|
-
return schemaId;
|
|
47
38
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare abstract class E2EDatabaseClient {
|
|
2
|
+
protected readonly url: string;
|
|
3
|
+
protected readonly schemaName: string;
|
|
4
|
+
constructor(url: string, schemaName: string);
|
|
5
|
+
abstract createDatabase(schemaName: string): Promise<void>;
|
|
6
|
+
abstract dropDatabase(): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.E2EDatabaseClient = void 0;
|
|
4
|
+
class E2EDatabaseClient {
|
|
5
|
+
url;
|
|
6
|
+
schemaName;
|
|
7
|
+
constructor(url, schemaName) {
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.schemaName = schemaName;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.E2EDatabaseClient = E2EDatabaseClient;
|