@loomcore/api 0.1.75 → 0.1.77
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__/postgres-test-migrations/postgres-test-schema.js +290 -239
- package/dist/__tests__/postgres.test-database.js +8 -8
- package/dist/databases/migrations/migration-runner.js +21 -21
- package/dist/databases/operations/__tests__/models/client-report.model.d.ts +25 -0
- package/dist/databases/operations/__tests__/models/client-report.model.js +3 -1
- package/dist/databases/operations/__tests__/models/policy.model.d.ts +32 -0
- package/dist/databases/operations/__tests__/models/policy.model.js +10 -0
- 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/postgres-initial-schema.js +197 -197
- package/dist/databases/postgres/postgres.database.js +17 -17
- package/dist/databases/postgres/utils/build-join-clauses.js +277 -24
- package/dist/databases/postgres/utils/build-select-clause.js +38 -7
- package/dist/databases/postgres/utils/does-table-exist.util.js +4 -4
- package/dist/databases/postgres/utils/transform-join-results.js +102 -6
- package/package.json +92 -92
|
@@ -78,9 +78,9 @@ export class TestPostgresDatabase {
|
|
|
78
78
|
}
|
|
79
79
|
async createIndexes(client) {
|
|
80
80
|
try {
|
|
81
|
-
await client.query(`
|
|
82
|
-
CREATE INDEX IF NOT EXISTS email_index ON users (LOWER(email));
|
|
83
|
-
CREATE UNIQUE INDEX IF NOT EXISTS email_unique_index ON users (LOWER(email));
|
|
81
|
+
await client.query(`
|
|
82
|
+
CREATE INDEX IF NOT EXISTS email_index ON users (LOWER(email));
|
|
83
|
+
CREATE UNIQUE INDEX IF NOT EXISTS email_unique_index ON users (LOWER(email));
|
|
84
84
|
`);
|
|
85
85
|
}
|
|
86
86
|
catch (error) {
|
|
@@ -91,11 +91,11 @@ export class TestPostgresDatabase {
|
|
|
91
91
|
if (!this.postgresClient) {
|
|
92
92
|
throw new Error('Database not initialized');
|
|
93
93
|
}
|
|
94
|
-
const result = await this.postgresClient.query(`
|
|
95
|
-
SELECT "table_name"
|
|
96
|
-
FROM information_schema.tables
|
|
97
|
-
WHERE "table_schema" = 'public'
|
|
98
|
-
AND "table_type" = 'BASE TABLE'
|
|
94
|
+
const result = await this.postgresClient.query(`
|
|
95
|
+
SELECT "table_name"
|
|
96
|
+
FROM information_schema.tables
|
|
97
|
+
WHERE "table_schema" = 'public'
|
|
98
|
+
AND "table_type" = 'BASE TABLE'
|
|
99
99
|
`);
|
|
100
100
|
result.rows.forEach(async (row) => {
|
|
101
101
|
await this.postgresClient?.query(`TRUNCATE TABLE "${row.table_name}" RESTART IDENTITY CASCADE`);
|
|
@@ -251,31 +251,31 @@ export class MigrationRunner {
|
|
|
251
251
|
let content = '';
|
|
252
252
|
if (this.dbType === 'postgres') {
|
|
253
253
|
extension = 'sql';
|
|
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...
|
|
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...
|
|
263
263
|
`;
|
|
264
264
|
}
|
|
265
265
|
else {
|
|
266
266
|
extension = 'ts';
|
|
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
|
-
};
|
|
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
|
+
};
|
|
279
279
|
`;
|
|
280
280
|
}
|
|
281
281
|
const fullPath = path.join(this.migrationsDir, `${filename}.${extension}`);
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { IPersonModel } from "./person.model.js";
|
|
2
2
|
import { IAgentModel } from "./agent.model.js";
|
|
3
|
+
import { IPolicyModel } from "./policy.model.js";
|
|
3
4
|
import type { IAuditable, IEntity } from "@loomcore/common/models";
|
|
4
5
|
export interface IClientReportsModel extends IEntity, IAuditable {
|
|
5
6
|
client_person: IPersonModel;
|
|
6
7
|
agent?: IAgentModel;
|
|
8
|
+
policies?: IPolicyModel[];
|
|
7
9
|
}
|
|
8
10
|
export declare const clientReportsSchema: import("@sinclair/typebox").TObject<{
|
|
9
11
|
client_person: import("@sinclair/typebox").TObject<{
|
|
@@ -39,5 +41,28 @@ export declare const clientReportsSchema: import("@sinclair/typebox").TObject<{
|
|
|
39
41
|
}>>;
|
|
40
42
|
}>>;
|
|
41
43
|
}>>;
|
|
44
|
+
policies: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
45
|
+
client_id: import("@sinclair/typebox").TNumber;
|
|
46
|
+
amount: import("@sinclair/typebox").TNumber;
|
|
47
|
+
frequency: import("@sinclair/typebox").TString;
|
|
48
|
+
agents: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
49
|
+
person_id: import("@sinclair/typebox").TNumber;
|
|
50
|
+
agent_person: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
|
|
51
|
+
first_name: import("@sinclair/typebox").TString;
|
|
52
|
+
middle_name: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
53
|
+
last_name: import("@sinclair/typebox").TString;
|
|
54
|
+
phone_numbers: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
55
|
+
phone_number: import("@sinclair/typebox").TString;
|
|
56
|
+
phone_number_type: import("@sinclair/typebox").TString;
|
|
57
|
+
is_default: import("@sinclair/typebox").TBoolean;
|
|
58
|
+
}>>;
|
|
59
|
+
email_addresses: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
60
|
+
person_id: import("@sinclair/typebox").TNumber;
|
|
61
|
+
email_address: import("@sinclair/typebox").TString;
|
|
62
|
+
is_default: import("@sinclair/typebox").TBoolean;
|
|
63
|
+
}>>;
|
|
64
|
+
}>>;
|
|
65
|
+
}>>>;
|
|
66
|
+
}>>>;
|
|
42
67
|
}>;
|
|
43
68
|
export declare const clientReportsModelSpec: import("@loomcore/common/models").IModelSpec<import("@sinclair/typebox").TSchema>;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { personSchema } from "./person.model.js";
|
|
2
2
|
import { agentSchema } from "./agent.model.js";
|
|
3
|
+
import { policySchema } from "./policy.model.js";
|
|
3
4
|
import { entityUtils } from "@loomcore/common/utils";
|
|
4
5
|
import { Type } from "@sinclair/typebox";
|
|
5
6
|
export const clientReportsSchema = Type.Object({
|
|
6
7
|
client_person: personSchema,
|
|
7
|
-
agent: Type.Optional(agentSchema)
|
|
8
|
+
agent: Type.Optional(agentSchema),
|
|
9
|
+
policies: Type.Optional(Type.Array(policySchema))
|
|
8
10
|
});
|
|
9
11
|
export const clientReportsModelSpec = entityUtils.getModelSpec(clientReportsSchema, { isAuditable: true });
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { IAuditable, IEntity } from "@loomcore/common/models";
|
|
2
|
+
import { IAgentModel } from "./agent.model.js";
|
|
3
|
+
export interface IPolicyModel extends IEntity, IAuditable {
|
|
4
|
+
client_id: number;
|
|
5
|
+
amount: number;
|
|
6
|
+
frequency: string;
|
|
7
|
+
agents?: IAgentModel[];
|
|
8
|
+
}
|
|
9
|
+
export declare const policySchema: import("@sinclair/typebox").TObject<{
|
|
10
|
+
client_id: import("@sinclair/typebox").TNumber;
|
|
11
|
+
amount: import("@sinclair/typebox").TNumber;
|
|
12
|
+
frequency: import("@sinclair/typebox").TString;
|
|
13
|
+
agents: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
14
|
+
person_id: import("@sinclair/typebox").TNumber;
|
|
15
|
+
agent_person: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TObject<{
|
|
16
|
+
first_name: import("@sinclair/typebox").TString;
|
|
17
|
+
middle_name: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
18
|
+
last_name: import("@sinclair/typebox").TString;
|
|
19
|
+
phone_numbers: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
20
|
+
phone_number: import("@sinclair/typebox").TString;
|
|
21
|
+
phone_number_type: import("@sinclair/typebox").TString;
|
|
22
|
+
is_default: import("@sinclair/typebox").TBoolean;
|
|
23
|
+
}>>;
|
|
24
|
+
email_addresses: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
25
|
+
person_id: import("@sinclair/typebox").TNumber;
|
|
26
|
+
email_address: import("@sinclair/typebox").TString;
|
|
27
|
+
is_default: import("@sinclair/typebox").TBoolean;
|
|
28
|
+
}>>;
|
|
29
|
+
}>>;
|
|
30
|
+
}>>>;
|
|
31
|
+
}>;
|
|
32
|
+
export declare const policyModelSpec: import("@loomcore/common/models").IModelSpec<import("@sinclair/typebox").TSchema>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { entityUtils } from "@loomcore/common/utils";
|
|
2
|
+
import { Type } from "@sinclair/typebox";
|
|
3
|
+
import { agentSchema } from "./agent.model.js";
|
|
4
|
+
export const policySchema = Type.Object({
|
|
5
|
+
client_id: Type.Number(),
|
|
6
|
+
amount: Type.Number(),
|
|
7
|
+
frequency: Type.String(),
|
|
8
|
+
agents: Type.Optional(Type.Array(agentSchema))
|
|
9
|
+
});
|
|
10
|
+
export const policyModelSpec = entityUtils.getModelSpec(policySchema, { isAuditable: true });
|
|
@@ -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;
|