@asla/yoursql 0.6.0 → 0.6.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/README.md CHANGED
@@ -5,15 +5,75 @@
5
5
 
6
6
  [package]: https://img.shields.io/badge/package-ESM-ffe536.svg
7
7
  [package-url]: https://nodejs.org/api/esm.html
8
- [npm]: https://img.shields.io/npm/v/yoursql.svg
9
- [npm-url]: https://npmjs.com/package/yoursql
8
+ [npm]: https://img.shields.io/npm/v/@asla/yoursql.svg
9
+ [npm-url]: https://npmjs.com/package/@asla/yoursql
10
10
  [jsr]: https://jsr.io/badges/@asla/yoursql
11
11
  [jsr-url]: https://jsr.io/@asla/yoursql
12
- [node]: https://img.shields.io/node/v/yoursql.svg
12
+ [node]: https://img.shields.io/node/v/@asla/yoursql.svg
13
13
  [node-url]: https://nodejs.org
14
- [size]: https://packagephobia.com/badge?p=yoursql
15
- [size-url]: https://packagephobia.com/result?p=yoursql
14
+ [size]: https://packagephobia.com/badge?p=@asla%2Fyoursql
15
+ [size-url]: https://packagephobia.com/result?p=@asla%2Fyoursql
16
16
 
17
17
  SQL 生成器
18
18
 
19
19
  [API 文档](https://jsr.io/@asla/yoursql/doc)
20
+
21
+ ### 安全转将 JS 值转换为 SQL 值,避免 SQL 注入
22
+
23
+ 导入
24
+
25
+ ```ts
26
+ import { v } from "@asla/yoursql";
27
+ ```
28
+
29
+ 默认情况下,支持 PostgresSQL, 因为不同数据库的值转换有些差异,如果使用其他数据库,可能需要配置对象转换器
30
+
31
+ ```ts
32
+ v(1); // "1"
33
+ v(1n); // "1"
34
+ v("te'xt"); // "'te''xt'"
35
+ v(new Date()); // "'2024-11-30T05:08:33.112Z'"
36
+ v([1, 2, 3]); // "ARRAY[1,2,3]"
37
+ v({ id: "abc", size: 1 }); // "'{\"id\":\"abc\",\"size\":1}'"
38
+ v(null); // "NULL"
39
+ v(undefined); // "DEFAULT"
40
+ ```
41
+
42
+ 如果传入 String 对象,将保留其字符串值,不会进行任何转换
43
+
44
+ ```ts
45
+ v(new String("1+1")); // "1+1"
46
+ ```
47
+
48
+ 你可以自定义对象到字符串的转换, 例如,你想将 Set 转换为 PostgresSql 的 ARRAY[] 输入格式
49
+
50
+ ```ts
51
+ v.setTransformer(Set, function (value: Set) {
52
+ return this.v(Array.from(value));
53
+ });
54
+ ```
55
+
56
+ 转换对象数组
57
+
58
+ ```ts
59
+ const values = [{ a: 1, b: 2 }, { c: 3 }];
60
+
61
+ // 这将自动选择数组中所有键的并集
62
+ v.objectListToValuesList(values); // "(1,2,null),(null,null,3)"
63
+
64
+ // 或者你可以指定选择键并指定顺序
65
+ v.objectListToValuesList(values, ["c", "b"]); // "(null,2),(3,3)"
66
+ ```
67
+
68
+ ### 生成 SQL 语句
69
+
70
+ ```ts
71
+ import { Selection, v } from "@asla/yoursql";
72
+
73
+ const searchName = "Bob";
74
+ const s = Selection.from("user", "u")
75
+ .innerJoin("role", "r", "u.id=r.user_id")
76
+ .select({ uid: "u.id", rid: "r.id", example: "u.id||r.id" }) // SELECT u.id AS uid, r.id AS rid u.id||u.id AS example
77
+ .where(`u.name LIKE %${v(searchName)}%`)
78
+ .toString();
79
+ ```
package/dist/mod.d.ts CHANGED
@@ -6,4 +6,10 @@ export * from "./select/selectable.ts";
6
6
  export * from "./select/TableQuery.ts";
7
7
  export * from "./util.ts";
8
8
  export * from "./your_table/mod.ts";
9
+ import { SqlValueFn } from "./sql_value/sql_value.ts";
10
+ /**
11
+ * 默认的 SqlValuesCreator 实列
12
+ * @public
13
+ */
14
+ export declare const v: SqlValueFn;
9
15
  //# sourceMappingURL=mod.d.ts.map
package/dist/mod.js CHANGED
@@ -284,21 +284,13 @@ class SqlQueryStatement extends SqlSelectable {
284
284
  }
285
285
  _SqlQueryStatement_sql = new WeakMap();
286
286
 
287
- var _SqlRaw_value, _YourValuesAs_asName, _YourValuesAs_valuesStr, _YourValuesAs_sql;
287
+ var _SqlValuesCreator_map, _YourValuesAs_asName, _YourValuesAs_valuesStr, _YourValuesAs_sql;
288
288
  /**
289
- * SQL 原始字符对象
289
+ * SQL 原始字符类。可以使用 String 类代替,这只是为了推断类型
290
290
  * @public
291
291
  */
292
- class SqlRaw {
293
- constructor(value) {
294
- _SqlRaw_value.set(this, void 0);
295
- __classPrivateFieldSet(this, _SqlRaw_value, value, "f");
296
- }
297
- toString() {
298
- return __classPrivateFieldGet(this, _SqlRaw_value, "f");
299
- }
292
+ class SqlRaw extends String {
300
293
  }
301
- _SqlRaw_value = new WeakMap();
302
294
  /**
303
295
  * SQL value 生成器
304
296
  * @public
@@ -320,28 +312,43 @@ class SqlValuesCreator {
320
312
  /**
321
313
  * @param map - 自定义对象转换
322
314
  */
323
- constructor(map = new Map()) {
324
- this.map = map;
315
+ constructor(map) {
316
+ _SqlValuesCreator_map.set(this, void 0);
317
+ __classPrivateFieldSet(this, _SqlValuesCreator_map, new Map(map), "f");
325
318
  }
326
- /** 设置转换器 */
327
- setTransformer(type, transformer) {
328
- if (!transformer)
329
- this.map.delete(type);
330
- else
331
- this.map.set(type, transformer);
319
+ setTransformer(type_map, encoder) {
320
+ if (typeof type_map === "function") {
321
+ if (encoder)
322
+ __classPrivateFieldGet(this, _SqlValuesCreator_map, "f").set(type_map, encoder);
323
+ else
324
+ __classPrivateFieldGet(this, _SqlValuesCreator_map, "f").delete(type_map);
325
+ }
326
+ else {
327
+ for (const [type, encoder] of type_map) {
328
+ if (typeof type === "function" && typeof encoder === "function") {
329
+ __classPrivateFieldGet(this, _SqlValuesCreator_map, "f").set(type, encoder);
330
+ }
331
+ }
332
+ }
332
333
  }
333
- /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
334
- toSqlStr(value, expectType) {
334
+ /**
335
+ * JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT
336
+ * ```ts
337
+ * const v=SqlValuesCreator.create()
338
+ * v() 和 v.toSqlStr() 是等价的
339
+ * ```
340
+ */
341
+ toSqlStr(value, assertType) {
335
342
  let basicType;
336
- if (expectType) {
337
- if (typeof expectType === "function") {
338
- let type = this.map.get(expectType);
343
+ if (assertType) {
344
+ if (typeof assertType === "function") {
345
+ let type = __classPrivateFieldGet(this, _SqlValuesCreator_map, "f").get(assertType);
339
346
  if (!type)
340
347
  throw new Error("类型不存在");
341
348
  return type.call(this, value);
342
349
  }
343
350
  else {
344
- basicType = expectType;
351
+ basicType = assertType;
345
352
  }
346
353
  }
347
354
  else
@@ -358,7 +365,7 @@ class SqlValuesCreator {
358
365
  case "object": {
359
366
  if (value === null)
360
367
  return "NULL";
361
- if (value instanceof SqlRaw)
368
+ if (value instanceof String)
362
369
  return value.toString();
363
370
  return this.getObjectType(value).call(this, value);
364
371
  }
@@ -372,9 +379,9 @@ class SqlValuesCreator {
372
379
  }
373
380
  /** 获取值对应的 SqlValueEncoder */
374
381
  getObjectType(value) {
375
- for (const Class of this.map.keys()) {
382
+ for (const Class of __classPrivateFieldGet(this, _SqlValuesCreator_map, "f").keys()) {
376
383
  if (value instanceof Class)
377
- return this.map.get(Class);
384
+ return __classPrivateFieldGet(this, _SqlValuesCreator_map, "f").get(Class);
378
385
  }
379
386
  return this.defaultObject;
380
387
  }
@@ -492,6 +499,7 @@ class SqlValuesCreator {
492
499
  return new YourValuesAs(insertKeys, asName, valuesStr.join(",\n"));
493
500
  }
494
501
  }
502
+ _SqlValuesCreator_map = new WeakMap();
495
503
  class YourValuesAs extends SqlSelectable {
496
504
  constructor(columns, asName, valuesStr) {
497
505
  super();
@@ -947,4 +955,10 @@ class YourTable extends DbTableQuery {
947
955
  }
948
956
  }
949
957
 
950
- export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, getObjectListKeys, having, orderBy, pgSqlTransformer, selectColumns, where };
958
+ /**
959
+ * 默认的 SqlValuesCreator 实列
960
+ * @public
961
+ */
962
+ const v = SqlValuesCreator.create(pgSqlTransformer);
963
+
964
+ export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, getObjectListKeys, having, orderBy, pgSqlTransformer, selectColumns, v, where };
@@ -1,33 +1,34 @@
1
1
  import { SqlSelectable } from "../select/selectable.ts";
2
2
  declare const SQL_RAW: unique symbol;
3
3
  /**
4
- * SQL 原始字符对象
4
+ * SQL 原始字符类。可以使用 String 类代替,这只是为了推断类型
5
5
  * @public
6
6
  */
7
- export declare class SqlRaw<T = any> {
8
- #private;
9
- constructor(value: string);
10
- toString(): string;
7
+ export declare class SqlRaw<T = any> extends String {
11
8
  /** 保留以推断类型 */
12
9
  protected [SQL_RAW]: T;
13
10
  }
14
- /** @public */
11
+ /** @public js 对象到编码函数的映射*/
15
12
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
16
- /** @public */
13
+ /** @public 将 js 值转为 SQl 字符串的函数*/
17
14
  export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T) => string;
18
- /** @public */
15
+ /** @public 断言类型 */
19
16
  export type ManualType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
20
17
  /** @public */
21
- export interface SqlValueFn {
22
- /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
23
- (value: any, expectType?: ManualType): string;
24
- }
18
+ export type SqlValueFn = SqlValuesCreator & {
19
+ /**
20
+ * 安全将 JS 对象转为 SQL 的字符值的形式,可避免 SQL 注入。
21
+ * undefined 将被转换为 DEFAULT
22
+ */
23
+ (value: any, assertType?: ManualType): string;
24
+ };
25
25
  /**
26
26
  * SQL value 生成器
27
27
  * @public
28
28
  */
29
29
  export declare class SqlValuesCreator {
30
- static create(map?: JsObjectMapSql): SqlValuesCreator & SqlValueFn;
30
+ #private;
31
+ static create(map?: JsObjectMapSql): SqlValueFn;
31
32
  /**
32
33
  * 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
33
34
  * @example 输入 a'b'c , 返回 a''b''c.
@@ -38,15 +39,21 @@ export declare class SqlValuesCreator {
38
39
  */
39
40
  constructor(map?: JsObjectMapSql);
40
41
  /** 设置转换器 */
41
- setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
42
- private readonly map;
43
- /** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
44
- toSqlStr(value: any, expectType?: ManualType): string;
42
+ setTransformer(type: new (...args: any[]) => any, encoder?: SqlValueEncoder): void;
43
+ setTransformer(map: JsObjectMapSql): void;
44
+ /**
45
+ * JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT
46
+ * ```ts
47
+ * const v=SqlValuesCreator.create()
48
+ * v() 和 v.toSqlStr() 是等价的
49
+ * ```
50
+ */
51
+ toSqlStr(value: any, assertType?: ManualType): string;
45
52
  /** 获取值对应的 SqlValueEncoder */
46
53
  getObjectType(value: object): SqlValueEncoder;
47
54
  protected defaultObject(value: object): string;
48
55
  /**
49
- * 将对象列表转为 SQL 的 VALUES
56
+ * 将对象列表转为 SQL 的 VALUES
50
57
  * @example 返回示例: " (...),(...) "
51
58
  * @param keys - 选择的键。如果指定了 keys, 值为 undefined 的属性将自动填充为 null; 如果未指定 keys,将选择 objectList 所有不是 undefined 项的键的并集
52
59
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",