@asla/yoursql 0.0.3 → 0.0.5

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/dist/mod.js CHANGED
@@ -65,48 +65,79 @@ class SqlValuesCreator {
65
65
  */
66
66
  constructor(map = new Map()) {
67
67
  this.map = map;
68
+ const fn = (value) => this.toSqlStr(value);
69
+ Reflect.setPrototypeOf(fn, this);
70
+ return fn;
71
+ }
72
+ /** 设置转换器 */
73
+ setTransformer(type, transformer) {
74
+ if (!transformer)
75
+ this.map.delete(type);
76
+ else
77
+ this.map.set(type, transformer);
68
78
  }
79
+ /** @deprecated 已废弃,改用 toSqlStr(value, "string") */
69
80
  string(value) {
70
81
  return SqlValuesCreator.string(value);
71
82
  }
83
+ /** @deprecated 已废弃,改用 toSqlStr(value, "number") */
72
84
  number(value) {
73
85
  return value.toString();
74
86
  }
75
- /** 将 JS 对象转为 SQL 的字符值的形式 */
76
- toSqlStr(value) {
77
- switch (typeof value) {
87
+ /**
88
+ * 将 JS 对象转为 SQL 的字符值的形式
89
+ */
90
+ toSqlStr(value, expectType) {
91
+ let basicType;
92
+ if (expectType) {
93
+ if (typeof expectType === "function") {
94
+ return this.map.get(expectType).call(this, value, this.map);
95
+ }
96
+ else {
97
+ basicType = expectType;
98
+ }
99
+ }
100
+ else
101
+ basicType = typeof value;
102
+ switch (basicType) {
78
103
  case "bigint":
79
- return this.number(value);
104
+ return value.toString();
80
105
  case "number":
81
- return this.number(value);
106
+ return value.toString();
82
107
  case "string":
83
- return this.string(value);
108
+ return SqlValuesCreator.string(value);
84
109
  case "boolean":
85
110
  return value.toString();
86
111
  case "object":
87
- if (value === null)
88
- return "NULL";
89
- if (value instanceof SqlRaw) {
90
- return value.toString();
91
- }
92
- for (const Class of this.map.keys()) {
93
- if (value instanceof Class)
94
- return this.map.get(Class).call(this, value);
95
- }
96
- return this.string(JSON.stringify(value));
112
+ return this.toObjectStr(value);
113
+ case "undefined":
114
+ return "NULL";
97
115
  default:
116
+ //function、symbol
98
117
  let type = typeof value;
99
- if (type === "object")
100
- type = value.constructor?.name ?? "object";
101
118
  throw new Error("不支持转换 " + type + " 类型");
102
119
  }
103
120
  }
121
+ toObjectStr(value) {
122
+ if (value === null)
123
+ return "NULL";
124
+ if (value instanceof SqlRaw)
125
+ return value.toString();
126
+ for (const Class of this.map.keys()) {
127
+ if (value instanceof Class)
128
+ return this.map.get(Class).call(this, value, this.map);
129
+ }
130
+ return this.defaultObject(value);
131
+ }
132
+ defaultObject(value) {
133
+ return SqlValuesCreator.string(JSON.stringify(value));
134
+ }
104
135
  objectListToValuesList(objectList, keys_types) {
105
136
  if (objectList.length <= 0)
106
137
  throw new Error("objectList 不能是空数组");
107
138
  let keys;
108
139
  if (!keys_types) {
109
- keys = getKeys(objectList);
140
+ keys = getObjectListKeys(objectList, true);
110
141
  }
111
142
  else if (keys_types instanceof Array) {
112
143
  keys = keys_types;
@@ -164,7 +195,7 @@ class SqlValuesCreator {
164
195
  * @example 返回示例: " 'abc', '6', 'now()' "
165
196
  */
166
197
  toValues(values) {
167
- return values.map(this.toSqlStr.bind(this)).join(",");
198
+ return values.map((v) => this.toSqlStr(v)).join(",");
168
199
  }
169
200
  }
170
201
  function toKeyType(object, keys_types) {
@@ -183,7 +214,10 @@ function toKeyType(object, keys_types) {
183
214
  }
184
215
  return { type, keys };
185
216
  }
186
- function getKeys(objectList) {
217
+ /**
218
+ * @public
219
+ */
220
+ function getObjectListKeys(objectList, keepNull) {
187
221
  let keys = new Set();
188
222
  for (let i = 0; i < objectList.length; i++) {
189
223
  let obj = objectList[i];
@@ -191,29 +225,41 @@ function getKeys(objectList) {
191
225
  let k;
192
226
  for (let j = 0; j < hasKeys.length; j++) {
193
227
  k = hasKeys[j];
194
- if (obj[k] !== undefined && typeof k === "string")
195
- keys.add(k);
228
+ if (keepNull && obj[k] === null)
229
+ continue;
230
+ if (obj[k] === undefined || typeof k !== "string")
231
+ continue;
232
+ keys.add(k);
196
233
  }
197
234
  }
198
235
  return Array.from(keys);
199
236
  }
200
237
 
238
+ /** @public PgSql 转换器 */
239
+ const pgSqlTransformer = new Map([
240
+ [
241
+ Array,
242
+ function encodePgArray(value) {
243
+ return "ARRAY[" + value.map(this).join(", ") + "]";
244
+ },
245
+ ],
246
+ [Date, (value) => SqlValuesCreator.string(value.toISOString())],
247
+ ]);
201
248
  /**
202
249
  * @public
250
+ * @deprecated 已废弃
203
251
  * PgSql的值转换
204
252
  */
205
253
  class PgSqlValue extends SqlValuesCreator {
206
- constructor() {
207
- const map = new Map();
208
- map.set(Array, PgSqlValue.prototype.array);
209
- map.set(Date, PgSqlValue.prototype.timestamp);
254
+ constructor(custom) {
255
+ const map = new Map(custom ? [...pgSqlTransformer, ...custom] : pgSqlTransformer);
210
256
  super(map);
211
257
  }
212
258
  timestamp(value) {
213
259
  return SqlValuesCreator.string(value.toISOString());
214
260
  }
215
261
  array(value) {
216
- return "ARRAY[" + value.map(this.toSqlStr.bind(this)).join(", ") + "]";
262
+ return this.toSqlStr(value, Array);
217
263
  }
218
264
  }
219
265
 
@@ -587,7 +633,7 @@ class DbTableQuery extends DbTable {
587
633
  }
588
634
  else if (values instanceof SqlQueryStatement) {
589
635
  // todo 验证 values.columns 和 this.columns 是否匹配
590
- valuesStr = values.toSelect();
636
+ valuesStr = values.toString();
591
637
  insertCol = values.columns;
592
638
  }
593
639
  else
@@ -665,4 +711,4 @@ function genWhere(where) {
665
711
  return "\nWHERE " + where;
666
712
  }
667
713
 
668
- export { DbTable, DbTableQuery, PgSqlValue, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, createSelect };
714
+ export { DbTable, DbTableQuery, PgSqlValue, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, createSelect, getObjectListKeys, pgSqlTransformer };
@@ -23,7 +23,7 @@ export declare abstract class SqlSelectable<T extends TableType> {
23
23
  /** 获取 SQL 语句 */
24
24
  abstract toString(): string;
25
25
  /** 保留以推断类型 */
26
- [SQL_SELECTABLE]: T;
26
+ private [SQL_SELECTABLE];
27
27
  }
28
28
  /**
29
29
  * 数据库表
@@ -1,10 +1,13 @@
1
- import { SqlValuesCreator } from "./sql_value.ts";
1
+ import { SqlValuesCreator, JsObjectMapSql } from "./sql_value.ts";
2
+ /** @public PgSql 转换器 */
3
+ export declare const pgSqlTransformer: JsObjectMapSql;
2
4
  /**
3
5
  * @public
6
+ * @deprecated 已废弃
4
7
  * PgSql的值转换
5
8
  */
6
9
  export declare class PgSqlValue extends SqlValuesCreator {
7
- constructor();
10
+ constructor(custom?: JsObjectMapSql);
8
11
  timestamp(value: Date): string;
9
12
  array(value: any[]): string;
10
13
  }
@@ -8,10 +8,19 @@ export declare class SqlRaw<T = any> {
8
8
  constructor(value: string);
9
9
  toString(): string;
10
10
  /** 保留以推断类型 */
11
- [SQL_RAW]: T;
11
+ private [SQL_RAW];
12
12
  }
13
13
  /** @public */
14
- export type JsObjectMapSql = Map<new (...args: any[]) => any, (value: object) => string>;
14
+ export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
15
+ /** @public */
16
+ export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T, map: JsObjectMapSql) => string;
17
+ /**
18
+ * @public
19
+ */
20
+ export interface SqlValuesCreator {
21
+ /** 将 JS 对象转为 SQL 的字符值的形式 */
22
+ (value: any): string;
23
+ }
15
24
  /**
16
25
  * SQL value 生成器
17
26
  * @public
@@ -26,11 +35,19 @@ export declare class SqlValuesCreator {
26
35
  * @param map - 自定义对象转换
27
36
  */
28
37
  constructor(map?: JsObjectMapSql);
38
+ /** 设置转换器 */
39
+ setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
29
40
  private readonly map;
41
+ /** @deprecated 已废弃,改用 toSqlStr(value, "string") */
30
42
  string(value: string): string;
43
+ /** @deprecated 已废弃,改用 toSqlStr(value, "number") */
31
44
  number(value: number | bigint): string;
32
- /** 将 JS 对象转为 SQL 的字符值的形式 */
33
- toSqlStr(value: any): string;
45
+ /**
46
+ * JS 对象转为 SQL 的字符值的形式
47
+ */
48
+ toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
49
+ protected toObjectStr(value: object): string;
50
+ protected defaultObject(value: object): string;
34
51
  /**
35
52
  * 将对象列表转为 SQL 的 VALUES
36
53
  * @example 返回示例: " (...),(...) "
@@ -53,5 +70,9 @@ export declare class SqlValuesCreator {
53
70
  */
54
71
  toValues(values: readonly any[]): string;
55
72
  }
73
+ /**
74
+ * @public
75
+ */
76
+ export declare function getObjectListKeys(objectList: any[], keepNull?: boolean): string[];
56
77
  export {};
57
78
  //# sourceMappingURL=sql_value.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",