@asla/yoursql 0.1.2 → 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.js CHANGED
@@ -75,6 +75,12 @@ _SqlRaw_value = new WeakMap();
75
75
  * @public
76
76
  */
77
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
+ }
78
84
  /**
79
85
  * 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
80
86
  * @example 输入 a'b'c , 返回 a''b''c.
@@ -87,10 +93,6 @@ class SqlValuesCreator {
87
93
  */
88
94
  constructor(map = new Map()) {
89
95
  this.map = map;
90
- const fn = this.toSqlStr.bind(this);
91
- this.toSqlStr = fn;
92
- Reflect.setPrototypeOf(fn, this);
93
- return fn;
94
96
  }
95
97
  /** 设置转换器 */
96
98
  setTransformer(type, transformer) {
@@ -233,7 +235,13 @@ const pgSqlTransformer = new Map([
233
235
  [
234
236
  Array,
235
237
  function encodePgArray(value) {
236
- return "ARRAY[" + value.map(this).join(", ") + "]";
238
+ return ("ARRAY[" +
239
+ value
240
+ .map(function (item) {
241
+ return this.toSqlStr(item);
242
+ })
243
+ .join(", ") +
244
+ "]");
237
245
  },
238
246
  ],
239
247
  [Date, (value) => SqlValuesCreator.string(value.toISOString())],
@@ -646,7 +654,7 @@ class DbTableQuery extends DbTable {
646
654
  for (const [k, v] of updateKey) {
647
655
  if (v === undefined)
648
656
  continue;
649
- setList.push(k + " = " + this.statement(v));
657
+ setList.push(k + " = " + this.statement.toSqlStr(v));
650
658
  }
651
659
  let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
652
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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",