@loomcore/api 0.1.73 → 0.1.75
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 +77 -77
- package/dist/__tests__/common-test.utils.js +4 -1
- package/dist/__tests__/postgres-test-migrations/postgres-test-schema.js +239 -239
- package/dist/__tests__/postgres.test-database.js +8 -8
- package/dist/controllers/auth.controller.d.ts +1 -2
- package/dist/controllers/auth.controller.js +2 -2
- package/dist/databases/migrations/migration-runner.d.ts +1 -3
- package/dist/databases/migrations/migration-runner.js +24 -26
- package/dist/databases/mongo-db/migrations/mongo-initial-schema.d.ts +1 -2
- package/dist/databases/mongo-db/migrations/mongo-initial-schema.js +4 -4
- package/dist/databases/postgres/commands/postgres-batch-update.command.js +7 -7
- package/dist/databases/postgres/commands/postgres-create-many.command.js +4 -4
- 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/__tests__/test-migration-helper.js +1 -2
- package/dist/databases/postgres/migrations/postgres-initial-schema.d.ts +1 -2
- package/dist/databases/postgres/migrations/postgres-initial-schema.js +201 -201
- package/dist/databases/postgres/postgres.database.js +17 -17
- package/dist/databases/postgres/utils/build-join-clauses.js +22 -22
- package/dist/databases/postgres/utils/build-select-clause.js +6 -6
- package/dist/databases/postgres/utils/does-table-exist.util.js +4 -4
- package/dist/models/base-api-config.interface.d.ts +4 -0
- package/dist/services/auth.service.d.ts +1 -2
- package/dist/services/auth.service.js +2 -2
- package/dist/services/email.service.d.ts +1 -2
- package/dist/services/email.service.js +7 -2
- package/dist/services/generic-query-service/generic-query-service.interface.d.ts +1 -1
- package/dist/services/generic-query-service/generic-query.service.d.ts +2 -1
- package/dist/services/generic-query-service/generic-query.service.js +8 -4
- package/package.json +92 -92
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { IBaseApiConfig } from '../../models/base-api-config.interface.js';
|
|
2
|
-
import { IEmailClient } from '../../models/email-client.interface.js';
|
|
3
2
|
export declare class MigrationRunner {
|
|
4
3
|
private config;
|
|
5
4
|
private dbType;
|
|
@@ -7,8 +6,7 @@ export declare class MigrationRunner {
|
|
|
7
6
|
private migrationsDir;
|
|
8
7
|
private primaryTimezone;
|
|
9
8
|
private dbConnection;
|
|
10
|
-
|
|
11
|
-
constructor(config: IBaseApiConfig, emailClient?: IEmailClient);
|
|
9
|
+
constructor(config: IBaseApiConfig);
|
|
12
10
|
private getTimestamp;
|
|
13
11
|
private parseSql;
|
|
14
12
|
private getMigrator;
|
|
@@ -15,15 +15,13 @@ export class MigrationRunner {
|
|
|
15
15
|
migrationsDir;
|
|
16
16
|
primaryTimezone;
|
|
17
17
|
dbConnection;
|
|
18
|
-
|
|
19
|
-
constructor(config, emailClient) {
|
|
18
|
+
constructor(config) {
|
|
20
19
|
setBaseApiConfig(config);
|
|
21
20
|
this.config = config;
|
|
22
21
|
this.dbType = config.app.dbType;
|
|
23
22
|
this.dbUrl = this.dbType === 'postgres' ? buildPostgresUrl(config) : buildMongoUrl(config);
|
|
24
23
|
this.migrationsDir = path.join(process.cwd(), 'database', 'migrations');
|
|
25
24
|
this.primaryTimezone = config.app.primaryTimezone || 'UTC';
|
|
26
|
-
this.emailClient = emailClient;
|
|
27
25
|
}
|
|
28
26
|
getTimestamp() {
|
|
29
27
|
const now = new Date();
|
|
@@ -66,7 +64,7 @@ export class MigrationRunner {
|
|
|
66
64
|
this.dbConnection = pool;
|
|
67
65
|
return new Umzug({
|
|
68
66
|
migrations: async () => {
|
|
69
|
-
const initialSchema = getPostgresInitialSchema(this.config
|
|
67
|
+
const initialSchema = getPostgresInitialSchema(this.config).map(m => ({
|
|
70
68
|
name: m.name,
|
|
71
69
|
up: async () => {
|
|
72
70
|
console.log(` Running [LIBRARY] ${m.name}...`);
|
|
@@ -105,7 +103,7 @@ export class MigrationRunner {
|
|
|
105
103
|
console.log(`🔎 Looking for migrations in: ${globPattern}`);
|
|
106
104
|
return new Umzug({
|
|
107
105
|
migrations: async () => {
|
|
108
|
-
const initialSchema = getMongoInitialSchema(this.config
|
|
106
|
+
const initialSchema = getMongoInitialSchema(this.config).map(m => ({
|
|
109
107
|
name: m.name,
|
|
110
108
|
up: async () => {
|
|
111
109
|
console.log(` Running [LIBRARY] ${m.name}...`);
|
|
@@ -253,31 +251,31 @@ export class MigrationRunner {
|
|
|
253
251
|
let content = '';
|
|
254
252
|
if (this.dbType === 'postgres') {
|
|
255
253
|
extension = 'sql';
|
|
256
|
-
content = `-- Migration: ${safeName}
|
|
257
|
-
-- Created: ${new Date().toISOString()}
|
|
258
|
-
|
|
259
|
-
-- up
|
|
260
|
-
-- Write your CREATE/ALTER statements here...
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
-- down
|
|
264
|
-
-- Write your DROP/UNDO statements here...
|
|
254
|
+
content = `-- Migration: ${safeName}
|
|
255
|
+
-- Created: ${new Date().toISOString()}
|
|
256
|
+
|
|
257
|
+
-- up
|
|
258
|
+
-- Write your CREATE/ALTER statements here...
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
-- down
|
|
262
|
+
-- Write your DROP/UNDO statements here...
|
|
265
263
|
`;
|
|
266
264
|
}
|
|
267
265
|
else {
|
|
268
266
|
extension = 'ts';
|
|
269
|
-
content = `import { Db } from 'mongodb';
|
|
270
|
-
|
|
271
|
-
// Migration: ${safeName}
|
|
272
|
-
// Created: ${new Date().toISOString()}
|
|
273
|
-
|
|
274
|
-
export const up = async ({ context: db }: { context: Db }) => {
|
|
275
|
-
// await db.collection('...')....
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
export const down = async ({ context: db }: { context: Db }) => {
|
|
279
|
-
// await db.collection('...')....
|
|
280
|
-
};
|
|
267
|
+
content = `import { Db } from 'mongodb';
|
|
268
|
+
|
|
269
|
+
// Migration: ${safeName}
|
|
270
|
+
// Created: ${new Date().toISOString()}
|
|
271
|
+
|
|
272
|
+
export const up = async ({ context: db }: { context: Db }) => {
|
|
273
|
+
// await db.collection('...')....
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export const down = async ({ context: db }: { context: Db }) => {
|
|
277
|
+
// await db.collection('...')....
|
|
278
|
+
};
|
|
281
279
|
`;
|
|
282
280
|
}
|
|
283
281
|
const fullPath = path.join(this.migrationsDir, `${filename}.${extension}`);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Db } from 'mongodb';
|
|
2
2
|
import { IBaseApiConfig } from '../../../models/base-api-config.interface.js';
|
|
3
|
-
import { IEmailClient } from '../../../models/email-client.interface.js';
|
|
4
3
|
export interface ISyntheticMigration {
|
|
5
4
|
name: string;
|
|
6
5
|
up: (context: {
|
|
@@ -10,4 +9,4 @@ export interface ISyntheticMigration {
|
|
|
10
9
|
context: Db;
|
|
11
10
|
}) => Promise<void>;
|
|
12
11
|
}
|
|
13
|
-
export declare const getMongoInitialSchema: (config: IBaseApiConfig
|
|
12
|
+
export declare const getMongoInitialSchema: (config: IBaseApiConfig) => ISyntheticMigration[];
|
|
@@ -2,7 +2,7 @@ import { randomUUID } from 'crypto';
|
|
|
2
2
|
import { initializeSystemUserContext, EmptyUserContext, getSystemUserContext, isSystemUserContextInitialized } from '@loomcore/common/models';
|
|
3
3
|
import { MongoDBDatabase } from '../mongo-db.database.js';
|
|
4
4
|
import { AuthService, OrganizationService } from '../../../services/index.js';
|
|
5
|
-
export const getMongoInitialSchema = (config
|
|
5
|
+
export const getMongoInitialSchema = (config) => {
|
|
6
6
|
const migrations = [];
|
|
7
7
|
const isMultiTenant = config.app.isMultiTenant;
|
|
8
8
|
if (isMultiTenant && !config.multiTenant) {
|
|
@@ -12,7 +12,7 @@ export const getMongoInitialSchema = (config, emailClient) => {
|
|
|
12
12
|
if (isAuthEnabled && !config.auth) {
|
|
13
13
|
throw new Error('Auth enabled without auth configuration');
|
|
14
14
|
}
|
|
15
|
-
if (isAuthEnabled && (!emailClient || !config.email)) {
|
|
15
|
+
if (isAuthEnabled && (!config.thirdPartyClients?.emailClient || !config.email)) {
|
|
16
16
|
throw new Error('Auth enabled without email client or email configuration');
|
|
17
17
|
}
|
|
18
18
|
if (isMultiTenant)
|
|
@@ -166,7 +166,7 @@ export const getMongoInitialSchema = (config, emailClient) => {
|
|
|
166
166
|
name: '00000000000009_data-admin-user',
|
|
167
167
|
up: async ({ context: db }) => {
|
|
168
168
|
const database = new MongoDBDatabase(db);
|
|
169
|
-
const authService = new AuthService(database
|
|
169
|
+
const authService = new AuthService(database);
|
|
170
170
|
if (!isSystemUserContextInitialized()) {
|
|
171
171
|
const errorMessage = isMultiTenant
|
|
172
172
|
? 'SystemUserContext has not been initialized. The meta-org migration (00000000000008_data-meta-org) should have run before this migration. ' +
|
|
@@ -201,7 +201,7 @@ export const getMongoInitialSchema = (config, emailClient) => {
|
|
|
201
201
|
up: async ({ context: db }) => {
|
|
202
202
|
const database = new MongoDBDatabase(db);
|
|
203
203
|
const organizationService = new OrganizationService(database);
|
|
204
|
-
const authService = new AuthService(database
|
|
204
|
+
const authService = new AuthService(database);
|
|
205
205
|
const metaOrg = await organizationService.getMetaOrg(EmptyUserContext);
|
|
206
206
|
if (!metaOrg) {
|
|
207
207
|
throw new Error('Meta organization not found. Ensure meta-org migration ran successfully.');
|
|
@@ -31,10 +31,10 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
|
|
|
31
31
|
const setClause = columns.map((col, index) => `${col} = $${index + 1}`).join(', ');
|
|
32
32
|
queryObject.filters._id = { eq: _id };
|
|
33
33
|
const { whereClause } = buildWhereClause(queryObject, values);
|
|
34
|
-
const query = `
|
|
35
|
-
UPDATE "${pluralResourceName}"
|
|
36
|
-
SET ${setClause}
|
|
37
|
-
${whereClause}
|
|
34
|
+
const query = `
|
|
35
|
+
UPDATE "${pluralResourceName}"
|
|
36
|
+
SET ${setClause}
|
|
37
|
+
${whereClause}
|
|
38
38
|
`;
|
|
39
39
|
await client.query(query, values);
|
|
40
40
|
}
|
|
@@ -47,9 +47,9 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
|
|
|
47
47
|
const selectClause = hasJoins
|
|
48
48
|
? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
|
|
49
49
|
: '*';
|
|
50
|
-
const selectQuery = `
|
|
51
|
-
SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
|
|
52
|
-
${whereClause}
|
|
50
|
+
const selectQuery = `
|
|
51
|
+
SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
|
|
52
|
+
${whereClause}
|
|
53
53
|
`;
|
|
54
54
|
const result = await client.query(selectQuery, values);
|
|
55
55
|
return hasJoins
|
|
@@ -33,10 +33,10 @@ export async function createMany(client, pluralResourceName, entities) {
|
|
|
33
33
|
valueClauses.push(`(${placeholders})`);
|
|
34
34
|
allValues.push(...values);
|
|
35
35
|
});
|
|
36
|
-
const query = `
|
|
37
|
-
INSERT INTO "${pluralResourceName}" (${columns.map(col => `"${col}"`).join(', ')})
|
|
38
|
-
VALUES ${valueClauses.join(', ')}
|
|
39
|
-
RETURNING *
|
|
36
|
+
const query = `
|
|
37
|
+
INSERT INTO "${pluralResourceName}" (${columns.map(col => `"${col}"`).join(', ')})
|
|
38
|
+
VALUES ${valueClauses.join(', ')}
|
|
39
|
+
RETURNING *
|
|
40
40
|
`;
|
|
41
41
|
const result = await client.query(query, allValues);
|
|
42
42
|
if (result.rows.length !== entities.length) {
|
|
@@ -5,10 +5,10 @@ export async function create(client, pluralResourceName, entity) {
|
|
|
5
5
|
delete entity._id;
|
|
6
6
|
const { columns, values } = columnsAndValuesFromEntity(entity);
|
|
7
7
|
const placeholders = columns.map((_, index) => `$${index + 1}`).join(', ');
|
|
8
|
-
const query = `
|
|
9
|
-
INSERT INTO "${pluralResourceName}" (${columns.join(', ')})
|
|
10
|
-
VALUES (${placeholders})
|
|
11
|
-
RETURNING *
|
|
8
|
+
const query = `
|
|
9
|
+
INSERT INTO "${pluralResourceName}" (${columns.join(', ')})
|
|
10
|
+
VALUES (${placeholders})
|
|
11
|
+
RETURNING *
|
|
12
12
|
`;
|
|
13
13
|
const result = await client.query(query, values);
|
|
14
14
|
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;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Umzug } from 'umzug';
|
|
2
2
|
import { getPostgresInitialSchema } from '../postgres-initial-schema.js';
|
|
3
3
|
import { getPostgresTestSchema } from '../../../../__tests__/postgres-test-migrations/postgres-test-schema.js';
|
|
4
|
-
import { TestEmailClient } from '../../../../__tests__/test-email-client.js';
|
|
5
4
|
export async function runInitialSchemaMigrations(pool, config) {
|
|
6
|
-
const initialSchema = getPostgresInitialSchema(config
|
|
5
|
+
const initialSchema = getPostgresInitialSchema(config);
|
|
7
6
|
const umzug = new Umzug({
|
|
8
7
|
migrations: async () => {
|
|
9
8
|
return initialSchema.map(m => ({
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Pool } from 'pg';
|
|
2
2
|
import { IBaseApiConfig } from '../../../models/base-api-config.interface.js';
|
|
3
|
-
import { IEmailClient } from '../../../models/email-client.interface.js';
|
|
4
3
|
export interface SyntheticMigration {
|
|
5
4
|
name: string;
|
|
6
5
|
up: (context: {
|
|
@@ -10,4 +9,4 @@ export interface SyntheticMigration {
|
|
|
10
9
|
context: Pool;
|
|
11
10
|
}) => Promise<void>;
|
|
12
11
|
}
|
|
13
|
-
export declare const getPostgresInitialSchema: (config: IBaseApiConfig
|
|
12
|
+
export declare const getPostgresInitialSchema: (config: IBaseApiConfig) => SyntheticMigration[];
|