@asla/yoursql 0.5.0 → 0.5.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/dist/mod.js CHANGED
@@ -33,6 +33,53 @@ 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
+ function selectColumnsOrTable(columns) {
37
+ let sqlSelect;
38
+ let select;
39
+ if (columns instanceof Array) {
40
+ sqlSelect = columns;
41
+ select = columns;
42
+ }
43
+ else {
44
+ sqlSelect = [];
45
+ select = [];
46
+ let c;
47
+ for (const key of Object.keys(columns)) {
48
+ c = columns[key];
49
+ if (typeof c === "string" && c !== key) {
50
+ sqlSelect.push(key + " AS " + c);
51
+ select.push(c);
52
+ }
53
+ else if (c) {
54
+ sqlSelect.push(key);
55
+ select.push(key);
56
+ }
57
+ }
58
+ }
59
+ if (select.length === 0)
60
+ throw new Error("选择列为空");
61
+ return { columns: select, sqlColumns: sqlSelect.join(", ") };
62
+ }
63
+ function condition(conditions, type = "AND") {
64
+ if (typeof conditions === "function")
65
+ conditions = conditions();
66
+ if (!conditions)
67
+ return;
68
+ if (typeof conditions === "string")
69
+ return conditions;
70
+ else {
71
+ if (conditions.length) {
72
+ let sql = "";
73
+ type = " " + type + " ";
74
+ sql += conditions[0];
75
+ for (let i = 1; i < conditions.length; i++)
76
+ sql += type + conditions[i];
77
+ return sql;
78
+ }
79
+ return;
80
+ }
81
+ }
82
+
36
83
  /**
37
84
  * 获取对象数组中的 key 的集合
38
85
  * @public
@@ -58,82 +105,72 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
58
105
  /**
59
106
  * 生成 WHERE 语句
60
107
  * @public
108
+ * @example
61
109
  * ```ts
62
110
  *
63
111
  * ```
64
112
  */
65
- function where(condition, type = "AND") {
66
- if (!condition)
67
- return "";
68
- return genCondition(condition, "WHERE", type);
113
+ function where(conditions, type) {
114
+ const sql = condition(conditions, type);
115
+ if (sql)
116
+ return "\nWHERE " + sql;
117
+ return "";
69
118
  }
70
119
  /**
71
120
  *
72
121
  * 生成 HAVING 语句
73
122
  * @public
74
123
  */
75
- function having(condition, type = "AND") {
76
- if (!condition)
77
- return "";
78
- return genCondition(condition, "HAVING", type);
79
- }
80
- function genCondition(condition, statement, type = "AND") {
81
- if (typeof condition === "function")
82
- condition = condition();
83
- type = " " + type + " ";
84
- let sql = "";
85
- if (typeof condition === "string")
86
- sql += "\n" + statement + " " + condition;
87
- else {
88
- if (condition.length)
89
- sql += "\n" + statement + " " + condition[0];
90
- for (let i = 1; i < condition.length; i++)
91
- sql += type + condition[i];
92
- }
93
- return sql;
124
+ function having(conditions, type) {
125
+ const sql = condition(conditions, type);
126
+ if (sql)
127
+ return "\nHAVING " + sql;
128
+ return "";
94
129
  }
95
130
  /**
96
131
  * @public
132
+ * @example
97
133
  * ```ts
98
134
  * selectColumns({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
99
- * selectColumns(["c1", "count(*) AS c2", "column as c3"]) // "c1,count(*) AS c2,column as c3"
135
+ * selectColumns("c1,count(*) AS c2,column as c3") // "c1,count(*) AS c2,column as c3"
100
136
  * ```
101
137
  */
102
138
  function selectColumns(columns) {
103
- let sql = "";
104
- if (columns instanceof Array) {
105
- if (columns.length)
106
- sql += columns[0];
107
- else
108
- throw new Error("没有选择任何列");
109
- for (let i = 1; i < columns.length; i++)
110
- sql += "," + columns[i];
111
- }
112
- else {
113
- const keys = Object.keys(columns);
114
- if (keys.length === 0)
115
- throw new Error("没有选择任何列");
116
- let k = keys[0];
117
- let v = columns[k];
118
- if (typeof v === "string")
119
- sql += v + " AS " + k;
120
- else
121
- sql += k;
122
- for (let i = 1; i < keys.length; i++) {
123
- k = keys[i];
124
- v = columns[k];
125
- sql += ",";
139
+ if (typeof columns === "function")
140
+ columns = columns();
141
+ switch (typeof columns) {
142
+ case "string":
143
+ return columns;
144
+ case "object": {
145
+ let sql = "";
146
+ const keys = Object.keys(columns);
147
+ if (keys.length === 0)
148
+ throw new Error("没有选择任何列");
149
+ let k = keys[0];
150
+ let v = columns[k];
126
151
  if (typeof v === "string")
127
152
  sql += v + " AS " + k;
128
153
  else
129
154
  sql += k;
155
+ for (let i = 1; i < keys.length; i++) {
156
+ k = keys[i];
157
+ v = columns[k];
158
+ sql += ",";
159
+ if (typeof v === "string")
160
+ sql += v + " AS " + k;
161
+ else
162
+ sql += k;
163
+ }
164
+ return sql;
130
165
  }
166
+ default:
167
+ throw new TypeError("columns 应为 string 或 object 类型");
131
168
  }
132
- return sql;
133
169
  }
134
170
  /**
135
171
  * 生成 ORDER BY 语句, d
136
172
  * @public
173
+ * @example
137
174
  * ```ts
138
175
  * // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
139
176
  * orderBy("age DESC NULLS FIRST,num ASC");
@@ -152,9 +189,9 @@ function orderBy(by) {
152
189
  if (typeof by === "function")
153
190
  by = by();
154
191
  let sql = "";
192
+ if (!by)
193
+ return sql;
155
194
  if (typeof by === "string") {
156
- if (!by)
157
- return sql;
158
195
  sql += "\nORDER BY " + by;
159
196
  }
160
197
  else if (by instanceof Array) {
@@ -168,14 +205,16 @@ function orderBy(by) {
168
205
  let keys = Object.keys(by);
169
206
  if (keys.length) {
170
207
  let key = keys[0];
171
- sql += "\nORDER BY " + key + " " + by[key];
208
+ let value = by[key];
209
+ sql += "\nORDER BY " + key + " " + (typeof value === "string" ? value : value ? "ASC" : "DESC");
172
210
  for (let i = 1; i < keys.length; i++) {
173
211
  key = keys[i];
212
+ value = by[key];
174
213
  sql += "," + key + " ";
175
- if (typeof by[key] === "string")
176
- sql += by[key];
214
+ if (typeof value === "string")
215
+ sql += value;
177
216
  else
178
- sql += by[key] ? "ASC" : "DESC";
217
+ sql += value ? "ASC" : "DESC";
179
218
  }
180
219
  }
181
220
  }
@@ -610,55 +649,24 @@ class Selection {
610
649
  return new _a(__classPrivateFieldGet(this, _Selection_sql, "f") + "," + fromAs(selectable, as));
611
650
  }
612
651
  select(columnsIn) {
613
- let sql = "SELECT ";
614
652
  let columns = [];
615
- if (typeof columnsIn === "string")
616
- sql += columnsIn;
617
- else {
618
- sql += selectColumns(columnsIn);
619
- if (columnsIn instanceof Array) ;
620
- else
621
- columns = Object.keys(columnsIn);
622
- }
653
+ if (typeof columnsIn === "function")
654
+ columnsIn = columnsIn();
655
+ if (typeof columnsIn === "object")
656
+ columns = Object.keys(columnsIn);
657
+ let sql = "SELECT " + selectColumns(columnsIn);
623
658
  sql += "\n" + this.toString();
624
659
  return new AfterSelectImpl(sql, columns);
625
660
  }
626
661
  }
627
662
  _a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakSet(), _Selection_join = function _Selection_join(type, selectable, as, on) {
628
663
  let sql = __classPrivateFieldGet(this, _Selection_sql, "f") + "\n" + type + " " + fromAs(selectable, as);
629
- if (on)
630
- sql += " ON " + on;
664
+ if (on) {
665
+ sql += " ON " + condition(on);
666
+ }
631
667
  return new _a(sql);
632
668
  };
633
669
 
634
- function selectColumnsOrTable(columns) {
635
- let sqlSelect;
636
- let select;
637
- if (columns instanceof Array) {
638
- sqlSelect = columns;
639
- select = columns;
640
- }
641
- else {
642
- sqlSelect = [];
643
- select = [];
644
- let c;
645
- for (const key of Object.keys(columns)) {
646
- c = columns[key];
647
- if (typeof c === "string" && c !== key) {
648
- sqlSelect.push(key + " AS " + c);
649
- select.push(c);
650
- }
651
- else if (c) {
652
- sqlSelect.push(key);
653
- select.push(key);
654
- }
655
- }
656
- }
657
- if (select.length === 0)
658
- throw new Error("选择列为空");
659
- return { columns: select, sqlColumns: sqlSelect.join(", ") };
660
- }
661
-
662
670
  /** @public */
663
671
  class DbTableQuery extends DbTable {
664
672
  constructor(name, columns, statement) {
@@ -668,6 +676,9 @@ class DbTableQuery extends DbTable {
668
676
  fromAs(as) {
669
677
  return new Selection(this, as);
670
678
  }
679
+ select(columns, as) {
680
+ return this.fromAs(as).select(columns);
681
+ }
671
682
  insert(values, option) {
672
683
  let insertCol;
673
684
  let valuesStr;
@@ -1,13 +1,30 @@
1
1
  import { SqlValuesCreator, SqlRaw } from "../sql_value/sql_value.ts";
2
2
  import { ColumnsSelected, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
3
- import { Selection } from "./select.ts";
3
+ import { CurrentWhere, Selection } from "./select.ts";
4
4
  import { DbTable, SqlQueryStatement } from "./selectable.ts";
5
- import { WhereParam } from "../util.ts";
5
+ import { ConditionParam } from "../util.ts";
6
6
  /** @public */
7
7
  export declare class DbTableQuery<T extends TableType = Record<string, any>, C extends TableType = Partial<T>> extends DbTable<T> {
8
8
  private statement;
9
9
  constructor(name: string, columns: readonly string[], statement: SqlValuesCreator);
10
10
  fromAs(as?: string): Selection;
11
+ /** 选择单表全部列 */
12
+ select(columns: "*", as?: string): CurrentWhere<T>;
13
+ /**
14
+ * 选择单表
15
+ * @param columns - 对象选择
16
+ */
17
+ select<R extends {
18
+ [key in keyof T]?: string | boolean;
19
+ } | Record<string, string>>(columns: R | (() => R), as?: string): CurrentWhere<{
20
+ [key in keyof R]: R[key] extends boolean ? key extends keyof T ? T[key] : unknown : R[key] extends keyof T ? T[R[key]] : unknown;
21
+ }>;
22
+ /** 选择单表- 所有类型 */
23
+ select<R extends {}>(columns: string | {
24
+ [key in keyof R]?: key extends keyof T ? string | boolean : string;
25
+ } | (() => string | {
26
+ [key in keyof R]?: key extends keyof T ? string | boolean : string;
27
+ }), as?: string): CurrentWhere<R>;
11
28
  insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
12
29
  insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
13
30
  update(values: UpdateRowValue<T>, option?: UpdateOption): string;
@@ -21,14 +38,14 @@ export interface InsertOption<T extends object> {
21
38
  updateValues?: {
22
39
  [key in keyof T]?: undefined | SqlRaw | T[key];
23
40
  };
24
- where?: WhereParam;
41
+ where?: ConditionParam;
25
42
  }
26
43
  /** @public */
27
44
  export interface UpdateOption {
28
- where?: WhereParam;
45
+ where?: ConditionParam;
29
46
  }
30
47
  /** @public */
31
48
  export interface DeleteOption {
32
- where?: WhereParam;
49
+ where?: ConditionParam;
33
50
  }
34
51
  //# sourceMappingURL=TableQuery.d.ts.map
@@ -3,4 +3,10 @@ export declare function selectColumnsOrTable(columns: ColumnsSelectAs<any> | str
3
3
  columns: string[];
4
4
  sqlColumns: string;
5
5
  };
6
+ type ConditionParam = string | string[];
7
+ /**
8
+ * 生成条件语句
9
+ */
10
+ export declare function condition(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
11
+ export {};
6
12
  //# sourceMappingURL=_statement.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
2
- import { OrderByParam, WhereParam } from "../util.ts";
2
+ import { OrderByParam, ConditionParam, SelectParam } from "../util.ts";
3
3
  import type { TableType } from "./type.ts";
4
4
  /** @public */
5
5
  export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T> {
@@ -7,11 +7,11 @@ export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T>
7
7
  }
8
8
  /** @public */
9
9
  export interface CurrentOrderBy<T extends TableType> extends CurrentLimit<T> {
10
- orderBy(param: OrderByParam): CurrentLimit<T>;
10
+ orderBy(param: OrderByParam | (() => OrderByParam | void)): CurrentLimit<T>;
11
11
  }
12
12
  /** @public */
13
13
  export interface CurrentHaving<T extends TableType> extends CurrentOrderBy<T> {
14
- having(param: WhereParam | (() => WhereParam)): CurrentLimit<T>;
14
+ having(param: ConditionParam | (() => ConditionParam | void)): CurrentLimit<T>;
15
15
  }
16
16
  /** @public */
17
17
  export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
@@ -19,7 +19,7 @@ export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
19
19
  }
20
20
  /** @public */
21
21
  export interface CurrentWhere<T extends TableType> extends CurrentGroupBy<T> {
22
- where(param: WhereParam | (() => WhereParam)): CurrentGroupBy<T>;
22
+ where(param: ConditionParam | (() => ConditionParam | void)): CurrentGroupBy<T>;
23
23
  }
24
24
  /** @public */
25
25
  export declare class Selection {
@@ -27,17 +27,35 @@ export declare class Selection {
27
27
  static from(selectable: SqlSelectable<any> | string, as?: string): Selection;
28
28
  constructor(selectable: SqlSelectable<any> | string, as?: string);
29
29
  toString(): string;
30
- fullJoin(selectable: SqlSelectable<any>, as: string | undefined, on: string): Selection;
31
- innerJoin(selectable: SqlSelectable<any>, as: string | undefined, on: string): Selection;
32
- leftJoin(selectable: SqlSelectable<any>, as: string | undefined, on: string): Selection;
33
- rightJoin(selectable: SqlSelectable<any>, as: string | undefined, on: string): Selection;
30
+ fullJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
31
+ innerJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
32
+ leftJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
33
+ rightJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
34
34
  naturalJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
35
35
  crossJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
36
36
  from(selectable: SqlSelectable<any> | string, as?: string): Selection;
37
- select<T extends TableType = TableType>(columns: "*" | string[]): CurrentWhere<T>;
38
- select<T extends TableType = TableType>(columns: {
37
+ /** 选择全部列 */
38
+ select<T extends TableType = TableType>(columns: "*"): CurrentWhere<T>;
39
+ /**
40
+ * 自定义SQL选择语句
41
+ * @example
42
+ * ```ts
43
+ * selection.select("t.age, count(*) AS c") // SELECT t.age,count(*) AS c FROM ...
44
+ * ```
45
+ */
46
+ select<T extends TableType = TableType>(columns: string): CurrentWhere<T>;
47
+ /**
48
+ * 通过 object 选择 列
49
+ * @example
50
+ * ```ts
51
+ * selection.select({"age":true, c:"count(*)"}) // SELECT age,count(*) AS c FROM ...
52
+ * ```
53
+ */
54
+ select<T extends TableType>(columns: {
39
55
  [key in keyof T]: string | boolean;
40
- }): CurrentWhere<T>;
41
- select<T extends TableType = TableType>(columns: "*" | string[] | TableType): CurrentWhere<T>;
56
+ } | (() => {
57
+ [key in keyof T]: string | boolean;
58
+ })): CurrentWhere<T>;
59
+ select(columns: SelectParam | (() => SelectParam)): CurrentWhere<TableType>;
42
60
  }
43
61
  //# sourceMappingURL=select.d.ts.map
package/dist/util.d.ts CHANGED
@@ -6,29 +6,33 @@ import { OrderValue } from "./select/type.ts";
6
6
  */
7
7
  export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): Set<string>;
8
8
  /** @public */
9
- export type WhereParam = string | string[];
9
+ export type ConditionParam = string | string[];
10
10
  /**
11
11
  * 生成 WHERE 语句
12
12
  * @public
13
+ * @example
13
14
  * ```ts
14
15
  *
15
16
  * ```
16
17
  */
17
- export declare function where(condition?: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
18
+ export declare function where(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
18
19
  /**
19
20
  *
20
21
  * 生成 HAVING 语句
21
22
  * @public
22
23
  */
23
- export declare function having(condition?: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
24
+ export declare function having(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
25
+ /** @public */
26
+ export type SelectParam = string | Record<string, string | boolean>;
24
27
  /**
25
28
  * @public
29
+ * @example
26
30
  * ```ts
27
31
  * selectColumns({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
28
- * selectColumns(["c1", "count(*) AS c2", "column as c3"]) // "c1,count(*) AS c2,column as c3"
32
+ * selectColumns("c1,count(*) AS c2,column as c3") // "c1,count(*) AS c2,column as c3"
29
33
  * ```
30
34
  */
31
- export declare function selectColumns(columns: string[] | Record<string, string | boolean>): string;
35
+ export declare function selectColumns(columns: SelectParam | (() => SelectParam)): string;
32
36
  /** @public */
33
37
  export type OrderBehavior = {
34
38
  key: string;
@@ -40,6 +44,7 @@ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string,
40
44
  /**
41
45
  * 生成 ORDER BY 语句, d
42
46
  * @public
47
+ * @example
43
48
  * ```ts
44
49
  * // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
45
50
  * orderBy("age DESC NULLS FIRST,num ASC");
@@ -54,5 +59,5 @@ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string,
54
59
  * orderBy({}) // ""
55
60
  * ```
56
61
  */
57
- export declare function orderBy(by: OrderByParam | (() => OrderByParam)): string;
62
+ export declare function orderBy(by?: OrderByParam | void | (() => OrderByParam | void)): string;
58
63
  //# sourceMappingURL=util.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",