@asla/yoursql 0.0.6 → 0.1.1

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,7 +65,8 @@ class SqlValuesCreator {
65
65
  */
66
66
  constructor(map = new Map()) {
67
67
  this.map = map;
68
- const fn = (value) => this.toSqlStr(value);
68
+ const fn = this.toSqlStr.bind(this);
69
+ this.toSqlStr = fn;
69
70
  Reflect.setPrototypeOf(fn, this);
70
71
  return fn;
71
72
  }
@@ -76,17 +77,7 @@ class SqlValuesCreator {
76
77
  else
77
78
  this.map.set(type, transformer);
78
79
  }
79
- /** @deprecated 已废弃,改用 toSqlStr(value, "string") */
80
- string(value) {
81
- return SqlValuesCreator.string(value);
82
- }
83
- /** @deprecated 已废弃,改用 toSqlStr(value, "number") */
84
- number(value) {
85
- return value.toString();
86
- }
87
- /**
88
- * 将 JS 对象转为 SQL 的字符值的形式
89
- */
80
+ /** JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
90
81
  toSqlStr(value, expectType) {
91
82
  let basicType;
92
83
  if (expectType) {
@@ -111,7 +102,7 @@ class SqlValuesCreator {
111
102
  case "object":
112
103
  return this.toObjectStr(value);
113
104
  case "undefined":
114
- return "NULL";
105
+ return "DEFAULT";
115
106
  default:
116
107
  //function、symbol
117
108
  let type = typeof value;
@@ -132,12 +123,12 @@ class SqlValuesCreator {
132
123
  defaultObject(value) {
133
124
  return SqlValuesCreator.string(JSON.stringify(value));
134
125
  }
135
- objectListToValuesList(objectList, keys_types) {
126
+ objectListToValuesList(objectList, keys_types, keepUndefinedKey) {
136
127
  if (objectList.length <= 0)
137
128
  throw new Error("objectList 不能是空数组");
138
129
  let keys;
139
130
  if (!keys_types) {
140
- keys = getObjectListKeys(objectList, true);
131
+ keys = getObjectListKeys(objectList, keepUndefinedKey);
141
132
  }
142
133
  else if (keys_types instanceof Array) {
143
134
  keys = keys_types;
@@ -156,7 +147,7 @@ class SqlValuesCreator {
156
147
  rows = [];
157
148
  j = 0;
158
149
  for (; j < keys.length; j++) {
159
- value = object[keys[j]] ?? null;
150
+ value = object[keys[j]];
160
151
  rows[j] = this.toSqlStr(value);
161
152
  }
162
153
  str += ",\n(" + rows.join(",") + ")";
@@ -177,7 +168,7 @@ class SqlValuesCreator {
177
168
  try {
178
169
  for (; i < keys.length; i++) {
179
170
  key = keys[i];
180
- value = object[key] ?? null;
171
+ value = object[key];
181
172
  if (type[key])
182
173
  values[i] = this.toSqlStr(value) + "::" + type[key];
183
174
  else
@@ -210,14 +201,15 @@ function toKeyType(object, keys_types) {
210
201
  type = keys_types;
211
202
  }
212
203
  else {
213
- keys = Object.keys(object).filter((key) => typeof key === "string" && object[key] !== undefined);
204
+ keys = Object.keys(object);
214
205
  }
215
206
  return { type, keys };
216
207
  }
217
208
  /**
218
209
  * @public
210
+ * @param keepUndefinedKey - 是否保留值为 undefined 的 key
219
211
  */
220
- function getObjectListKeys(objectList, keepNull) {
212
+ function getObjectListKeys(objectList, keepUndefinedKey) {
221
213
  let keys = new Set();
222
214
  for (let i = 0; i < objectList.length; i++) {
223
215
  let obj = objectList[i];
@@ -225,9 +217,9 @@ function getObjectListKeys(objectList, keepNull) {
225
217
  let k;
226
218
  for (let j = 0; j < hasKeys.length; j++) {
227
219
  k = hasKeys[j];
228
- if (keepNull && obj[k] === null)
220
+ if (typeof k !== "string")
229
221
  continue;
230
- if (obj[k] === undefined || typeof k !== "string")
222
+ if (!keepUndefinedKey && obj[k] === undefined)
231
223
  continue;
232
224
  keys.add(k);
233
225
  }
@@ -245,23 +237,6 @@ const pgSqlTransformer = new Map([
245
237
  ],
246
238
  [Date, (value) => SqlValuesCreator.string(value.toISOString())],
247
239
  ]);
248
- /**
249
- * @public
250
- * @deprecated 已废弃
251
- * PgSql的值转换
252
- */
253
- class PgSqlValue extends SqlValuesCreator {
254
- constructor(custom) {
255
- const map = new Map(custom ? [...pgSqlTransformer, ...custom] : pgSqlTransformer);
256
- super(map);
257
- }
258
- timestamp(value) {
259
- return SqlValuesCreator.string(value.toISOString());
260
- }
261
- array(value) {
262
- return this.toSqlStr(value, Array);
263
- }
264
- }
265
240
 
266
241
  /**
267
242
  * 可选择项。可以是 table、查询结果等,它能被 select 语句选择
@@ -670,7 +645,7 @@ class DbTableQuery extends DbTable {
670
645
  for (const [k, v] of updateKey) {
671
646
  if (v === undefined)
672
647
  continue;
673
- setList.push(k + " = " + this.statement.toSqlStr(v));
648
+ setList.push(k + " = " + this.statement(v));
674
649
  }
675
650
  let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
676
651
  if (where)
@@ -711,4 +686,4 @@ function genWhere(where) {
711
686
  return "\nWHERE " + where;
712
687
  }
713
688
 
714
- export { DbTable, DbTableQuery, PgSqlValue, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, createSelect, getObjectListKeys, pgSqlTransformer };
689
+ export { DbTable, DbTableQuery, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, createSelect, getObjectListKeys, pgSqlTransformer };
@@ -1,14 +1,4 @@
1
- import { SqlValuesCreator, JsObjectMapSql } from "./sql_value.ts";
1
+ import { JsObjectMapSql } from "./sql_value.ts";
2
2
  /** @public PgSql 转换器 */
3
3
  export declare const pgSqlTransformer: JsObjectMapSql;
4
- /**
5
- * @public
6
- * @deprecated 已废弃
7
- * PgSql的值转换
8
- */
9
- export declare class PgSqlValue extends SqlValuesCreator {
10
- constructor(custom?: JsObjectMapSql);
11
- timestamp(value: Date): string;
12
- array(value: any[]): string;
13
- }
14
4
  //# sourceMappingURL=db_type.d.ts.map
@@ -14,12 +14,14 @@ export declare class SqlRaw<T = any> {
14
14
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
15
15
  /** @public */
16
16
  export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T, map: JsObjectMapSql) => string;
17
+ /** @public */
18
+ export type ManualType = "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any);
17
19
  /**
18
20
  * @public
19
21
  */
20
22
  export interface SqlValuesCreator {
21
- /** 将 JS 对象转为 SQL 的字符值的形式 */
22
- (value: any): string;
23
+ /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
24
+ (value: any, expectType?: ManualType): string;
23
25
  }
24
26
  /**
25
27
  * SQL value 生成器
@@ -38,14 +40,8 @@ export declare class SqlValuesCreator {
38
40
  /** 设置转换器 */
39
41
  setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
40
42
  private readonly map;
41
- /** @deprecated 已废弃,改用 toSqlStr(value, "string") */
42
- string(value: string): string;
43
- /** @deprecated 已废弃,改用 toSqlStr(value, "number") */
44
- number(value: number | bigint): string;
45
- /**
46
- * 将 JS 对象转为 SQL 的字符值的形式
47
- */
48
- toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
43
+ /** JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
44
+ protected toSqlStr(value: any, expectType?: "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any)): string;
49
45
  protected toObjectStr(value: object): string;
50
46
  protected defaultObject(value: object): string;
51
47
  /**
@@ -55,7 +51,7 @@ export declare class SqlValuesCreator {
55
51
  */
56
52
  objectListToValuesList<T extends object>(objectList: T[], keys?: readonly (keyof T)[] | {
57
53
  [key in keyof T]?: string | undefined;
58
- }): string;
54
+ }, keepUndefinedKey?: boolean): string;
59
55
  /**
60
56
  * 将对象转为 SQL 的 value
61
57
  * @example 返回示例: " 'abc', '6', 'now()' "
@@ -72,7 +68,8 @@ export declare class SqlValuesCreator {
72
68
  }
73
69
  /**
74
70
  * @public
71
+ * @param keepUndefinedKey - 是否保留值为 undefined 的 key
75
72
  */
76
- export declare function getObjectListKeys(objectList: any[], keepNull?: boolean): string[];
73
+ export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): string[];
77
74
  export {};
78
75
  //# 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.6",
3
+ "version": "0.1.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",