@loomcore/api 0.1.101 → 0.1.102

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`);
@@ -12,7 +12,7 @@ export declare class MigrationRunner {
12
12
  private getMigrator;
13
13
  private loadFileMigrations;
14
14
  private wipeDatabase;
15
- run(command?: 'up' | 'down' | 'reset', target?: string): Promise<void>;
15
+ run(command?: 'up' | 'down' | 'reset' | 'create', target?: string): Promise<void>;
16
16
  private closeConnection;
17
17
  create(name: string): Promise<void>;
18
18
  }
@@ -183,6 +183,13 @@ export class MigrationRunner {
183
183
  }
184
184
  async run(command = 'up', target) {
185
185
  try {
186
+ if (command === 'create') {
187
+ if (!target) {
188
+ throw new Error('Migration name is required for create. Example: npm run migrate create add-users-table');
189
+ }
190
+ await this.create(target);
191
+ return;
192
+ }
186
193
  if (command === 'reset') {
187
194
  if (!this.dbMigrationConfig) {
188
195
  throw new Error('Reset configuration not found');
@@ -252,31 +259,31 @@ export class MigrationRunner {
252
259
  let content = '';
253
260
  if (this.dbType === 'postgres') {
254
261
  extension = 'sql';
255
- content = `-- Migration: ${safeName}
256
- -- Created: ${new Date().toISOString()}
257
-
258
- -- up
259
- -- Write your CREATE/ALTER statements here...
260
-
261
-
262
- -- down
263
- -- 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...
264
271
  `;
265
272
  }
266
273
  else {
267
274
  extension = 'ts';
268
- content = `import { Db } from 'mongodb';
269
-
270
- // Migration: ${safeName}
271
- // Created: ${new Date().toISOString()}
272
-
273
- export const up = async ({ context: db }: { context: Db }) => {
274
- // await db.collection('...')....
275
- };
276
-
277
- export const down = async ({ context: db }: { context: Db }) => {
278
- // await db.collection('...')....
279
- };
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
+ };
280
287
  `;
281
288
  }
282
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;