@loomcore/api 0.0.59 → 0.1.0
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/LICENSE +201 -0
- package/README.md +50 -0
- package/dist/__tests__/common-test.utils.d.ts +29 -60
- package/dist/__tests__/common-test.utils.js +88 -166
- package/dist/__tests__/index.d.ts +6 -0
- package/dist/__tests__/index.js +6 -0
- package/dist/__tests__/models/category.model.d.ts +8 -0
- package/dist/__tests__/models/category.model.js +6 -0
- package/dist/__tests__/models/mongo-test-entity.model.d.ts +11 -0
- package/dist/__tests__/models/mongo-test-entity.model.js +13 -0
- package/dist/__tests__/models/product.model.d.ts +17 -0
- package/dist/__tests__/models/product.model.js +10 -0
- package/dist/__tests__/models/test-entity.model.d.ts +11 -0
- package/dist/__tests__/models/test-entity.model.js +10 -0
- package/dist/__tests__/models/test-item.model.d.ts +12 -0
- package/dist/__tests__/models/test-item.model.js +9 -0
- package/dist/__tests__/mongo-db.test-database.d.ts +15 -0
- package/dist/__tests__/mongo-db.test-database.js +74 -0
- package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.js +59 -0
- package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.js +52 -0
- package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.js +62 -0
- package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.js +66 -0
- package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.js +50 -0
- package/dist/__tests__/postgres-test-migrations/run-test-migrations.d.ts +2 -0
- package/dist/__tests__/postgres-test-migrations/run-test-migrations.js +22 -0
- package/dist/__tests__/postgres.test-database.d.ts +13 -0
- package/dist/__tests__/postgres.test-database.js +85 -0
- package/dist/__tests__/test-database.interface.d.ts +7 -0
- package/dist/__tests__/test-express-app.d.ts +9 -7
- package/dist/__tests__/test-express-app.js +38 -48
- package/dist/__tests__/test-mongo-db.d.ts +14 -0
- package/dist/__tests__/test-mongo-db.js +81 -0
- package/dist/__tests__/test-objects.d.ts +23 -0
- package/dist/__tests__/test-objects.js +45 -0
- package/dist/__tests__/test-user.d.ts +3 -0
- package/dist/__tests__/test-user.js +16 -0
- package/dist/config/base-api-config.d.ts +2 -2
- package/dist/config/base-api-config.js +2 -2
- package/dist/controllers/api.controller.d.ts +1 -5
- package/dist/controllers/api.controller.js +4 -11
- package/dist/controllers/auth.controller.d.ts +2 -2
- package/dist/controllers/auth.controller.js +4 -5
- package/dist/controllers/organizations.controller.d.ts +2 -2
- package/dist/controllers/organizations.controller.js +4 -4
- package/dist/controllers/users.controller.d.ts +2 -2
- package/dist/controllers/users.controller.js +2 -2
- package/dist/databases/index.d.ts +1 -0
- package/dist/databases/index.js +1 -0
- package/dist/databases/models/constants.d.ts +1 -0
- package/dist/databases/models/constants.js +1 -0
- package/dist/databases/models/database.d.ts +3 -0
- package/dist/databases/models/database.interface.d.ts +28 -0
- package/dist/databases/models/delete-result.d.ts +5 -0
- package/dist/databases/models/delete-result.js +8 -0
- package/dist/databases/models/index.d.ts +5 -0
- package/dist/databases/models/index.js +5 -0
- package/dist/databases/models/update-result.d.ts +5 -0
- package/dist/databases/models/update-result.js +8 -0
- package/dist/databases/mongo-db/commands/batch-update.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/batch-update.command.js +41 -0
- package/dist/databases/mongo-db/commands/create-many.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/create-many.command.js +17 -0
- package/dist/databases/mongo-db/commands/create.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/create.command.js +17 -0
- package/dist/databases/mongo-db/commands/delete-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/delete-by-id.command.js +9 -0
- package/dist/databases/mongo-db/commands/delete-many.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/delete-many.command.js +9 -0
- package/dist/databases/mongo-db/commands/full-updateby-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/full-updateby-id.command.js +21 -0
- package/dist/databases/mongo-db/commands/index.d.ts +8 -0
- package/dist/databases/mongo-db/commands/index.js +8 -0
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.js +41 -0
- package/dist/databases/mongo-db/commands/mongo-create-many.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/mongo-create-many.command.js +17 -0
- package/dist/databases/mongo-db/commands/mongo-create.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/mongo-create.command.js +17 -0
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.js +14 -0
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.js +9 -0
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.js +25 -0
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.js +25 -0
- package/dist/databases/mongo-db/commands/mongo-update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-update.command.js +19 -0
- package/dist/databases/mongo-db/commands/partial-update-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/partial-update-by-id.command.js +21 -0
- package/dist/databases/mongo-db/commands/update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/update.command.js +19 -0
- package/dist/databases/mongo-db/index.d.ts +4 -0
- package/dist/databases/mongo-db/index.js +4 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.d.ts +15 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.interface.d.ts +11 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.js +43 -0
- package/dist/databases/mongo-db/mongo-db.database.d.ts +32 -0
- package/dist/databases/mongo-db/mongo-db.database.js +65 -0
- package/dist/databases/mongo-db/queries/find-one.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/find-one.query.js +9 -0
- package/dist/databases/mongo-db/queries/find.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/find.query.js +9 -0
- package/dist/databases/mongo-db/queries/get-all.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/get-all.query.js +17 -0
- package/dist/databases/mongo-db/queries/get-by-id.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/get-by-id.query.js +20 -0
- package/dist/databases/mongo-db/queries/get-count.query.d.ts +2 -0
- package/dist/databases/mongo-db/queries/get-count.query.js +5 -0
- package/dist/databases/mongo-db/queries/get.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/get.query.js +14 -0
- package/dist/databases/mongo-db/queries/index.d.ts +6 -0
- package/dist/databases/mongo-db/queries/index.js +6 -0
- package/dist/databases/mongo-db/queries/mongo-find-one.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-find-one.query.js +9 -0
- package/dist/databases/mongo-db/queries/mongo-find.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-find.query.js +9 -0
- package/dist/databases/mongo-db/queries/mongo-get-all.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-get-all.query.js +17 -0
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.js +17 -0
- package/dist/databases/mongo-db/queries/mongo-get-count.query.d.ts +2 -0
- package/dist/databases/mongo-db/queries/mongo-get-count.query.js +5 -0
- package/dist/databases/mongo-db/queries/mongo-get.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/mongo-get.query.js +14 -0
- package/dist/databases/mongo-db/utils/build-find-options.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/build-find-options.util.js +15 -0
- package/dist/databases/mongo-db/utils/build-no-sql-match.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/build-no-sql-match.util.js +59 -0
- package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.d.ts +1 -0
- package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.js +32 -0
- package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.js +68 -0
- package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.js +31 -0
- package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.js +72 -0
- package/dist/databases/mongo-db/utils/index.d.ts +7 -0
- package/dist/databases/mongo-db/utils/index.js +7 -0
- package/dist/databases/operations/join.operation.d.ts +7 -0
- package/dist/databases/operations/join.operation.js +12 -0
- package/dist/databases/operations/operation.d.ts +2 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.js +56 -0
- package/dist/databases/postgres/commands/postgres-create-many.command.d.ts +6 -0
- package/dist/databases/postgres/commands/postgres-create-many.command.js +63 -0
- package/dist/databases/postgres/commands/postgres-create.command.d.ts +6 -0
- package/dist/databases/postgres/commands/postgres-create.command.js +29 -0
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.d.ts +3 -0
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.js +6 -0
- package/dist/databases/postgres/commands/postgres-delete-many.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-delete-many.command.js +13 -0
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +72 -0
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +42 -0
- package/dist/databases/postgres/commands/postgres-update.command.d.ts +5 -0
- package/dist/databases/postgres/commands/postgres-update.command.js +48 -0
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +52 -0
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +55 -0
- package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/003-create-users-table.migration.js +65 -0
- package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.js +57 -0
- package/dist/databases/postgres/migrations/index.d.ts +3 -0
- package/dist/databases/postgres/migrations/index.js +3 -0
- package/dist/databases/postgres/migrations/migration.d.ts +6 -0
- package/dist/databases/postgres/migrations/migration.interface.d.ts +6 -0
- package/dist/databases/postgres/migrations/migration.interface.js +1 -0
- package/dist/databases/postgres/migrations/migration.js +14 -0
- package/dist/databases/postgres/migrations/runMigrations.d.ts +2 -0
- package/dist/databases/postgres/migrations/runMigrations.js +20 -0
- package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +2 -0
- package/dist/databases/postgres/migrations/setup-for-auth.migration.js +18 -0
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.d.ts +2 -0
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +16 -0
- package/dist/databases/postgres/postgres.database.d.ts +31 -0
- package/dist/databases/postgres/postgres.database.js +69 -0
- package/dist/databases/postgres/queries/postgres-find-one.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-find-one.query.js +13 -0
- package/dist/databases/postgres/queries/postgres-find.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-find.query.js +11 -0
- package/dist/databases/postgres/queries/postgres-get-all.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-get-all.query.js +14 -0
- package/dist/databases/postgres/queries/postgres-get-by-id.query.d.ts +4 -0
- package/dist/databases/postgres/queries/postgres-get-by-id.query.js +26 -0
- package/dist/databases/postgres/queries/postgres-get-count.query.d.ts +2 -0
- package/dist/databases/postgres/queries/postgres-get-count.query.js +4 -0
- package/dist/databases/postgres/queries/postgres-get.query.d.ts +4 -0
- package/dist/databases/postgres/queries/postgres-get.query.js +26 -0
- package/dist/databases/postgres/utils/build-count-query.d.ts +3 -0
- package/dist/databases/postgres/utils/build-count-query.js +7 -0
- package/dist/databases/postgres/utils/build-join-clauses.d.ts +2 -0
- package/dist/databases/postgres/utils/build-join-clauses.js +12 -0
- package/dist/databases/postgres/utils/build-order-by-clause.d.ts +2 -0
- package/dist/databases/postgres/utils/build-order-by-clause.js +8 -0
- package/dist/databases/postgres/utils/build-pagination-clause.d.ts +2 -0
- package/dist/databases/postgres/utils/build-pagination-clause.js +9 -0
- package/dist/databases/postgres/utils/build-select-clause.d.ts +3 -0
- package/dist/databases/postgres/utils/build-select-clause.js +28 -0
- package/dist/databases/postgres/utils/build-where-clause.d.ts +5 -0
- package/dist/databases/postgres/utils/build-where-clause.js +50 -0
- package/dist/databases/postgres/utils/columns-and-values-from-entity.d.ts +5 -0
- package/dist/databases/postgres/utils/columns-and-values-from-entity.js +9 -0
- package/dist/databases/postgres/utils/convert-null-to-undefined.util.d.ts +2 -0
- package/dist/databases/postgres/utils/convert-null-to-undefined.util.js +70 -0
- package/dist/databases/postgres/utils/transform-join-results.d.ts +2 -0
- package/dist/databases/postgres/utils/transform-join-results.js +33 -0
- package/dist/databases/utils/database-to-idatabase.util.d.ts +3 -0
- package/dist/databases/utils/database-to-idatabase.util.js +14 -0
- package/dist/databases/utils/get-property-schema.util.d.ts +2 -0
- package/dist/databases/utils/get-property-schema.util.js +15 -0
- package/dist/databases/utils/index.d.ts +1 -0
- package/dist/databases/utils/index.js +1 -0
- package/dist/models/base-api-config.interface.d.ts +3 -2
- package/dist/models/index.d.ts +1 -1
- package/dist/models/index.js +1 -1
- package/dist/models/refresh-token.d.ts +9 -0
- package/dist/models/refresh-token.js +2 -0
- package/dist/models/refresh-token.model.d.ts +18 -0
- package/dist/models/refresh-token.model.js +13 -0
- package/dist/models/refresh-token.spec.d.ts +1 -0
- package/dist/models/refresh-token.spec.js +12 -0
- package/dist/services/auth.service.d.ts +11 -18
- package/dist/services/auth.service.js +29 -50
- package/dist/services/generic-api-service/generic-api-service.interface.d.ts +29 -0
- package/dist/services/generic-api-service/generic-api-service.interface.js +1 -0
- package/dist/services/generic-api-service/generic-api.service.d.ts +37 -0
- package/dist/services/generic-api-service/generic-api.service.js +178 -0
- package/dist/services/index.d.ts +2 -2
- package/dist/services/index.js +2 -2
- package/dist/services/multi-tenant-api.service.d.ts +9 -6
- package/dist/services/multi-tenant-api.service.js +10 -18
- package/dist/services/organization.service.d.ts +5 -5
- package/dist/services/organization.service.js +9 -6
- package/dist/services/password-reset-token.service.d.ts +3 -3
- package/dist/services/password-reset-token.service.js +5 -5
- package/dist/services/tenant-query-decorator.d.ts +1 -1
- package/dist/services/tenant-query-decorator.js +1 -1
- package/dist/services/user.service.d.ts +4 -6
- package/dist/services/user.service.js +4 -10
- package/dist/services/utils/audit-for-create.util.d.ts +2 -0
- package/dist/services/utils/audit-for-create.util.js +9 -0
- package/dist/services/utils/audit-for-update.util.d.ts +2 -0
- package/dist/services/utils/audit-for-update.util.js +7 -0
- package/dist/services/utils/strip-sender-provided-system-properties.util.d.ts +2 -0
- package/dist/services/utils/strip-sender-provided-system-properties.util.js +15 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/api.utils.js +2 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/dist/utils/sql.db.utils.d.ts +14 -0
- package/dist/utils/sql.db.utils.js +94 -0
- package/package.json +4 -2
- package/dist/services/generic-api-service.interface.d.ts +0 -27
- package/dist/services/generic-api.service.d.ts +0 -50
- package/dist/services/generic-api.service.js +0 -424
- package/dist/utils/db.utils.d.ts +0 -27
- package/dist/utils/db.utils.js +0 -318
- /package/dist/{controllers/api-controller.utils.d.ts → __tests__/test-database.interface.js} +0 -0
- /package/dist/{controllers/api-controller.utils.js → databases/models/database.interface.js} +0 -0
- /package/dist/{models/types/index.d.ts → databases/models/database.js} +0 -0
- /package/dist/{models/types/index.js → databases/mongo-db/models/no-sql-pipeline.interface.js} +0 -0
- /package/dist/{services/generic-api-service.interface.js → databases/operations/operation.js} +0 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class CreateTestEntitiesTableMigration {
|
|
3
|
+
client;
|
|
4
|
+
orgId;
|
|
5
|
+
constructor(client, orgId) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.orgId = orgId;
|
|
8
|
+
}
|
|
9
|
+
index = 1;
|
|
10
|
+
_id = randomUUID().toString();
|
|
11
|
+
async execute() {
|
|
12
|
+
try {
|
|
13
|
+
await this.client.query(`
|
|
14
|
+
CREATE TABLE "testEntities" (
|
|
15
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
16
|
+
"_orgId" VARCHAR(255),
|
|
17
|
+
"name" VARCHAR(255) NOT NULL,
|
|
18
|
+
"description" TEXT,
|
|
19
|
+
"isActive" BOOLEAN,
|
|
20
|
+
"tags" TEXT[],
|
|
21
|
+
"count" INTEGER,
|
|
22
|
+
"_created" TIMESTAMP NOT NULL,
|
|
23
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
24
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
25
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
26
|
+
"_deleted" TIMESTAMP,
|
|
27
|
+
"_deletedBy" VARCHAR(255)
|
|
28
|
+
)
|
|
29
|
+
`);
|
|
30
|
+
if (this.orgId) {
|
|
31
|
+
await this.client.query(`
|
|
32
|
+
Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
|
|
33
|
+
`);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
await this.client.query(`
|
|
37
|
+
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
|
|
38
|
+
`);
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error('Error creating test entities table:', error);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async revert() {
|
|
48
|
+
try {
|
|
49
|
+
await this.client.query(`
|
|
50
|
+
DROP TABLE test_entities;
|
|
51
|
+
`);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('Error reverting test entities table:', error);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMigration } from "../../databases/postgres/migrations/index.js";
|
|
2
|
+
import { Client } from "pg";
|
|
3
|
+
export declare class CreateCategoriesTableMigration implements IMigration {
|
|
4
|
+
private readonly client;
|
|
5
|
+
private readonly orgId?;
|
|
6
|
+
constructor(client: Client, orgId?: string | undefined);
|
|
7
|
+
index: number;
|
|
8
|
+
_id: string;
|
|
9
|
+
execute(): Promise<boolean>;
|
|
10
|
+
revert(): Promise<boolean>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class CreateCategoriesTableMigration {
|
|
3
|
+
client;
|
|
4
|
+
orgId;
|
|
5
|
+
constructor(client, orgId) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.orgId = orgId;
|
|
8
|
+
}
|
|
9
|
+
index = 2;
|
|
10
|
+
_id = randomUUID().toString();
|
|
11
|
+
async execute() {
|
|
12
|
+
try {
|
|
13
|
+
await this.client.query(`
|
|
14
|
+
CREATE TABLE "categories" (
|
|
15
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
16
|
+
"_orgId" VARCHAR(255),
|
|
17
|
+
"name" VARCHAR(255) NOT NULL
|
|
18
|
+
)
|
|
19
|
+
`);
|
|
20
|
+
if (this.orgId) {
|
|
21
|
+
await this.client.query(`
|
|
22
|
+
Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
|
|
23
|
+
`);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
await this.client.query(`
|
|
27
|
+
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.error('Error creating categories table:', error);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async revert() {
|
|
38
|
+
try {
|
|
39
|
+
await this.client.query(`
|
|
40
|
+
DROP TABLE "categories";
|
|
41
|
+
`);
|
|
42
|
+
await this.client.query(`
|
|
43
|
+
Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
|
|
44
|
+
`);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error('Error reverting categories table:', error);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
import { IMigration } from "../../databases/postgres/migrations/index.js";
|
|
3
|
+
export declare class CreateProductsTableMigration implements IMigration {
|
|
4
|
+
private readonly client;
|
|
5
|
+
private readonly orgId?;
|
|
6
|
+
constructor(client: Client, orgId?: string | undefined);
|
|
7
|
+
index: number;
|
|
8
|
+
_id: string;
|
|
9
|
+
execute(): Promise<boolean>;
|
|
10
|
+
revert(): Promise<boolean>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class CreateProductsTableMigration {
|
|
3
|
+
client;
|
|
4
|
+
orgId;
|
|
5
|
+
constructor(client, orgId) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.orgId = orgId;
|
|
8
|
+
}
|
|
9
|
+
index = 3;
|
|
10
|
+
_id = randomUUID().toString();
|
|
11
|
+
async execute() {
|
|
12
|
+
try {
|
|
13
|
+
await this.client.query(`
|
|
14
|
+
CREATE TABLE "products" (
|
|
15
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
16
|
+
"_orgId" VARCHAR(255),
|
|
17
|
+
"name" VARCHAR(255) NOT NULL,
|
|
18
|
+
"description" TEXT,
|
|
19
|
+
"internalNumber" VARCHAR(255),
|
|
20
|
+
"categoryId" VARCHAR(255) NOT NULL REFERENCES "categories"("_id"),
|
|
21
|
+
"_created" TIMESTAMP NOT NULL,
|
|
22
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
23
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
24
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
25
|
+
"_deleted" TIMESTAMP,
|
|
26
|
+
"_deletedBy" VARCHAR(255)
|
|
27
|
+
)
|
|
28
|
+
`);
|
|
29
|
+
if (this.orgId) {
|
|
30
|
+
await this.client.query(`
|
|
31
|
+
Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
|
|
32
|
+
`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
await this.client.query(`
|
|
36
|
+
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
|
|
37
|
+
`);
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error('Error creating products table:', error);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
async revert() {
|
|
48
|
+
try {
|
|
49
|
+
await this.client.query(`
|
|
50
|
+
DROP TABLE "products";
|
|
51
|
+
`);
|
|
52
|
+
await this.client.query(`
|
|
53
|
+
Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
|
|
54
|
+
`);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.error('Error reverting products table:', error);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
import { IMigration } from "../../databases/postgres/migrations/index.js";
|
|
3
|
+
export declare class CreateTestUsersTableMigration implements IMigration {
|
|
4
|
+
private readonly client;
|
|
5
|
+
private readonly orgId?;
|
|
6
|
+
constructor(client: Client, orgId?: string | undefined);
|
|
7
|
+
index: number;
|
|
8
|
+
_id: string;
|
|
9
|
+
execute(): Promise<boolean>;
|
|
10
|
+
revert(): Promise<boolean>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class CreateTestUsersTableMigration {
|
|
3
|
+
client;
|
|
4
|
+
orgId;
|
|
5
|
+
constructor(client, orgId) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.orgId = orgId;
|
|
8
|
+
}
|
|
9
|
+
index = 4;
|
|
10
|
+
_id = randomUUID().toString();
|
|
11
|
+
async execute() {
|
|
12
|
+
try {
|
|
13
|
+
await this.client.query(`
|
|
14
|
+
CREATE TABLE "testUsers" (
|
|
15
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
16
|
+
"_orgId" VARCHAR(255),
|
|
17
|
+
"email" VARCHAR(255) NOT NULL,
|
|
18
|
+
"password" VARCHAR(255) NOT NULL,
|
|
19
|
+
"firstName" VARCHAR(255),
|
|
20
|
+
"lastName" VARCHAR(255),
|
|
21
|
+
"displayName" VARCHAR(255),
|
|
22
|
+
"roles" TEXT[],
|
|
23
|
+
"_lastLoggedIn" TIMESTAMP,
|
|
24
|
+
"_lastPasswordChange" TIMESTAMP,
|
|
25
|
+
"_created" TIMESTAMP NOT NULL,
|
|
26
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
27
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
28
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
29
|
+
"_deleted" TIMESTAMP,
|
|
30
|
+
"_deletedBy" VARCHAR(255)
|
|
31
|
+
)
|
|
32
|
+
`);
|
|
33
|
+
if (this.orgId) {
|
|
34
|
+
await this.client.query(`
|
|
35
|
+
Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
|
|
36
|
+
`);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
await this.client.query(`
|
|
40
|
+
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.error('Error creating test users table:', error);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
async revert() {
|
|
52
|
+
try {
|
|
53
|
+
await this.client.query(`
|
|
54
|
+
DROP TABLE "testUsers";
|
|
55
|
+
`);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
await this.client.query(`
|
|
59
|
+
Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
|
|
60
|
+
`);
|
|
61
|
+
console.error('Error reverting test users table:', error);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
import { IMigration } from "../../databases/postgres/migrations/index.js";
|
|
3
|
+
export declare class CreateTestItemsTableMigration implements IMigration {
|
|
4
|
+
private readonly client;
|
|
5
|
+
private readonly orgId?;
|
|
6
|
+
constructor(client: Client, orgId?: string | undefined);
|
|
7
|
+
index: number;
|
|
8
|
+
_id: string;
|
|
9
|
+
execute(): Promise<boolean>;
|
|
10
|
+
revert(): Promise<boolean>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class CreateTestItemsTableMigration {
|
|
3
|
+
client;
|
|
4
|
+
orgId;
|
|
5
|
+
constructor(client, orgId) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
this.orgId = orgId;
|
|
8
|
+
}
|
|
9
|
+
index = 5;
|
|
10
|
+
_id = randomUUID().toString();
|
|
11
|
+
async execute() {
|
|
12
|
+
try {
|
|
13
|
+
await this.client.query(`
|
|
14
|
+
CREATE TABLE "testItems" (
|
|
15
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
16
|
+
"_orgId" VARCHAR(255),
|
|
17
|
+
"name" VARCHAR(255) NOT NULL,
|
|
18
|
+
"value" INTEGER,
|
|
19
|
+
"eventDate" TIMESTAMP,
|
|
20
|
+
"_created" TIMESTAMP NOT NULL,
|
|
21
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
22
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
23
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
24
|
+
"_deleted" TIMESTAMP,
|
|
25
|
+
"_deletedBy" VARCHAR(255)
|
|
26
|
+
)
|
|
27
|
+
`);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error('Error creating test items table:', error);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
async revert() {
|
|
36
|
+
try {
|
|
37
|
+
await this.client.query(`
|
|
38
|
+
DROP TABLE "testItems";
|
|
39
|
+
`);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
await this.client.query(`
|
|
43
|
+
Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
|
|
44
|
+
`);
|
|
45
|
+
console.error('Error reverting test items table:', error);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CreateTestEntitiesTableMigration } from "./001-create-test-entities-table.migration.js";
|
|
2
|
+
import { CreateCategoriesTableMigration } from "./002-create-categories-table.migration.js";
|
|
3
|
+
import { CreateProductsTableMigration } from "./003-create-products-table.migration.js";
|
|
4
|
+
import { CreateTestUsersTableMigration } from "./004-create-test-users-table.migration.js";
|
|
5
|
+
import { CreateTestItemsTableMigration } from "./005-create-test-items-table.migration.js";
|
|
6
|
+
export async function runTestMigrations(client, orgId) {
|
|
7
|
+
const migrations = [
|
|
8
|
+
new CreateTestEntitiesTableMigration(client, orgId),
|
|
9
|
+
new CreateCategoriesTableMigration(client, orgId),
|
|
10
|
+
new CreateProductsTableMigration(client, orgId),
|
|
11
|
+
new CreateTestUsersTableMigration(client, orgId),
|
|
12
|
+
new CreateTestItemsTableMigration(client, orgId),
|
|
13
|
+
];
|
|
14
|
+
let success = true;
|
|
15
|
+
for (const migration of migrations) {
|
|
16
|
+
success = await migration.execute();
|
|
17
|
+
if (!success) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return success;
|
|
22
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ITestDatabase } from './test-database.interface.js';
|
|
2
|
+
import { IDatabase } from '../databases/models/index.js';
|
|
3
|
+
export declare class TestPostgresDatabase implements ITestDatabase {
|
|
4
|
+
private database;
|
|
5
|
+
private postgresClient;
|
|
6
|
+
private initPromise;
|
|
7
|
+
init(databaseName: string): Promise<IDatabase>;
|
|
8
|
+
getRandomId(): string;
|
|
9
|
+
private _performInit;
|
|
10
|
+
private createIndexes;
|
|
11
|
+
clearCollections(): Promise<void>;
|
|
12
|
+
cleanup(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import testUtils from './common-test.utils.js';
|
|
3
|
+
import { initSystemUserContext } from '../config/base-api-config.js';
|
|
4
|
+
import { newDb } from "pg-mem";
|
|
5
|
+
import { setupDatabaseForMultitenant } from '../databases/postgres/migrations/setup-for-multitenant.migration.js';
|
|
6
|
+
import { setupDatabaseForAuth } from '../databases/postgres/migrations/setup-for-auth.migration.js';
|
|
7
|
+
import { runTestMigrations } from './postgres-test-migrations/run-test-migrations.js';
|
|
8
|
+
import { PostgresDatabase } from '../databases/postgres/postgres.database.js';
|
|
9
|
+
import { testOrg } from './test-objects.js';
|
|
10
|
+
export class TestPostgresDatabase {
|
|
11
|
+
database = null;
|
|
12
|
+
postgresClient = null;
|
|
13
|
+
initPromise = null;
|
|
14
|
+
async init(databaseName) {
|
|
15
|
+
if (this.initPromise) {
|
|
16
|
+
return this.initPromise;
|
|
17
|
+
}
|
|
18
|
+
this.initPromise = this._performInit(databaseName);
|
|
19
|
+
return this.initPromise;
|
|
20
|
+
}
|
|
21
|
+
getRandomId() {
|
|
22
|
+
return randomUUID();
|
|
23
|
+
}
|
|
24
|
+
async _performInit(databaseName) {
|
|
25
|
+
if (!this.database) {
|
|
26
|
+
const { Client } = newDb().adapters.createPg();
|
|
27
|
+
const postgresClient = new Client();
|
|
28
|
+
await postgresClient.connect();
|
|
29
|
+
const testDatabase = new PostgresDatabase(postgresClient);
|
|
30
|
+
this.database = testDatabase;
|
|
31
|
+
this.postgresClient = postgresClient;
|
|
32
|
+
let success = await setupDatabaseForMultitenant(postgresClient, testOrg._id);
|
|
33
|
+
if (!success) {
|
|
34
|
+
throw new Error('Failed to setup for multitenant');
|
|
35
|
+
}
|
|
36
|
+
success = await setupDatabaseForAuth(postgresClient, testOrg._id);
|
|
37
|
+
if (!success) {
|
|
38
|
+
throw new Error('Failed to setup for auth');
|
|
39
|
+
}
|
|
40
|
+
success = await runTestMigrations(postgresClient, testOrg._id);
|
|
41
|
+
if (!success) {
|
|
42
|
+
throw new Error('Failed to run test migrations');
|
|
43
|
+
}
|
|
44
|
+
testUtils.initialize(testDatabase);
|
|
45
|
+
await this.createIndexes(postgresClient);
|
|
46
|
+
await testUtils.createMetaOrg();
|
|
47
|
+
}
|
|
48
|
+
await initSystemUserContext(this.database);
|
|
49
|
+
return this.database;
|
|
50
|
+
}
|
|
51
|
+
async createIndexes(client) {
|
|
52
|
+
try {
|
|
53
|
+
await client.query(`
|
|
54
|
+
CREATE INDEX IF NOT EXISTS email_index ON users (LOWER(email));
|
|
55
|
+
CREATE UNIQUE INDEX IF NOT EXISTS email_unique_index ON users (LOWER(email));
|
|
56
|
+
`);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.log('Note: Indexes may be created later when tables are initialized:', error.message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async clearCollections() {
|
|
63
|
+
if (!this.postgresClient) {
|
|
64
|
+
throw new Error('Database not initialized');
|
|
65
|
+
}
|
|
66
|
+
const result = await this.postgresClient.query(`
|
|
67
|
+
SELECT "table_name"
|
|
68
|
+
FROM information_schema.tables
|
|
69
|
+
WHERE "table_schema" = 'public'
|
|
70
|
+
AND "table_type" = 'BASE TABLE'
|
|
71
|
+
`);
|
|
72
|
+
result.rows.forEach(async (row) => {
|
|
73
|
+
await this.postgresClient?.query(`TRUNCATE TABLE "${row.table_name}" RESTART IDENTITY CASCADE`);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async cleanup() {
|
|
77
|
+
await testUtils.cleanup();
|
|
78
|
+
if (!this.postgresClient) {
|
|
79
|
+
throw new Error('Database not initialized');
|
|
80
|
+
}
|
|
81
|
+
await this.postgresClient.end();
|
|
82
|
+
this.initPromise = null;
|
|
83
|
+
this.database = null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { Application } from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import { ITestDatabase } from './test-database.interface.js';
|
|
3
|
+
import { IDatabase } from '../databases/models/database.interface.js';
|
|
3
4
|
export declare class TestExpressApp {
|
|
4
5
|
private static app;
|
|
5
|
-
private static
|
|
6
|
-
private static
|
|
7
|
-
private static db;
|
|
6
|
+
private static database;
|
|
7
|
+
private static testDatabase;
|
|
8
8
|
private static initPromise;
|
|
9
|
-
static
|
|
9
|
+
private static databaseName;
|
|
10
|
+
static init(useMongoDb?: boolean): Promise<{
|
|
10
11
|
app: Application;
|
|
11
|
-
|
|
12
|
+
database: IDatabase;
|
|
13
|
+
testDatabase: ITestDatabase;
|
|
12
14
|
agent: any;
|
|
13
15
|
}>;
|
|
14
16
|
private static _performInit;
|
|
15
17
|
static setupErrorHandling(): Promise<void>;
|
|
16
|
-
static cleanup(): Promise<void>;
|
|
17
18
|
static clearCollections(): Promise<void>;
|
|
19
|
+
static cleanup(): Promise<void>;
|
|
18
20
|
}
|
|
@@ -2,34 +2,38 @@ import express from 'express';
|
|
|
2
2
|
import bodyParser from 'body-parser';
|
|
3
3
|
import cookieParser from 'cookie-parser';
|
|
4
4
|
import supertest from 'supertest';
|
|
5
|
-
import { MongoMemoryServer } from 'mongodb-memory-server';
|
|
6
|
-
import { MongoClient } from 'mongodb';
|
|
7
5
|
import { initializeTypeBox } from '@loomcore/common/validation';
|
|
8
|
-
import
|
|
9
|
-
import { setBaseApiConfig, initSystemUserContext } from '../config/base-api-config.js';
|
|
6
|
+
import { setBaseApiConfig } from '../config/base-api-config.js';
|
|
10
7
|
import { errorHandler } from '../middleware/error-handler.js';
|
|
11
8
|
import { ensureUserContext } from '../middleware/ensure-user-context.js';
|
|
9
|
+
import { TestMongoDatabase } from './mongo-db.test-database.js';
|
|
10
|
+
import { TestPostgresDatabase } from './postgres.test-database.js';
|
|
12
11
|
export class TestExpressApp {
|
|
13
12
|
static app;
|
|
14
|
-
static
|
|
15
|
-
static
|
|
16
|
-
static db;
|
|
13
|
+
static database;
|
|
14
|
+
static testDatabase;
|
|
17
15
|
static initPromise = null;
|
|
18
|
-
static
|
|
16
|
+
static databaseName = 'test-db';
|
|
17
|
+
static async init(useMongoDb) {
|
|
18
|
+
if (useMongoDb === undefined) {
|
|
19
|
+
const testDb = process.env.TEST_DATABASE;
|
|
20
|
+
useMongoDb = testDb === 'mongodb';
|
|
21
|
+
}
|
|
19
22
|
if (this.initPromise) {
|
|
20
23
|
return this.initPromise;
|
|
21
24
|
}
|
|
22
|
-
this.initPromise = this._performInit();
|
|
25
|
+
this.initPromise = this._performInit(useMongoDb);
|
|
23
26
|
return this.initPromise;
|
|
24
27
|
}
|
|
25
|
-
static async _performInit() {
|
|
28
|
+
static async _performInit(useMongoDb) {
|
|
26
29
|
setBaseApiConfig({
|
|
27
30
|
env: 'test',
|
|
28
31
|
hostName: 'localhost',
|
|
29
32
|
appName: 'test-app',
|
|
30
33
|
clientSecret: 'test-secret',
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
database: {
|
|
35
|
+
name: this.databaseName,
|
|
36
|
+
},
|
|
33
37
|
externalPort: 4000,
|
|
34
38
|
internalPort: 8083,
|
|
35
39
|
corsAllowedOrigins: ['*'],
|
|
@@ -53,24 +57,18 @@ export class TestExpressApp {
|
|
|
53
57
|
}
|
|
54
58
|
});
|
|
55
59
|
initializeTypeBox();
|
|
56
|
-
if (!this.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.client = await MongoClient.connect(uri);
|
|
68
|
-
this.db = this.client.db();
|
|
69
|
-
testUtils.initialize(this.db);
|
|
70
|
-
await testUtils.createIndexes(this.db);
|
|
71
|
-
await testUtils.createMetaOrg();
|
|
60
|
+
if (!this.database) {
|
|
61
|
+
if (useMongoDb) {
|
|
62
|
+
const testMongoDb = new TestMongoDatabase();
|
|
63
|
+
this.testDatabase = testMongoDb;
|
|
64
|
+
this.database = await testMongoDb.init(this.databaseName);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const testPostgresDb = new TestPostgresDatabase();
|
|
68
|
+
this.testDatabase = testPostgresDb;
|
|
69
|
+
this.database = await testPostgresDb.init(this.databaseName);
|
|
70
|
+
}
|
|
72
71
|
}
|
|
73
|
-
await initSystemUserContext(this.db);
|
|
74
72
|
if (!this.app) {
|
|
75
73
|
this.app = express();
|
|
76
74
|
this.app.use(bodyParser.json());
|
|
@@ -83,34 +81,26 @@ export class TestExpressApp {
|
|
|
83
81
|
const agent = supertest.agent(this.app);
|
|
84
82
|
return {
|
|
85
83
|
app: this.app,
|
|
86
|
-
|
|
84
|
+
database: this.database,
|
|
85
|
+
testDatabase: this.testDatabase,
|
|
87
86
|
agent
|
|
88
87
|
};
|
|
89
88
|
}
|
|
90
89
|
static async setupErrorHandling() {
|
|
91
90
|
this.app.use(errorHandler);
|
|
92
91
|
}
|
|
93
|
-
static async
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
await this.client.close();
|
|
92
|
+
static async clearCollections() {
|
|
93
|
+
if (this.testDatabase) {
|
|
94
|
+
await this.testDatabase.clearCollections();
|
|
97
95
|
}
|
|
98
|
-
|
|
99
|
-
|
|
96
|
+
}
|
|
97
|
+
static async cleanup() {
|
|
98
|
+
if (this.testDatabase) {
|
|
99
|
+
await this.testDatabase.cleanup();
|
|
100
100
|
}
|
|
101
101
|
this.initPromise = null;
|
|
102
102
|
this.app = undefined;
|
|
103
|
-
this.
|
|
104
|
-
this.
|
|
105
|
-
this.mongoServer = undefined;
|
|
106
|
-
}
|
|
107
|
-
static async clearCollections() {
|
|
108
|
-
if (!this.db) {
|
|
109
|
-
throw new Error('Database not initialized');
|
|
110
|
-
}
|
|
111
|
-
const collections = await this.db.collections();
|
|
112
|
-
for (const collection of collections) {
|
|
113
|
-
await collection.deleteMany({});
|
|
114
|
-
}
|
|
103
|
+
this.database = undefined;
|
|
104
|
+
this.testDatabase = undefined;
|
|
115
105
|
}
|
|
116
106
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Db } from 'mongodb';
|
|
2
|
+
export declare class TestMongoDb {
|
|
3
|
+
private static mongoServer;
|
|
4
|
+
private static client;
|
|
5
|
+
private static db;
|
|
6
|
+
private static initPromise;
|
|
7
|
+
static init(): Promise<Db>;
|
|
8
|
+
static getRandomId(): string;
|
|
9
|
+
private static _performInit;
|
|
10
|
+
private static createIndexes;
|
|
11
|
+
static getDb(): Db;
|
|
12
|
+
static clearCollections(): Promise<void>;
|
|
13
|
+
static cleanup(): Promise<void>;
|
|
14
|
+
}
|