@loomcore/api 0.1.7 → 0.1.8

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 (27) hide show
  1. package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.d.ts +3 -5
  2. package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.js +8 -10
  3. package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.d.ts +3 -5
  4. package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.js +8 -10
  5. package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.d.ts +3 -5
  6. package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.js +8 -10
  7. package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.d.ts +3 -5
  8. package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.js +8 -10
  9. package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.d.ts +3 -5
  10. package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.js +8 -10
  11. package/dist/__tests__/postgres-test-migrations/run-test-migrations.d.ts +1 -1
  12. package/dist/__tests__/postgres-test-migrations/run-test-migrations.js +7 -7
  13. package/dist/__tests__/postgres.test-database.js +1 -1
  14. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +5 -7
  15. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +19 -24
  16. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +7 -7
  17. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +26 -9
  18. package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +5 -7
  19. package/dist/databases/postgres/migrations/003-create-users-table.migration.js +14 -11
  20. package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.d.ts +5 -7
  21. package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.js +14 -11
  22. package/dist/databases/postgres/migrations/migration.interface.d.ts +3 -4
  23. package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +1 -1
  24. package/dist/databases/postgres/migrations/setup-for-auth.migration.js +5 -5
  25. package/dist/databases/postgres/migrations/setup-for-multitenant.migration.d.ts +1 -1
  26. package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +3 -3
  27. package/package.json +1 -1
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "../../databases/postgres/migrations/migration.interface.js";
3
3
  export declare class CreateTestEntitiesTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
9
  error: Error;
12
10
  } | {
13
11
  success: boolean;
14
12
  error: null;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateTestEntitiesTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 1;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "testEntities" (
@@ -31,10 +29,10 @@ export class CreateTestEntitiesTableMigration {
31
29
  catch (error) {
32
30
  return { success: false, error: new Error(`Error creating test entities table: ${error.message}`) };
33
31
  }
34
- if (this.orgId) {
32
+ if (_orgId) {
35
33
  try {
36
34
  await this.client.query(`
37
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
35
+ Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
38
36
  `);
39
37
  }
40
38
  catch (error) {
@@ -44,7 +42,7 @@ export class CreateTestEntitiesTableMigration {
44
42
  else {
45
43
  try {
46
44
  await this.client.query(`
47
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
45
+ Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
48
46
  `);
49
47
  }
50
48
  catch (error) {
@@ -53,7 +51,7 @@ export class CreateTestEntitiesTableMigration {
53
51
  }
54
52
  return { success: true, error: null };
55
53
  }
56
- async revert() {
54
+ async revert(_orgId) {
57
55
  try {
58
56
  await this.client.query(`
59
57
  DROP TABLE test_entities;
@@ -64,7 +62,7 @@ export class CreateTestEntitiesTableMigration {
64
62
  }
65
63
  try {
66
64
  await this.client.query(`
67
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
65
+ Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
68
66
  `);
69
67
  }
70
68
  catch (error) {
@@ -2,18 +2,16 @@ import { IMigration } from "../../databases/postgres/migrations/index.js";
2
2
  import { Client } from "pg";
3
3
  export declare class CreateCategoriesTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
9
  error: Error;
12
10
  } | {
13
11
  success: boolean;
14
12
  error: null;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateCategoriesTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 2;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "categories" (
@@ -21,10 +19,10 @@ export class CreateCategoriesTableMigration {
21
19
  catch (error) {
22
20
  return { success: false, error: new Error(`Error creating categories table: ${error.message}`) };
23
21
  }
24
- if (this.orgId) {
22
+ if (_orgId) {
25
23
  try {
26
24
  await this.client.query(`
27
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
25
+ Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
28
26
  `);
29
27
  }
30
28
  catch (error) {
@@ -34,7 +32,7 @@ export class CreateCategoriesTableMigration {
34
32
  else {
35
33
  try {
36
34
  await this.client.query(`
37
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
35
+ Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
38
36
  `);
39
37
  }
40
38
  catch (error) {
@@ -43,7 +41,7 @@ export class CreateCategoriesTableMigration {
43
41
  }
44
42
  return { success: true, error: null };
45
43
  }
46
- async revert() {
44
+ async revert(_orgId) {
47
45
  try {
48
46
  await this.client.query(`
49
47
  DROP TABLE "categories";
@@ -54,7 +52,7 @@ export class CreateCategoriesTableMigration {
54
52
  }
55
53
  try {
56
54
  await this.client.query(`
57
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
55
+ Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
58
56
  `);
59
57
  }
60
58
  catch (error) {
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "../../databases/postgres/migrations/index.js";
3
3
  export declare class CreateProductsTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
9
  error: Error;
12
10
  } | {
13
11
  success: boolean;
14
12
  error: null;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateProductsTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 3;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "products" (
@@ -30,10 +28,10 @@ export class CreateProductsTableMigration {
30
28
  catch (error) {
31
29
  return { success: false, error: new Error(`Error creating products table: ${error.message}`) };
32
30
  }
33
- if (this.orgId) {
31
+ if (_orgId) {
34
32
  try {
35
33
  await this.client.query(`
36
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
34
+ Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
37
35
  `);
38
36
  }
39
37
  catch (error) {
@@ -43,7 +41,7 @@ export class CreateProductsTableMigration {
43
41
  else {
44
42
  try {
45
43
  await this.client.query(`
46
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
44
+ Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
47
45
  `);
48
46
  }
49
47
  catch (error) {
@@ -52,7 +50,7 @@ export class CreateProductsTableMigration {
52
50
  }
53
51
  return { success: true, error: null };
54
52
  }
55
- async revert() {
53
+ async revert(_orgId) {
56
54
  try {
57
55
  await this.client.query(`
58
56
  DROP TABLE "products";
@@ -63,7 +61,7 @@ export class CreateProductsTableMigration {
63
61
  }
64
62
  try {
65
63
  await this.client.query(`
66
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
64
+ Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
67
65
  `);
68
66
  }
69
67
  catch (error) {
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "../../databases/postgres/migrations/index.js";
3
3
  export declare class CreateTestUsersTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
9
  error: Error;
12
10
  } | {
13
11
  success: boolean;
14
12
  error: null;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateTestUsersTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 4;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "testUsers" (
@@ -34,10 +32,10 @@ export class CreateTestUsersTableMigration {
34
32
  catch (error) {
35
33
  return { success: false, error: new Error(`Error creating test users table: ${error.message}`) };
36
34
  }
37
- if (this.orgId) {
35
+ if (_orgId) {
38
36
  try {
39
37
  await this.client.query(`
40
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
38
+ Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
41
39
  `);
42
40
  }
43
41
  catch (error) {
@@ -47,7 +45,7 @@ export class CreateTestUsersTableMigration {
47
45
  else {
48
46
  try {
49
47
  await this.client.query(`
50
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
48
+ Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
51
49
  `);
52
50
  }
53
51
  catch (error) {
@@ -56,7 +54,7 @@ export class CreateTestUsersTableMigration {
56
54
  }
57
55
  return { success: true, error: null };
58
56
  }
59
- async revert() {
57
+ async revert(_orgId) {
60
58
  try {
61
59
  await this.client.query(`
62
60
  DROP TABLE "testUsers";
@@ -67,7 +65,7 @@ export class CreateTestUsersTableMigration {
67
65
  }
68
66
  try {
69
67
  await this.client.query(`
70
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
68
+ Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
71
69
  `);
72
70
  }
73
71
  catch (error) {
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "../../databases/postgres/migrations/index.js";
3
3
  export declare class CreateTestItemsTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
9
  error: Error;
12
10
  } | {
13
11
  success: boolean;
14
12
  error: null;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateTestItemsTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 5;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "testItems" (
@@ -29,10 +27,10 @@ export class CreateTestItemsTableMigration {
29
27
  catch (error) {
30
28
  return { success: false, error: new Error(`Error creating test items table: ${error.message}`) };
31
29
  }
32
- if (this.orgId) {
30
+ if (_orgId) {
33
31
  try {
34
32
  await this.client.query(`
35
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
33
+ Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
36
34
  `);
37
35
  }
38
36
  catch (error) {
@@ -42,7 +40,7 @@ export class CreateTestItemsTableMigration {
42
40
  else {
43
41
  try {
44
42
  await this.client.query(`
45
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
43
+ Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
46
44
  `);
47
45
  }
48
46
  catch (error) {
@@ -51,7 +49,7 @@ export class CreateTestItemsTableMigration {
51
49
  }
52
50
  return { success: true, error: null };
53
51
  }
54
- async revert() {
52
+ async revert(_orgId) {
55
53
  try {
56
54
  await this.client.query(`
57
55
  DROP TABLE "testItems";
@@ -62,7 +60,7 @@ export class CreateTestItemsTableMigration {
62
60
  }
63
61
  try {
64
62
  await this.client.query(`
65
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
63
+ Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
66
64
  `);
67
65
  }
68
66
  catch (error) {
@@ -1,5 +1,5 @@
1
1
  import { Client } from "pg";
2
- export declare function runTestMigrations(client: Client, orgId?: string): Promise<{
2
+ export declare function runTestMigrations(client: Client, _orgId?: string): Promise<{
3
3
  success: boolean;
4
4
  error: Error | null;
5
5
  }>;
@@ -3,17 +3,17 @@ import { CreateCategoriesTableMigration } from "./002-create-categories-table.mi
3
3
  import { CreateProductsTableMigration } from "./003-create-products-table.migration.js";
4
4
  import { CreateTestUsersTableMigration } from "./004-create-test-users-table.migration.js";
5
5
  import { CreateTestItemsTableMigration } from "./005-create-test-items-table.migration.js";
6
- export async function runTestMigrations(client, orgId) {
6
+ export async function runTestMigrations(client, _orgId) {
7
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),
8
+ new CreateTestEntitiesTableMigration(client),
9
+ new CreateCategoriesTableMigration(client),
10
+ new CreateProductsTableMigration(client),
11
+ new CreateTestUsersTableMigration(client),
12
+ new CreateTestItemsTableMigration(client),
13
13
  ];
14
14
  try {
15
15
  for (const migration of migrations) {
16
- await migration.execute();
16
+ await migration.execute(_orgId);
17
17
  }
18
18
  }
19
19
  catch (error) {
@@ -29,7 +29,7 @@ export class TestPostgresDatabase {
29
29
  const testDatabase = new PostgresDatabase(postgresClient);
30
30
  this.database = testDatabase;
31
31
  this.postgresClient = postgresClient;
32
- let success = await setupDatabaseForMultitenant(postgresClient, testOrg._id);
32
+ let success = await setupDatabaseForMultitenant(postgresClient, 'Test Org', 'test-org');
33
33
  if (!success) {
34
34
  throw new Error('Failed to setup for multitenant');
35
35
  }
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "./migration.interface.js";
3
3
  export declare class CreateMigrationTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
- error: Error;
9
+ error: null;
12
10
  } | {
13
11
  success: boolean;
14
- error: null;
12
+ error: Error;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,15 +1,12 @@
1
1
  import { randomUUID } from 'crypto';
2
2
  export class CreateMigrationTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 1;
10
- _id = randomUUID().toString();
11
- async execute() {
12
- let alreadyRun = false;
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
13
10
  try {
14
11
  await this.client.query(`
15
12
  CREATE TABLE "migrations" (
@@ -22,34 +19,32 @@ export class CreateMigrationTableMigration {
22
19
  `);
23
20
  }
24
21
  catch (error) {
25
- if (error.data.error.includes('already exists')) {
26
- alreadyRun = true;
22
+ if (error.code === '42P07' || error.data?.error?.includes('already exists')) {
23
+ return { success: true, error: null };
27
24
  }
28
25
  else {
29
26
  return { success: false, error: new Error(`Error creating migrations table: ${error.message}`) };
30
27
  }
31
28
  }
32
- if (!alreadyRun) {
33
- try {
34
- if (this.orgId) {
35
- await this.client.query(`
36
- INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
37
- VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);`);
38
- }
39
- else {
40
- await this.client.query(`
41
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
42
- VALUES ('${this._id}', ${this.index}, TRUE, FALSE);
43
- `);
44
- }
29
+ try {
30
+ if (_orgId) {
31
+ await this.client.query(`
32
+ INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
33
+ VALUES ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);`);
45
34
  }
46
- catch (error) {
47
- return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
35
+ else {
36
+ await this.client.query(`
37
+ INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
38
+ VALUES ('${_id}', ${this.index}, TRUE, FALSE);
39
+ `);
48
40
  }
49
41
  }
42
+ catch (error) {
43
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
44
+ }
50
45
  return { success: true, error: null };
51
46
  }
52
- async revert() {
47
+ async revert(_orgId) {
53
48
  try {
54
49
  await this.client.query(`
55
50
  DROP TABLE "migrations";
@@ -2,18 +2,18 @@ import { Client } from "pg";
2
2
  import { IMigration } from "./migration.interface.js";
3
3
  export declare class CreateOrganizationTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId;
6
- constructor(client: Client, orgId: string);
5
+ private readonly orgName;
6
+ private readonly orgCode;
7
+ constructor(client: Client, orgName: string, orgCode: string);
7
8
  index: number;
8
- _id: string;
9
- execute(): Promise<{
9
+ execute(_orgId?: string): Promise<{
10
10
  success: boolean;
11
- error: Error;
11
+ error: null;
12
12
  } | {
13
13
  success: boolean;
14
- error: null;
14
+ error: Error;
15
15
  }>;
16
- revert(): Promise<{
16
+ revert(_orgId?: string): Promise<{
17
17
  success: boolean;
18
18
  error: Error;
19
19
  } | {
@@ -1,14 +1,17 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateOrganizationTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ orgName;
5
+ orgCode;
6
+ constructor(client, orgName, orgCode) {
6
7
  this.client = client;
7
- this.orgId = orgId;
8
+ this.orgName = orgName;
9
+ this.orgCode = orgCode;
8
10
  }
9
11
  index = 2;
10
- _id = randomUUID().toString();
11
- async execute() {
12
+ async execute(_orgId) {
13
+ const _id = randomUUID().toString();
14
+ const _orgIdToUse = _orgId || randomUUID().toString();
12
15
  try {
13
16
  await this.client.query(`
14
17
  CREATE TABLE "organizations" (
@@ -29,12 +32,26 @@ export class CreateOrganizationTableMigration {
29
32
  `);
30
33
  }
31
34
  catch (error) {
32
- return { success: false, error: new Error(`Error creating organization table: ${error.message}`) };
35
+ if (error.code === '42P07' || error.data?.error?.includes('already exists')) {
36
+ return { success: true, error: null };
37
+ }
38
+ else {
39
+ return { success: false, error: new Error(`Error creating organization table: ${error.message}`) };
40
+ }
41
+ }
42
+ try {
43
+ await this.client.query(`
44
+ INSERT INTO "organizations" ("_id", "name", "code", "status", "isMetaOrg", "_created", "_createdBy", "_updated", "_updatedBy")
45
+ VALUES ('${_orgIdToUse}', '${this.orgName}', '${this.orgCode}', 1, true, NOW(), 'system', NOW(), 'system');
46
+ `);
47
+ }
48
+ catch (error) {
49
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
33
50
  }
34
51
  try {
35
52
  await this.client.query(`
36
53
  INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
37
- VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
54
+ VALUES ('${_id}', '${_orgIdToUse}', ${this.index}, TRUE, FALSE);
38
55
  `);
39
56
  }
40
57
  catch (error) {
@@ -42,7 +59,7 @@ export class CreateOrganizationTableMigration {
42
59
  }
43
60
  return { success: true, error: null };
44
61
  }
45
- async revert() {
62
+ async revert(_orgId) {
46
63
  try {
47
64
  await this.client.query(`
48
65
  DROP TABLE "organizations";
@@ -53,7 +70,7 @@ export class CreateOrganizationTableMigration {
53
70
  }
54
71
  try {
55
72
  await this.client.query(`
56
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
73
+ Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
57
74
  `);
58
75
  }
59
76
  catch (error) {
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "./migration.interface.js";
3
3
  export declare class CreateUsersTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
- error: Error;
9
+ error: null;
12
10
  } | {
13
11
  success: boolean;
14
- error: null;
12
+ error: Error;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateUsersTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 3;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "users" (
@@ -32,13 +30,18 @@ export class CreateUsersTableMigration {
32
30
  `);
33
31
  }
34
32
  catch (error) {
35
- return { success: false, error: new Error(`Error creating users table: ${error.message}`) };
33
+ if (error.code === '42P07' || error.data?.error?.includes('already exists')) {
34
+ return { success: true, error: null };
35
+ }
36
+ else {
37
+ return { success: false, error: new Error(`Error creating users table: ${error.message}`) };
38
+ }
36
39
  }
37
- if (this.orgId) {
40
+ if (_orgId) {
38
41
  try {
39
42
  await this.client.query(`
40
43
  INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
41
- VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
44
+ VALUES ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
42
45
  `);
43
46
  }
44
47
  catch (error) {
@@ -49,7 +52,7 @@ export class CreateUsersTableMigration {
49
52
  try {
50
53
  await this.client.query(`
51
54
  INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
52
- VALUES ('${this._id}', ${this.index}, TRUE, FALSE);
55
+ VALUES ('${_id}', ${this.index}, TRUE, FALSE);
53
56
  `);
54
57
  }
55
58
  catch (error) {
@@ -58,7 +61,7 @@ export class CreateUsersTableMigration {
58
61
  }
59
62
  return { success: true, error: null };
60
63
  }
61
- async revert() {
64
+ async revert(_orgId) {
62
65
  try {
63
66
  await this.client.query(`
64
67
  DROP TABLE "users";
@@ -69,7 +72,7 @@ export class CreateUsersTableMigration {
69
72
  }
70
73
  try {
71
74
  await this.client.query(`
72
- UPDATE "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
75
+ UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
73
76
  `);
74
77
  }
75
78
  catch (error) {
@@ -2,18 +2,16 @@ import { Client } from "pg";
2
2
  import { IMigration } from "./migration.interface.js";
3
3
  export declare class CreateRefreshTokenTableMigration implements IMigration {
4
4
  private readonly client;
5
- private readonly orgId?;
6
- constructor(client: Client, orgId?: string | undefined);
5
+ constructor(client: Client);
7
6
  index: number;
8
- _id: string;
9
- execute(): Promise<{
7
+ execute(_orgId?: string): Promise<{
10
8
  success: boolean;
11
- error: Error;
9
+ error: null;
12
10
  } | {
13
11
  success: boolean;
14
- error: null;
12
+ error: Error;
15
13
  }>;
16
- revert(): Promise<{
14
+ revert(_orgId?: string): Promise<{
17
15
  success: boolean;
18
16
  error: Error;
19
17
  } | {
@@ -1,14 +1,12 @@
1
1
  import { randomUUID } from "crypto";
2
2
  export class CreateRefreshTokenTableMigration {
3
3
  client;
4
- orgId;
5
- constructor(client, orgId) {
4
+ constructor(client) {
6
5
  this.client = client;
7
- this.orgId = orgId;
8
6
  }
9
7
  index = 4;
10
- _id = randomUUID().toString();
11
- async execute() {
8
+ async execute(_orgId) {
9
+ const _id = randomUUID().toString();
12
10
  try {
13
11
  await this.client.query(`
14
12
  CREATE TABLE "refreshTokens" (
@@ -24,13 +22,18 @@ export class CreateRefreshTokenTableMigration {
24
22
  `);
25
23
  }
26
24
  catch (error) {
27
- return { success: false, error: new Error(`Error creating refresh token table: ${error.message}`) };
25
+ if (error.code === '42P07' || error.data?.error?.includes('already exists')) {
26
+ return { success: true, error: null };
27
+ }
28
+ else {
29
+ return { success: false, error: new Error(`Error creating refresh token table: ${error.message}`) };
30
+ }
28
31
  }
29
- if (this.orgId) {
32
+ if (_orgId) {
30
33
  try {
31
34
  await this.client.query(`
32
35
  INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
33
- VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
36
+ VALUES ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);
34
37
  `);
35
38
  }
36
39
  catch (error) {
@@ -41,7 +44,7 @@ export class CreateRefreshTokenTableMigration {
41
44
  try {
42
45
  await this.client.query(`
43
46
  INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
44
- VALUES ('${this._id}', ${this.index}, TRUE, FALSE);
47
+ VALUES ('${_id}', ${this.index}, TRUE, FALSE);
45
48
  `);
46
49
  }
47
50
  catch (error) {
@@ -50,7 +53,7 @@ export class CreateRefreshTokenTableMigration {
50
53
  }
51
54
  return { success: true, error: null };
52
55
  }
53
- async revert() {
56
+ async revert(_orgId) {
54
57
  try {
55
58
  await this.client.query(`
56
59
  DROP TABLE "refreshTokens";
@@ -61,7 +64,7 @@ export class CreateRefreshTokenTableMigration {
61
64
  }
62
65
  try {
63
66
  await this.client.query(`
64
- UPDATE "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
67
+ UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
65
68
  `);
66
69
  }
67
70
  catch (error) {
@@ -1,11 +1,10 @@
1
- import { IEntity } from "@loomcore/common/models";
2
- export interface IMigration extends IEntity {
1
+ export interface IMigration {
3
2
  index: number;
4
- execute(): Promise<{
3
+ execute(_orgId?: string): Promise<{
5
4
  success: boolean;
6
5
  error: Error | null;
7
6
  }>;
8
- revert(): Promise<{
7
+ revert(_orgId?: string): Promise<{
9
8
  success: boolean;
10
9
  error: Error | null;
11
10
  }>;
@@ -1,5 +1,5 @@
1
1
  import { Client } from "pg";
2
- export declare function setupDatabaseForAuth(client: Client, orgId?: string): Promise<{
2
+ export declare function setupDatabaseForAuth(client: Client, _orgId?: string): Promise<{
3
3
  success: boolean;
4
4
  error: Error | null;
5
5
  }>;
@@ -1,15 +1,15 @@
1
1
  import { CreateRefreshTokenTableMigration } from "./004-create-refresh-token-table.migration.js";
2
2
  import { CreateMigrationTableMigration } from "./001-create-migrations-table.migration.js";
3
3
  import { CreateUsersTableMigration } from "./003-create-users-table.migration.js";
4
- export async function setupDatabaseForAuth(client, orgId) {
4
+ export async function setupDatabaseForAuth(client, _orgId) {
5
5
  const migrations = [
6
- new CreateMigrationTableMigration(client, orgId),
7
- new CreateUsersTableMigration(client, orgId),
8
- new CreateRefreshTokenTableMigration(client, orgId),
6
+ new CreateMigrationTableMigration(client),
7
+ new CreateUsersTableMigration(client),
8
+ new CreateRefreshTokenTableMigration(client),
9
9
  ];
10
10
  try {
11
11
  for (const migration of migrations) {
12
- await migration.execute();
12
+ await migration.execute(_orgId);
13
13
  }
14
14
  }
15
15
  catch (error) {
@@ -1,5 +1,5 @@
1
1
  import { Client } from "pg";
2
- export declare function setupDatabaseForMultitenant(client: Client, orgId: string): Promise<{
2
+ export declare function setupDatabaseForMultitenant(client: Client, orgName: string, orgCode: string): Promise<{
3
3
  success: boolean;
4
4
  error: Error | null;
5
5
  }>;
@@ -1,9 +1,9 @@
1
1
  import { CreateMigrationTableMigration } from "./001-create-migrations-table.migration.js";
2
2
  import { CreateOrganizationTableMigration } from "./002-create-organizations-table.migration.js";
3
- export async function setupDatabaseForMultitenant(client, orgId) {
3
+ export async function setupDatabaseForMultitenant(client, orgName, orgCode) {
4
4
  const migrations = [
5
- new CreateMigrationTableMigration(client, orgId),
6
- new CreateOrganizationTableMigration(client, orgId),
5
+ new CreateMigrationTableMigration(client),
6
+ new CreateOrganizationTableMigration(client, orgName, orgCode),
7
7
  ];
8
8
  try {
9
9
  for (const migration of migrations) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
6
  "scripts": {