@loomcore/api 0.1.107 → 0.1.110

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.
@@ -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`);
@@ -7,6 +7,10 @@ export declare class OrganizationsController extends ApiController<IOrganization
7
7
  orgService: OrganizationService;
8
8
  constructor(app: Application, database: IDatabase);
9
9
  mapRoutes(app: Application): void;
10
- getByName(req: Request, res: Response, next: NextFunction): Promise<void>;
11
- getByCode(req: Request, res: Response, next: NextFunction): Promise<void>;
10
+ getByName(req: Request<{
11
+ name: string;
12
+ }>, res: Response, next: NextFunction): Promise<void>;
13
+ getByCode(req: Request<{
14
+ code: string;
15
+ }>, res: Response, next: NextFunction): Promise<void>;
12
16
  }
@@ -17,7 +17,7 @@ export class OrganizationsController extends ApiController {
17
17
  }
18
18
  async getByName(req, res, next) {
19
19
  console.log('in OrganizationController.getByName');
20
- let name = req.params?.name;
20
+ const { name } = req.params;
21
21
  try {
22
22
  res.set('Content-Type', 'application/json');
23
23
  const entity = await this.orgService.findOne(req.userContext, { filters: { name: { contains: name } } });
@@ -31,7 +31,7 @@ export class OrganizationsController extends ApiController {
31
31
  }
32
32
  }
33
33
  async getByCode(req, res, next) {
34
- let code = req.params?.code;
34
+ const { code } = req.params;
35
35
  try {
36
36
  res.set('Content-Type', 'application/json');
37
37
  const entity = await this.orgService.findOne(req.userContext, { filters: { code: { eq: code } } });
@@ -259,31 +259,31 @@ export class MigrationRunner {
259
259
  let content = '';
260
260
  if (this.dbType === 'postgres') {
261
261
  extension = 'sql';
262
- content = `-- Migration: ${safeName}
263
- -- Created: ${new Date().toISOString()}
264
-
265
- -- up
266
- -- Write your CREATE/ALTER statements here...
267
-
268
-
269
- -- down
270
- -- Write your DROP/UNDO statements here...
262
+ content = `-- Migration: ${safeName}
263
+ -- Created: ${new Date().toISOString()}
264
+
265
+ -- up
266
+ -- Write your CREATE/ALTER statements here...
267
+
268
+
269
+ -- down
270
+ -- Write your DROP/UNDO statements here...
271
271
  `;
272
272
  }
273
273
  else {
274
274
  extension = 'ts';
275
- content = `import { Db } from 'mongodb';
276
-
277
- // Migration: ${safeName}
278
- // Created: ${new Date().toISOString()}
279
-
280
- export const up = async ({ context: db }: { context: Db }) => {
281
- // await db.collection('...')....
282
- };
283
-
284
- export const down = async ({ context: db }: { context: Db }) => {
285
- // await db.collection('...')....
286
- };
275
+ content = `import { Db } from 'mongodb';
276
+
277
+ // Migration: ${safeName}
278
+ // Created: ${new Date().toISOString()}
279
+
280
+ export const up = async ({ context: db }: { context: Db }) => {
281
+ // await db.collection('...')....
282
+ };
283
+
284
+ export const down = async ({ context: db }: { context: Db }) => {
285
+ // await db.collection('...')....
286
+ };
287
287
  `;
288
288
  }
289
289
  const fullPath = path.join(this.migrationsDir, `${filename}.${extension}`);
@@ -30,10 +30,10 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
30
30
  const setClause = columns.map((col, index) => `${col} = $${index + 1}`).join(', ');
31
31
  queryObject.filters._id = { eq: _id };
32
32
  const { whereClause } = buildWhereClause(queryObject, values);
33
- const query = `
34
- UPDATE "${pluralResourceName}"
35
- SET ${setClause}
36
- ${whereClause}
33
+ const query = `
34
+ UPDATE "${pluralResourceName}"
35
+ SET ${setClause}
36
+ ${whereClause}
37
37
  `;
38
38
  await client.query(query, values);
39
39
  }
@@ -46,9 +46,9 @@ export async function batchUpdate(client, entities, operations, queryObject, plu
46
46
  const selectClause = hasJoins
47
47
  ? await buildSelectClause(client, pluralResourceName, pluralResourceName, operations)
48
48
  : '*';
49
- const selectQuery = `
50
- SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
51
- ${whereClause}
49
+ const selectQuery = `
50
+ SELECT ${selectClause} FROM "${pluralResourceName}" ${joinClauses}
51
+ ${whereClause}
52
52
  `;
53
53
  const result = await client.query(selectQuery, values);
54
54
  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, pluralResourceName);
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, pluralResourceName);
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, pluralResourceName);
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;