@loomcore/api 0.1.5 → 0.1.7

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 (32) hide show
  1. package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.d.ts +14 -2
  2. package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.js +26 -10
  3. package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.d.ts +14 -2
  4. package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.js +23 -10
  5. package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.d.ts +14 -2
  6. package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.js +23 -11
  7. package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.d.ts +14 -2
  8. package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.js +23 -11
  9. package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.d.ts +14 -2
  10. package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.js +29 -6
  11. package/dist/__tests__/postgres-test-migrations/run-test-migrations.d.ts +4 -1
  12. package/dist/__tests__/postgres-test-migrations/run-test-migrations.js +7 -6
  13. package/dist/databases/index.d.ts +2 -0
  14. package/dist/databases/index.js +2 -0
  15. package/dist/databases/operations/index.d.ts +2 -0
  16. package/dist/databases/operations/index.js +2 -0
  17. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +14 -2
  18. package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +22 -11
  19. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +14 -2
  20. package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +16 -7
  21. package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +14 -2
  22. package/dist/databases/postgres/migrations/003-create-users-table.migration.js +28 -13
  23. package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.d.ts +15 -3
  24. package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.js +40 -25
  25. package/dist/databases/postgres/migrations/migration.interface.d.ts +8 -2
  26. package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +4 -1
  27. package/dist/databases/postgres/migrations/setup-for-auth.migration.js +7 -6
  28. package/dist/databases/postgres/migrations/setup-for-multitenant.migration.d.ts +4 -1
  29. package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +7 -6
  30. package/dist/utils/express.utils.d.ts +4 -3
  31. package/dist/utils/express.utils.js +2 -2
  32. package/package.json +1 -1
@@ -6,6 +6,18 @@ export declare class CreateTestEntitiesTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -27,22 +27,31 @@ export class CreateTestEntitiesTableMigration {
27
27
  "_deletedBy" VARCHAR(255)
28
28
  )
29
29
  `);
30
- if (this.orgId) {
30
+ }
31
+ catch (error) {
32
+ return { success: false, error: new Error(`Error creating test entities table: ${error.message}`) };
33
+ }
34
+ if (this.orgId) {
35
+ try {
31
36
  await this.client.query(`
32
37
  Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
33
38
  `);
34
39
  }
35
- else {
40
+ catch (error) {
41
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
42
+ }
43
+ }
44
+ else {
45
+ try {
36
46
  await this.client.query(`
37
47
  Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
38
48
  `);
39
49
  }
40
- return true;
41
- }
42
- catch (error) {
43
- console.error('Error creating test entities table:', error);
44
- return false;
50
+ catch (error) {
51
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
52
+ }
45
53
  }
54
+ return { success: true, error: null };
46
55
  }
47
56
  async revert() {
48
57
  try {
@@ -51,9 +60,16 @@ export class CreateTestEntitiesTableMigration {
51
60
  `);
52
61
  }
53
62
  catch (error) {
54
- console.error('Error reverting test entities table:', error);
55
- return false;
63
+ return { success: false, error: new Error(`Error dropping test entities table: ${error.message}`) };
64
+ }
65
+ try {
66
+ await this.client.query(`
67
+ Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
68
+ `);
69
+ }
70
+ catch (error) {
71
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
56
72
  }
57
- return true;
73
+ return { success: true, error: null };
58
74
  }
59
75
  }
@@ -6,6 +6,18 @@ export declare class CreateCategoriesTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -17,36 +17,49 @@ export class CreateCategoriesTableMigration {
17
17
  "name" VARCHAR(255) NOT NULL
18
18
  )
19
19
  `);
20
- if (this.orgId) {
20
+ }
21
+ catch (error) {
22
+ return { success: false, error: new Error(`Error creating categories table: ${error.message}`) };
23
+ }
24
+ if (this.orgId) {
25
+ try {
21
26
  await this.client.query(`
22
27
  Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
23
28
  `);
24
29
  }
25
- else {
30
+ catch (error) {
31
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
32
+ }
33
+ }
34
+ else {
35
+ try {
26
36
  await this.client.query(`
27
37
  Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
28
38
  `);
29
39
  }
30
- return true;
31
- }
32
- catch (error) {
33
- console.error('Error creating categories table:', error);
34
- return false;
40
+ catch (error) {
41
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
42
+ }
35
43
  }
44
+ return { success: true, error: null };
36
45
  }
37
46
  async revert() {
38
47
  try {
39
48
  await this.client.query(`
40
49
  DROP TABLE "categories";
41
50
  `);
51
+ }
52
+ catch (error) {
53
+ return { success: false, error: new Error(`Error dropping categories table: ${error.message}`) };
54
+ }
55
+ try {
42
56
  await this.client.query(`
43
57
  Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
44
58
  `);
45
59
  }
46
60
  catch (error) {
47
- console.error('Error reverting categories table:', error);
48
- return false;
61
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
49
62
  }
50
- return true;
63
+ return { success: true, error: null };
51
64
  }
52
65
  }
@@ -6,6 +6,18 @@ export declare class CreateProductsTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -26,37 +26,49 @@ export class CreateProductsTableMigration {
26
26
  "_deletedBy" VARCHAR(255)
27
27
  )
28
28
  `);
29
- if (this.orgId) {
29
+ }
30
+ catch (error) {
31
+ return { success: false, error: new Error(`Error creating products table: ${error.message}`) };
32
+ }
33
+ if (this.orgId) {
34
+ try {
30
35
  await this.client.query(`
31
36
  Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
32
37
  `);
33
38
  }
34
- else {
39
+ catch (error) {
40
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
41
+ }
42
+ }
43
+ else {
44
+ try {
35
45
  await this.client.query(`
36
46
  Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
37
47
  `);
38
48
  }
39
- return true;
40
- }
41
- catch (error) {
42
- console.error('Error creating products table:', error);
43
- return false;
49
+ catch (error) {
50
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
51
+ }
44
52
  }
45
- return true;
53
+ return { success: true, error: null };
46
54
  }
47
55
  async revert() {
48
56
  try {
49
57
  await this.client.query(`
50
58
  DROP TABLE "products";
51
59
  `);
60
+ }
61
+ catch (error) {
62
+ return { success: false, error: new Error(`Error dropping products table: ${error.message}`) };
63
+ }
64
+ try {
52
65
  await this.client.query(`
53
66
  Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
54
67
  `);
55
68
  }
56
69
  catch (error) {
57
- console.error('Error reverting products table:', error);
58
- return false;
70
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
59
71
  }
60
- return true;
72
+ return { success: true, error: null };
61
73
  }
62
74
  }
@@ -6,6 +6,18 @@ export declare class CreateTestUsersTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -30,23 +30,31 @@ export class CreateTestUsersTableMigration {
30
30
  "_deletedBy" VARCHAR(255)
31
31
  )
32
32
  `);
33
- if (this.orgId) {
33
+ }
34
+ catch (error) {
35
+ return { success: false, error: new Error(`Error creating test users table: ${error.message}`) };
36
+ }
37
+ if (this.orgId) {
38
+ try {
34
39
  await this.client.query(`
35
40
  Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
36
41
  `);
37
42
  }
38
- else {
43
+ catch (error) {
44
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
45
+ }
46
+ }
47
+ else {
48
+ try {
39
49
  await this.client.query(`
40
50
  Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
41
51
  `);
42
52
  }
43
- return true;
44
- }
45
- catch (error) {
46
- console.error('Error creating test users table:', error);
47
- return false;
53
+ catch (error) {
54
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
55
+ }
48
56
  }
49
- return true;
57
+ return { success: true, error: null };
50
58
  }
51
59
  async revert() {
52
60
  try {
@@ -55,12 +63,16 @@ export class CreateTestUsersTableMigration {
55
63
  `);
56
64
  }
57
65
  catch (error) {
66
+ return { success: false, error: new Error(`Error dropping test users table: ${error.message}`) };
67
+ }
68
+ try {
58
69
  await this.client.query(`
59
70
  Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
60
71
  `);
61
- console.error('Error reverting test users table:', error);
62
- return false;
63
72
  }
64
- return true;
73
+ catch (error) {
74
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
75
+ }
76
+ return { success: true, error: null };
65
77
  }
66
78
  }
@@ -6,6 +6,18 @@ export declare class CreateTestItemsTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -27,10 +27,29 @@ export class CreateTestItemsTableMigration {
27
27
  `);
28
28
  }
29
29
  catch (error) {
30
- console.error('Error creating test items table:', error);
31
- return false;
30
+ return { success: false, error: new Error(`Error creating test items table: ${error.message}`) };
32
31
  }
33
- return true;
32
+ if (this.orgId) {
33
+ try {
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
+ catch (error) {
39
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
40
+ }
41
+ }
42
+ else {
43
+ try {
44
+ await this.client.query(`
45
+ Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
46
+ `);
47
+ }
48
+ catch (error) {
49
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
50
+ }
51
+ }
52
+ return { success: true, error: null };
34
53
  }
35
54
  async revert() {
36
55
  try {
@@ -39,12 +58,16 @@ export class CreateTestItemsTableMigration {
39
58
  `);
40
59
  }
41
60
  catch (error) {
61
+ return { success: false, error: new Error(`Error dropping test items table: ${error.message}`) };
62
+ }
63
+ try {
42
64
  await this.client.query(`
43
65
  Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
44
66
  `);
45
- console.error('Error reverting test items table:', error);
46
- return false;
47
67
  }
48
- return true;
68
+ catch (error) {
69
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
70
+ }
71
+ return { success: true, error: null };
49
72
  }
50
73
  }
@@ -1,2 +1,5 @@
1
1
  import { Client } from "pg";
2
- export declare function runTestMigrations(client: Client, orgId?: string): Promise<boolean>;
2
+ export declare function runTestMigrations(client: Client, orgId?: string): Promise<{
3
+ success: boolean;
4
+ error: Error | null;
5
+ }>;
@@ -11,12 +11,13 @@ export async function runTestMigrations(client, orgId) {
11
11
  new CreateTestUsersTableMigration(client, orgId),
12
12
  new CreateTestItemsTableMigration(client, orgId),
13
13
  ];
14
- let success = true;
15
- for (const migration of migrations) {
16
- success = await migration.execute();
17
- if (!success) {
18
- return false;
14
+ try {
15
+ for (const migration of migrations) {
16
+ await migration.execute();
19
17
  }
20
18
  }
21
- return success;
19
+ catch (error) {
20
+ return { success: false, error: error };
21
+ }
22
+ return { success: true, error: null };
22
23
  }
@@ -1,2 +1,4 @@
1
1
  export * from './mongo-db/index.js';
2
2
  export * from './postgres/index.js';
3
+ export * from './models/index.js';
4
+ export * from './operations/index.js';
@@ -1,2 +1,4 @@
1
1
  export * from './mongo-db/index.js';
2
2
  export * from './postgres/index.js';
3
+ export * from './models/index.js';
4
+ export * from './operations/index.js';
@@ -0,0 +1,2 @@
1
+ export * from './operation.js';
2
+ export * from './join.operation.js';
@@ -0,0 +1,2 @@
1
+ export * from './operation.js';
2
+ export * from './join.operation.js';
@@ -6,6 +6,18 @@ export declare class CreateMigrationTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -26,27 +26,38 @@ export class CreateMigrationTableMigration {
26
26
  alreadyRun = true;
27
27
  }
28
28
  else {
29
- throw error;
29
+ return { success: false, error: new Error(`Error creating migrations table: ${error.message}`) };
30
30
  }
31
31
  }
32
32
  if (!alreadyRun) {
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);
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);
36
43
  `);
44
+ }
37
45
  }
38
- else {
39
- await this.client.query(`
40
- INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted") VALUES ('${this._id}', ${this.index}, TRUE, FALSE);
41
- `);
46
+ catch (error) {
47
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
42
48
  }
43
49
  }
44
- return true;
50
+ return { success: true, error: null };
45
51
  }
46
52
  async revert() {
47
- await this.client.query(`
53
+ try {
54
+ await this.client.query(`
48
55
  DROP TABLE "migrations";
49
56
  `);
50
- return true;
57
+ }
58
+ catch (error) {
59
+ return { success: false, error: new Error(`Error reverting migration ${this.index} from migrations table: ${error.message}`) };
60
+ }
61
+ return { success: true, error: null };
51
62
  }
52
63
  }
@@ -6,6 +6,18 @@ export declare class CreateOrganizationTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId: string);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -27,29 +27,38 @@ export class CreateOrganizationTableMigration {
27
27
  "_deletedBy" VARCHAR(255)
28
28
  )
29
29
  `);
30
+ }
31
+ catch (error) {
32
+ return { success: false, error: new Error(`Error creating organization table: ${error.message}`) };
33
+ }
34
+ try {
30
35
  await this.client.query(`
31
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
36
+ INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
37
+ VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
32
38
  `);
33
- return true;
34
39
  }
35
40
  catch (error) {
36
- console.error('Error creating organization table:', error);
37
- return false;
41
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
38
42
  }
43
+ return { success: true, error: null };
39
44
  }
40
45
  async revert() {
41
46
  try {
42
47
  await this.client.query(`
43
48
  DROP TABLE "organizations";
44
49
  `);
50
+ }
51
+ catch (error) {
52
+ return { success: false, error: new Error(`Error dropping organizations table: ${error.message}`) };
53
+ }
54
+ try {
45
55
  await this.client.query(`
46
56
  Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
47
57
  `);
48
58
  }
49
59
  catch (error) {
50
- console.error('Error reverting organization table:', error);
51
- return false;
60
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
52
61
  }
53
- return true;
62
+ return { success: true, error: null };
54
63
  }
55
64
  }
@@ -6,6 +6,18 @@ export declare class CreateUsersTableMigration implements IMigration {
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -30,36 +30,51 @@ export class CreateUsersTableMigration {
30
30
  "_deletedBy" VARCHAR(255)
31
31
  )
32
32
  `);
33
- if (this.orgId) {
33
+ }
34
+ catch (error) {
35
+ return { success: false, error: new Error(`Error creating users table: ${error.message}`) };
36
+ }
37
+ if (this.orgId) {
38
+ try {
34
39
  await this.client.query(`
35
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
40
+ INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
41
+ VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
36
42
  `);
37
43
  }
38
- else {
44
+ catch (error) {
45
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
46
+ }
47
+ }
48
+ else {
49
+ try {
39
50
  await this.client.query(`
40
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
51
+ INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
52
+ VALUES ('${this._id}', ${this.index}, TRUE, FALSE);
41
53
  `);
42
54
  }
55
+ catch (error) {
56
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
57
+ }
43
58
  }
44
- catch (error) {
45
- console.error('Error creating users table:', error);
46
- return false;
47
- }
48
- return true;
59
+ return { success: true, error: null };
49
60
  }
50
61
  async revert() {
51
62
  try {
52
63
  await this.client.query(`
53
64
  DROP TABLE "users";
54
65
  `);
66
+ }
67
+ catch (error) {
68
+ return { success: false, error: new Error(`Error dropping users table: ${error.message}`) };
69
+ }
70
+ try {
55
71
  await this.client.query(`
56
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
72
+ UPDATE "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
57
73
  `);
58
74
  }
59
75
  catch (error) {
60
- console.error('Error reverting users table:', error);
61
- return false;
76
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
62
77
  }
63
- return true;
78
+ return { success: true, error: null };
64
79
  }
65
80
  }
@@ -1,11 +1,23 @@
1
1
  import { Client } from "pg";
2
- import { IMigration } from "./index.js";
2
+ import { IMigration } from "./migration.interface.js";
3
3
  export declare class CreateRefreshTokenTableMigration implements IMigration {
4
4
  private readonly client;
5
5
  private readonly orgId?;
6
6
  constructor(client: Client, orgId?: string | undefined);
7
7
  index: number;
8
8
  _id: string;
9
- execute(): Promise<boolean>;
10
- revert(): Promise<boolean>;
9
+ execute(): Promise<{
10
+ success: boolean;
11
+ error: Error;
12
+ } | {
13
+ success: boolean;
14
+ error: null;
15
+ }>;
16
+ revert(): Promise<{
17
+ success: boolean;
18
+ error: Error;
19
+ } | {
20
+ success: boolean;
21
+ error: null;
22
+ }>;
11
23
  }
@@ -11,47 +11,62 @@ export class CreateRefreshTokenTableMigration {
11
11
  async execute() {
12
12
  try {
13
13
  await this.client.query(`
14
- CREATE TABLE "refreshTokens" (
15
- "_id" VARCHAR(255) PRIMARY KEY,
16
- "_orgId" VARCHAR(255),
17
- "token" VARCHAR(255) NOT NULL,
18
- "deviceId" VARCHAR(255) NOT NULL,
19
- "userId" VARCHAR(255) NOT NULL,
20
- "expiresOn" BIGINT NOT NULL,
21
- "created" TIMESTAMP NOT NULL,
22
- "createdBy" VARCHAR(255) NOT NULL
23
- )
24
- `);
25
- if (this.orgId) {
14
+ CREATE TABLE "refreshTokens" (
15
+ "_id" VARCHAR(255) PRIMARY KEY,
16
+ "_orgId" VARCHAR(255),
17
+ "token" VARCHAR(255) NOT NULL,
18
+ "deviceId" VARCHAR(255) NOT NULL,
19
+ "userId" VARCHAR(255) NOT NULL,
20
+ "expiresOn" BIGINT NOT NULL,
21
+ "created" TIMESTAMP NOT NULL,
22
+ "createdBy" VARCHAR(255) NOT NULL
23
+ )
24
+ `);
25
+ }
26
+ catch (error) {
27
+ return { success: false, error: new Error(`Error creating refresh token table: ${error.message}`) };
28
+ }
29
+ if (this.orgId) {
30
+ try {
26
31
  await this.client.query(`
27
- Insert into "migrations" ("_id", "_orgId", "index", "hasRun", "reverted") values ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
32
+ INSERT INTO "migrations" ("_id", "_orgId", "index", "hasRun", "reverted")
33
+ VALUES ('${this._id}', '${this.orgId}', ${this.index}, TRUE, FALSE);
28
34
  `);
29
35
  }
30
- else {
36
+ catch (error) {
37
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
38
+ }
39
+ }
40
+ else {
41
+ try {
31
42
  await this.client.query(`
32
- Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${this._id}', ${this.index}, TRUE, FALSE);
43
+ INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
44
+ VALUES ('${this._id}', ${this.index}, TRUE, FALSE);
33
45
  `);
34
46
  }
35
- return true;
36
- }
37
- catch (error) {
38
- console.error('Error creating refresh token table:', error);
39
- return false;
47
+ catch (error) {
48
+ return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
49
+ }
40
50
  }
51
+ return { success: true, error: null };
41
52
  }
42
53
  async revert() {
43
54
  try {
44
55
  await this.client.query(`
45
- DROP TABLE "refresh_tokens";
56
+ DROP TABLE "refreshTokens";
46
57
  `);
58
+ }
59
+ catch (error) {
60
+ return { success: false, error: new Error(`Error dropping refresh token table: ${error.message}`) };
61
+ }
62
+ try {
47
63
  await this.client.query(`
48
- Update "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
64
+ UPDATE "migrations" SET "reverted" = TRUE WHERE "_id" = '${this._id}';
49
65
  `);
50
- return true;
51
66
  }
52
67
  catch (error) {
53
- console.error('Error reverting refresh token table:', error);
54
- return false;
68
+ return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
55
69
  }
70
+ return { success: true, error: null };
56
71
  }
57
72
  }
@@ -1,6 +1,12 @@
1
1
  import { IEntity } from "@loomcore/common/models";
2
2
  export interface IMigration extends IEntity {
3
3
  index: number;
4
- execute(): Promise<boolean>;
5
- revert(): Promise<boolean>;
4
+ execute(): Promise<{
5
+ success: boolean;
6
+ error: Error | null;
7
+ }>;
8
+ revert(): Promise<{
9
+ success: boolean;
10
+ error: Error | null;
11
+ }>;
6
12
  }
@@ -1,2 +1,5 @@
1
1
  import { Client } from "pg";
2
- export declare function setupDatabaseForAuth(client: Client, orgId?: string): Promise<boolean>;
2
+ export declare function setupDatabaseForAuth(client: Client, orgId?: string): Promise<{
3
+ success: boolean;
4
+ error: Error | null;
5
+ }>;
@@ -7,12 +7,13 @@ export async function setupDatabaseForAuth(client, orgId) {
7
7
  new CreateUsersTableMigration(client, orgId),
8
8
  new CreateRefreshTokenTableMigration(client, orgId),
9
9
  ];
10
- let success = true;
11
- for (const migration of migrations) {
12
- success = await migration.execute();
13
- if (!success) {
14
- return false;
10
+ try {
11
+ for (const migration of migrations) {
12
+ await migration.execute();
15
13
  }
16
14
  }
17
- return success;
15
+ catch (error) {
16
+ return { success: false, error: error };
17
+ }
18
+ return { success: true, error: null };
18
19
  }
@@ -1,2 +1,5 @@
1
1
  import { Client } from "pg";
2
- export declare function setupDatabaseForMultitenant(client: Client, orgId: string): Promise<boolean>;
2
+ export declare function setupDatabaseForMultitenant(client: Client, orgId: string): Promise<{
3
+ success: boolean;
4
+ error: Error | null;
5
+ }>;
@@ -5,12 +5,13 @@ export async function setupDatabaseForMultitenant(client, orgId) {
5
5
  new CreateMigrationTableMigration(client, orgId),
6
6
  new CreateOrganizationTableMigration(client, orgId),
7
7
  ];
8
- let success = true;
9
- for (const migration of migrations) {
10
- success = await migration.execute();
11
- if (!success) {
12
- return false;
8
+ try {
9
+ for (const migration of migrations) {
10
+ await migration.execute();
13
11
  }
14
12
  }
15
- return success;
13
+ catch (error) {
14
+ return { success: false, error: error };
15
+ }
16
+ return { success: true, error: null };
16
17
  }
@@ -1,9 +1,10 @@
1
1
  import { Application } from 'express';
2
- import { Db, MongoClient } from "mongodb";
2
+ import { MongoClient } from "mongodb";
3
3
  import { Server } from "http";
4
4
  import { IBaseApiConfig } from "../models/index.js";
5
- type RouteSetupFunction = (app: Application, db: Db, config: IBaseApiConfig) => void;
6
- declare function setupExpressApp(db: Db, config: IBaseApiConfig, setupRoutes: RouteSetupFunction): Application;
5
+ import { IDatabase } from '../databases/models/index.js';
6
+ type RouteSetupFunction = (app: Application, database: IDatabase, config: IBaseApiConfig) => void;
7
+ declare function setupExpressApp(database: IDatabase, config: IBaseApiConfig, setupRoutes: RouteSetupFunction): Application;
7
8
  declare function performGracefulShutdown(event: any, mongoClient: MongoClient | null, externalServer: Server | null, internalServer: Server | null): void;
8
9
  export declare const expressUtils: {
9
10
  setupExpressApp: typeof setupExpressApp;
@@ -6,7 +6,7 @@ import qs from 'qs';
6
6
  import { NotFoundError } from "../errors/not-found.error.js";
7
7
  import { errorHandler } from "../middleware/error-handler.js";
8
8
  import { ensureUserContext } from '../middleware/ensure-user-context.js';
9
- function setupExpressApp(db, config, setupRoutes) {
9
+ function setupExpressApp(database, config, setupRoutes) {
10
10
  const app = express();
11
11
  app.set('query parser', (str) => {
12
12
  return qs.parse(str, {});
@@ -24,7 +24,7 @@ function setupExpressApp(db, config, setupRoutes) {
24
24
  credentials: true
25
25
  }));
26
26
  app.use(ensureUserContext);
27
- setupRoutes(app, db, config);
27
+ setupRoutes(app, database, config);
28
28
  app.use(async (req, res) => {
29
29
  throw new NotFoundError(`Requested path, ${req.path}, Not Found`);
30
30
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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": {