pangea-server 3.2.4 → 3.3.0

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.
@@ -1,2 +1,3 @@
1
- export declare function ColJson(): (target: any, propertyName: string) => void;
1
+ import type { ColGeneralOptions } from '../../../database/database.types';
2
+ export declare function ColJson(options?: ColGeneralOptions): (target: any, propertyName: string) => void;
2
3
  export declare function ColStrArray(): (target: any, propertyName: string) => void;
@@ -26,26 +26,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.ColStrArray = exports.ColJson = void 0;
27
27
  const seq = __importStar(require("sequelize-typescript"));
28
28
  const column_1 = require("./column");
29
- function ColJson() {
30
- return (0, column_1.Column)('JSON', { defaultValue: {} });
29
+ function ColJson(options) {
30
+ return function (target, propertyName) {
31
+ (0, column_1.Column)(seq.DataType.TEXT('long'), {
32
+ ...options,
33
+ defaultValue: options?.defaultValue !== undefined ? JSON.stringify(options.defaultValue) : undefined,
34
+ get() {
35
+ const val = this.getDataValue(propertyName);
36
+ return val ? JSON.parse(val) : null;
37
+ },
38
+ set(val) {
39
+ this.setDataValue(propertyName, val ? JSON.stringify(val) : null);
40
+ },
41
+ })(target, propertyName);
42
+ };
31
43
  }
32
44
  exports.ColJson = ColJson;
33
45
  function ColStrArray() {
34
46
  return function (target, propertyName) {
35
47
  (0, column_1.Column)(seq.DataType.TEXT('long'), {
36
- nullable: true,
37
- defaultValue: null,
48
+ defaultValue: '',
38
49
  get() {
39
- console.log('get: ', propertyName);
40
50
  const val = this.getDataValue(propertyName);
41
- console.log('val: ', val);
42
- console.log('type: ', typeof val);
43
- return typeof val === 'string' && val.length > 0 ? val.split(',') : [];
51
+ return val.length ? val.split(',') : [];
44
52
  },
45
53
  set(val) {
46
- console.log('set: ', propertyName);
47
- console.log('val: ', val);
48
- console.log('type: ', typeof val);
49
54
  this.setDataValue(propertyName, val.join(','));
50
55
  },
51
56
  })(target, propertyName);
@@ -1,7 +1,15 @@
1
1
  import { Db } from '../database';
2
- type RunFn = (db: Db) => Promise<void>;
2
+ type TaskFn = (db: Db) => Promise<void>;
3
+ type Timezone = 'UTC' | 'America/New_York' | 'America/Los_Angeles' | 'America/Argentina/Buenos_Aires' | 'America/Sao_Paulo' | 'Europe/London' | 'Europe/Madrid' | 'Africa/Johannesburg' | 'Asia/Tokyo' | 'Australia/Sydney';
4
+ type ScheduleConfig = {
5
+ expression: string;
6
+ taskFn: TaskFn;
7
+ timezone?: Timezone;
8
+ runOnInit?: boolean;
9
+ };
3
10
  export declare abstract class Job {
4
- static Run(runFn: RunFn): () => Promise<void>;
11
+ static Schedule(config: ScheduleConfig): void;
12
+ private static __Run;
5
13
  }
6
14
  export interface JobCtor {
7
15
  Init(): void;
@@ -1,21 +1,27 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Job = void 0;
7
+ const node_cron_1 = __importDefault(require("node-cron"));
4
8
  const database_1 = require("../database");
5
9
  class Job {
6
- static Run(runFn) {
7
- return async () => {
8
- const tx = await (0, database_1.getDbClient)().transaction();
9
- const db = new database_1.Db(tx);
10
- try {
11
- await runFn(db);
12
- await tx.commit();
13
- }
14
- catch (err) {
15
- await tx.rollback();
16
- throw err;
17
- }
18
- };
10
+ static Schedule(config) {
11
+ const { expression, taskFn, timezone = 'UTC', runOnInit = false } = config;
12
+ node_cron_1.default.schedule(expression, () => this.__Run(taskFn), { timezone, runOnInit });
13
+ }
14
+ static async __Run(taskFn) {
15
+ const tx = await (0, database_1.getDbClient)().transaction();
16
+ const db = new database_1.Db(tx);
17
+ try {
18
+ await taskFn(db);
19
+ await tx.commit();
20
+ }
21
+ catch (err) {
22
+ await tx.rollback();
23
+ throw err;
24
+ }
19
25
  }
20
26
  }
21
27
  exports.Job = Job;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "3.2.4",
4
+ "version": "3.3.0",
5
5
  "files": [
6
6
  "dist"
7
7
  ],
@@ -49,6 +49,7 @@
49
49
  "morgan": "1.10.0",
50
50
  "multer": "1.4.5-lts.2",
51
51
  "mysql2": "3.14.0",
52
+ "node-cron": "3.0.3",
52
53
  "nodemailer": "6.10.0",
53
54
  "pangea-helpers": "1.3.85",
54
55
  "reflect-metadata": "0.2.2",
@@ -71,6 +72,7 @@
71
72
  "@types/morgan": "1.9.9",
72
73
  "@types/multer": "1.4.12",
73
74
  "@types/node": "20.2.5",
75
+ "@types/node-cron": "3.0.11",
74
76
  "@types/nodemailer": "6.4.17",
75
77
  "tsc-alias": "1.8.15",
76
78
  "typescript": "5.2.2",