@asla/yoursql 0.8.0 → 0.8.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
@@ -33,33 +33,6 @@ 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
36
  function condition(conditions, type = "AND") {
64
37
  if (typeof conditions === "function")
65
38
  conditions = conditions();
@@ -186,26 +159,33 @@ function selectColumns(columns) {
186
159
  case "string":
187
160
  return columns;
188
161
  case "object": {
189
- let sql = "";
190
- const keys = Object.keys(columns);
191
- if (keys.length === 0)
192
- throw new Error("没有选择任何列");
193
- let k = keys[0];
194
- let v = columns[k];
195
- if (typeof v === "string")
196
- sql += v + " AS " + k;
197
- else
198
- sql += k;
199
- for (let i = 1; i < keys.length; i++) {
200
- k = keys[i];
201
- v = columns[k];
202
- sql += ",";
162
+ if (columns instanceof Array) {
163
+ if (columns.length === 0)
164
+ throw new Error("没有选择任何列");
165
+ return columns.join(",");
166
+ }
167
+ else {
168
+ let sql = "";
169
+ const keys = Object.keys(columns);
170
+ if (keys.length === 0)
171
+ throw new Error("没有选择任何列");
172
+ let k = keys[0];
173
+ let v = columns[k];
203
174
  if (typeof v === "string")
204
175
  sql += v + " AS " + k;
205
176
  else
206
177
  sql += k;
178
+ for (let i = 1; i < keys.length; i++) {
179
+ k = keys[i];
180
+ v = columns[k];
181
+ sql += ",";
182
+ if (typeof v === "string")
183
+ sql += v + " AS " + k;
184
+ else
185
+ sql += k;
186
+ }
187
+ return sql;
207
188
  }
208
- return sql;
209
189
  }
210
190
  default:
211
191
  throw new TypeError("columns 应为 string 或 object 类型");
@@ -751,8 +731,7 @@ class SqlChainModify extends SqlStatement {
751
731
  columnsStr = "*";
752
732
  }
753
733
  else {
754
- const res = selectColumnsOrTable(returns);
755
- columnsStr = res.sqlColumns;
734
+ columnsStr = selectColumns(returns);
756
735
  }
757
736
  let sql = this.toString() + "\nRETURNING " + columnsStr;
758
737
  return new SqlTextStatementDataset(sql);
@@ -1,8 +1,4 @@
1
1
  import { Constructable } from "../util.ts";
2
- export declare function selectColumnsOrTable(columns: Record<string, boolean | string> | string[]): {
3
- columns: string[];
4
- sqlColumns: string;
5
- };
6
2
  type ConditionParam = string | string[];
7
3
  /**
8
4
  * 生成条件语句
@@ -1,5 +1,5 @@
1
- import { ConditionParam, Constructable, OrderByParam } from "../util.ts";
2
- import { ColumnsSelected, TableType } from "./type.ts";
1
+ import { ConditionParam, Constructable, OrderByParam, SelectParam } from "../util.ts";
2
+ import { TableType } from "./type.ts";
3
3
  /** @public */
4
4
  export declare abstract class SqlStatement {
5
5
  /** 获取 SQL 语句 */
@@ -55,8 +55,8 @@ export interface ChainSelectWhere<T extends TableType> extends ChainSelectGroupB
55
55
  /** @public */
56
56
  export interface ChainModifyReturning<T extends TableType = {}> extends SqlStatement {
57
57
  returning(columns: "*"): SqlStatementDataset<T>;
58
- returning(columns: Constructable<ColumnsSelected<T> | string>): SqlStatementDataset<Record<string, any>>;
59
- returning<R extends TableType>(columns: Constructable<ColumnsSelected<R> | string>): SqlStatementDataset<R>;
58
+ returning(columns: Constructable<SelectParam>): SqlStatementDataset<Record<string, any>>;
59
+ returning<R extends TableType>(columns: Constructable<SelectParam>): SqlStatementDataset<R>;
60
60
  }
61
61
  /** @public */
62
62
  export interface ChainModifyWhere<T extends TableType = {}> extends ChainModifyReturning<T> {
@@ -1,10 +1,10 @@
1
- import { ConditionParam, Constructable } from "../util.ts";
1
+ import { ConditionParam, Constructable, SelectParam } from "../util.ts";
2
2
  import { SqlStatementDataset, SqlStatement, ChainModifyWhere, ChainOnConflict, ChainConflictDo, ChainModifyReturning } from "./query_chain_abstract.ts";
3
- import { ColumnsSelected, TableType } from "./type.ts";
3
+ import { TableType } from "./type.ts";
4
4
  export declare class SqlChainModify<T extends TableType = {}> extends SqlStatement implements ChainOnConflict<T>, ChainModifyWhere<T> {
5
5
  readonly sql: string;
6
6
  constructor(sql: string);
7
- returning<R extends {}>(returns: Constructable<ColumnsSelected<any> | "*">): SqlStatementDataset<R>;
7
+ returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
8
8
  onConflict(onConflict: Constructable<readonly string[] | string>): ChainConflictDo<T>;
9
9
  where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
10
10
  toString(): string;
@@ -20,26 +20,8 @@ export type UpdateRowValue<T extends object> = {
20
20
  };
21
21
  /** @public */
22
22
  export type OrderValue = "ASC" | "DESC";
23
- /**
24
- * 表的选择参数
25
- * @public
26
- */
27
- export type ColumnsSelected<T extends TableType> = {
28
- [key in keyof T]?: boolean | string;
29
- };
30
- /**
31
- * 从一个表格选择列,生成新的表格类型
32
- * @public
33
- */
34
- export type SelectColumns<T extends TableType, R extends ColumnsSelected<T>> = R extends {
35
- [key in keyof T]?: boolean | string;
36
- } ? {
37
- [key in keyof T as R[key] extends true ? key : StringOnly<R[key]>]: T[key];
38
- } : never;
39
- type StringOnly<T> = T extends string ? T : never;
40
23
  /** @public */
41
24
  export type TableType = {
42
25
  [key: string]: any;
43
26
  };
44
- export {};
45
27
  //# sourceMappingURL=type.d.ts.map
package/dist/util.d.ts CHANGED
@@ -30,7 +30,7 @@ export declare function where(conditions?: Constructable<ConditionParam | void>,
30
30
  */
31
31
  export declare function having(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
32
32
  /** @public */
33
- export type SelectParam = string | Record<string, string | boolean>;
33
+ export type SelectParam = string | string[] | Record<string, string | boolean>;
34
34
  /**
35
35
  * @public
36
36
  * @example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",