baja-lite 1.4.6 → 1.4.7

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.
Files changed (6) hide show
  1. package/boot.js +11 -8
  2. package/object.d.ts +2 -2
  3. package/object.js +25 -3
  4. package/package.json +1 -1
  5. package/sql.d.ts +415 -338
  6. package/sql.js +825 -395
package/boot.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { _Hump, DBType, getEnums } from 'baja-lite-field';
2
2
  import events from 'events';
3
- import { _dao, _defOption, _enum, _EventBus, _fs, _GlobalSqlOption, _path, _primaryDB, _sqlCache, ColumnMode, logger, Mysql, Postgresql, SqlCache, Sqlite, SqliteRemote } from './sql.js';
3
+ import { _dao, _dataConvert, _defOption, _enum, _EventBus, _fs, _GlobalSqlOption, _path, _primaryDB, _sqlCache, ColumnMode, logger, Mysql, Postgresql, SqlCache, Sqlite, SqliteRemote } from './sql.js';
4
4
  export const Boot = async function (options) {
5
5
  globalThis[_GlobalSqlOption] = Object.assign({}, _defOption);
6
6
  if (options.skipEmptyString !== undefined) {
@@ -16,12 +16,12 @@ export const Boot = async function (options) {
16
16
  globalThis[_GlobalSqlOption].maxDeal = options.maxDeal;
17
17
  }
18
18
  globalThis[_Hump] = options.columnMode === ColumnMode.HUMP;
19
+ logger.level = options.log ?? 'info';
20
+ globalThis[_sqlCache] = new SqlCache();
19
21
  if (options.sqlDir) {
20
22
  globalThis[_path] = await import('path');
21
23
  globalThis[_fs] = await import('fs');
22
24
  }
23
- logger.level = options.log ?? 'info';
24
- globalThis[_sqlCache] = new SqlCache();
25
25
  if (options.sqlMap || options.sqlDir) {
26
26
  await globalThis[_sqlCache].init(options);
27
27
  }
@@ -36,6 +36,9 @@ export const Boot = async function (options) {
36
36
  if (options.enums) {
37
37
  globalThis[_enum] = getEnums(options.enums);
38
38
  }
39
+ if (options.dataConvert) {
40
+ globalThis[_dataConvert] = options.dataConvert;
41
+ }
39
42
  if (options.Mysql) {
40
43
  const { createPool } = await import('mysql2/promise');
41
44
  if (options.Mysql['host']) {
@@ -55,7 +58,7 @@ export const Boot = async function (options) {
55
58
  decimalNumbers: true,
56
59
  supportBigNumbers: true
57
60
  }));
58
- if (flag === false) {
61
+ if (!flag) {
59
62
  globalThis[_dao][DBType.Mysql][_primaryDB] = db;
60
63
  flag = true;
61
64
  }
@@ -71,7 +74,7 @@ export const Boot = async function (options) {
71
74
  let flag = false;
72
75
  for (const [key, fileName] of Object.entries(options.Sqlite)) {
73
76
  const db = new Sqlite(new options.BetterSqlite3(fileName, { fileMustExist: false }));
74
- if (flag === false) {
77
+ if (!flag) {
75
78
  globalThis[_dao][DBType.Sqlite][_primaryDB] = db;
76
79
  flag = true;
77
80
  }
@@ -89,7 +92,7 @@ export const Boot = async function (options) {
89
92
  for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
90
93
  options.SqliteRemote.service.initDB(fileName);
91
94
  const db = new SqliteRemote(options.SqliteRemote.service, fileName);
92
- if (flag === false) {
95
+ if (!flag) {
93
96
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
94
97
  flag = true;
95
98
  }
@@ -107,7 +110,7 @@ export const Boot = async function (options) {
107
110
  let flag = false;
108
111
  for (const [key, option] of Object.entries(options.Redis)) {
109
112
  const db = new Redis(option);
110
- if (flag === false) {
113
+ if (!flag) {
111
114
  globalThis[_dao][DBType.Redis][_primaryDB] = db;
112
115
  flag = true;
113
116
  }
@@ -149,7 +152,7 @@ export const Boot = async function (options) {
149
152
  let flag = false;
150
153
  for (const [key, option] of Object.entries(options.Postgresql)) {
151
154
  const db = new Postgresql(new Pool.default(option));
152
- if (flag === false) {
155
+ if (!flag) {
153
156
  globalThis[_dao][DBType.Postgresql][_primaryDB] = db;
154
157
  flag = true;
155
158
  }
package/object.d.ts CHANGED
@@ -77,8 +77,8 @@ export declare const arraySplit: <T = any>(datas: T[], { everyLength, groupCount
77
77
  }) => T[][];
78
78
  export declare const P2C: (pro: string, IF?: boolean) => string;
79
79
  export declare const C2P: (pro: string, IF?: boolean) => string;
80
- export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
81
- export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L): T;
80
+ export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L[], hump?: boolean, convert?: Record<string, (data: any) => any>): T[];
81
+ export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L, hump?: boolean, convert?: Record<string, (data: any) => any>): T;
82
82
  export declare function P2C2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
83
83
  export declare function P2C2<T extends Object = any, L extends Object = T>(datas: L): T;
84
84
  export declare function fillArrayToMinLength<T>(arr: T[], minLength: number, fillValue: T): T[];
package/object.js CHANGED
@@ -198,12 +198,34 @@ const P2CEX = /[A-Z]/g;
198
198
  export const P2C = (pro, IF = true) => IF ? pro.replace(P2CEX, (a) => `_${a.toLowerCase()}`) : pro;
199
199
  const C2PEX = /_([a-z])/g;
200
200
  export const C2P = (pro, IF = true) => IF ? pro.replace(C2PEX, (a, b) => `${b.toUpperCase()}`) : pro;
201
- export function C2P2(datas) {
201
+ export function C2P2(datas, hump, convert) {
202
202
  if (datas instanceof Array) {
203
- return iterate(datas).map((data) => Object.fromEntries(Object.entries(data).map(([K, V]) => [C2P(K), V]))).toArray();
203
+ return iterate(datas)
204
+ .map((data) => Object.fromEntries(Object.entries(data).map(([K, V]) => {
205
+ if (hump) {
206
+ K = C2P(K);
207
+ }
208
+ if (convert && convert[K]) {
209
+ return [K, convert[K](V)];
210
+ }
211
+ else {
212
+ return [K, V];
213
+ }
214
+ })))
215
+ .toArray();
204
216
  }
205
217
  else if (datas) {
206
- return Object.fromEntries(Object.entries(datas).map(([K, V]) => [C2P(K), V]));
218
+ return Object.fromEntries(Object.entries(datas).map(([K, V]) => {
219
+ if (hump) {
220
+ K = C2P(K);
221
+ }
222
+ if (convert && convert[K]) {
223
+ return [K, convert[K](V)];
224
+ }
225
+ else {
226
+ return [K, V];
227
+ }
228
+ }));
207
229
  }
208
230
  else {
209
231
  return datas;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.4.6",
3
+ "version": "1.4.7",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/baja-lite",
6
6
  "repository": {