namirasoft-node 1.4.131 → 1.4.133

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.
package/src/BaseCron.ts CHANGED
@@ -1,115 +1,115 @@
1
- import cron from 'node-cron';
2
- import { BaseApplication } from './BaseApplication';
3
- import { Timer } from './Timer';
4
- import { IDatabase } from './IDatabase';
5
-
6
- export type BaseCronInfoType =
7
- {
8
- name: string,
9
- expression?: string,
10
- interval?: number,
11
- runOnStart: boolean,
12
- ignoreDatabase?: boolean
13
- }
14
-
15
- export abstract class BaseCron<D extends { [name: string]: IDatabase }>
16
- {
17
- private running: boolean = true;
18
- protected log: {
19
- enabled: boolean;
20
- onStarted: boolean;
21
- onFinished: boolean;
22
- } = {
23
- enabled: true,
24
- onStarted: true,
25
- onFinished: true
26
- };
27
- protected app: BaseApplication<D>;
28
- protected info!: BaseCronInfoType;
29
- protected timer: Timer = new Timer();
30
-
31
- constructor(app: BaseApplication<D>)
32
- {
33
- this.app = app;
34
- this.run = this.run.bind(this);
35
- this.loop = this.loop.bind(this);
36
- this.handler = this.handler.bind(this);
37
- }
38
-
39
- abstract getInfo(): BaseCronInfoType;
40
-
41
- run()
42
- {
43
- this.info = this.getInfo();
44
-
45
- if (this.info.expression && this.info.interval)
46
- throw new Error("Both experssion and interval should not be provided in getInfo function");
47
- else if (this.info.expression)
48
- this.app.logger.info(`Cron ${this.info.name} was schedued for ${this.info.expression}.`);
49
- else if (this.info.interval)
50
- this.app.logger.info(`Cron ${this.info.name} was schedued for every ${this.info.interval}ms.`);
51
- else
52
- throw new Error("Either experssion or interval should be provided in getInfo function");
53
-
54
- if (this.info.runOnStart)
55
- this.handler();
56
- if (this.info.expression)
57
- cron.schedule(this.info.expression, this.handler);
58
- else if (this.info.interval)
59
- this.loop();
60
- }
61
-
62
- private async loop()
63
- {
64
- while (this.running)
65
- {
66
- await this.handler()
67
- await new Promise(resolver =>
68
- {
69
- setTimeout(() =>
70
- {
71
- resolver({});
72
- }, this.info.interval);
73
- })
74
- }
75
- }
76
-
77
- private async handler()
78
- {
79
- if (this.log.enabled)
80
- if (this.log.onStarted)
81
- this.app.logger.info(`Cron ${this.info.name} was started.`);
82
- try
83
- {
84
- // preHandle
85
- if (this.running)
86
- this.timer.onStart();
87
- if (this.running)
88
- await this.preHandle();
89
- // Handler
90
- if (this.running)
91
- await this.handleOne();
92
- // postHandle
93
- if (this.running)
94
- await this.postHandle();
95
- if (this.running)
96
- this.timer.onFinish();
97
-
98
- if (this.log.enabled)
99
- if (this.log.onFinished)
100
- this.app.logger.info(`Cron ${this.info.name} was finished in ${this.timer.duration}ms.`);
101
- }
102
- catch (error: any)
103
- {
104
- this.app.logger.onCatchCritical(error);
105
- }
106
- };
107
-
108
- async preHandle(): Promise<void> { }
109
- abstract handleOne(): Promise<void>;
110
- async postHandle(): Promise<void> { }
111
- public stop()
112
- {
113
- this.running = false;
114
- }
1
+ import cron from 'node-cron';
2
+ import { BaseApplication } from './BaseApplication';
3
+ import { Timer } from './Timer';
4
+ import { IDatabase } from './IDatabase';
5
+
6
+ export type BaseCronInfoType =
7
+ {
8
+ name: string,
9
+ expression?: string,
10
+ interval?: number,
11
+ runOnStart: boolean,
12
+ ignoreDatabase?: boolean
13
+ }
14
+
15
+ export abstract class BaseCron<D extends { [name: string]: IDatabase }>
16
+ {
17
+ private running: boolean = true;
18
+ protected log: {
19
+ enabled: boolean;
20
+ onStarted: boolean;
21
+ onFinished: boolean;
22
+ } = {
23
+ enabled: true,
24
+ onStarted: true,
25
+ onFinished: true
26
+ };
27
+ protected app: BaseApplication<D>;
28
+ protected info!: BaseCronInfoType;
29
+ protected timer: Timer = new Timer();
30
+
31
+ constructor(app: BaseApplication<D>)
32
+ {
33
+ this.app = app;
34
+ this.run = this.run.bind(this);
35
+ this.loop = this.loop.bind(this);
36
+ this.handler = this.handler.bind(this);
37
+ }
38
+
39
+ abstract getInfo(): BaseCronInfoType;
40
+
41
+ run()
42
+ {
43
+ this.info = this.getInfo();
44
+
45
+ if (this.info.expression && this.info.interval)
46
+ throw new Error("Both experssion and interval should not be provided in getInfo function");
47
+ else if (this.info.expression)
48
+ this.app.logger.info(`Cron ${this.info.name} was schedued for ${this.info.expression}.`);
49
+ else if (this.info.interval)
50
+ this.app.logger.info(`Cron ${this.info.name} was schedued for every ${this.info.interval}ms.`);
51
+ else
52
+ throw new Error("Either experssion or interval should be provided in getInfo function");
53
+
54
+ if (this.info.runOnStart)
55
+ this.handler();
56
+ if (this.info.expression)
57
+ cron.schedule(this.info.expression, this.handler);
58
+ else if (this.info.interval)
59
+ this.loop();
60
+ }
61
+
62
+ private async loop()
63
+ {
64
+ while (this.running)
65
+ {
66
+ await this.handler()
67
+ await new Promise(resolver =>
68
+ {
69
+ setTimeout(() =>
70
+ {
71
+ resolver({});
72
+ }, this.info.interval);
73
+ })
74
+ }
75
+ }
76
+
77
+ private async handler()
78
+ {
79
+ if (this.log.enabled)
80
+ if (this.log.onStarted)
81
+ this.app.logger.info(`Cron ${this.info.name} was started.`);
82
+ try
83
+ {
84
+ // preHandle
85
+ if (this.running)
86
+ this.timer.onStart();
87
+ if (this.running)
88
+ await this.preHandle();
89
+ // Handler
90
+ if (this.running)
91
+ await this.handleOne();
92
+ // postHandle
93
+ if (this.running)
94
+ await this.postHandle();
95
+ if (this.running)
96
+ this.timer.onFinish();
97
+
98
+ if (this.log.enabled)
99
+ if (this.log.onFinished)
100
+ this.app.logger.info(`Cron ${this.info.name} was finished in ${this.timer.duration}ms.`);
101
+ }
102
+ catch (error: any)
103
+ {
104
+ this.app.logger.onCatchCritical(error);
105
+ }
106
+ };
107
+
108
+ async preHandle(): Promise<void> { }
109
+ abstract handleOne(): Promise<void>;
110
+ async postHandle(): Promise<void> { }
111
+ public stop()
112
+ {
113
+ this.running = false;
114
+ }
115
115
  }
@@ -1,53 +1,53 @@
1
- import * as express from "express";
2
- import { ErrorOperation, SortItem } from "namirasoft-core";
3
- import { IDatabase } from "./IDatabase";
4
-
5
- export abstract class BaseDatabase implements IDatabase
6
- {
7
- private tables: { [name: string]: any } = {};
8
- public req?: express.Request;
9
- public res?: express.Response;
10
- abstract getType(): string;
11
- abstract getName(): string;
12
- abstract init(): Promise<void>;
13
- abstract connect(): Promise<void>;
14
- async prepare(): Promise<void>
15
- { }
16
- abstract sync(force: boolean): Promise<void>;
17
- async seedBefore(): Promise<void>
18
- {
19
- }
20
- async seedAfter(): Promise<void>
21
- {
22
- }
23
- abstract isHealthy(): Promise<boolean>;
24
- abstract close(): Promise<void>;
25
- addTable(name: string, table: any)
26
- {
27
- this.tables[name] = table;
28
- }
29
- getTable<T>(name: string): T
30
- {
31
- let ans = this.tables[name] as T;
32
- if (!ans)
33
- ErrorOperation.throwHTTP(404, `Table '${name}' not found`);
34
- return ans;
35
- }
36
- paginate(page_number: number, page_size: number, page_size_default?: number): { offset: number, limit: number }
37
- {
38
- // page_number
39
- if (isNaN(page_number))
40
- page_number = 1;
41
- // page_size
42
- if (isNaN(page_size))
43
- if (page_size_default)
44
- page_size = page_size_default;
45
- if (isNaN(page_size))
46
- page_size = 25;
47
- //
48
- let offset = (page_number - 1) * page_size;
49
- let limit = page_size;
50
- return { offset, limit };
51
- }
52
- public abstract getSortOptions(sorts?: SortItem[] | undefined): any;
1
+ import * as express from "express";
2
+ import { ErrorOperation, SortItem } from "namirasoft-core";
3
+ import { IDatabase } from "./IDatabase";
4
+
5
+ export abstract class BaseDatabase implements IDatabase
6
+ {
7
+ private tables: { [name: string]: any } = {};
8
+ public req?: express.Request;
9
+ public res?: express.Response;
10
+ abstract getType(): string;
11
+ abstract getName(): string;
12
+ abstract init(): Promise<void>;
13
+ abstract connect(): Promise<void>;
14
+ async prepare(): Promise<void>
15
+ { }
16
+ abstract sync(force: boolean): Promise<void>;
17
+ async seedBefore(): Promise<void>
18
+ {
19
+ }
20
+ async seedAfter(): Promise<void>
21
+ {
22
+ }
23
+ abstract isHealthy(): Promise<boolean>;
24
+ abstract close(): Promise<void>;
25
+ addTable(name: string, table: any)
26
+ {
27
+ this.tables[name] = table;
28
+ }
29
+ getTable<T>(name: string): T
30
+ {
31
+ let ans = this.tables[name] as T;
32
+ if (!ans)
33
+ ErrorOperation.throwHTTP(404, `Table '${name}' not found`);
34
+ return ans;
35
+ }
36
+ paginate(page_number: number, page_size: number, page_size_default?: number): { offset: number, limit: number }
37
+ {
38
+ // page_number
39
+ if (isNaN(page_number))
40
+ page_number = 1;
41
+ // page_size
42
+ if (isNaN(page_size))
43
+ if (page_size_default)
44
+ page_size = page_size_default;
45
+ if (isNaN(page_size))
46
+ page_size = 25;
47
+ //
48
+ let offset = (page_number - 1) * page_size;
49
+ let limit = page_size;
50
+ return { offset, limit };
51
+ }
52
+ public abstract getSortOptions(sorts?: SortItem[] | undefined): any;
53
53
  }
@@ -1,39 +1,39 @@
1
- import nodemailer from 'nodemailer';
2
- import Mail, { AttachmentLike } from "nodemailer/lib/mailer";
3
- import { Readable } from "stream";
4
-
5
- export abstract class BaseEmailService
6
- {
7
- username: string;
8
- constructor(username: string)
9
- {
10
- this.username = username;
11
- }
12
- protected abstract getTransform(): any;
13
- send(from: string | null, to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void)
14
- {
15
- let transform = this.getTransform();
16
- let transporter = nodemailer.createTransport(transform);
17
-
18
- let mailOptions: Mail.Options = {
19
- from: from ?? this.username,
20
- to,
21
- subject,
22
- text,
23
- html
24
- };
25
- if (html)
26
- mailOptions.html = html;
27
-
28
- transporter.sendMail(mailOptions, function (error, info)
29
- {
30
- if (callback)
31
- callback(error, info);
32
- else
33
- {
34
- if (error)
35
- console.log(error);
36
- }
37
- });
38
- }
1
+ import nodemailer from 'nodemailer';
2
+ import Mail, { AttachmentLike } from "nodemailer/lib/mailer";
3
+ import { Readable } from "stream";
4
+
5
+ export abstract class BaseEmailService
6
+ {
7
+ username: string;
8
+ constructor(username: string)
9
+ {
10
+ this.username = username;
11
+ }
12
+ protected abstract getTransform(): any;
13
+ send(from: string | null, to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void)
14
+ {
15
+ let transform = this.getTransform();
16
+ let transporter = nodemailer.createTransport(transform);
17
+
18
+ let mailOptions: Mail.Options = {
19
+ from: from ?? this.username,
20
+ to,
21
+ subject,
22
+ text,
23
+ html
24
+ };
25
+ if (html)
26
+ mailOptions.html = html;
27
+
28
+ transporter.sendMail(mailOptions, function (error, info)
29
+ {
30
+ if (callback)
31
+ callback(error, info);
32
+ else
33
+ {
34
+ if (error)
35
+ console.log(error);
36
+ }
37
+ });
38
+ }
39
39
  }