pangea-server 3.3.158 → 3.3.160

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.
@@ -120,10 +120,7 @@ class Db {
120
120
  const searchWords = search.split(/\s+/).filter(Boolean);
121
121
  searchWhere = {
122
122
  [_1.Ops.and]: searchWords.map((word) => ({
123
- [_1.Ops.or]: searchFields.map((field) => {
124
- const finalField = field.includes('.') ? `$${field}$` : field;
125
- return { [finalField]: { [_1.Ops.like]: `%${word}%` } };
126
- }),
123
+ [_1.Ops.or]: searchFields.map((field) => getSearchFieldCondition(model, field, word)),
127
124
  })),
128
125
  };
129
126
  }
@@ -231,6 +228,32 @@ function formatField(modelName, field) {
231
228
  }
232
229
  return `${modelName}.${(0, pangea_helpers_1.camelToSnake)(field)}`;
233
230
  }
231
+ function getSearchCorrelation(association, parentAlias, childAlias) {
232
+ if (association.associationType === 'BelongsTo') {
233
+ return `${childAlias}.\`${association.targetKeyField || 'id'}\` = ${parentAlias}.\`${association.identifierField}\``;
234
+ }
235
+ return `${childAlias}.\`${association.identifierField}\` = ${parentAlias}.\`${association.sourceKeyField || 'id'}\``;
236
+ }
237
+ function buildSearchExists(parentModel, parentAlias, segments, escapedWord, depth) {
238
+ const [relName, ...rest] = segments;
239
+ const association = parentModel.associations[relName];
240
+ const childModel = association.target;
241
+ const childAlias = `\`__search_${relName}_${depth}\``;
242
+ const conditions = [getSearchCorrelation(association, parentAlias, childAlias)];
243
+ if (parentModel.Relations[relName]?.paranoid)
244
+ conditions.push(`${childAlias}.\`deleted_at\` IS NULL`);
245
+ if (rest.length === 1)
246
+ conditions.push(`${childAlias}.\`${(0, pangea_helpers_1.camelToSnake)(rest[0])}\` LIKE ${escapedWord}`);
247
+ else
248
+ conditions.push(buildSearchExists(childModel, childAlias, rest, escapedWord, depth + 1));
249
+ return `EXISTS (SELECT 1 FROM \`${childModel.tableName}\` AS ${childAlias} WHERE ${conditions.join(' AND ')})`;
250
+ }
251
+ function getSearchFieldCondition(model, field, word) {
252
+ if (!field.includes('.'))
253
+ return { [field]: { [_1.Ops.like]: `%${word}%` } };
254
+ const escapedWord = (0, db_client_1.getDbClient)().escape(`%${word}%`);
255
+ return (0, sequelize_1.literal)(buildSearchExists(model, `\`${model.name}\``, field.split('.'), escapedWord, 0));
256
+ }
234
257
  function getFinalAttributes(model, attributes) {
235
258
  if (!attributes)
236
259
  return;
@@ -251,7 +274,7 @@ function convertWhereKeys(obj) {
251
274
  !(obj instanceof RegExp) &&
252
275
  !(obj instanceof Map) &&
253
276
  !(obj instanceof Set) &&
254
- !(obj instanceof utils_1.Where) &&
277
+ !(obj instanceof utils_1.SequelizeMethod) &&
255
278
  !(typeof obj === 'function')) {
256
279
  const newObj = {};
257
280
  for (const key of Reflect.ownKeys(obj)) {
@@ -13,3 +13,4 @@ export * from './database.types';
13
13
  export * from './db-client';
14
14
  export * from './db.class';
15
15
  export * from './seed.helpers';
16
+ export * from './use-auto-commit';
@@ -26,3 +26,4 @@ __exportStar(require("./database.types"), exports);
26
26
  __exportStar(require("./db-client"), exports);
27
27
  __exportStar(require("./db.class"), exports);
28
28
  __exportStar(require("./seed.helpers"), exports);
29
+ __exportStar(require("./use-auto-commit"), exports);
@@ -5,7 +5,7 @@ exports.emptyTables = emptyTables;
5
5
  exports.syncTables = syncTables;
6
6
  exports.seedTables = seedTables;
7
7
  const db_client_1 = require("./db-client");
8
- const db_class_1 = require("./db.class");
8
+ const use_auto_commit_1 = require("./use-auto-commit");
9
9
  // helpers
10
10
  const helpers_1 = require("../helpers");
11
11
  async function dropTables() {
@@ -30,18 +30,14 @@ async function seedTables(models, seeds) {
30
30
  .filter(() => (0, helpers_1.getEnvBool)('DB_INCLUDE_TESTING_SEEDS'))
31
31
  .map((model) => model.name);
32
32
  const modelsWithData = modelsToSeed.filter((modelName) => seeds[modelName]?.length);
33
- const tx = await (0, db_client_1.getDbClient)().transaction();
34
- const db = new db_class_1.Db(tx);
35
- try {
36
- await db.disableForeignKeyChecks();
37
- await Promise.all(modelsWithData.map((modelName) => db.insertMany(models[modelName], seeds[modelName])));
38
- await db.enableForeignKeyChecks();
39
- await tx.commit();
40
- }
41
- catch (err) {
42
- await db.enableForeignKeyChecks();
43
- await tx.rollback();
44
- throw err;
45
- }
33
+ await (0, use_auto_commit_1.useAutoCommit)(async (db) => {
34
+ try {
35
+ await db.disableForeignKeyChecks();
36
+ await Promise.all(modelsWithData.map((modelName) => db.insertMany(models[modelName], seeds[modelName])));
37
+ }
38
+ finally {
39
+ await db.enableForeignKeyChecks();
40
+ }
41
+ });
46
42
  (0, helpers_1.printSuccess)('database', 'tables seeded');
47
43
  }
@@ -0,0 +1,2 @@
1
+ import { Db } from './db.class';
2
+ export declare function useAutoCommit<T>(cb: (db: Db) => Promise<T>): Promise<T>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useAutoCommit = useAutoCommit;
4
+ const db_client_1 = require("./db-client");
5
+ const db_class_1 = require("./db.class");
6
+ async function useAutoCommit(cb) {
7
+ const tx = await (0, db_client_1.getDbClient)().transaction();
8
+ const db = new db_class_1.Db(tx);
9
+ try {
10
+ const result = await cb(db);
11
+ await tx.commit();
12
+ return result;
13
+ }
14
+ catch (err) {
15
+ await tx.rollback();
16
+ throw err;
17
+ }
18
+ }
@@ -1,4 +1,3 @@
1
- import { Db } from '../database';
2
1
  type Now = {
3
2
  year: number;
4
3
  yearStr: string;
@@ -12,17 +12,8 @@ class Job {
12
12
  node_cron_1.default.schedule(expression, () => this.__Run(timezone, task), { timezone, runOnInit });
13
13
  }
14
14
  static async __Run(timezone, task) {
15
- const tx = await (0, database_1.getDbClient)().transaction();
16
- const db = new database_1.Db(tx);
17
15
  const now = this.__GetNow(timezone);
18
- try {
19
- await task(db, now);
20
- await tx.commit();
21
- }
22
- catch (err) {
23
- await tx.rollback();
24
- throw err;
25
- }
16
+ await (0, database_1.useAutoCommit)((db) => task(db, now));
26
17
  }
27
18
  static __GetNow(timezone) {
28
19
  const today = new Date();
@@ -20,20 +20,12 @@ function callController(controller, validate, appVersion, authConfig) {
20
20
  }
21
21
  const inputs = (0, validate_request_1.validateRequest)(validate, req);
22
22
  const ctx = { headers, ...inputs, file: req.file, files: req.files };
23
- const tx = await (0, database_1.getDbClient)().transaction();
24
- const db = new database_1.Db(tx);
25
- let result;
26
- try {
23
+ const result = await (0, database_1.useAutoCommit)(async (db) => {
27
24
  const authUsers = await (0, auth_1.getUsersFromToken)(headers.authorization, db, accessToken, authMap);
28
25
  const initRes = initAuthCtor ? await initAuthCtor(db, headers) : undefined;
29
26
  const auth = new authCtor(authUsers, initRes);
30
- result = await controller(ctx, db, auth);
31
- await tx.commit();
32
- }
33
- catch (err) {
34
- await tx.rollback();
35
- throw err;
36
- }
27
+ return controller(ctx, db, auth);
28
+ });
37
29
  let data = result;
38
30
  let totalCount;
39
31
  if (hasInstancesAndTotalCount(result)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "3.3.158",
4
+ "version": "3.3.160",
5
5
  "engines": {
6
6
  "node": "22.14.0"
7
7
  },