@loomcore/api 0.1.53 → 0.1.57

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.
Files changed (96) hide show
  1. package/dist/__tests__/common-test.utils.d.ts +8 -2
  2. package/dist/__tests__/common-test.utils.js +50 -16
  3. package/dist/__tests__/index.d.ts +0 -1
  4. package/dist/__tests__/index.js +0 -1
  5. package/dist/__tests__/postgres-test-migrations/postgres-test-schema.d.ts +3 -0
  6. package/dist/__tests__/postgres-test-migrations/postgres-test-schema.js +97 -0
  7. package/dist/__tests__/postgres.test-database.js +9 -8
  8. package/dist/__tests__/setup/vitest-setup.js +15 -0
  9. package/dist/__tests__/test-objects.d.ts +6 -3
  10. package/dist/__tests__/test-objects.js +10 -2
  11. package/dist/controllers/api.controller.d.ts +2 -0
  12. package/dist/controllers/api.controller.js +66 -11
  13. package/dist/databases/migrations/migration-runner.js +6 -6
  14. package/dist/databases/models/database.interface.d.ts +9 -8
  15. package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.d.ts +2 -1
  16. package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.d.ts +2 -1
  17. package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.d.ts +2 -1
  18. package/dist/databases/mongo-db/migrations/{mongo-foundational.d.ts → mongo-initial-schema.d.ts} +1 -1
  19. package/dist/databases/mongo-db/migrations/mongo-initial-schema.js +264 -0
  20. package/dist/databases/mongo-db/mongo-db.database.d.ts +9 -8
  21. package/dist/databases/mongo-db/mongo-db.database.js +2 -2
  22. package/dist/databases/mongo-db/queries/mongo-get-by-id.query.d.ts +2 -1
  23. package/dist/databases/postgres/commands/postgres-batch-update.command.js +1 -1
  24. package/dist/databases/postgres/commands/postgres-create-many.command.d.ts +2 -1
  25. package/dist/databases/postgres/commands/postgres-create-many.command.js +23 -28
  26. package/dist/databases/postgres/commands/postgres-create.command.d.ts +2 -1
  27. package/dist/databases/postgres/commands/postgres-create.command.js +6 -5
  28. package/dist/databases/postgres/commands/postgres-delete-by-id.command.d.ts +2 -1
  29. package/dist/databases/postgres/commands/postgres-full-update-by-id.command.d.ts +2 -1
  30. package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.d.ts +2 -1
  31. package/dist/databases/postgres/migrations/__tests__/test-migration-helper.d.ts +4 -0
  32. package/dist/databases/postgres/migrations/__tests__/test-migration-helper.js +67 -0
  33. package/dist/databases/postgres/migrations/index.d.ts +1 -2
  34. package/dist/databases/postgres/migrations/index.js +1 -2
  35. package/dist/databases/postgres/migrations/{postgres-foundational.d.ts → postgres-initial-schema.d.ts} +1 -1
  36. package/dist/databases/postgres/migrations/postgres-initial-schema.js +436 -0
  37. package/dist/databases/postgres/postgres.database.d.ts +10 -9
  38. package/dist/databases/postgres/postgres.database.js +2 -2
  39. package/dist/databases/postgres/queries/postgres-get-by-id.query.d.ts +2 -1
  40. package/dist/models/refresh-token.model.d.ts +5 -4
  41. package/dist/models/refresh-token.model.js +3 -3
  42. package/dist/services/auth.service.d.ts +3 -2
  43. package/dist/services/auth.service.js +4 -4
  44. package/dist/services/generic-api-service/generic-api-service.interface.d.ts +8 -7
  45. package/dist/services/generic-api-service/generic-api.service.d.ts +8 -7
  46. package/dist/services/generic-api-service/generic-api.service.js +23 -23
  47. package/dist/services/multi-tenant-api.service.d.ts +1 -1
  48. package/dist/services/multi-tenant-api.service.js +3 -3
  49. package/dist/services/organization.service.d.ts +4 -3
  50. package/dist/services/organization.service.js +3 -3
  51. package/dist/services/user.service.d.ts +3 -2
  52. package/dist/services/user.service.js +2 -2
  53. package/dist/services/utils/audit-for-create.util.js +2 -1
  54. package/dist/services/utils/audit-for-update.util.js +2 -1
  55. package/dist/services/utils/strip-sender-provided-system-properties.util.js +2 -1
  56. package/package.json +2 -2
  57. package/dist/__tests__/postgres-test-migrations/100-create-test-entities-table.migration.d.ts +0 -21
  58. package/dist/__tests__/postgres-test-migrations/100-create-test-entities-table.migration.js +0 -61
  59. package/dist/__tests__/postgres-test-migrations/101-create-categories-table.migration.d.ts +0 -21
  60. package/dist/__tests__/postgres-test-migrations/101-create-categories-table.migration.js +0 -51
  61. package/dist/__tests__/postgres-test-migrations/102-create-products-table.migration.d.ts +0 -21
  62. package/dist/__tests__/postgres-test-migrations/102-create-products-table.migration.js +0 -60
  63. package/dist/__tests__/postgres-test-migrations/103-create-test-items-table.migration.d.ts +0 -21
  64. package/dist/__tests__/postgres-test-migrations/103-create-test-items-table.migration.js +0 -59
  65. package/dist/__tests__/postgres-test-migrations/test-migrations.d.ts +0 -3
  66. package/dist/__tests__/postgres-test-migrations/test-migrations.js +0 -12
  67. package/dist/databases/mongo-db/migrations/mongo-foundational.js +0 -30
  68. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +0 -21
  69. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +0 -51
  70. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +0 -21
  71. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +0 -76
  72. package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +0 -21
  73. package/dist/databases/postgres/migrations/003-create-users-table.migration.js +0 -77
  74. package/dist/databases/postgres/migrations/004-create-refresh-tokens-table.migration.d.ts +0 -21
  75. package/dist/databases/postgres/migrations/004-create-refresh-tokens-table.migration.js +0 -72
  76. package/dist/databases/postgres/migrations/005-create-meta-org.migration.d.ts +0 -22
  77. package/dist/databases/postgres/migrations/005-create-meta-org.migration.js +0 -64
  78. package/dist/databases/postgres/migrations/006-create-admin-user.migration.d.ts +0 -19
  79. package/dist/databases/postgres/migrations/006-create-admin-user.migration.js +0 -52
  80. package/dist/databases/postgres/migrations/007-create-roles-table.migration.d.ts +0 -21
  81. package/dist/databases/postgres/migrations/007-create-roles-table.migration.js +0 -65
  82. package/dist/databases/postgres/migrations/008-create-user-roles-table.migration.d.ts +0 -21
  83. package/dist/databases/postgres/migrations/008-create-user-roles-table.migration.js +0 -75
  84. package/dist/databases/postgres/migrations/009-create-features-table.migration.d.ts +0 -21
  85. package/dist/databases/postgres/migrations/009-create-features-table.migration.js +0 -65
  86. package/dist/databases/postgres/migrations/010-create-authorizations-table.migration.d.ts +0 -21
  87. package/dist/databases/postgres/migrations/010-create-authorizations-table.migration.js +0 -77
  88. package/dist/databases/postgres/migrations/011-create-admin-authorization.migration.d.ts +0 -23
  89. package/dist/databases/postgres/migrations/011-create-admin-authorization.migration.js +0 -130
  90. package/dist/databases/postgres/migrations/database-builder.d.ts +0 -15
  91. package/dist/databases/postgres/migrations/database-builder.interface.d.ts +0 -10
  92. package/dist/databases/postgres/migrations/database-builder.js +0 -65
  93. package/dist/databases/postgres/migrations/migration.interface.d.ts +0 -11
  94. package/dist/databases/postgres/migrations/migration.interface.js +0 -1
  95. package/dist/databases/postgres/migrations/postgres-foundational.js +0 -47
  96. /package/dist/{databases/postgres/migrations/database-builder.interface.js → __tests__/setup/vitest-setup.d.ts} +0 -0
@@ -1,12 +0,0 @@
1
- import { CreateTestEntitiesTableMigration } from "./100-create-test-entities-table.migration.js";
2
- import { CreateCategoriesTableMigration } from "./101-create-categories-table.migration.js";
3
- import { CreateProductsTableMigration } from "./102-create-products-table.migration.js";
4
- import { CreateTestItemsTableMigration } from "./103-create-test-items-table.migration.js";
5
- export const testMigrations = (client) => {
6
- return [
7
- new CreateTestEntitiesTableMigration(client),
8
- new CreateCategoriesTableMigration(client),
9
- new CreateProductsTableMigration(client),
10
- new CreateTestItemsTableMigration(client),
11
- ];
12
- };
@@ -1,30 +0,0 @@
1
- export const getMongoFoundational = (config) => {
2
- const migrations = [];
3
- const isMultiTenant = config.app.isMultiTenant === true;
4
- migrations.push({
5
- name: '00000000000001_base-users',
6
- up: async ({ context: db }) => {
7
- await db.createCollection('users');
8
- await db.collection('users').createIndex({ email: 1 }, { unique: true });
9
- if (isMultiTenant) {
10
- await db.collection('users').createIndex({ _orgId: 1 });
11
- }
12
- },
13
- down: async ({ context: db }) => {
14
- await db.collection('users').drop();
15
- }
16
- });
17
- if (isMultiTenant) {
18
- migrations.push({
19
- name: '00000000000002_base-organizations',
20
- up: async ({ context: db }) => {
21
- await db.createCollection('organizations');
22
- await db.collection('organizations').createIndex({ name: 1 });
23
- },
24
- down: async ({ context: db }) => {
25
- await db.collection('organizations').drop();
26
- }
27
- });
28
- }
29
- return migrations;
30
- };
@@ -1,21 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "./migration.interface.js";
3
- export declare class CreateMigrationTableMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- index: number;
7
- execute(): Promise<{
8
- success: boolean;
9
- error: Error;
10
- } | {
11
- success: boolean;
12
- error: null;
13
- }>;
14
- revert(): Promise<{
15
- success: boolean;
16
- error: Error;
17
- } | {
18
- success: boolean;
19
- error: null;
20
- }>;
21
- }
@@ -1,51 +0,0 @@
1
- import { randomUUID } from 'crypto';
2
- import { doesTableExist } from "../utils/does-table-exist.util.js";
3
- export class CreateMigrationTableMigration {
4
- client;
5
- constructor(client) {
6
- this.client = client;
7
- }
8
- index = 1;
9
- async execute() {
10
- const _id = randomUUID().toString();
11
- try {
12
- await this.client.query('BEGIN');
13
- const tableExists = await doesTableExist(this.client, 'migrations');
14
- if (!tableExists) {
15
- await this.client.query(`
16
- CREATE TABLE "migrations" (
17
- "_id" VARCHAR(255) PRIMARY KEY,
18
- "index" INTEGER NOT NULL UNIQUE,
19
- "hasRun" BOOLEAN NOT NULL,
20
- "reverted" BOOLEAN NOT NULL
21
- )
22
- `);
23
- }
24
- const result = await this.client.query(`
25
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
26
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
27
- `);
28
- if (result.rowCount === 0) {
29
- await this.client.query('ROLLBACK');
30
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
31
- }
32
- await this.client.query('COMMIT');
33
- return { success: true, error: null };
34
- }
35
- catch (error) {
36
- await this.client.query('ROLLBACK');
37
- return { success: false, error: new Error(`Error executing migration ${this.index}: ${error.message}`) };
38
- }
39
- }
40
- async revert() {
41
- try {
42
- await this.client.query(`
43
- DROP TABLE "migrations";
44
- `);
45
- }
46
- catch (error) {
47
- return { success: false, error: new Error(`Error reverting migration ${this.index} from migrations table: ${error.message}`) };
48
- }
49
- return { success: true, error: null };
50
- }
51
- }
@@ -1,21 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "./migration.interface.js";
3
- export declare class CreateOrganizationsTableMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- index: number;
7
- execute(): Promise<{
8
- success: boolean;
9
- error: Error;
10
- } | {
11
- success: boolean;
12
- error: null;
13
- }>;
14
- revert(): Promise<{
15
- success: boolean;
16
- error: Error;
17
- } | {
18
- success: boolean;
19
- error: null;
20
- }>;
21
- }
@@ -1,76 +0,0 @@
1
- import { randomUUID } from "crypto";
2
- import { doesTableExist } from "../utils/does-table-exist.util.js";
3
- export class CreateOrganizationsTableMigration {
4
- client;
5
- constructor(client) {
6
- this.client = client;
7
- }
8
- index = 2;
9
- async execute() {
10
- const _id = randomUUID().toString();
11
- try {
12
- await this.client.query('BEGIN');
13
- const tableExists = await doesTableExist(this.client, 'organizations');
14
- if (!tableExists) {
15
- await this.client.query(`
16
- CREATE TABLE "organizations" (
17
- "_id" VARCHAR(255) PRIMARY KEY,
18
- "name" VARCHAR(255) NOT NULL UNIQUE,
19
- "code" VARCHAR(255) NOT NULL UNIQUE,
20
- "description" TEXT,
21
- "status" INTEGER NOT NULL,
22
- "isMetaOrg" BOOLEAN NOT NULL,
23
- "authToken" TEXT,
24
- "_created" TIMESTAMP NOT NULL,
25
- "_createdBy" VARCHAR(255) NOT NULL,
26
- "_updated" TIMESTAMP NOT NULL,
27
- "_updatedBy" VARCHAR(255) NOT NULL,
28
- "_deleted" TIMESTAMP,
29
- "_deletedBy" VARCHAR(255)
30
- )
31
- `);
32
- }
33
- const result = await this.client.query(`
34
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
35
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
36
- `);
37
- if (result.rowCount === 0) {
38
- await this.client.query('ROLLBACK');
39
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
40
- }
41
- await this.client.query('COMMIT');
42
- return { success: true, error: null };
43
- }
44
- catch (error) {
45
- await this.client.query('ROLLBACK');
46
- return { success: false, error: new Error(`Error executing migration ${this.index}: ${error.message}`) };
47
- }
48
- }
49
- async revert() {
50
- try {
51
- await this.client.query('BEGIN');
52
- const tableExists = await doesTableExist(this.client, 'organizations');
53
- if (tableExists) {
54
- await this.client.query(`
55
- DROP TABLE "organizations";
56
- `);
57
- }
58
- const updateResult = await this.client.query(`
59
- UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
60
- `);
61
- if (updateResult.rowCount === 0) {
62
- await this.client.query('ROLLBACK');
63
- return {
64
- success: false, error: new Error(`Error updating migration record for index ${this.index}: Migration record not found.
65
- Migration index: ${this.index}`)
66
- };
67
- }
68
- await this.client.query('COMMIT');
69
- return { success: true, error: null };
70
- }
71
- catch (error) {
72
- await this.client.query('ROLLBACK');
73
- return { success: false, error: new Error(`Error reverting migration ${this.index}: ${error.message}`) };
74
- }
75
- }
76
- }
@@ -1,21 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "./migration.interface.js";
3
- export declare class CreateUsersTableMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- index: number;
7
- execute(_orgId?: string): Promise<{
8
- success: boolean;
9
- error: Error;
10
- } | {
11
- success: boolean;
12
- error: null;
13
- }>;
14
- revert(): Promise<{
15
- success: boolean;
16
- error: Error;
17
- } | {
18
- success: boolean;
19
- error: null;
20
- }>;
21
- }
@@ -1,77 +0,0 @@
1
- import { randomUUID } from "crypto";
2
- import { doesTableExist } from "../utils/does-table-exist.util.js";
3
- import { config } from "../../../config/index.js";
4
- export class CreateUsersTableMigration {
5
- client;
6
- constructor(client) {
7
- this.client = client;
8
- }
9
- index = 3;
10
- async execute(_orgId) {
11
- const _id = randomUUID().toString();
12
- try {
13
- await this.client.query('BEGIN');
14
- const tableExists = await doesTableExist(this.client, 'users');
15
- if (!tableExists) {
16
- const fkConstraint = config.app.isMultiTenant
17
- ? ',\n CONSTRAINT "fk_users_organization" FOREIGN KEY ("_orgId") REFERENCES "organizations"("_id") ON DELETE CASCADE'
18
- : '';
19
- await this.client.query(`
20
- CREATE TABLE "users" (
21
- "_id" VARCHAR(255) PRIMARY KEY,
22
- "_orgId" VARCHAR(255),
23
- "email" VARCHAR(255) NOT NULL,
24
- "firstName" VARCHAR(255),
25
- "lastName" VARCHAR(255),
26
- "displayName" VARCHAR(255),
27
- "password" VARCHAR(255) NOT NULL,
28
- "_lastLoggedIn" TIMESTAMP,
29
- "_lastPasswordChange" TIMESTAMP,
30
- "_created" TIMESTAMP NOT NULL,
31
- "_createdBy" VARCHAR(255) NOT NULL,
32
- "_updated" TIMESTAMP NOT NULL,
33
- "_updatedBy" VARCHAR(255) NOT NULL,
34
- "_deleted" TIMESTAMP,
35
- "_deletedBy" VARCHAR(255),
36
- CONSTRAINT "uk_users_email" UNIQUE ("_orgId", "email")${fkConstraint}
37
- )
38
- `);
39
- }
40
- const result = await this.client.query(`
41
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
42
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
43
- `);
44
- if (result.rowCount === 0) {
45
- await this.client.query('ROLLBACK');
46
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
47
- }
48
- await this.client.query('COMMIT');
49
- return { success: true, error: null };
50
- }
51
- catch (error) {
52
- await this.client.query('ROLLBACK');
53
- return { success: false, error: new Error(`Error executing migration ${this.index}: ${error.message}`) };
54
- }
55
- }
56
- async revert() {
57
- try {
58
- await this.client.query('BEGIN');
59
- await this.client.query(`
60
- DROP TABLE "users";
61
- `);
62
- const updateResult = await this.client.query(`
63
- UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
64
- `);
65
- if (updateResult.rowCount === 0) {
66
- await this.client.query('ROLLBACK');
67
- return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
68
- }
69
- await this.client.query('COMMIT');
70
- return { success: true, error: null };
71
- }
72
- catch (error) {
73
- await this.client.query('ROLLBACK');
74
- return { success: false, error: new Error(`Error reverting migration ${this.index}: ${error.message}`) };
75
- }
76
- }
77
- }
@@ -1,21 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "./migration.interface.js";
3
- export declare class CreateRefreshTokenTableMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- index: number;
7
- execute(_orgId?: string): Promise<{
8
- success: boolean;
9
- error: Error;
10
- } | {
11
- success: boolean;
12
- error: null;
13
- }>;
14
- revert(): Promise<{
15
- success: boolean;
16
- error: Error;
17
- } | {
18
- success: boolean;
19
- error: null;
20
- }>;
21
- }
@@ -1,72 +0,0 @@
1
- import { randomUUID } from "crypto";
2
- import { doesTableExist } from "../utils/does-table-exist.util.js";
3
- import { config } from "../../../config/index.js";
4
- export class CreateRefreshTokenTableMigration {
5
- client;
6
- constructor(client) {
7
- this.client = client;
8
- }
9
- index = 4;
10
- async execute(_orgId) {
11
- const _id = randomUUID().toString();
12
- try {
13
- await this.client.query('BEGIN');
14
- const tableExists = await doesTableExist(this.client, 'refreshTokens');
15
- if (!tableExists) {
16
- const fkConstraint = config.app.isMultiTenant
17
- ? ',\n CONSTRAINT "fk_refreshTokens_organization" FOREIGN KEY ("_orgId") REFERENCES "organizations"("_id") ON DELETE CASCADE'
18
- : '';
19
- await this.client.query(`
20
- CREATE TABLE "refreshTokens" (
21
- "_id" VARCHAR(255) PRIMARY KEY,
22
- "_orgId" VARCHAR(255),
23
- "token" VARCHAR(255) NOT NULL,
24
- "deviceId" VARCHAR(255) NOT NULL,
25
- "userId" VARCHAR(255) NOT NULL,
26
- "expiresOn" BIGINT NOT NULL,
27
- "created" TIMESTAMP NOT NULL,
28
- "createdBy" VARCHAR(255) NOT NULL${fkConstraint}
29
- )
30
- `);
31
- }
32
- const result = await this.client.query(`
33
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
34
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
35
- `);
36
- if (result.rowCount === 0) {
37
- await this.client.query('ROLLBACK');
38
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
39
- }
40
- await this.client.query('COMMIT');
41
- return { success: true, error: null };
42
- }
43
- catch (error) {
44
- await this.client.query('ROLLBACK');
45
- return { success: false, error: new Error(`Error executing migration ${this.index}: ${error.message}`) };
46
- }
47
- }
48
- async revert() {
49
- try {
50
- await this.client.query('BEGIN');
51
- const tableExists = await doesTableExist(this.client, 'refreshTokens');
52
- if (tableExists) {
53
- await this.client.query(`
54
- DROP TABLE "refreshTokens";
55
- `);
56
- }
57
- const updateResult = await this.client.query(`
58
- UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
59
- `);
60
- if (updateResult.rowCount === 0) {
61
- await this.client.query('ROLLBACK');
62
- return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
63
- }
64
- await this.client.query('COMMIT');
65
- return { success: true, error: null };
66
- }
67
- catch (error) {
68
- await this.client.query('ROLLBACK');
69
- return { success: false, error: new Error(`Error reverting migration ${this.index}: ${error.message}`) };
70
- }
71
- }
72
- }
@@ -1,22 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "./migration.interface.js";
3
- export declare class CreateMetaOrgMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- index: number;
7
- execute(): Promise<{
8
- success: boolean;
9
- error: Error;
10
- } | {
11
- success: boolean;
12
- error: null;
13
- }>;
14
- revert(): Promise<{
15
- success: boolean;
16
- error: Error;
17
- } | {
18
- success: boolean;
19
- error: null;
20
- }>;
21
- }
22
- export default CreateMetaOrgMigration;
@@ -1,64 +0,0 @@
1
- import { randomUUID } from "crypto";
2
- import { config } from "../../../config/index.js";
3
- import { initializeSystemUserContext } from "@loomcore/common/models";
4
- export class CreateMetaOrgMigration {
5
- client;
6
- constructor(client) {
7
- this.client = client;
8
- }
9
- index = 5;
10
- async execute() {
11
- const _id = randomUUID().toString();
12
- try {
13
- await this.client.query('BEGIN');
14
- const orgResult = await this.client.query(`
15
- INSERT INTO "organizations" ("_id", "name", "code", "status", "isMetaOrg", "_created", "_createdBy", "_updated", "_updatedBy")
16
- VALUES ('${_id}', '${config.app.metaOrgName}', '${config.app.metaOrgCode}', 1, true, NOW(), 'system', NOW(), 'system')
17
- RETURNING "_id", "name", "code", "status", "isMetaOrg", "_created", "_createdBy", "_updated", "_updatedBy";
18
- `);
19
- if (orgResult.rowCount === 0) {
20
- await this.client.query('ROLLBACK');
21
- return { success: false, error: new Error(`Error creating meta org: No row returned`) };
22
- }
23
- initializeSystemUserContext(config.email?.systemEmailAddress || 'system@example.com', orgResult.rows[0]);
24
- const migrationResult = await this.client.query(`
25
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
26
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
27
- `);
28
- if (migrationResult.rowCount === 0) {
29
- await this.client.query('ROLLBACK');
30
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
31
- }
32
- await this.client.query('COMMIT');
33
- return { success: true, error: null };
34
- }
35
- catch (error) {
36
- await this.client.query('ROLLBACK');
37
- return { success: false, error: new Error(`Error executing migration ${this.index}: ${error.message}`) };
38
- }
39
- }
40
- async revert() {
41
- try {
42
- await this.client.query('BEGIN');
43
- const deleteResult = await this.client.query(`DELETE FROM "organizations" WHERE "isMetaOrg" = TRUE;`);
44
- if (deleteResult.rowCount === 0) {
45
- await this.client.query('ROLLBACK');
46
- return { success: false, error: new Error(`Error reverting meta org: No row returned`) };
47
- }
48
- const updateResult = await this.client.query(`
49
- UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
50
- `);
51
- if (updateResult.rowCount === 0) {
52
- await this.client.query('ROLLBACK');
53
- return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
54
- }
55
- await this.client.query('COMMIT');
56
- return { success: true, error: null };
57
- }
58
- catch (error) {
59
- await this.client.query('ROLLBACK');
60
- return { success: false, error: new Error(`Error reverting migration ${this.index}: ${error.message}`) };
61
- }
62
- }
63
- }
64
- export default CreateMetaOrgMigration;
@@ -1,19 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "../index.js";
3
- export declare class CreateAdminUserMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- private authService;
7
- index: number;
8
- execute(): Promise<{
9
- success: boolean;
10
- error: Error;
11
- } | {
12
- success: boolean;
13
- error: null;
14
- }>;
15
- revert(): Promise<{
16
- success: boolean;
17
- error: Error | null;
18
- }>;
19
- }
@@ -1,52 +0,0 @@
1
- import { PostgresDatabase } from "../index.js";
2
- import { randomUUID } from "crypto";
3
- import { getSystemUserContext } from "@loomcore/common/models";
4
- import { AuthService } from "../../../services/auth.service.js";
5
- import { config } from "../../../config/index.js";
6
- export class CreateAdminUserMigration {
7
- client;
8
- constructor(client) {
9
- this.client = client;
10
- const database = new PostgresDatabase(this.client);
11
- this.authService = new AuthService(database);
12
- }
13
- authService;
14
- index = 6;
15
- async execute() {
16
- const _id = randomUUID().toString();
17
- const systemUserContext = getSystemUserContext();
18
- try {
19
- await this.authService.createUser(systemUserContext, {
20
- _id: _id,
21
- _orgId: systemUserContext.organization?._id,
22
- email: config.adminUser?.email,
23
- password: config.adminUser?.password,
24
- firstName: 'Admin',
25
- lastName: 'User',
26
- displayName: 'Admin User',
27
- });
28
- }
29
- catch (error) {
30
- console.error(`Error creating admin user: ${error.message}`);
31
- return { success: false, error: new Error(`Error creating admin user: ${error.message}`) };
32
- }
33
- try {
34
- const result = await this.client.query(`
35
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
36
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
37
- `);
38
- if (result.rowCount === 0) {
39
- console.error(`Error inserting migration ${this.index} to migrations table: No row returned`);
40
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
41
- }
42
- }
43
- catch (error) {
44
- console.error(`Error inserting migration ${this.index} to migrations table: ${error.message}`);
45
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
46
- }
47
- return { success: true, error: null };
48
- }
49
- async revert() {
50
- throw new Error('Not implemented');
51
- }
52
- }
@@ -1,21 +0,0 @@
1
- import { Client } from "pg";
2
- import { IMigration } from "./migration.interface.js";
3
- export declare class CreateRoleTableMigration implements IMigration {
4
- private readonly client;
5
- constructor(client: Client);
6
- index: number;
7
- execute(): Promise<{
8
- success: boolean;
9
- error: Error;
10
- } | {
11
- success: boolean;
12
- error: null;
13
- }>;
14
- revert(): Promise<{
15
- success: boolean;
16
- error: Error;
17
- } | {
18
- success: boolean;
19
- error: null;
20
- }>;
21
- }
@@ -1,65 +0,0 @@
1
- import { randomUUID } from "crypto";
2
- import { doesTableExist } from "../utils/does-table-exist.util.js";
3
- import { config } from "../../../config/index.js";
4
- export class CreateRoleTableMigration {
5
- client;
6
- constructor(client) {
7
- this.client = client;
8
- }
9
- index = 7;
10
- async execute() {
11
- const _id = randomUUID().toString();
12
- try {
13
- await this.client.query('BEGIN');
14
- const tableExists = await doesTableExist(this.client, 'roles');
15
- if (!tableExists) {
16
- const fkConstraint = config.app.isMultiTenant
17
- ? ',\n CONSTRAINT "fk_roles_organization" FOREIGN KEY ("_orgId") REFERENCES "organizations"("_id") ON DELETE CASCADE'
18
- : '';
19
- await this.client.query(`
20
- CREATE TABLE "roles" (
21
- "_id" VARCHAR(255) PRIMARY KEY,
22
- "_orgId" VARCHAR(255),
23
- "name" VARCHAR(255) NOT NULL,
24
- CONSTRAINT "uk_roles_name" UNIQUE ("_orgId", "name")${fkConstraint}
25
- )
26
- `);
27
- }
28
- const result = await this.client.query(`
29
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
30
- VALUES ('${_id}', ${this.index}, TRUE, FALSE);
31
- `);
32
- if (result.rowCount === 0) {
33
- await this.client.query('ROLLBACK');
34
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
35
- }
36
- await this.client.query('COMMIT');
37
- return { success: true, error: null };
38
- }
39
- catch (error) {
40
- await this.client.query('ROLLBACK');
41
- return { success: false, error: new Error(`Error executing migration ${this.index}: ${error.message}`) };
42
- }
43
- }
44
- async revert() {
45
- try {
46
- await this.client.query('BEGIN');
47
- await this.client.query(`
48
- DROP TABLE "roles";
49
- `);
50
- const updateResult = await this.client.query(`
51
- UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
52
- `);
53
- if (updateResult.rowCount === 0) {
54
- await this.client.query('ROLLBACK');
55
- return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
56
- }
57
- await this.client.query('COMMIT');
58
- return { success: true, error: null };
59
- }
60
- catch (error) {
61
- await this.client.query('ROLLBACK');
62
- return { success: false, error: new Error(`Error reverting migration ${this.index}: ${error.message}`) };
63
- }
64
- }
65
- }