@asla/yoursql 0.5.0 → 0.5.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/dist/mod.js CHANGED
@@ -62,35 +62,41 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
62
62
  *
63
63
  * ```
64
64
  */
65
- function where(condition, type = "AND") {
66
- if (!condition)
67
- return "";
68
- return genCondition(condition, "WHERE", type);
65
+ function where(conditions, type) {
66
+ const sql = condition(conditions, type);
67
+ if (sql)
68
+ return "\nWHERE " + sql;
69
+ return "";
69
70
  }
70
71
  /**
71
72
  *
72
73
  * 生成 HAVING 语句
73
74
  * @public
74
75
  */
75
- function having(condition, type = "AND") {
76
- if (!condition)
77
- return "";
78
- return genCondition(condition, "HAVING", type);
76
+ function having(conditions, type) {
77
+ const sql = condition(conditions, type);
78
+ if (sql)
79
+ return "\nHAVING " + sql;
80
+ return "";
79
81
  }
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;
82
+ function condition(conditions, type = "AND") {
83
+ if (typeof conditions === "function")
84
+ conditions = conditions();
85
+ if (!conditions)
86
+ return;
87
+ if (typeof conditions === "string")
88
+ return conditions;
87
89
  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];
90
+ if (conditions.length) {
91
+ let sql = "";
92
+ type = " " + type + " ";
93
+ sql += conditions[0];
94
+ for (let i = 1; i < conditions.length; i++)
95
+ sql += type + conditions[i];
96
+ return sql;
97
+ }
98
+ return;
92
99
  }
93
- return sql;
94
100
  }
95
101
  /**
96
102
  * @public
@@ -152,9 +158,9 @@ function orderBy(by) {
152
158
  if (typeof by === "function")
153
159
  by = by();
154
160
  let sql = "";
161
+ if (!by)
162
+ return sql;
155
163
  if (typeof by === "string") {
156
- if (!by)
157
- return sql;
158
164
  sql += "\nORDER BY " + by;
159
165
  }
160
166
  else if (by instanceof Array) {
@@ -668,6 +674,9 @@ class DbTableQuery extends DbTable {
668
674
  fromAs(as) {
669
675
  return new Selection(this, as);
670
676
  }
677
+ select(columns, as) {
678
+ return this.fromAs(as).select(columns);
679
+ }
671
680
  insert(values, option) {
672
681
  let insertCol;
673
682
  let valuesStr;
@@ -1,13 +1,28 @@
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, 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
+ }, as?: string): CurrentWhere<R>;
11
26
  insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
12
27
  insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
13
28
  update(values: UpdateRowValue<T>, option?: UpdateOption): string;
@@ -21,14 +36,14 @@ export interface InsertOption<T extends object> {
21
36
  updateValues?: {
22
37
  [key in keyof T]?: undefined | SqlRaw | T[key];
23
38
  };
24
- where?: WhereParam;
39
+ where?: ConditionParam;
25
40
  }
26
41
  /** @public */
27
42
  export interface UpdateOption {
28
- where?: WhereParam;
43
+ where?: ConditionParam;
29
44
  }
30
45
  /** @public */
31
46
  export interface DeleteOption {
32
- where?: WhereParam;
47
+ where?: ConditionParam;
33
48
  }
34
49
  //# sourceMappingURL=TableQuery.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 } 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 {
@@ -35,9 +35,9 @@ export declare class Selection {
35
35
  crossJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
36
36
  from(selectable: SqlSelectable<any> | string, as?: string): Selection;
37
37
  select<T extends TableType = TableType>(columns: "*" | string[]): CurrentWhere<T>;
38
- select<T extends TableType = TableType>(columns: {
38
+ select<T extends TableType>(columns: {
39
39
  [key in keyof T]: string | boolean;
40
40
  }): CurrentWhere<T>;
41
- select<T extends TableType = TableType>(columns: "*" | string[] | TableType): CurrentWhere<T>;
41
+ select(columns: "*" | string[] | Record<string, string | boolean>): CurrentWhere<TableType>;
42
42
  }
43
43
  //# sourceMappingURL=select.d.ts.map
package/dist/util.d.ts CHANGED
@@ -6,7 +6,7 @@ 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
@@ -14,13 +14,13 @@ export type WhereParam = string | string[];
14
14
  *
15
15
  * ```
16
16
  */
17
- export declare function where(condition?: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
17
+ export declare function where(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
18
18
  /**
19
19
  *
20
20
  * 生成 HAVING 语句
21
21
  * @public
22
22
  */
23
- export declare function having(condition?: WhereParam | (() => WhereParam), type?: "AND" | "OR"): string;
23
+ export declare function having(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
24
24
  /**
25
25
  * @public
26
26
  * ```ts
@@ -54,5 +54,5 @@ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string,
54
54
  * orderBy({}) // ""
55
55
  * ```
56
56
  */
57
- export declare function orderBy(by: OrderByParam | (() => OrderByParam)): string;
57
+ export declare function orderBy(by?: OrderByParam | void | (() => OrderByParam | void)): string;
58
58
  //# 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.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",