@loomcore/api 0.1.39 → 0.1.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +49 -49
- package/dist/__tests__/postgres-test-migrations/100-create-test-entities-table.migration.js +22 -22
- package/dist/__tests__/postgres-test-migrations/101-create-categories-table.migration.js +12 -12
- package/dist/__tests__/postgres-test-migrations/102-create-products-table.migration.js +21 -21
- package/dist/__tests__/postgres-test-migrations/103-create-test-items-table.migration.js +20 -20
- package/dist/__tests__/postgres.test-database.js +8 -8
- package/dist/databases/index.d.ts +1 -0
- package/dist/databases/index.js +1 -0
- package/dist/databases/migrations/migration-runner.d.ts +1 -0
- package/dist/databases/migrations/migration-runner.js +6 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.js +7 -7
- package/dist/databases/postgres/commands/postgres-create-many.command.js +10 -10
- package/dist/databases/postgres/commands/postgres-create.command.js +4 -4
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +13 -13
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +7 -7
- package/dist/databases/postgres/commands/postgres-update.command.js +7 -7
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +12 -12
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +24 -24
- package/dist/databases/postgres/migrations/003-create-users-table.migration.js +26 -26
- package/dist/databases/postgres/migrations/004-create-refresh-tokens-table.migration.js +18 -18
- package/dist/databases/postgres/migrations/005-create-meta-org.migration.js +9 -9
- package/dist/databases/postgres/migrations/006-create-admin-user.migration.js +3 -3
- package/dist/databases/postgres/migrations/007-create-roles-table.migration.js +14 -14
- package/dist/databases/postgres/migrations/008-create-user-roles-table.migration.js +24 -24
- package/dist/databases/postgres/migrations/009-create-features-table.migration.js +14 -14
- package/dist/databases/postgres/migrations/010-create-authorizations-table.migration.js +26 -26
- package/dist/databases/postgres/migrations/011-create-admin-authorization.migration.js +44 -44
- package/dist/databases/postgres/migrations/database-builder.js +4 -4
- package/dist/databases/postgres/postgres.database.js +17 -17
- package/dist/databases/postgres/utils/build-select-clause.js +6 -6
- package/dist/databases/postgres/utils/does-table-exist.util.js +4 -4
- package/package.json +88 -88
|
@@ -8,29 +8,29 @@ export class CreateProductsTableMigration {
|
|
|
8
8
|
async execute() {
|
|
9
9
|
const _id = randomUUID().toString();
|
|
10
10
|
try {
|
|
11
|
-
await this.client.query(`
|
|
12
|
-
CREATE TABLE "products" (
|
|
13
|
-
"_id" VARCHAR(255) PRIMARY KEY,
|
|
14
|
-
"_orgId" VARCHAR(255),
|
|
15
|
-
"name" VARCHAR(255) NOT NULL,
|
|
16
|
-
"description" TEXT,
|
|
17
|
-
"internalNumber" VARCHAR(255),
|
|
18
|
-
"categoryId" VARCHAR(255) NOT NULL REFERENCES "categories"("_id"),
|
|
19
|
-
"_created" TIMESTAMP NOT NULL,
|
|
20
|
-
"_createdBy" VARCHAR(255) NOT NULL,
|
|
21
|
-
"_updated" TIMESTAMP NOT NULL,
|
|
22
|
-
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
23
|
-
"_deleted" TIMESTAMP,
|
|
24
|
-
"_deletedBy" VARCHAR(255)
|
|
25
|
-
)
|
|
11
|
+
await this.client.query(`
|
|
12
|
+
CREATE TABLE "products" (
|
|
13
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
14
|
+
"_orgId" VARCHAR(255),
|
|
15
|
+
"name" VARCHAR(255) NOT NULL,
|
|
16
|
+
"description" TEXT,
|
|
17
|
+
"internalNumber" VARCHAR(255),
|
|
18
|
+
"categoryId" VARCHAR(255) NOT NULL REFERENCES "categories"("_id"),
|
|
19
|
+
"_created" TIMESTAMP NOT NULL,
|
|
20
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
21
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
22
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
23
|
+
"_deleted" TIMESTAMP,
|
|
24
|
+
"_deletedBy" VARCHAR(255)
|
|
25
|
+
)
|
|
26
26
|
`);
|
|
27
27
|
}
|
|
28
28
|
catch (error) {
|
|
29
29
|
return { success: false, error: new Error(`Error creating products table: ${error.message}`) };
|
|
30
30
|
}
|
|
31
31
|
try {
|
|
32
|
-
await this.client.query(`
|
|
33
|
-
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
|
|
32
|
+
await this.client.query(`
|
|
33
|
+
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
|
|
34
34
|
`);
|
|
35
35
|
}
|
|
36
36
|
catch (error) {
|
|
@@ -40,16 +40,16 @@ export class CreateProductsTableMigration {
|
|
|
40
40
|
}
|
|
41
41
|
async revert() {
|
|
42
42
|
try {
|
|
43
|
-
await this.client.query(`
|
|
44
|
-
DROP TABLE "products";
|
|
43
|
+
await this.client.query(`
|
|
44
|
+
DROP TABLE "products";
|
|
45
45
|
`);
|
|
46
46
|
}
|
|
47
47
|
catch (error) {
|
|
48
48
|
return { success: false, error: new Error(`Error dropping products table: ${error.message}`) };
|
|
49
49
|
}
|
|
50
50
|
try {
|
|
51
|
-
await this.client.query(`
|
|
52
|
-
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
51
|
+
await this.client.query(`
|
|
52
|
+
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
53
53
|
`);
|
|
54
54
|
}
|
|
55
55
|
catch (error) {
|
|
@@ -8,28 +8,28 @@ export class CreateTestItemsTableMigration {
|
|
|
8
8
|
async execute() {
|
|
9
9
|
const _id = randomUUID().toString();
|
|
10
10
|
try {
|
|
11
|
-
await this.client.query(`
|
|
12
|
-
CREATE TABLE "testItems" (
|
|
13
|
-
"_id" VARCHAR(255) PRIMARY KEY,
|
|
14
|
-
"_orgId" VARCHAR(255),
|
|
15
|
-
"name" VARCHAR(255) NOT NULL,
|
|
16
|
-
"value" INTEGER,
|
|
17
|
-
"eventDate" TIMESTAMP,
|
|
18
|
-
"_created" TIMESTAMP NOT NULL,
|
|
19
|
-
"_createdBy" VARCHAR(255) NOT NULL,
|
|
20
|
-
"_updated" TIMESTAMP NOT NULL,
|
|
21
|
-
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
22
|
-
"_deleted" TIMESTAMP,
|
|
23
|
-
"_deletedBy" VARCHAR(255)
|
|
24
|
-
)
|
|
11
|
+
await this.client.query(`
|
|
12
|
+
CREATE TABLE "testItems" (
|
|
13
|
+
"_id" VARCHAR(255) PRIMARY KEY,
|
|
14
|
+
"_orgId" VARCHAR(255),
|
|
15
|
+
"name" VARCHAR(255) NOT NULL,
|
|
16
|
+
"value" INTEGER,
|
|
17
|
+
"eventDate" TIMESTAMP,
|
|
18
|
+
"_created" TIMESTAMP NOT NULL,
|
|
19
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
20
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
21
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
22
|
+
"_deleted" TIMESTAMP,
|
|
23
|
+
"_deletedBy" VARCHAR(255)
|
|
24
|
+
)
|
|
25
25
|
`);
|
|
26
26
|
}
|
|
27
27
|
catch (error) {
|
|
28
28
|
return { success: false, error: new Error(`Error creating test items table: ${error.message}`) };
|
|
29
29
|
}
|
|
30
30
|
try {
|
|
31
|
-
await this.client.query(`
|
|
32
|
-
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
|
|
31
|
+
await this.client.query(`
|
|
32
|
+
Insert into "migrations" ("_id", "index", "hasRun", "reverted") values ('${_id}', ${this.index}, TRUE, FALSE);
|
|
33
33
|
`);
|
|
34
34
|
}
|
|
35
35
|
catch (error) {
|
|
@@ -39,16 +39,16 @@ export class CreateTestItemsTableMigration {
|
|
|
39
39
|
}
|
|
40
40
|
async revert() {
|
|
41
41
|
try {
|
|
42
|
-
await this.client.query(`
|
|
43
|
-
DROP TABLE "testItems";
|
|
42
|
+
await this.client.query(`
|
|
43
|
+
DROP TABLE "testItems";
|
|
44
44
|
`);
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
47
47
|
return { success: false, error: new Error(`Error dropping test items table: ${error.message}`) };
|
|
48
48
|
}
|
|
49
49
|
try {
|
|
50
|
-
await this.client.query(`
|
|
51
|
-
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
50
|
+
await this.client.query(`
|
|
51
|
+
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
52
52
|
`);
|
|
53
53
|
}
|
|
54
54
|
catch (error) {
|
|
@@ -39,9 +39,9 @@ export class TestPostgresDatabase {
|
|
|
39
39
|
}
|
|
40
40
|
async createIndexes(client) {
|
|
41
41
|
try {
|
|
42
|
-
await client.query(`
|
|
43
|
-
CREATE INDEX IF NOT EXISTS email_index ON users (LOWER(email));
|
|
44
|
-
CREATE UNIQUE INDEX IF NOT EXISTS email_unique_index ON users (LOWER(email));
|
|
42
|
+
await client.query(`
|
|
43
|
+
CREATE INDEX IF NOT EXISTS email_index ON users (LOWER(email));
|
|
44
|
+
CREATE UNIQUE INDEX IF NOT EXISTS email_unique_index ON users (LOWER(email));
|
|
45
45
|
`);
|
|
46
46
|
}
|
|
47
47
|
catch (error) {
|
|
@@ -52,11 +52,11 @@ export class TestPostgresDatabase {
|
|
|
52
52
|
if (!this.postgresClient) {
|
|
53
53
|
throw new Error('Database not initialized');
|
|
54
54
|
}
|
|
55
|
-
const result = await this.postgresClient.query(`
|
|
56
|
-
SELECT "table_name"
|
|
57
|
-
FROM information_schema.tables
|
|
58
|
-
WHERE "table_schema" = 'public'
|
|
59
|
-
AND "table_type" = 'BASE TABLE'
|
|
55
|
+
const result = await this.postgresClient.query(`
|
|
56
|
+
SELECT "table_name"
|
|
57
|
+
FROM information_schema.tables
|
|
58
|
+
WHERE "table_schema" = 'public'
|
|
59
|
+
AND "table_type" = 'BASE TABLE'
|
|
60
60
|
`);
|
|
61
61
|
result.rows.forEach(async (row) => {
|
|
62
62
|
await this.postgresClient?.query(`TRUNCATE TABLE "${row.table_name}" RESTART IDENTITY CASCADE`);
|
package/dist/databases/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import fs from 'fs';
|
|
|
5
5
|
import path from 'path';
|
|
6
6
|
export class MigrationRunner {
|
|
7
7
|
config;
|
|
8
|
+
mongoClient = null;
|
|
8
9
|
constructor(config) {
|
|
9
10
|
this.config = config;
|
|
10
11
|
}
|
|
@@ -52,6 +53,7 @@ export class MigrationRunner {
|
|
|
52
53
|
}
|
|
53
54
|
else if (dbType === 'mongo') {
|
|
54
55
|
const client = await MongoClient.connect(dbUrl);
|
|
56
|
+
this.mongoClient = client;
|
|
55
57
|
const db = client.db();
|
|
56
58
|
return new Umzug({
|
|
57
59
|
migrations: { glob: path.join(migrationsDir, '*.ts') },
|
|
@@ -114,5 +116,9 @@ export class MigrationRunner {
|
|
|
114
116
|
if (this.config.dbType === 'postgres') {
|
|
115
117
|
await migrator.context.end();
|
|
116
118
|
}
|
|
119
|
+
else if (this.config.dbType === 'mongo' && this.mongoClient) {
|
|
120
|
+
await this.mongoClient.close();
|
|
121
|
+
this.mongoClient = null;
|
|
122
|
+
}
|
|
117
123
|
}
|
|
118
124
|
}
|
|
@@ -26,10 +26,10 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
|
|
|
26
26
|
const setClause = columns.map((col, index) => `${col} = $${index + 1}`).join(', ');
|
|
27
27
|
queryObject.filters._id = { eq: _id };
|
|
28
28
|
const { whereClause } = buildWhereClause(queryObject, values);
|
|
29
|
-
const query = `
|
|
30
|
-
UPDATE "${pluralResourceName}"
|
|
31
|
-
SET ${setClause}
|
|
32
|
-
${whereClause}
|
|
29
|
+
const query = `
|
|
30
|
+
UPDATE "${pluralResourceName}"
|
|
31
|
+
SET ${setClause}
|
|
32
|
+
${whereClause}
|
|
33
33
|
`;
|
|
34
34
|
await client.query(query, values);
|
|
35
35
|
}
|
|
@@ -39,9 +39,9 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
|
|
|
39
39
|
const tablePrefix = hasJoins ? pluralResourceName : undefined;
|
|
40
40
|
queryObject.filters._id = { in: entityIds };
|
|
41
41
|
const { whereClause, values } = buildWhereClause(queryObject, [], tablePrefix);
|
|
42
|
-
const selectQuery = `
|
|
43
|
-
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
44
|
-
${whereClause}
|
|
42
|
+
const selectQuery = `
|
|
43
|
+
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
44
|
+
${whereClause}
|
|
45
45
|
`;
|
|
46
46
|
const result = await client.query(selectQuery, values);
|
|
47
47
|
return result.rows;
|
|
@@ -12,12 +12,12 @@ export async function createMany(client, pluralResourceName, entities) {
|
|
|
12
12
|
entity._id = entity._id ?? randomUUID().toString();
|
|
13
13
|
return entity;
|
|
14
14
|
});
|
|
15
|
-
const tableColumns = await client.query(`
|
|
16
|
-
SELECT column_name, column_default
|
|
17
|
-
FROM information_schema.columns
|
|
18
|
-
WHERE table_schema = current_schema()
|
|
19
|
-
AND table_name = $1
|
|
20
|
-
ORDER BY ordinal_position
|
|
15
|
+
const tableColumns = await client.query(`
|
|
16
|
+
SELECT column_name, column_default
|
|
17
|
+
FROM information_schema.columns
|
|
18
|
+
WHERE table_schema = current_schema()
|
|
19
|
+
AND table_name = $1
|
|
20
|
+
ORDER BY ordinal_position
|
|
21
21
|
`, [pluralResourceName]);
|
|
22
22
|
if (tableColumns.rows.length === 0) {
|
|
23
23
|
throw new BadRequestError(`Unable to resolve columns for ${pluralResourceName}`);
|
|
@@ -39,10 +39,10 @@ export async function createMany(client, pluralResourceName, entities) {
|
|
|
39
39
|
valueClauses.push(`(${placeholders})`);
|
|
40
40
|
allValues.push(...values);
|
|
41
41
|
});
|
|
42
|
-
const query = `
|
|
43
|
-
INSERT INTO "${pluralResourceName}" (${tableColumns.rows.map(column => `"${column.column_name}"`).join(', ')})
|
|
44
|
-
VALUES ${valueClauses.join(', ')}
|
|
45
|
-
RETURNING _id
|
|
42
|
+
const query = `
|
|
43
|
+
INSERT INTO "${pluralResourceName}" (${tableColumns.rows.map(column => `"${column.column_name}"`).join(', ')})
|
|
44
|
+
VALUES ${valueClauses.join(', ')}
|
|
45
|
+
RETURNING _id
|
|
46
46
|
`;
|
|
47
47
|
const result = await client.query(query, allValues);
|
|
48
48
|
if (result.rows.length !== entitiesWithIds.length) {
|
|
@@ -6,10 +6,10 @@ export async function create(client, pluralResourceName, entity) {
|
|
|
6
6
|
entity._id = entity._id ?? randomUUID().toString();
|
|
7
7
|
const { columns, values } = columnsAndValuesFromEntity(entity);
|
|
8
8
|
const placeholders = columns.map((_, index) => `$${index + 1}`).join(', ');
|
|
9
|
-
const query = `
|
|
10
|
-
INSERT INTO "${pluralResourceName}" (${columns.join(', ')})
|
|
11
|
-
VALUES (${placeholders})
|
|
12
|
-
RETURNING _id
|
|
9
|
+
const query = `
|
|
10
|
+
INSERT INTO "${pluralResourceName}" (${columns.join(', ')})
|
|
11
|
+
VALUES (${placeholders})
|
|
12
|
+
RETURNING _id
|
|
13
13
|
`;
|
|
14
14
|
const result = await client.query(query, values);
|
|
15
15
|
if (result.rows.length === 0) {
|
|
@@ -2,12 +2,12 @@ import { BadRequestError, IdNotFoundError } from "../../../errors/index.js";
|
|
|
2
2
|
import { buildJoinClauses } from '../utils/build-join-clauses.js';
|
|
3
3
|
export async function fullUpdateById(client, operations, id, entity, pluralResourceName) {
|
|
4
4
|
try {
|
|
5
|
-
const tableColumns = await client.query(`
|
|
6
|
-
SELECT column_name, column_default
|
|
7
|
-
FROM information_schema.columns
|
|
8
|
-
WHERE table_schema = current_schema()
|
|
9
|
-
AND table_name = $1
|
|
10
|
-
ORDER BY ordinal_position
|
|
5
|
+
const tableColumns = await client.query(`
|
|
6
|
+
SELECT column_name, column_default
|
|
7
|
+
FROM information_schema.columns
|
|
8
|
+
WHERE table_schema = current_schema()
|
|
9
|
+
AND table_name = $1
|
|
10
|
+
ORDER BY ordinal_position
|
|
11
11
|
`, [pluralResourceName]);
|
|
12
12
|
if (tableColumns.rows.length === 0) {
|
|
13
13
|
throw new BadRequestError(`Unable to resolve columns for ${pluralResourceName}`);
|
|
@@ -40,19 +40,19 @@ export async function fullUpdateById(client, operations, id, entity, pluralResou
|
|
|
40
40
|
throw new BadRequestError('Cannot perform full update with no fields to update');
|
|
41
41
|
}
|
|
42
42
|
const setClause = updateColumns.join(', ');
|
|
43
|
-
const query = `
|
|
44
|
-
UPDATE "${pluralResourceName}"
|
|
45
|
-
SET ${setClause}
|
|
46
|
-
WHERE "_id" = $${paramIndex}
|
|
43
|
+
const query = `
|
|
44
|
+
UPDATE "${pluralResourceName}"
|
|
45
|
+
SET ${setClause}
|
|
46
|
+
WHERE "_id" = $${paramIndex}
|
|
47
47
|
`;
|
|
48
48
|
const result = await client.query(query, [...updateValues, id]);
|
|
49
49
|
if (result.rowCount === 0) {
|
|
50
50
|
throw new IdNotFoundError();
|
|
51
51
|
}
|
|
52
52
|
const joinClauses = buildJoinClauses(operations);
|
|
53
|
-
const selectQuery = `
|
|
54
|
-
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
55
|
-
WHERE "_id" = $1 LIMIT 1
|
|
53
|
+
const selectQuery = `
|
|
54
|
+
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
55
|
+
WHERE "_id" = $1 LIMIT 1
|
|
56
56
|
`;
|
|
57
57
|
const selectResult = await client.query(selectQuery, [id]);
|
|
58
58
|
if (selectResult.rows.length === 0) {
|
|
@@ -10,19 +10,19 @@ export async function partialUpdateById(client, operations, id, entity, pluralRe
|
|
|
10
10
|
throw new BadRequestError('Cannot perform partial update with no fields to update');
|
|
11
11
|
}
|
|
12
12
|
const setClause = updateColumns.map((col, index) => `${col} = $${index + 1}`).join(', ');
|
|
13
|
-
const query = `
|
|
14
|
-
UPDATE "${pluralResourceName}"
|
|
15
|
-
SET ${setClause}
|
|
16
|
-
WHERE "_id" = $${updateValues.length + 1}
|
|
13
|
+
const query = `
|
|
14
|
+
UPDATE "${pluralResourceName}"
|
|
15
|
+
SET ${setClause}
|
|
16
|
+
WHERE "_id" = $${updateValues.length + 1}
|
|
17
17
|
`;
|
|
18
18
|
const result = await client.query(query, [...updateValues, id]);
|
|
19
19
|
if (result.rowCount === 0) {
|
|
20
20
|
throw new IdNotFoundError();
|
|
21
21
|
}
|
|
22
22
|
const joinClauses = buildJoinClauses(operations);
|
|
23
|
-
const selectQuery = `
|
|
24
|
-
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
25
|
-
WHERE "_id" = $1 LIMIT 1
|
|
23
|
+
const selectQuery = `
|
|
24
|
+
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
25
|
+
WHERE "_id" = $1 LIMIT 1
|
|
26
26
|
`;
|
|
27
27
|
const selectResult = await client.query(selectQuery, [id]);
|
|
28
28
|
if (selectResult.rows.length === 0) {
|
|
@@ -17,10 +17,10 @@ export async function update(client, queryObject, entity, operations, pluralReso
|
|
|
17
17
|
const originalIndex = parseInt(num, 10);
|
|
18
18
|
return `$${originalIndex + updateValues.length}`;
|
|
19
19
|
});
|
|
20
|
-
const updateQuery = `
|
|
21
|
-
UPDATE "${pluralResourceName}"
|
|
22
|
-
SET ${setClause}
|
|
23
|
-
${whereClauseWithAdjustedParams}
|
|
20
|
+
const updateQuery = `
|
|
21
|
+
UPDATE "${pluralResourceName}"
|
|
22
|
+
SET ${setClause}
|
|
23
|
+
${whereClauseWithAdjustedParams}
|
|
24
24
|
`;
|
|
25
25
|
const allUpdateValues = [...updateValues, ...whereValues];
|
|
26
26
|
const result = await client.query(updateQuery, allUpdateValues);
|
|
@@ -29,9 +29,9 @@ export async function update(client, queryObject, entity, operations, pluralReso
|
|
|
29
29
|
}
|
|
30
30
|
const joinClauses = buildJoinClauses(operations);
|
|
31
31
|
const orderByClause = buildOrderByClause(queryObject);
|
|
32
|
-
const selectQuery = `
|
|
33
|
-
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
34
|
-
${whereClause} ${orderByClause}
|
|
32
|
+
const selectQuery = `
|
|
33
|
+
SELECT * FROM "${pluralResourceName}" ${joinClauses}
|
|
34
|
+
${whereClause} ${orderByClause}
|
|
35
35
|
`.trim();
|
|
36
36
|
const selectResult = await client.query(selectQuery, whereValues);
|
|
37
37
|
return selectResult.rows;
|
|
@@ -12,18 +12,18 @@ export class CreateMigrationTableMigration {
|
|
|
12
12
|
await this.client.query('BEGIN');
|
|
13
13
|
const tableExists = await doesTableExist(this.client, 'migrations');
|
|
14
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
|
-
)
|
|
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
22
|
`);
|
|
23
23
|
}
|
|
24
|
-
const result = await this.client.query(`
|
|
25
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
26
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
24
|
+
const result = await this.client.query(`
|
|
25
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
26
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
27
27
|
`);
|
|
28
28
|
if (result.rowCount === 0) {
|
|
29
29
|
await this.client.query('ROLLBACK');
|
|
@@ -39,8 +39,8 @@ export class CreateMigrationTableMigration {
|
|
|
39
39
|
}
|
|
40
40
|
async revert() {
|
|
41
41
|
try {
|
|
42
|
-
await this.client.query(`
|
|
43
|
-
DROP TABLE "migrations";
|
|
42
|
+
await this.client.query(`
|
|
43
|
+
DROP TABLE "migrations";
|
|
44
44
|
`);
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
@@ -12,27 +12,27 @@ export class CreateOrganizationsTableMigration {
|
|
|
12
12
|
await this.client.query('BEGIN');
|
|
13
13
|
const tableExists = await doesTableExist(this.client, 'organizations');
|
|
14
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
|
-
)
|
|
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
31
|
`);
|
|
32
32
|
}
|
|
33
|
-
const result = await this.client.query(`
|
|
34
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
35
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
33
|
+
const result = await this.client.query(`
|
|
34
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
35
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
36
36
|
`);
|
|
37
37
|
if (result.rowCount === 0) {
|
|
38
38
|
await this.client.query('ROLLBACK');
|
|
@@ -51,17 +51,17 @@ export class CreateOrganizationsTableMigration {
|
|
|
51
51
|
await this.client.query('BEGIN');
|
|
52
52
|
const tableExists = await doesTableExist(this.client, 'organizations');
|
|
53
53
|
if (tableExists) {
|
|
54
|
-
await this.client.query(`
|
|
55
|
-
DROP TABLE "organizations";
|
|
54
|
+
await this.client.query(`
|
|
55
|
+
DROP TABLE "organizations";
|
|
56
56
|
`);
|
|
57
57
|
}
|
|
58
|
-
const updateResult = await this.client.query(`
|
|
59
|
-
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
58
|
+
const updateResult = await this.client.query(`
|
|
59
|
+
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
60
60
|
`);
|
|
61
61
|
if (updateResult.rowCount === 0) {
|
|
62
62
|
await this.client.query('ROLLBACK');
|
|
63
63
|
return {
|
|
64
|
-
success: false, error: new Error(`Error updating migration record for index ${this.index}: Migration record not found.
|
|
64
|
+
success: false, error: new Error(`Error updating migration record for index ${this.index}: Migration record not found.
|
|
65
65
|
Migration index: ${this.index}`)
|
|
66
66
|
};
|
|
67
67
|
}
|
|
@@ -16,30 +16,30 @@ export class CreateUsersTableMigration {
|
|
|
16
16
|
const fkConstraint = config.app.isMultiTenant
|
|
17
17
|
? ',\n CONSTRAINT "fk_users_organization" FOREIGN KEY ("_orgId") REFERENCES "organizations"("_id") ON DELETE CASCADE'
|
|
18
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
|
-
)
|
|
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
38
|
`);
|
|
39
39
|
}
|
|
40
|
-
const result = await this.client.query(`
|
|
41
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
42
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
40
|
+
const result = await this.client.query(`
|
|
41
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
42
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
43
43
|
`);
|
|
44
44
|
if (result.rowCount === 0) {
|
|
45
45
|
await this.client.query('ROLLBACK');
|
|
@@ -56,11 +56,11 @@ export class CreateUsersTableMigration {
|
|
|
56
56
|
async revert() {
|
|
57
57
|
try {
|
|
58
58
|
await this.client.query('BEGIN');
|
|
59
|
-
await this.client.query(`
|
|
60
|
-
DROP TABLE "users";
|
|
59
|
+
await this.client.query(`
|
|
60
|
+
DROP TABLE "users";
|
|
61
61
|
`);
|
|
62
|
-
const updateResult = await this.client.query(`
|
|
63
|
-
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
62
|
+
const updateResult = await this.client.query(`
|
|
63
|
+
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
64
64
|
`);
|
|
65
65
|
if (updateResult.rowCount === 0) {
|
|
66
66
|
await this.client.query('ROLLBACK');
|
|
@@ -16,22 +16,22 @@ export class CreateRefreshTokenTableMigration {
|
|
|
16
16
|
const fkConstraint = config.app.isMultiTenant
|
|
17
17
|
? ',\n CONSTRAINT "fk_refreshTokens_organization" FOREIGN KEY ("_orgId") REFERENCES "organizations"("_id") ON DELETE CASCADE'
|
|
18
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
|
-
)
|
|
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
30
|
`);
|
|
31
31
|
}
|
|
32
|
-
const result = await this.client.query(`
|
|
33
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
34
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
32
|
+
const result = await this.client.query(`
|
|
33
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
34
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
35
35
|
`);
|
|
36
36
|
if (result.rowCount === 0) {
|
|
37
37
|
await this.client.query('ROLLBACK');
|
|
@@ -50,12 +50,12 @@ export class CreateRefreshTokenTableMigration {
|
|
|
50
50
|
await this.client.query('BEGIN');
|
|
51
51
|
const tableExists = await doesTableExist(this.client, 'refreshTokens');
|
|
52
52
|
if (tableExists) {
|
|
53
|
-
await this.client.query(`
|
|
54
|
-
DROP TABLE "refreshTokens";
|
|
53
|
+
await this.client.query(`
|
|
54
|
+
DROP TABLE "refreshTokens";
|
|
55
55
|
`);
|
|
56
56
|
}
|
|
57
|
-
const updateResult = await this.client.query(`
|
|
58
|
-
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
57
|
+
const updateResult = await this.client.query(`
|
|
58
|
+
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
59
59
|
`);
|
|
60
60
|
if (updateResult.rowCount === 0) {
|
|
61
61
|
await this.client.query('ROLLBACK');
|