@asla/yoursql 0.0.4 → 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
@@ -76,36 +76,40 @@ class SqlValuesCreator {
76
76
  else
77
77
  this.map.set(type, transformer);
78
78
  }
79
+ /** @deprecated 已废弃,改用 toSqlStr(value, "string") */
79
80
  string(value) {
80
81
  return SqlValuesCreator.string(value);
81
82
  }
83
+ /** @deprecated 已废弃,改用 toSqlStr(value, "number") */
82
84
  number(value) {
83
85
  return value.toString();
84
86
  }
85
87
  /**
86
88
  * 将 JS 对象转为 SQL 的字符值的形式
87
89
  */
88
- toSqlStr(value) {
89
- switch (typeof value) {
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) {
90
103
  case "bigint":
91
- return this.number(value);
104
+ return value.toString();
92
105
  case "number":
93
- return this.number(value);
106
+ return value.toString();
94
107
  case "string":
95
- return this.string(value);
108
+ return SqlValuesCreator.string(value);
96
109
  case "boolean":
97
110
  return value.toString();
98
111
  case "object":
99
- if (value === null)
100
- return "NULL";
101
- if (value instanceof SqlRaw) {
102
- return value.toString();
103
- }
104
- for (const Class of this.map.keys()) {
105
- if (value instanceof Class)
106
- return this.map.get(Class).call(this, value);
107
- }
108
- return this.defaultObject(value);
112
+ return this.toObjectStr(value);
109
113
  case "undefined":
110
114
  return "NULL";
111
115
  default:
@@ -114,15 +118,26 @@ class SqlValuesCreator {
114
118
  throw new Error("不支持转换 " + type + " 类型");
115
119
  }
116
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
+ }
117
132
  defaultObject(value) {
118
- return this.string(JSON.stringify(value));
133
+ return SqlValuesCreator.string(JSON.stringify(value));
119
134
  }
120
135
  objectListToValuesList(objectList, keys_types) {
121
136
  if (objectList.length <= 0)
122
137
  throw new Error("objectList 不能是空数组");
123
138
  let keys;
124
139
  if (!keys_types) {
125
- keys = getKeys(objectList);
140
+ keys = getObjectListKeys(objectList, true);
126
141
  }
127
142
  else if (keys_types instanceof Array) {
128
143
  keys = keys_types;
@@ -180,7 +195,7 @@ class SqlValuesCreator {
180
195
  * @example 返回示例: " 'abc', '6', 'now()' "
181
196
  */
182
197
  toValues(values) {
183
- return values.map(this.toSqlStr.bind(this)).join(",");
198
+ return values.map((v) => this.toSqlStr(v)).join(",");
184
199
  }
185
200
  }
186
201
  function toKeyType(object, keys_types) {
@@ -199,7 +214,10 @@ function toKeyType(object, keys_types) {
199
214
  }
200
215
  return { type, keys };
201
216
  }
202
- function getKeys(objectList) {
217
+ /**
218
+ * @public
219
+ */
220
+ function getObjectListKeys(objectList, keepNull) {
203
221
  let keys = new Set();
204
222
  for (let i = 0; i < objectList.length; i++) {
205
223
  let obj = objectList[i];
@@ -207,29 +225,41 @@ function getKeys(objectList) {
207
225
  let k;
208
226
  for (let j = 0; j < hasKeys.length; j++) {
209
227
  k = hasKeys[j];
210
- if (obj[k] !== undefined && typeof k === "string")
211
- 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);
212
233
  }
213
234
  }
214
235
  return Array.from(keys);
215
236
  }
216
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
+ ]);
217
248
  /**
218
249
  * @public
250
+ * @deprecated 已废弃
219
251
  * PgSql的值转换
220
252
  */
221
253
  class PgSqlValue extends SqlValuesCreator {
222
254
  constructor(custom) {
223
- const map = new Map(custom);
224
- map.set(Array, PgSqlValue.prototype.array);
225
- map.set(Date, PgSqlValue.prototype.timestamp);
255
+ const map = new Map(custom ? [...pgSqlTransformer, ...custom] : pgSqlTransformer);
226
256
  super(map);
227
257
  }
228
258
  timestamp(value) {
229
259
  return SqlValuesCreator.string(value.toISOString());
230
260
  }
231
261
  array(value) {
232
- return "ARRAY[" + value.map(this.toSqlStr.bind(this)).join(", ") + "]";
262
+ return this.toSqlStr(value, Array);
233
263
  }
234
264
  }
235
265
 
@@ -681,4 +711,4 @@ function genWhere(where) {
681
711
  return "\nWHERE " + where;
682
712
  }
683
713
 
684
- 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,6 +1,9 @@
1
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 {
@@ -8,10 +8,12 @@ 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: any) => 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;
15
17
  /**
16
18
  * @public
17
19
  */
@@ -34,14 +36,17 @@ export declare class SqlValuesCreator {
34
36
  */
35
37
  constructor(map?: JsObjectMapSql);
36
38
  /** 设置转换器 */
37
- setTransformer<T>(type: new (...args: any[]) => T, transformer?: (value: T) => string): void;
39
+ setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
38
40
  private readonly map;
41
+ /** @deprecated 已废弃,改用 toSqlStr(value, "string") */
39
42
  string(value: string): string;
43
+ /** @deprecated 已废弃,改用 toSqlStr(value, "number") */
40
44
  number(value: number | bigint): string;
41
45
  /**
42
46
  * 将 JS 对象转为 SQL 的字符值的形式
43
47
  */
44
- toSqlStr(value: any): string;
48
+ toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
49
+ protected toObjectStr(value: object): string;
45
50
  protected defaultObject(value: object): string;
46
51
  /**
47
52
  * 将对象列表转为 SQL 的 VALUES
@@ -65,5 +70,9 @@ export declare class SqlValuesCreator {
65
70
  */
66
71
  toValues(values: readonly any[]): string;
67
72
  }
73
+ /**
74
+ * @public
75
+ */
76
+ export declare function getObjectListKeys(objectList: any[], keepNull?: boolean): string[];
68
77
  export {};
69
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.4",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",