@asla/yoursql 0.6.0 → 0.6.2
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 +65 -5
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +41 -29
- package/dist/sql_value/sql_value.d.ts +25 -18
- package/package.json +1 -1
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
|
|
15
|
-
[size-url]: https://packagephobia.com/result?p
|
|
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
|
|
287
|
+
var _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,42 @@ class SqlValuesCreator {
|
|
|
320
312
|
/**
|
|
321
313
|
* @param map - 自定义对象转换
|
|
322
314
|
*/
|
|
323
|
-
constructor(map
|
|
324
|
-
this.
|
|
315
|
+
constructor(map) {
|
|
316
|
+
this._map = new Map(map);
|
|
325
317
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
318
|
+
setTransformer(type_map, encoder) {
|
|
319
|
+
if (typeof type_map === "function") {
|
|
320
|
+
if (encoder)
|
|
321
|
+
this._map.set(type_map, encoder);
|
|
322
|
+
else
|
|
323
|
+
this._map.delete(type_map);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
for (const [type, encoder] of type_map) {
|
|
327
|
+
if (typeof type === "function" && typeof encoder === "function") {
|
|
328
|
+
this._map.set(type, encoder);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
332
|
}
|
|
333
|
-
/**
|
|
334
|
-
|
|
333
|
+
/**
|
|
334
|
+
* 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT
|
|
335
|
+
* ```ts
|
|
336
|
+
* const v=SqlValuesCreator.create()
|
|
337
|
+
* v() 和 v.toSqlStr() 是等价的
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
toSqlStr(value, assertType) {
|
|
335
341
|
let basicType;
|
|
336
|
-
if (
|
|
337
|
-
if (typeof
|
|
338
|
-
let type = this.
|
|
342
|
+
if (assertType) {
|
|
343
|
+
if (typeof assertType === "function") {
|
|
344
|
+
let type = this._map.get(assertType);
|
|
339
345
|
if (!type)
|
|
340
346
|
throw new Error("类型不存在");
|
|
341
347
|
return type.call(this, value);
|
|
342
348
|
}
|
|
343
349
|
else {
|
|
344
|
-
basicType =
|
|
350
|
+
basicType = assertType;
|
|
345
351
|
}
|
|
346
352
|
}
|
|
347
353
|
else
|
|
@@ -358,7 +364,7 @@ class SqlValuesCreator {
|
|
|
358
364
|
case "object": {
|
|
359
365
|
if (value === null)
|
|
360
366
|
return "NULL";
|
|
361
|
-
if (value instanceof
|
|
367
|
+
if (value instanceof String)
|
|
362
368
|
return value.toString();
|
|
363
369
|
return this.getObjectType(value).call(this, value);
|
|
364
370
|
}
|
|
@@ -372,9 +378,9 @@ class SqlValuesCreator {
|
|
|
372
378
|
}
|
|
373
379
|
/** 获取值对应的 SqlValueEncoder */
|
|
374
380
|
getObjectType(value) {
|
|
375
|
-
for (const Class of this.
|
|
381
|
+
for (const Class of this._map.keys()) {
|
|
376
382
|
if (value instanceof Class)
|
|
377
|
-
return this.
|
|
383
|
+
return this._map.get(Class);
|
|
378
384
|
}
|
|
379
385
|
return this.defaultObject;
|
|
380
386
|
}
|
|
@@ -947,4 +953,10 @@ class YourTable extends DbTableQuery {
|
|
|
947
953
|
}
|
|
948
954
|
}
|
|
949
955
|
|
|
950
|
-
|
|
956
|
+
/**
|
|
957
|
+
* 默认的 SqlValuesCreator 实列
|
|
958
|
+
* @public
|
|
959
|
+
*/
|
|
960
|
+
const v = SqlValuesCreator.create(pgSqlTransformer);
|
|
961
|
+
|
|
962
|
+
export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, getObjectListKeys, having, orderBy, pgSqlTransformer, selectColumns, v, where };
|
|
@@ -1,33 +1,33 @@
|
|
|
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
|
|
22
|
-
/**
|
|
23
|
-
|
|
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):
|
|
30
|
+
static create(map?: JsObjectMapSql): SqlValueFn;
|
|
31
31
|
/**
|
|
32
32
|
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
33
33
|
* @example 输入 a'b'c , 返回 a''b''c.
|
|
@@ -38,15 +38,22 @@ export declare class SqlValuesCreator {
|
|
|
38
38
|
*/
|
|
39
39
|
constructor(map?: JsObjectMapSql);
|
|
40
40
|
/** 设置转换器 */
|
|
41
|
-
setTransformer
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
setTransformer(type: new (...args: any[]) => any, encoder?: SqlValueEncoder): void;
|
|
42
|
+
setTransformer(map: JsObjectMapSql): void;
|
|
43
|
+
private readonly _map;
|
|
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
|
*/
|