ztechno_core 0.0.90 → 0.0.93

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,7 +2,9 @@ import { HashStruct } from './typings';
2
2
  export declare class ZCryptoService {
3
3
  static encrypt(text: string): HashStruct;
4
4
  static decrypt(data: HashStruct): string;
5
- static decryptJSON(data: HashStruct): any;
5
+ static decrypt(encrypted: string): string;
6
+ static decryptJSON<T = any>(data: HashStruct): T;
7
+ static decryptJSON<T = any>(data: string): T;
6
8
  static hash(
7
9
  hashAlgorithm: 'sha256' | 'sha512' | 'md5',
8
10
  data: string,
@@ -50,15 +50,22 @@ const key = Buffer.from([
50
50
  const iv = Buffer.from([0, 209, 223, 20, 147, 45, 14, 107, 93, 6, 76, 206, 176, 55, 245, 134]);
51
51
  class ZCryptoService {
52
52
  static encrypt(text) {
53
- const cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv);
53
+ const cipher = crypto.createCipheriv(algorithm, key, iv);
54
54
  let encrypted = cipher.update(text);
55
55
  encrypted = Buffer.concat([encrypted, cipher.final()]);
56
56
  return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
57
57
  }
58
58
  static decrypt(data) {
59
+ if (typeof data === 'string') {
60
+ const encryptedText = Buffer.from(data, 'hex');
61
+ const decipher = crypto.createDecipheriv(algorithm, key, iv);
62
+ let decrypted = decipher.update(encryptedText);
63
+ decrypted = Buffer.concat([decrypted, decipher.final()]);
64
+ return decrypted.toString();
65
+ }
59
66
  const niv = Buffer.from(data.iv, 'hex');
60
67
  const encryptedText = Buffer.from(data.encryptedData, 'hex');
61
- const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), niv);
68
+ const decipher = crypto.createDecipheriv(algorithm, key, niv);
62
69
  let decrypted = decipher.update(encryptedText);
63
70
  decrypted = Buffer.concat([decrypted, decipher.final()]);
64
71
  return decrypted.toString();
@@ -11,3 +11,4 @@ export * from './types';
11
11
  export * from './MySQLSchemaExtractor';
12
12
  export * from './MySQLSchemaImporter';
13
13
  export * from './utils';
14
+ export * from './examples';
@@ -39,3 +39,4 @@ __exportStar(require('./types'), exports);
39
39
  __exportStar(require('./MySQLSchemaExtractor'), exports);
40
40
  __exportStar(require('./MySQLSchemaImporter'), exports);
41
41
  __exportStar(require('./utils'), exports);
42
+ __exportStar(require('./examples'), exports);
@@ -67,10 +67,20 @@ export declare class ZSQLService {
67
67
  */
68
68
  fetch<T = any>(opt: {
69
69
  Query: string;
70
- Params: {
71
- [key: string]: any;
72
- };
70
+ Params:
71
+ | {
72
+ [key: string]: any;
73
+ }
74
+ | any[];
73
75
  }): Promise<T[]>;
76
+ fetch<T = any>(
77
+ query: string,
78
+ params:
79
+ | {
80
+ [key: string]: any;
81
+ }
82
+ | any[],
83
+ ): Promise<T[]>;
74
84
  /**
75
85
  * Executes a SQL query without type conversions.
76
86
  * Supports both positional (array) and named (object) parameters.
@@ -117,16 +117,10 @@ class ZSQLService {
117
117
  return row;
118
118
  });
119
119
  }
120
- /**
121
- * Legacy method to execute a SQL query. Uses uppercase property names for backwards compatibility.
122
- * Applies date and boolean conversions if configured. Consider using exec() directly for new code.
123
- * @template T - The expected result type
124
- * @param opt - Query options
125
- * @param opt.Query - The SQL query string to execute
126
- * @param opt.Params - Named parameters for the query
127
- * @returns Promise resolving to query results
128
- */
129
- async fetch(opt) {
120
+ async fetch(opt, params) {
121
+ if (typeof opt === 'string') {
122
+ return await this.exec({ query: opt, params });
123
+ }
130
124
  return await this.exec({ query: opt.Query, params: opt.Params });
131
125
  }
132
126
  async query(sql, params) {
@@ -149,20 +149,22 @@ class ZUserService {
149
149
  async find(opt) {
150
150
  const selectColumns = this.getSelectColumns();
151
151
  if (opt.email !== undefined) {
152
- const rows = await this.sqlService.query(
153
- `
154
- SELECT ${selectColumns} FROM \`${this.tableName}\`
155
- WHERE email=?`,
156
- [opt.email],
157
- );
152
+ const rows = await this.sqlService.fetch({
153
+ Query: /*SQL*/ `
154
+ SELECT ${selectColumns} FROM \`${this.tableName}\`
155
+ WHERE email=:email
156
+ `,
157
+ Params: { email: opt.email },
158
+ });
158
159
  return rows[0];
159
160
  } else if (opt.user_id !== undefined) {
160
- const rows = await this.sqlService.query(
161
- `
162
- SELECT ${selectColumns} FROM \`${this.tableName}\`
163
- WHERE user_id=?`,
164
- [opt.user_id],
165
- );
161
+ const rows = await this.sqlService.fetch({
162
+ Query: /*SQL*/ `
163
+ SELECT ${selectColumns} FROM \`${this.tableName}\`
164
+ WHERE user_id=:user_id
165
+ `,
166
+ Params: { user_id: opt.user_id },
167
+ });
166
168
  return rows[0];
167
169
  } else {
168
170
  throw new Error(`Unexpected Input for ZUserService.find(${JSON.stringify(opt)})`);
@@ -174,24 +176,24 @@ class ZUserService {
174
176
  }
175
177
  const selectColumns = this.getSelectColumns();
176
178
  const res = await (opt.session
177
- ? this.sqlService.query(
178
- `
179
- SELECT ${selectColumns} FROM \`${this.tableName}\`
180
- WHERE session=?`,
181
- [opt.session],
182
- )
183
- : this.sqlService.query(
184
- `
185
- SELECT ${selectColumns} FROM \`${this.tableName}\`
186
- WHERE email=? AND pass=?`,
187
- [opt.email, this.hashPass(opt)],
188
- ));
179
+ ? this.sqlService.fetch({
180
+ Query: /*SQL*/ `
181
+ SELECT ${selectColumns} FROM \`${this.tableName}\` WHERE session=?
182
+ `,
183
+ Params: [opt.session],
184
+ })
185
+ : this.sqlService.fetch({
186
+ Query: /*SQL*/ `
187
+ SELECT ${selectColumns} FROM \`${this.tableName}\` WHERE email=? AND pass=?
188
+ `,
189
+ Params: [opt.email, this.hashPass(opt)],
190
+ }));
189
191
  return res.length === 0 ? { authenticated: false } : { user: res[0], session: res[0].session, authenticated: true };
190
192
  }
191
193
  async fetch(opt) {
192
194
  const selectColumns = this.getSelectColumns();
193
195
  const limit = opt?.limit || 100;
194
- const rows = await this.sqlService.query(
196
+ const rows = await this.sqlService.fetch(
195
197
  `
196
198
  SELECT ${selectColumns} FROM \`${this.tableName}\`
197
199
  ORDER BY created_at DESC
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.90",
3
+ "version": "0.0.93",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",