ztechno_core 0.0.48 → 0.0.50

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.
@@ -2,16 +2,40 @@ import * as mysql from 'mysql';
2
2
  type ZOnErrorCallback = (err: mysql.MysqlError) => any;
3
3
  type ZOnLogCallback = (log: string) => any;
4
4
  export declare class ZSqlService {
5
+ private options;
5
6
  private pool;
6
7
  private defaultPoolconfig;
7
8
  private listeners;
8
9
  private databaseName;
9
10
  get database(): string;
10
- constructor(options: mysql.PoolConfig);
11
+ constructor(
12
+ options: mysql.PoolConfig & {
13
+ dateStringTimezone?: string;
14
+ },
15
+ );
11
16
  on(eventName: 'err', listener: ZOnErrorCallback): void;
12
17
  on(eventName: 'log', listener: ZOnLogCallback): void;
13
18
  private triggerEvent;
14
19
  private getPoolConnection;
20
+ exec(opt: {
21
+ query: string;
22
+ params?:
23
+ | any[]
24
+ | {
25
+ [key: string]: any;
26
+ };
27
+ }): Promise<{
28
+ insertId: number;
29
+ affectedRows: number;
30
+ }>;
31
+ exec<T = any>(opt: {
32
+ query: string;
33
+ params?:
34
+ | any[]
35
+ | {
36
+ [key: string]: any;
37
+ };
38
+ }): Promise<T>;
15
39
  query(
16
40
  sql: string,
17
41
  params?:
@@ -30,7 +54,8 @@ export declare class ZSqlService {
30
54
  | {
31
55
  [key: string]: any;
32
56
  },
33
- ): Promise<T>;
57
+ ): Promise<T[]>;
34
58
  private formatQueryParams;
59
+ private isSqlDate;
35
60
  }
36
61
  export {};
@@ -47,6 +47,7 @@ class ZSqlService {
47
47
  return this.databaseName;
48
48
  }
49
49
  constructor(options) {
50
+ this.options = options;
50
51
  this.defaultPoolconfig = {
51
52
  connectionLimit: 10,
52
53
  timeout: 20000,
@@ -82,6 +83,23 @@ class ZSqlService {
82
83
  this.pool.getConnection((err, con) => (err ? reject(err) : resolve(con)));
83
84
  });
84
85
  }
86
+ async exec(opt) {
87
+ const rows = await this.query(opt.query, opt.params);
88
+ if (!Array.isArray(rows)) {
89
+ return rows;
90
+ }
91
+ if (!this.options.dateStringTimezone) {
92
+ return rows;
93
+ }
94
+ return rows.map((row) => {
95
+ Object.keys(row).map((key) => {
96
+ if (this.isSqlDate(row[key])) {
97
+ row[key] = new Date(row[key] + this.options.dateStringTimezone);
98
+ }
99
+ });
100
+ return row;
101
+ });
102
+ }
85
103
  async query(sql, params) {
86
104
  try {
87
105
  const con = await this.getPoolConnection();
@@ -112,5 +130,19 @@ class ZSqlService {
112
130
  return values.hasOwnProperty(key) ? con.escape(values[key]) : txt;
113
131
  });
114
132
  }
133
+ isSqlDate(str) {
134
+ if (
135
+ str &&
136
+ typeof str === 'string' &&
137
+ str[4] === '-' &&
138
+ str[7] === '-' &&
139
+ str[10] === ' ' &&
140
+ str[13] === ':' &&
141
+ str[16] === ':'
142
+ ) {
143
+ return true;
144
+ }
145
+ return false;
146
+ }
115
147
  }
116
148
  exports.ZSqlService = ZSqlService;
@@ -161,10 +161,6 @@ class ZTranslateService {
161
161
  return hasLocal ? false : this.localCache[lang][key];
162
162
  }
163
163
  insertLocalCache(key, lang, data) {
164
- if (this.localCache[lang].hasOwnProperty(key)) {
165
- // console.warn(`Translations already cached!`, { key, lang, data})
166
- return;
167
- }
168
164
  this.localCache[lang][key] = data;
169
165
  }
170
166
  clearLocalCache() {
@@ -12,7 +12,7 @@ export declare class ZUserService {
12
12
  register({ email, pass, role, admin }: ZRequiredUserColumns): Promise<{
13
13
  session: string;
14
14
  }>;
15
- fetch(opt?: { limit: number }): Promise<any[]>;
15
+ fetch(opt?: { limit: number }): Promise<any[][]>;
16
16
  exists(
17
17
  opt:
18
18
  | {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.48",
3
+ "version": "0.0.50",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",