@asla/yoursql 0.5.2 → 0.6.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
@@ -107,7 +107,12 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
107
107
  * @public
108
108
  * @example
109
109
  * ```ts
110
- *
110
+ * where(['a=1','b=2']) // "\nWHERE a=1 AND b=2"
111
+ * where(['a=1','b=2'],"OR") // "\nWHERE a=1 OR b=2"
112
+ * where("a=1 OR b=2") // "\nWHERE a=1 OR b=2"
113
+ * where(()=>"a=1 OR b=2") // "\nWHERE a=1 AND b=2"
114
+ * where([]) // ""
115
+ * where(undefined) // ""
111
116
  * ```
112
117
  */
113
118
  function where(conditions, type) {
@@ -243,41 +248,14 @@ var _SqlQueryStatement_sql;
243
248
  * @public
244
249
  */
245
250
  class SqlSelectable {
246
- constructor(columns) {
247
- // Reflect.set(this, SQL_SELECTABLE, undefined);
248
- let readonlyColumns;
249
- if (typeof columns[Symbol.iterator] === "function") {
250
- let iterable = columns;
251
- readonlyColumns = [];
252
- let iter = iterable[Symbol.iterator]();
253
- let i = 0;
254
- let item = iter.next();
255
- while (!item.done) {
256
- readonlyColumns[i++] = item.value;
257
- item = iter.next();
258
- }
259
- // readonlyColumns.length = i;
260
- }
261
- else {
262
- let arrayLike = columns;
263
- readonlyColumns = new Array(arrayLike.length);
264
- // readonlyColumns.length = arrayLike.length;
265
- for (let i = 0; i < arrayLike.length; i++) {
266
- readonlyColumns[i] = arrayLike[i];
267
- }
268
- }
269
- this.columns = readonlyColumns;
270
- }
271
251
  }
272
252
  /**
273
253
  * 数据库表
274
254
  * @public
275
255
  */
276
256
  class DbTable extends SqlSelectable {
277
- constructor(name, columns) {
278
- if (!(columns instanceof Array))
279
- columns = Object.keys(columns);
280
- super(columns);
257
+ constructor(name) {
258
+ super();
281
259
  this.name = name;
282
260
  }
283
261
  toSelect() {
@@ -292,14 +270,10 @@ class DbTable extends SqlSelectable {
292
270
  * @public
293
271
  */
294
272
  class SqlQueryStatement extends SqlSelectable {
295
- constructor(sql, columns) {
296
- if (sql instanceof SqlQueryStatement) {
297
- columns = sql.columns;
298
- sql = __classPrivateFieldGet(sql, _SqlQueryStatement_sql, "f");
299
- }
300
- super(columns);
273
+ constructor(sql) {
274
+ super();
301
275
  _SqlQueryStatement_sql.set(this, void 0);
302
- __classPrivateFieldSet(this, _SqlQueryStatement_sql, sql, "f");
276
+ __classPrivateFieldSet(this, _SqlQueryStatement_sql, sql.toString(), "f");
303
277
  }
304
278
  toString() {
305
279
  return __classPrivateFieldGet(this, _SqlQueryStatement_sql, "f");
@@ -520,13 +494,13 @@ class SqlValuesCreator {
520
494
  }
521
495
  class YourValuesAs extends SqlSelectable {
522
496
  constructor(columns, asName, valuesStr) {
523
- super(columns);
497
+ super();
524
498
  _YourValuesAs_asName.set(this, void 0);
525
499
  _YourValuesAs_valuesStr.set(this, void 0);
526
500
  _YourValuesAs_sql.set(this, void 0);
527
501
  __classPrivateFieldSet(this, _YourValuesAs_asName, asName, "f");
528
502
  __classPrivateFieldSet(this, _YourValuesAs_valuesStr, valuesStr, "f");
529
- __classPrivateFieldSet(this, _YourValuesAs_sql, `(VALUES\n${__classPrivateFieldGet(this, _YourValuesAs_valuesStr, "f")})\nAS ${__classPrivateFieldGet(this, _YourValuesAs_asName, "f")}(${this.columns.join(",")})`, "f");
503
+ __classPrivateFieldSet(this, _YourValuesAs_sql, `(VALUES\n${__classPrivateFieldGet(this, _YourValuesAs_valuesStr, "f")})\nAS ${__classPrivateFieldGet(this, _YourValuesAs_asName, "f")}(${columns.join(",")})`, "f");
530
504
  }
531
505
  toSelect() {
532
506
  return __classPrivateFieldGet(this, _YourValuesAs_sql, "f");
@@ -579,11 +553,11 @@ const pgSqlTransformer = new Map([
579
553
 
580
554
  var _Selection_instances, _a, _Selection_sql, _Selection_join;
581
555
  class AfterSelectImpl extends SqlQueryStatement {
582
- constructor(sql, columns) {
583
- super(sql, columns);
556
+ constructor(sql) {
557
+ super(sql);
584
558
  }
585
559
  where(param) {
586
- return new AfterSelectImpl(this.toString() + where(param), this.columns);
560
+ return new AfterSelectImpl(this.toString() + where(param));
587
561
  }
588
562
  groupBy(columns) {
589
563
  let sql = this.toString();
@@ -591,13 +565,13 @@ class AfterSelectImpl extends SqlQueryStatement {
591
565
  sql += " GROUP BY " + columns;
592
566
  else
593
567
  sql += " GROUP BY " + columns.join(",");
594
- return new AfterSelectImpl(sql, this.columns);
568
+ return new AfterSelectImpl(sql);
595
569
  }
596
570
  having(param) {
597
- return new AfterSelectImpl(this.toString() + having(param), this.columns);
571
+ return new AfterSelectImpl(this.toString() + having(param));
598
572
  }
599
573
  orderBy(param) {
600
- return new AfterSelectImpl(this.toString() + orderBy(param), this.columns);
574
+ return new AfterSelectImpl(this.toString() + orderBy(param));
601
575
  }
602
576
  limit(limit, offset) {
603
577
  let sql = this.toString();
@@ -605,10 +579,12 @@ class AfterSelectImpl extends SqlQueryStatement {
605
579
  sql += "\nLIMIT " + limit;
606
580
  if (offset)
607
581
  sql += "\nOFFSET " + offset;
608
- return new SqlQueryStatement(sql, Array.from(this.columns));
582
+ return new SqlQueryStatement(sql);
609
583
  }
610
584
  }
611
585
  function fromAs(selectable, as) {
586
+ if (typeof selectable === "function")
587
+ selectable = selectable();
612
588
  let sql = typeof selectable === "string" ? selectable : selectable.toSelect();
613
589
  if (as)
614
590
  sql += " AS " + as;
@@ -649,14 +625,11 @@ class Selection {
649
625
  return new _a(__classPrivateFieldGet(this, _Selection_sql, "f") + "," + fromAs(selectable, as));
650
626
  }
651
627
  select(columnsIn) {
652
- let columns = [];
653
628
  if (typeof columnsIn === "function")
654
629
  columnsIn = columnsIn();
655
- if (typeof columnsIn === "object")
656
- columns = Object.keys(columnsIn);
657
630
  let sql = "SELECT " + selectColumns(columnsIn);
658
631
  sql += "\n" + this.toString();
659
- return new AfterSelectImpl(sql, columns);
632
+ return new AfterSelectImpl(sql);
660
633
  }
661
634
  }
662
635
  _a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakSet(), _Selection_join = function _Selection_join(type, selectable, as, on) {
@@ -669,8 +642,8 @@ _a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakS
669
642
 
670
643
  /** @public */
671
644
  class DbTableQuery extends DbTable {
672
- constructor(name, columns, statement) {
673
- super(name, columns);
645
+ constructor(name, statement) {
646
+ super(name);
674
647
  this.statement = statement;
675
648
  }
676
649
  fromAs(as) {
@@ -679,89 +652,127 @@ class DbTableQuery extends DbTable {
679
652
  select(columns, as) {
680
653
  return this.fromAs(as).select(columns);
681
654
  }
682
- insert(values, option) {
683
- let insertCol;
655
+ insert(values, columns_option, option) {
656
+ if (typeof values === "function")
657
+ values = values();
658
+ let columnStr;
684
659
  let valuesStr;
685
- if (values instanceof Array) {
686
- if (values.length === 0)
687
- throw new Error("值不能为空");
688
- insertCol = Array.from(getObjectListKeys(values));
689
- valuesStr = `VALUES\n${this.statement.objectListToValuesList(values, insertCol)}`;
660
+ if (typeof values === "string") {
661
+ valuesStr = values;
662
+ if (typeof columns_option === "string")
663
+ columnStr = columns_option;
664
+ else if (columns_option instanceof Array) {
665
+ if (columns_option.length === 0)
666
+ throw new Error("插入列为空");
667
+ columnStr = columns_option.join(",");
668
+ }
669
+ else
670
+ throw new Error("当 values 为 string 类型时,必须指定 columns");
690
671
  }
691
- else if (values instanceof SqlQueryStatement) {
692
- // todo 验证 values.columns 和 this.columns 是否匹配
693
- valuesStr = values.toString();
694
- insertCol = values.columns;
672
+ else {
673
+ let insertCol;
674
+ option = columns_option;
675
+ if (typeof values === "object") {
676
+ if (values instanceof Array) {
677
+ if (values.length === 0)
678
+ throw new Error("值不能为空");
679
+ insertCol = Array.from(getObjectListKeys(values));
680
+ valuesStr = `VALUES\n${this.statement.objectListToValuesList(values, insertCol)}`;
681
+ }
682
+ else if (values instanceof SqlQueryStatement) {
683
+ // todo 验证 values.columns 和 this.columns 是否匹配
684
+ valuesStr = values.toString();
685
+ insertCol = values.columns;
686
+ }
687
+ else {
688
+ insertCol = Object.keys(values);
689
+ valuesStr = `VALUES\n(${this.statement.objectToValues(values, insertCol)})`;
690
+ }
691
+ }
692
+ else
693
+ throw new Error("values 应该是 Array 或 TableQuery 类型");
694
+ if (insertCol.length === 0)
695
+ throw new Error("插入列不能为空");
696
+ columnStr = insertCol.join(",");
695
697
  }
696
- else
697
- throw new Error("values 应该是 Array 或 TableQuery 类型");
698
- if (insertCol.length === 0)
699
- throw new Error("插入列不能为空");
700
- let sql = `INSERT INTO ${this.name} (${insertCol.join(",")})\n${valuesStr}`;
698
+ let sql = `INSERT INTO ${this.name} (${columnStr})\n${valuesStr}`;
701
699
  if (option) {
702
- const { updateValues, conflict, where } = option;
700
+ let { updateValues, conflict, where: inputWhere } = option;
703
701
  if (conflict) {
704
- sql += `\nON CONFLICT (${conflict.join(",")})`;
702
+ if (typeof conflict !== "string")
703
+ conflict = conflict.join(",");
704
+ sql += `\nON CONFLICT (${conflict})`;
705
+ if (typeof updateValues === "function")
706
+ updateValues = updateValues();
705
707
  if (updateValues) {
706
708
  const updateKey = Object.entries(updateValues);
707
709
  sql += `\nDO UPDATE SET\n${updateKey.map(([key, v = "EXCLUDED." + key]) => key + " = " + v).join(",\n")}`;
708
710
  }
709
711
  else
710
712
  sql += "DO NOTHING";
711
- if (where)
712
- sql += "\nWHERE " + where;
713
+ sql += where(inputWhere);
713
714
  }
714
715
  }
715
716
  return sql;
716
717
  }
717
- insertWithResult(values, returns, option) {
718
- let sql = this.insert(values, option);
719
- return genRetuningSql(sql, returns, values instanceof SqlQueryStatement ? values.columns : this.columns);
718
+ insertWithResult(values, returns, columns, option) {
719
+ let sql = this.insert(values, columns, option);
720
+ return genRetuningSql(sql, returns);
720
721
  }
722
+ /**
723
+ * @example
724
+ * ```ts
725
+ * table.update("age=3, name='hi'") // "UPDATE table SET age=3, name='hi'"
726
+ * table.update({age:3, name:"hi"}) // "UPDATE table SET age=3, name='hi'"
727
+ * ```
728
+ */
721
729
  update(values, option = {}) {
722
- const updateKey = Object.entries(values);
723
- if (updateKey.length === 0)
724
- throw new Error("值不能为空");
725
- let setList = [];
726
- for (const [k, v] of updateKey) {
727
- if (v === undefined)
728
- continue;
729
- setList.push(k + " = " + this.statement.toSqlStr(v));
730
+ if (typeof values === "function")
731
+ values = values();
732
+ let setStr;
733
+ if (typeof values === "string")
734
+ setStr = values;
735
+ else {
736
+ const updateKey = Object.entries(values);
737
+ let setList = [];
738
+ for (const [k, v] of updateKey) {
739
+ if (v === undefined)
740
+ continue;
741
+ setList.push(k + " = " + this.statement.toSqlStr(v));
742
+ }
743
+ setStr = setList.join(",\n");
730
744
  }
731
- let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
732
- if (option.where)
733
- sql += where(option.where);
745
+ if (!setStr)
746
+ throw new Error("值不能为空");
747
+ let sql = `UPDATE ${this.name}\nSET ${setStr}`;
748
+ sql += where(option.where);
734
749
  return sql;
735
750
  }
736
751
  updateWithResult(values, returns, option) {
737
752
  let sql = this.update(values, option);
738
- return genRetuningSql(sql, returns, values instanceof SqlQueryStatement ? values.columns : this.columns);
753
+ return genRetuningSql(sql, returns);
739
754
  }
740
755
  delete(option = {}) {
741
756
  let sql = "DELETE FROM " + this.name;
742
- if (option.where)
743
- sql += where(option.where);
757
+ sql += where(option.where);
744
758
  return sql;
745
759
  }
746
760
  deleteWithResult(returns = "*", option) {
747
761
  let sql = this.delete(option);
748
- return genRetuningSql(sql, returns, this.columns);
762
+ return genRetuningSql(sql, returns);
749
763
  }
750
764
  }
751
- function genRetuningSql(sql, returns, tableColumns) {
765
+ function genRetuningSql(sql, returns) {
752
766
  let columnsStr;
753
- let columns;
754
767
  if (returns === "*") {
755
- columns = tableColumns;
756
768
  columnsStr = "*";
757
769
  }
758
770
  else {
759
771
  const res = selectColumnsOrTable(returns);
760
772
  columnsStr = res.sqlColumns;
761
- columns = res.columns;
762
773
  }
763
774
  sql += "\nRETURNING " + columnsStr;
764
- return new SqlQueryStatement(sql, columns);
775
+ return new SqlQueryStatement(sql);
765
776
  }
766
777
 
767
778
  /**
@@ -915,8 +926,9 @@ function getErrStr(expect, actual) {
915
926
  */
916
927
  class YourTable extends DbTableQuery {
917
928
  constructor(name, define, sqlValue) {
918
- super(name, Object.keys(define), sqlValue);
929
+ super(name, sqlValue);
919
930
  this.define = define;
931
+ this.columns = Object.keys(define);
920
932
  }
921
933
  getColumnMeta(name) {
922
934
  return Reflect.get(this.define, name);
@@ -2,11 +2,11 @@ import { SqlValuesCreator, SqlRaw } from "../sql_value/sql_value.ts";
2
2
  import { ColumnsSelected, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
3
3
  import { CurrentWhere, Selection } from "./select.ts";
4
4
  import { DbTable, SqlQueryStatement } from "./selectable.ts";
5
- import { ConditionParam } from "../util.ts";
5
+ import { ConditionParam, Constructable } 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
- constructor(name: string, columns: readonly string[], statement: SqlValuesCreator);
9
+ constructor(name: string, statement: SqlValuesCreator);
10
10
  fromAs(as?: string): Selection;
11
11
  /** 选择单表全部列 */
12
12
  select(columns: "*", as?: string): CurrentWhere<T>;
@@ -15,37 +15,57 @@ export declare class DbTableQuery<T extends TableType = Record<string, any>, C e
15
15
  * @param columns - 对象选择
16
16
  */
17
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;
18
+ [key in keyof T]?: boolean;
19
+ }>(columns: Constructable<R>, as?: string): CurrentWhere<{
20
+ [key in keyof R]: key extends keyof T ? T[key] : unknown;
21
21
  }>;
22
22
  /** 选择单表- 所有类型 */
23
- select<R extends {}>(columns: string | {
23
+ select<R extends {}>(columns: Constructable<{
24
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>;
28
- insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
29
- insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
30
- update(values: UpdateRowValue<T>, option?: UpdateOption): string;
31
- updateWithResult<R extends ColumnsSelected<T>>(values: UpdateRowValue<T>, returns: R, option?: UpdateOption): SqlQueryStatement<SelectColumns<T, R>>;
25
+ } | string>, as?: string): CurrentWhere<R>;
26
+ /**
27
+ * @example
28
+ * ```ts
29
+ * table.insert({age:18, name:"hi"}) // INSERT INTO table(age,name) VALUES (18, 'hi')
30
+ * table.insert([{age:18, name:"hi"}, {age:17, name:"hh"}]) // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
31
+ * ```
32
+ */
33
+ insert(values: Constructable<C | C[]>, option?: InsertOption<T>): string;
34
+ /**
35
+ * @example
36
+ * ```ts
37
+ * table.insert("VALUES (18, 'hi'), (17, 'hh')", ["age","name"]) // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
38
+ * ```
39
+ */
40
+ insert(values: Constructable<string>, columns: string[], option?: InsertOption<T>): string;
41
+ insertWithResult<R extends ColumnsSelected<T>>(values: Constructable<C | C[]>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
42
+ insertWithResult<R extends ColumnsSelected<T>>(values: Constructable<string>, returns: R, columns: string | string[], option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
43
+ /**
44
+ * @example
45
+ * ```ts
46
+ * table.update("age=3, name='hi'") // "UPDATE table SET age=3, name='hi'"
47
+ * table.update({age:3, name:"hi"}) // "UPDATE table SET age=3, name='hi'"
48
+ * ```
49
+ */
50
+ update(values: Constructable<UpdateRowValue<T> | string>, option?: UpdateOption): string;
51
+ updateWithResult<R extends ColumnsSelected<T>>(values: Constructable<UpdateRowValue<T>>, returns: R, option?: UpdateOption): SqlQueryStatement<SelectColumns<T, R>>;
32
52
  delete(option?: DeleteOption): string;
33
53
  deleteWithResult<R extends ColumnsSelected<T>>(returns?: ColumnsSelected<T> | "*", option?: DeleteOption): SqlQueryStatement<SelectColumns<T, R>>;
34
54
  }
35
55
  /** @public */
36
56
  export interface InsertOption<T extends object> {
37
- conflict?: (keyof T)[];
38
- updateValues?: {
57
+ conflict?: (keyof T)[] | string;
58
+ updateValues?: Constructable<{
39
59
  [key in keyof T]?: undefined | SqlRaw | T[key];
40
- };
41
- where?: ConditionParam;
60
+ } | string | void>;
61
+ where?: Constructable<ConditionParam | void>;
42
62
  }
43
63
  /** @public */
44
64
  export interface UpdateOption {
45
- where?: ConditionParam;
65
+ where?: Constructable<ConditionParam | void>;
46
66
  }
47
67
  /** @public */
48
68
  export interface DeleteOption {
49
- where?: ConditionParam;
69
+ where?: Constructable<ConditionParam | void>;
50
70
  }
51
71
  //# sourceMappingURL=TableQuery.d.ts.map
@@ -1,5 +1,5 @@
1
- import { ColumnsSelectAs } from "./type.ts";
2
- export declare function selectColumnsOrTable(columns: ColumnsSelectAs<any> | string[]): {
1
+ import { Constructable } from "../util.ts";
2
+ export declare function selectColumnsOrTable(columns: Record<string, boolean | string> | string[]): {
3
3
  columns: string[];
4
4
  sqlColumns: string;
5
5
  };
@@ -7,6 +7,6 @@ type ConditionParam = string | string[];
7
7
  /**
8
8
  * 生成条件语句
9
9
  */
10
- export declare function condition(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
10
+ export declare function condition(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
11
11
  export {};
12
12
  //# sourceMappingURL=_statement.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
2
- import { OrderByParam, ConditionParam, SelectParam } from "../util.ts";
2
+ import { OrderByParam, ConditionParam, SelectParam, Constructable } 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 | (() => OrderByParam | void)): CurrentLimit<T>;
10
+ orderBy(param: Constructable<OrderByParam | void>): CurrentLimit<T>;
11
11
  }
12
12
  /** @public */
13
13
  export interface CurrentHaving<T extends TableType> extends CurrentOrderBy<T> {
14
- having(param: ConditionParam | (() => ConditionParam | void)): CurrentLimit<T>;
14
+ having(param: Constructable<ConditionParam | void>): CurrentLimit<T>;
15
15
  }
16
16
  /** @public */
17
17
  export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
@@ -19,21 +19,21 @@ 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: ConditionParam | (() => ConditionParam | void)): CurrentGroupBy<T>;
22
+ where(param: Constructable<ConditionParam | void>): CurrentGroupBy<T>;
23
23
  }
24
24
  /** @public */
25
25
  export declare class Selection {
26
26
  #private;
27
- static from(selectable: SqlSelectable<any> | string, as?: string): Selection;
28
- constructor(selectable: SqlSelectable<any> | string, as?: string);
27
+ static from(selectable: Constructable<SqlSelectable<any> | string>, as?: string): Selection;
28
+ constructor(selectable: Constructable<SqlSelectable<any> | string>, as?: string);
29
29
  toString(): string;
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
- 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;
30
+ fullJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
31
+ innerJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
32
+ leftJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
33
+ rightJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
34
+ naturalJoin(selectable: Constructable<SqlSelectable<any> | string>, as?: string | undefined): Selection;
35
+ crossJoin(selectable: Constructable<SqlSelectable<any> | string>, as?: string | undefined): Selection;
36
+ from(selectable: Constructable<SqlSelectable<any> | string>, as?: string): Selection;
37
37
  /** 选择全部列 */
38
38
  select<T extends TableType = TableType>(columns: "*"): CurrentWhere<T>;
39
39
  /**
@@ -43,7 +43,7 @@ export declare class Selection {
43
43
  * selection.select("t.age, count(*) AS c") // SELECT t.age,count(*) AS c FROM ...
44
44
  * ```
45
45
  */
46
- select<T extends TableType = TableType>(columns: string): CurrentWhere<T>;
46
+ select<T extends TableType = TableType>(columns: Constructable<string>): CurrentWhere<T>;
47
47
  /**
48
48
  * 通过 object 选择 列
49
49
  * @example
@@ -51,11 +51,9 @@ export declare class Selection {
51
51
  * selection.select({"age":true, c:"count(*)"}) // SELECT age,count(*) AS c FROM ...
52
52
  * ```
53
53
  */
54
- select<T extends TableType>(columns: {
54
+ select<T extends TableType>(columns: Constructable<{
55
55
  [key in keyof T]: string | boolean;
56
- } | (() => {
57
- [key in keyof T]: string | boolean;
58
- })): CurrentWhere<T>;
59
- select(columns: SelectParam | (() => SelectParam)): CurrentWhere<TableType>;
56
+ }>): CurrentWhere<T>;
57
+ select(columns: Constructable<SelectParam>): CurrentWhere<TableType>;
60
58
  }
61
59
  //# sourceMappingURL=select.d.ts.map
@@ -11,9 +11,6 @@ declare const SQL_SELECTABLE: unique symbol;
11
11
  * @public
12
12
  */
13
13
  export declare abstract class SqlSelectable<T extends TableType> {
14
- constructor(columns: ArrayLike<string> | Iterable<string>);
15
- /** 结果列 */
16
- readonly columns: readonly string[];
17
14
  /**
18
15
  * 转成子选择语句, 你可以使用 select form xxx 选择
19
16
  * 如果是 table 则是 table name
@@ -31,7 +28,7 @@ export declare abstract class SqlSelectable<T extends TableType> {
31
28
  */
32
29
  export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
33
30
  readonly name: string;
34
- constructor(name: string, columns: readonly (keyof T)[]);
31
+ constructor(name: string);
35
32
  toSelect(): string;
36
33
  toString(): string;
37
34
  }
@@ -41,8 +38,7 @@ export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
41
38
  */
42
39
  export declare class SqlQueryStatement<T extends TableType = TableType> extends SqlSelectable<T> {
43
40
  #private;
44
- constructor(sql: string, columns: readonly string[]);
45
- constructor(sql: SqlQueryStatement);
41
+ constructor(sql: string | SqlQueryStatement);
46
42
  toString(): string;
47
43
  toSelect(): string;
48
44
  }
@@ -18,25 +18,22 @@ export type PickColumn<T extends {
18
18
  export type UpdateRowValue<T extends object> = {
19
19
  [key in keyof T]?: T[key] | SqlRaw;
20
20
  };
21
- /**
22
- * 选择列并重命名
23
- * @public
24
- */
25
- export type ColumnsSelectAs<T extends TableType> = {
26
- [key in keyof T]?: boolean | string;
27
- };
28
21
  /** @public */
29
22
  export type OrderValue = "ASC" | "DESC";
30
23
  /**
31
24
  * 表的选择参数
32
25
  * @public
33
26
  */
34
- export type ColumnsSelected<T extends TableType> = ColumnsSelectAs<T> | "*";
27
+ export type ColumnsSelected<T extends TableType> = {
28
+ [key in keyof T]?: boolean | string;
29
+ } | "*";
35
30
  /**
36
31
  * 从一个表格选择列,生成新的表格类型
37
32
  * @public
38
33
  */
39
- export type SelectColumns<T extends TableType, R extends ColumnsSelected<T>> = R extends "*" ? T : R extends ColumnsSelectAs<T> ? {
34
+ export type SelectColumns<T extends TableType, R extends ColumnsSelected<T>> = R extends "*" ? T : R extends {
35
+ [key in keyof T]?: boolean | string;
36
+ } ? {
40
37
  [key in keyof T as R[key] extends true ? key : StringOnly<R[key]>]: T[key];
41
38
  } : never;
42
39
  type StringOnly<T> = T extends string ? T : never;
package/dist/util.d.ts CHANGED
@@ -6,22 +6,29 @@ 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 Constructable<T> = T | (() => T);
10
+ /** @public */
9
11
  export type ConditionParam = string | string[];
10
12
  /**
11
13
  * 生成 WHERE 语句
12
14
  * @public
13
15
  * @example
14
16
  * ```ts
15
- *
17
+ * where(['a=1','b=2']) // "\nWHERE a=1 AND b=2"
18
+ * where(['a=1','b=2'],"OR") // "\nWHERE a=1 OR b=2"
19
+ * where("a=1 OR b=2") // "\nWHERE a=1 OR b=2"
20
+ * where(()=>"a=1 OR b=2") // "\nWHERE a=1 AND b=2"
21
+ * where([]) // ""
22
+ * where(undefined) // ""
16
23
  * ```
17
24
  */
18
- export declare function where(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
25
+ export declare function where(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
19
26
  /**
20
27
  *
21
28
  * 生成 HAVING 语句
22
29
  * @public
23
30
  */
24
- export declare function having(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
31
+ export declare function having(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
25
32
  /** @public */
26
33
  export type SelectParam = string | Record<string, string | boolean>;
27
34
  /**
@@ -32,7 +39,7 @@ export type SelectParam = string | Record<string, string | boolean>;
32
39
  * selectColumns("c1,count(*) AS c2,column as c3") // "c1,count(*) AS c2,column as c3"
33
40
  * ```
34
41
  */
35
- export declare function selectColumns(columns: SelectParam | (() => SelectParam)): string;
42
+ export declare function selectColumns(columns: Constructable<SelectParam>): string;
36
43
  /** @public */
37
44
  export type OrderBehavior = {
38
45
  key: string;
@@ -59,5 +66,5 @@ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string,
59
66
  * orderBy({}) // ""
60
67
  * ```
61
68
  */
62
- export declare function orderBy(by?: OrderByParam | void | (() => OrderByParam | void)): string;
69
+ export declare function orderBy(by?: Constructable<OrderByParam | void>): string;
63
70
  //# sourceMappingURL=util.d.ts.map
@@ -10,6 +10,7 @@ import { TypeChecker } from "./checker.ts";
10
10
  export declare class YourTable<T extends TableType = TableType, C extends TableType = T> extends DbTableQuery<T, C> {
11
11
  private define;
12
12
  constructor(name: string, define: TableDefined, sqlValue: SqlValuesCreator);
13
+ readonly columns: readonly string[];
13
14
  getColumnMeta(name: keyof T): ColumnMeta<unknown>;
14
15
  createTypeChecker<T>(keys: readonly string[]): TypeChecker<T>;
15
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",