@asla/yoursql 0.1.1 → 0.2.0

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.d.ts CHANGED
@@ -4,4 +4,5 @@ export * from "./select/type.ts";
4
4
  export * from "./select/select.ts";
5
5
  export * from "./select/selectable.ts";
6
6
  export * from "./select/TableQuery.ts";
7
+ export * from "./util.ts";
7
8
  //# sourceMappingURL=mod.d.ts.map
package/dist/mod.js CHANGED
@@ -33,6 +33,28 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
33
33
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
34
34
  };
35
35
 
36
+ /**
37
+ * @public
38
+ * @param keepUndefinedKey - 是否保留值为 undefined 的 key
39
+ */
40
+ function getObjectListKeys(objectList, keepUndefinedKey) {
41
+ let keys = new Set();
42
+ for (let i = 0; i < objectList.length; i++) {
43
+ let obj = objectList[i];
44
+ let hasKeys = Object.keys(obj);
45
+ let k;
46
+ for (let j = 0; j < hasKeys.length; j++) {
47
+ k = hasKeys[j];
48
+ if (typeof k !== "string")
49
+ continue;
50
+ if (!keepUndefinedKey && obj[k] === undefined)
51
+ continue;
52
+ keys.add(k);
53
+ }
54
+ }
55
+ return Array.from(keys);
56
+ }
57
+
36
58
  var _SqlRaw_value;
37
59
  /**
38
60
  * SQL 原始字符对象
@@ -53,6 +75,12 @@ _SqlRaw_value = new WeakMap();
53
75
  * @public
54
76
  */
55
77
  class SqlValuesCreator {
78
+ static create(map) {
79
+ const obj = new this(map);
80
+ const fn = obj.toSqlStr.bind(obj);
81
+ Reflect.setPrototypeOf(fn, obj);
82
+ return fn;
83
+ }
56
84
  /**
57
85
  * 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
58
86
  * @example 输入 a'b'c , 返回 a''b''c.
@@ -65,10 +93,6 @@ class SqlValuesCreator {
65
93
  */
66
94
  constructor(map = new Map()) {
67
95
  this.map = map;
68
- const fn = this.toSqlStr.bind(this);
69
- this.toSqlStr = fn;
70
- Reflect.setPrototypeOf(fn, this);
71
- return fn;
72
96
  }
73
97
  /** 设置转换器 */
74
98
  setTransformer(type, transformer) {
@@ -205,34 +229,19 @@ function toKeyType(object, keys_types) {
205
229
  }
206
230
  return { type, keys };
207
231
  }
208
- /**
209
- * @public
210
- * @param keepUndefinedKey - 是否保留值为 undefined 的 key
211
- */
212
- function getObjectListKeys(objectList, keepUndefinedKey) {
213
- let keys = new Set();
214
- for (let i = 0; i < objectList.length; i++) {
215
- let obj = objectList[i];
216
- let hasKeys = Object.keys(obj);
217
- let k;
218
- for (let j = 0; j < hasKeys.length; j++) {
219
- k = hasKeys[j];
220
- if (typeof k !== "string")
221
- continue;
222
- if (!keepUndefinedKey && obj[k] === undefined)
223
- continue;
224
- keys.add(k);
225
- }
226
- }
227
- return Array.from(keys);
228
- }
229
232
 
230
233
  /** @public PgSql 转换器 */
231
234
  const pgSqlTransformer = new Map([
232
235
  [
233
236
  Array,
234
237
  function encodePgArray(value) {
235
- return "ARRAY[" + value.map(this).join(", ") + "]";
238
+ return ("ARRAY[" +
239
+ value
240
+ .map(function (item) {
241
+ return this.toSqlStr(item);
242
+ })
243
+ .join(", ") +
244
+ "]");
236
245
  },
237
246
  ],
238
247
  [Date, (value) => SqlValuesCreator.string(value.toISOString())],
@@ -603,7 +612,7 @@ class DbTableQuery extends DbTable {
603
612
  if (values instanceof Array) {
604
613
  if (values.length === 0)
605
614
  throw new Error("值不能为空");
606
- insertCol = Object.keys(values[0]);
615
+ insertCol = getObjectListKeys(values);
607
616
  valuesStr = `VALUES\n${this.statement.objectListToValuesList(values, insertCol)}`;
608
617
  }
609
618
  else if (values instanceof SqlQueryStatement) {
@@ -645,7 +654,7 @@ class DbTableQuery extends DbTable {
645
654
  for (const [k, v] of updateKey) {
646
655
  if (v === undefined)
647
656
  continue;
648
- setList.push(k + " = " + this.statement(v));
657
+ setList.push(k + " = " + this.statement.toSqlStr(v));
649
658
  }
650
659
  let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
651
660
  if (where)
@@ -16,18 +16,15 @@ export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
16
16
  export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T, map: JsObjectMapSql) => string;
17
17
  /** @public */
18
18
  export type ManualType = "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any);
19
- /**
20
- * @public
21
- */
22
- export interface SqlValuesCreator {
23
- /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
24
- (value: any, expectType?: ManualType): string;
25
- }
26
19
  /**
27
20
  * SQL value 生成器
28
21
  * @public
29
22
  */
30
23
  export declare class SqlValuesCreator {
24
+ static create(map?: JsObjectMapSql): SqlValuesCreator & {
25
+ /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
26
+ (value: any, expectType?: ManualType): string;
27
+ };
31
28
  /**
32
29
  * 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
33
30
  * @example 输入 a'b'c , 返回 a''b''c.
@@ -41,7 +38,7 @@ export declare class SqlValuesCreator {
41
38
  setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
42
39
  private readonly map;
43
40
  /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
44
- protected toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
41
+ toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
45
42
  protected toObjectStr(value: object): string;
46
43
  protected defaultObject(value: object): string;
47
44
  /**
@@ -66,10 +63,5 @@ export declare class SqlValuesCreator {
66
63
  */
67
64
  toValues(values: readonly any[]): string;
68
65
  }
69
- /**
70
- * @public
71
- * @param keepUndefinedKey - 是否保留值为 undefined 的 key
72
- */
73
- export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): string[];
74
66
  export {};
75
67
  //# sourceMappingURL=sql_value.d.ts.map
package/dist/util.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @public
3
+ * @param keepUndefinedKey - 是否保留值为 undefined 的 key
4
+ */
5
+ export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): string[];
6
+ //# sourceMappingURL=util.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",