pangea-server 3.3.159 → 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.
@@ -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.159",
4
+ "version": "3.3.160",
5
5
  "engines": {
6
6
  "node": "22.14.0"
7
7
  },