@asla/yoursql 0.4.0 → 0.5.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
@@ -34,6 +34,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
34
34
  };
35
35
 
36
36
  /**
37
+ * 获取对象数组中的 key 的集合
37
38
  * @public
38
39
  * @param keepUndefinedKey - 是否保留值为 undefined 的 key
39
40
  */
@@ -52,24 +53,53 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
52
53
  keys.add(k);
53
54
  }
54
55
  }
55
- return Array.from(keys);
56
+ return keys;
56
57
  }
57
- /** @public */
58
- function genWhere(where, type = "AND") {
59
- return genWhereHaving(where, "WHERE", type);
58
+ /**
59
+ * 生成 WHERE 语句
60
+ * @public
61
+ * ```ts
62
+ *
63
+ * ```
64
+ */
65
+ function where(condition, type = "AND") {
66
+ if (!condition)
67
+ return "";
68
+ return genCondition(condition, "WHERE", type);
60
69
  }
61
- /** @public */
62
- function genHaving(having, type = "AND") {
63
- return genWhereHaving(having, "HAVING", type);
70
+ /**
71
+ *
72
+ * 生成 HAVING 语句
73
+ * @public
74
+ */
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;
64
94
  }
65
95
  /**
66
96
  * @public
67
97
  * ```ts
68
- * genSelect({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
69
- * genSelect(["c1", "count(*) AS c2", "column as c3"]) // "c1,count(*) AS c2,column as c3"
98
+ * 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"
70
100
  * ```
71
101
  */
72
- function genSelect(columns) {
102
+ function selectColumns(columns) {
73
103
  let sql = "";
74
104
  if (columns instanceof Array) {
75
105
  if (columns.length)
@@ -101,41 +131,65 @@ function genSelect(columns) {
101
131
  }
102
132
  return sql;
103
133
  }
104
- /** @public */
105
- function genOderBy(orderBy) {
106
- if (typeof orderBy === "function")
107
- orderBy = orderBy();
134
+ /**
135
+ * 生成 ORDER BY 语句, d
136
+ * @public
137
+ * ```ts
138
+ * // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
139
+ * orderBy("age DESC NULLS FIRST,num ASC");
140
+ * orderBy(["age DESC NULLS FIRST", "num ASC"]);
141
+ * orderBy([
142
+ * { key: "age", asc: false, nullLast: false },
143
+ * { key: "num", asc: true },
144
+ * ]);
145
+ * orderBy({ age: "DESC NULLS FIRST", num: true });
146
+ *
147
+ * orderBy([]) // ""
148
+ * orderBy({}) // ""
149
+ * ```
150
+ */
151
+ function orderBy(by) {
152
+ if (typeof by === "function")
153
+ by = by();
108
154
  let sql = "";
109
- if (typeof orderBy === "string")
110
- sql += "\nORDER BY " + orderBy;
111
- else if (orderBy instanceof Array) {
112
- if (orderBy.length)
113
- sql += "\nORDER BY " + orderBy.join(",");
155
+ if (typeof by === "string") {
156
+ if (!by)
157
+ return sql;
158
+ sql += "\nORDER BY " + by;
159
+ }
160
+ else if (by instanceof Array) {
161
+ if (by.length) {
162
+ sql += "\nORDER BY " + handlerOrderValue(by[0]);
163
+ for (let i = 1; i < by.length; i++)
164
+ sql += "," + handlerOrderValue(by[i]);
165
+ }
114
166
  }
115
167
  else {
116
- let keys = Object.keys(orderBy);
117
- if (keys.length)
118
- sql += "\nORDER BY " + keys[0] + " " + orderBy[keys[0]];
119
- for (let i = 1; i < keys.length; i++) {
120
- sql += "," + orderBy[keys[i]] + " " + orderBy[keys[i]];
168
+ let keys = Object.keys(by);
169
+ if (keys.length) {
170
+ let key = keys[0];
171
+ sql += "\nORDER BY " + key + " " + by[key];
172
+ for (let i = 1; i < keys.length; i++) {
173
+ key = keys[i];
174
+ sql += "," + key + " ";
175
+ if (typeof by[key] === "string")
176
+ sql += by[key];
177
+ else
178
+ sql += by[key] ? "ASC" : "DESC";
179
+ }
121
180
  }
122
181
  }
123
182
  return sql;
124
183
  }
125
- function genWhereHaving(where, statement, type = "AND") {
126
- if (typeof where === "function")
127
- where = where();
128
- type = " " + type + " ";
129
- let sql = "";
130
- if (typeof where === "string")
131
- sql += "\n" + statement + " " + where;
184
+ function handlerOrderValue(value) {
185
+ if (typeof value === "string")
186
+ return value;
132
187
  else {
133
- if (where.length)
134
- sql += "\n" + statement + " " + +where[0];
135
- for (let i = 1; i < where.length; i++)
136
- sql += type + where[i];
188
+ let str = value.key + " " + (value.asc ? "ASC" : "DESC");
189
+ if (value.nullLast !== undefined)
190
+ str += value.nullLast ? " NULLS LAST" : " NULLS FIRST";
191
+ return str;
137
192
  }
138
- return sql;
139
193
  }
140
194
 
141
195
  var _SqlQueryStatement_sql;
@@ -319,7 +373,7 @@ class SqlValuesCreator {
319
373
  throw new Error("objectList 不能是空数组");
320
374
  let keys;
321
375
  if (!keys_types) {
322
- keys = getObjectListKeys(objectList, keepUndefinedKey);
376
+ keys = Array.from(getObjectListKeys(objectList, keepUndefinedKey));
323
377
  }
324
378
  else if (keys_types instanceof Array) {
325
379
  keys = keys_types;
@@ -484,61 +538,76 @@ const pgSqlTransformer = new Map([
484
538
  [Date, (value) => SqlValuesCreator.string(value.toISOString())],
485
539
  ]);
486
540
 
487
- var _SelectImpl_instances, _a, _SelectImpl_join;
488
- class FinalSelectImpl extends SqlQueryStatement {
489
- constructor(statement) {
490
- super(statement);
541
+ var _Selection_instances, _a, _Selection_sql, _Selection_join;
542
+ class AfterSelectImpl extends SqlQueryStatement {
543
+ constructor(sql, columns) {
544
+ super(sql, columns);
545
+ }
546
+ where(param) {
547
+ return new AfterSelectImpl(this.toString() + where(param), this.columns);
548
+ }
549
+ groupBy(columns) {
550
+ let sql = this.toString();
551
+ if (typeof columns === "string")
552
+ sql += " GROUP BY " + columns;
553
+ else
554
+ sql += " GROUP BY " + columns.join(",");
555
+ return new AfterSelectImpl(sql, this.columns);
556
+ }
557
+ having(param) {
558
+ return new AfterSelectImpl(this.toString() + having(param), this.columns);
491
559
  }
492
- filter(option) {
493
- let { orderBy } = option;
560
+ orderBy(param) {
561
+ return new AfterSelectImpl(this.toString() + orderBy(param), this.columns);
562
+ }
563
+ limit(limit, offset) {
494
564
  let sql = this.toString();
495
- if (orderBy)
496
- sql += genOderBy(orderBy);
497
- if (option.limit)
498
- sql += "\nLIMIT " + option.limit;
499
- if (option.offset)
500
- sql += "\nOFFSET " + option.offset;
565
+ if (limit)
566
+ sql += "\nLIMIT " + limit;
567
+ if (offset)
568
+ sql += "\nOFFSET " + offset;
501
569
  return new SqlQueryStatement(sql, Array.from(this.columns));
502
570
  }
503
571
  }
504
- /** @public */
505
- function createSelect(selectable, as) {
506
- return new SelectImpl("FROM " + fromAs(selectable, as));
507
- }
508
572
  function fromAs(selectable, as) {
509
573
  let sql = typeof selectable === "string" ? selectable : selectable.toSelect();
510
574
  if (as)
511
575
  sql += " AS " + as;
512
576
  return sql;
513
577
  }
514
- class SelectImpl {
515
- constructor(str) {
516
- _SelectImpl_instances.add(this);
517
- this.str = str;
578
+ /** @public */
579
+ class Selection {
580
+ static from(selectable, as) {
581
+ return new this(selectable, as);
582
+ }
583
+ constructor(selectable, as) {
584
+ _Selection_instances.add(this);
585
+ _Selection_sql.set(this, void 0);
586
+ __classPrivateFieldSet(this, _Selection_sql, fromAs(selectable, as), "f");
518
587
  }
519
588
  toString() {
520
- return this.str;
589
+ return "FROM " + __classPrivateFieldGet(this, _Selection_sql, "f");
521
590
  }
522
591
  fullJoin(selectable, as, on) {
523
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "FULL JOIN", selectable, as, on);
592
+ return __classPrivateFieldGet(this, _Selection_instances, "m", _Selection_join).call(this, "FULL JOIN", selectable, as, on);
524
593
  }
525
594
  innerJoin(selectable, as, on) {
526
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "INNER JOIN", selectable, as, on);
595
+ return __classPrivateFieldGet(this, _Selection_instances, "m", _Selection_join).call(this, "INNER JOIN", selectable, as, on);
527
596
  }
528
597
  leftJoin(selectable, as, on) {
529
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "LEFT JOIN", selectable, as, on);
598
+ return __classPrivateFieldGet(this, _Selection_instances, "m", _Selection_join).call(this, "LEFT JOIN", selectable, as, on);
530
599
  }
531
600
  rightJoin(selectable, as, on) {
532
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "RIGHT JOIN", selectable, as, on);
601
+ return __classPrivateFieldGet(this, _Selection_instances, "m", _Selection_join).call(this, "RIGHT JOIN", selectable, as, on);
533
602
  }
534
603
  naturalJoin(selectable, as) {
535
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "NATURAL JOIN", selectable, as);
604
+ return __classPrivateFieldGet(this, _Selection_instances, "m", _Selection_join).call(this, "NATURAL JOIN", selectable, as);
536
605
  }
537
606
  crossJoin(selectable, as) {
538
- return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "CROSS JOIN", selectable, as);
607
+ return __classPrivateFieldGet(this, _Selection_instances, "m", _Selection_join).call(this, "CROSS JOIN", selectable, as);
539
608
  }
540
609
  from(selectable, as) {
541
- return new _a(this.str + "," + fromAs(selectable, as));
610
+ return new _a(__classPrivateFieldGet(this, _Selection_sql, "f") + "," + fromAs(selectable, as));
542
611
  }
543
612
  select(columnsIn) {
544
613
  let sql = "SELECT ";
@@ -546,31 +615,17 @@ class SelectImpl {
546
615
  if (typeof columnsIn === "string")
547
616
  sql += columnsIn;
548
617
  else {
549
- sql += genSelect(columnsIn);
618
+ sql += selectColumns(columnsIn);
550
619
  if (columnsIn instanceof Array) ;
551
620
  else
552
621
  columns = Object.keys(columnsIn);
553
622
  }
554
- sql += "\n" + this.str;
555
- return new FinalSelectImpl(new SqlQueryStatement(sql, columns));
556
- }
557
- groupBy(columns) {
558
- let sql = this.str;
559
- if (typeof columns === "string")
560
- sql += " GROUP BY " + columns;
561
- else
562
- sql += " GROUP BY " + columns.join(",");
563
- return new _a(sql);
564
- }
565
- having(param) {
566
- return new _a(this.str + genWhere(param));
567
- }
568
- where(param) {
569
- return new _a(this.str + genWhere(param));
623
+ sql += "\n" + this.toString();
624
+ return new AfterSelectImpl(sql, columns);
570
625
  }
571
626
  }
572
- _a = SelectImpl, _SelectImpl_instances = new WeakSet(), _SelectImpl_join = function _SelectImpl_join(type, selectable, as, on) {
573
- let sql = this.str + "\n" + type + " " + fromAs(selectable, as);
627
+ _a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakSet(), _Selection_join = function _Selection_join(type, selectable, as, on) {
628
+ let sql = __classPrivateFieldGet(this, _Selection_sql, "f") + "\n" + type + " " + fromAs(selectable, as);
574
629
  if (on)
575
630
  sql += " ON " + on;
576
631
  return new _a(sql);
@@ -611,7 +666,7 @@ class DbTableQuery extends DbTable {
611
666
  this.statement = statement;
612
667
  }
613
668
  fromAs(as) {
614
- return createSelect(this, as);
669
+ return new Selection(this, as);
615
670
  }
616
671
  insert(values, option) {
617
672
  let insertCol;
@@ -619,7 +674,7 @@ class DbTableQuery extends DbTable {
619
674
  if (values instanceof Array) {
620
675
  if (values.length === 0)
621
676
  throw new Error("值不能为空");
622
- insertCol = getObjectListKeys(values);
677
+ insertCol = Array.from(getObjectListKeys(values));
623
678
  valuesStr = `VALUES\n${this.statement.objectListToValuesList(values, insertCol)}`;
624
679
  }
625
680
  else if (values instanceof SqlQueryStatement) {
@@ -653,7 +708,6 @@ class DbTableQuery extends DbTable {
653
708
  return genRetuningSql(sql, returns, values instanceof SqlQueryStatement ? values.columns : this.columns);
654
709
  }
655
710
  update(values, option = {}) {
656
- const { where } = option;
657
711
  const updateKey = Object.entries(values);
658
712
  if (updateKey.length === 0)
659
713
  throw new Error("值不能为空");
@@ -664,8 +718,8 @@ class DbTableQuery extends DbTable {
664
718
  setList.push(k + " = " + this.statement.toSqlStr(v));
665
719
  }
666
720
  let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
667
- if (where)
668
- sql += genWhere(where);
721
+ if (option.where)
722
+ sql += where(option.where);
669
723
  return sql;
670
724
  }
671
725
  updateWithResult(values, returns, option) {
@@ -675,7 +729,7 @@ class DbTableQuery extends DbTable {
675
729
  delete(option = {}) {
676
730
  let sql = "DELETE FROM " + this.name;
677
731
  if (option.where)
678
- sql += genWhere(option.where);
732
+ sql += where(option.where);
679
733
  return sql;
680
734
  }
681
735
  deleteWithResult(returns = "*", option) {
@@ -870,4 +924,4 @@ class YourTable extends DbTableQuery {
870
924
  }
871
925
  }
872
926
 
873
- export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, createSelect, genHaving, genOderBy, genSelect, genWhere, getObjectListKeys, pgSqlTransformer };
927
+ export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, getObjectListKeys, having, orderBy, pgSqlTransformer, selectColumns, where };
@@ -1,13 +1,13 @@
1
1
  import { SqlValuesCreator, SqlRaw } from "../sql_value/sql_value.ts";
2
2
  import { ColumnsSelected, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
3
- import { JoinSelect } from "./select.ts";
3
+ import { Selection } from "./select.ts";
4
4
  import { DbTable, SqlQueryStatement } from "./selectable.ts";
5
5
  import { WhereParam } 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
- fromAs(as?: string): JoinSelect;
10
+ fromAs(as?: string): Selection;
11
11
  insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
12
12
  insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
13
13
  update(values: UpdateRowValue<T>, option?: UpdateOption): string;
@@ -2,43 +2,42 @@ import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
2
2
  import { OrderByParam, WhereParam } from "../util.ts";
3
3
  import type { TableType } from "./type.ts";
4
4
  /** @public */
5
- export interface SelectFilterOption<T extends object> {
6
- orderBy?: string | OrderByParam<T> | (() => string | OrderByParam<T>);
7
- offset?: number;
8
- limit?: number;
5
+ export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T> {
6
+ limit(limit?: number, offset?: number): SqlQueryStatement<T>;
9
7
  }
10
8
  /** @public */
11
- export interface FinalSelect<T extends TableType> extends SqlSelectable<T> {
12
- filter(option: SelectFilterOption<T>): SqlQueryStatement<T>;
9
+ export interface CurrentOrderBy<T extends TableType> extends CurrentLimit<T> {
10
+ orderBy(param: OrderByParam): CurrentLimit<T>;
13
11
  }
14
12
  /** @public */
15
- export type LastSelect = {
16
- select<T extends TableType = TableType>(columns: "*" | string[] | {
17
- [key in keyof T]: string | boolean;
18
- }): FinalSelect<T>;
19
- };
20
- /** @public */
21
- export type AfterGroup = LastSelect & {
22
- having(param: WhereParam | (() => WhereParam)): LastSelect;
23
- };
24
- /** @public */
25
- export type AfterWhere = AfterGroup & {
26
- groupBy(columns: string | string[]): AfterGroup;
27
- };
13
+ export interface CurrentHaving<T extends TableType> extends CurrentOrderBy<T> {
14
+ having(param: WhereParam | (() => WhereParam)): CurrentLimit<T>;
15
+ }
28
16
  /** @public */
29
- export type AfterJoin = AfterWhere & {
30
- where(param: WhereParam | (() => WhereParam)): AfterWhere;
31
- };
17
+ export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
18
+ groupBy(columns: string | string[]): CurrentHaving<T>;
19
+ }
32
20
  /** @public */
33
- export interface JoinSelect extends AfterJoin {
34
- from(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
35
- crossJoin(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
36
- naturalJoin(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
37
- innerJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
38
- leftJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
39
- rightJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
40
- fullJoin(selectable: SqlSelectable<any> | string, as: string | undefined, on: string): JoinSelect;
21
+ export interface CurrentWhere<T extends TableType> extends CurrentGroupBy<T> {
22
+ where(param: WhereParam | (() => WhereParam)): CurrentGroupBy<T>;
41
23
  }
42
24
  /** @public */
43
- export declare function createSelect(selectable: SqlSelectable<any> | string, as?: string): JoinSelect;
25
+ export declare class Selection {
26
+ #private;
27
+ static from(selectable: SqlSelectable<any> | string, as?: string): Selection;
28
+ constructor(selectable: SqlSelectable<any> | string, as?: string);
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;
34
+ naturalJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
35
+ crossJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
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: {
39
+ [key in keyof T]: string | boolean;
40
+ }): CurrentWhere<T>;
41
+ select<T extends TableType = TableType>(columns: "*" | string[] | TableType): CurrentWhere<T>;
42
+ }
44
43
  //# sourceMappingURL=select.d.ts.map
package/dist/util.d.ts CHANGED
@@ -1,27 +1,58 @@
1
1
  import { OrderValue } from "./select/type.ts";
2
2
  /**
3
+ * 获取对象数组中的 key 的集合
3
4
  * @public
4
5
  * @param keepUndefinedKey - 是否保留值为 undefined 的 key
5
6
  */
6
- export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): string[];
7
+ export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): Set<string>;
7
8
  /** @public */
8
9
  export type WhereParam = string | string[];
9
- /** @public */
10
- export declare function genWhere(where: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
11
- /** @public */
12
- export declare function genHaving(having: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
13
10
  /**
11
+ * 生成 WHERE 语句
14
12
  * @public
15
13
  * ```ts
16
- * genSelect({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
17
- * genSelect(["c1", "count(*) AS c2", "column as c3"]) // "c1,count(*) AS c2,column as c3"
14
+ *
18
15
  * ```
19
16
  */
20
- export declare function genSelect(columns: string[] | Record<string, string | boolean>): string;
17
+ export declare function where(condition?: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
18
+ /**
19
+ *
20
+ * 生成 HAVING 语句
21
+ * @public
22
+ */
23
+ export declare function having(condition?: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
24
+ /**
25
+ * @public
26
+ * ```ts
27
+ * 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"
29
+ * ```
30
+ */
31
+ export declare function selectColumns(columns: string[] | Record<string, string | boolean>): string;
21
32
  /** @public */
22
- export type OrderByParam<T extends {} = {}> = string | string[] | ({
23
- [key in keyof T]: OrderValue;
24
- } & Record<string, OrderValue>);
33
+ export type OrderBehavior = {
34
+ key: string;
35
+ asc: boolean;
36
+ nullLast?: boolean;
37
+ };
25
38
  /** @public */
26
- export declare function genOderBy<T extends {} = {}>(orderBy: OrderByParam<T> | (() => OrderByParam<T>)): string;
39
+ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string, boolean | `${OrderValue} ${"NULLS FIRST" | "NULLS LAST"}`>;
40
+ /**
41
+ * 生成 ORDER BY 语句, d
42
+ * @public
43
+ * ```ts
44
+ * // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
45
+ * orderBy("age DESC NULLS FIRST,num ASC");
46
+ * orderBy(["age DESC NULLS FIRST", "num ASC"]);
47
+ * orderBy([
48
+ * { key: "age", asc: false, nullLast: false },
49
+ * { key: "num", asc: true },
50
+ * ]);
51
+ * orderBy({ age: "DESC NULLS FIRST", num: true });
52
+ *
53
+ * orderBy([]) // ""
54
+ * orderBy({}) // ""
55
+ * ```
56
+ */
57
+ export declare function orderBy(by: OrderByParam | (() => OrderByParam)): string;
27
58
  //# sourceMappingURL=util.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",