@asla/yoursql 0.10.0 → 0.10.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.
@@ -1,5 +1,5 @@
1
1
  import { SqlStatementDataset, SqlTemplate } from "../SqlStatement.ts";
2
- import { AssertJsType, ObjectToValueKeys, SqlValueData } from "./type.ts";
2
+ import { AssertJsType, ObjectToValueKeys, SqlValuesTextData } from "./type.ts";
3
3
  /** @public js 对象到编码函数的映射*/
4
4
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
5
5
  /** @public 将 js 值转为 SQl 字符串的函数*/
@@ -53,7 +53,7 @@ export declare class SqlValuesCreator {
53
53
  * @param keys - 选择的键。如果指定了 keys, 值为 undefined 的属性将自动填充为 null; 如果未指定 keys,将选择 objectList 所有不是 undefined 项的键的并集
54
54
  * @param keepUndefinedKey - 是否保留 undefined 的键。默认值为 false,如果为 true , 数组的某一个字段均为 undefined时,将忽略字段,
55
55
  */
56
- objectListToValues<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>, keepUndefinedKey?: boolean): SqlValueData;
56
+ objectListToValues<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>, keepUndefinedKey?: boolean): SqlValuesTextData;
57
57
  /**
58
58
  * @deprecated 请使用 objectToValue 代替
59
59
  */
@@ -62,16 +62,17 @@ export declare class SqlValuesCreator {
62
62
  * 将对象转为 SQL 的 value
63
63
  * @example
64
64
  * ```ts
65
- * v.objectToValues({ a: 1, b: "2", c: null, d: undefined }).text // " '1', '2', NULL, DEFAULT "
66
- * v.objectToValues({ a: 1, b: "2", c: null, d: undefined }, ["a", "b"]).text // " '1', '2' "
67
- * v.objectToValues(
65
+ * v.objectToValue({ a: 1, b: "2", c: null, d: undefined }).text // "('1', '2', NULL, DEFAULT)"
66
+ * v.objectToValue({ a: 1, b: "2", c: null, d: undefined }, ["a", "b"]).text // "('1', '2')"
67
+ * v.objectToValue(
68
68
  * { a: 1, b: "2", c: null, d: undefined },
69
69
  * { a: "INT", b: "TEXT", c: "JSONB", d: "TEXT" }
70
- * ).text // " '1'::INT, '2'::TEXT, NULL::JSONB, DEFAULT::TEXT "
70
+ * ).text // "('1'::INT, '2'::TEXT, NULL::JSONB, DEFAULT::TEXT)"
71
71
  * ```
72
72
  * @param keys - 如果指定了key, object undefined 的属性值将填充为 null,如果不指定,将自获取 object 所有非 undefined 的属性的key
73
73
  */
74
- objectToValue<T extends object>(object: T, keys?: ObjectToValueKeys<T>): SqlValueData;
74
+ objectToValue<T extends object>(object: T, keys?: ObjectToValueKeys<T>): SqlValuesTextData;
75
+ private _getObjectValueInfo;
75
76
  private _internalObjectToValues;
76
77
  /**
77
78
  * 将数组列表转为 SQL 的一个 value
@@ -159,17 +159,20 @@ class SqlValuesCreator {
159
159
  }
160
160
  return { columns: keys, text: str };
161
161
  }
162
- /**
163
- * @deprecated 请使用 objectToValue 代替
164
- */
165
- objectToValues(object, keys) {
166
- return this.objectToValue(object, keys).text;
162
+ objectToValues(object, keys_types) {
163
+ const { keys, type } = this._getObjectValueInfo(object, keys_types);
164
+ return this._internalObjectToValues(object, keys, type);
167
165
  }
168
166
  objectToValue(object, keys_types) {
167
+ const { keys, type } = this._getObjectValueInfo(object, keys_types);
168
+ const text = this._internalObjectToValues(object, keys, type);
169
+ return { columns: keys, text: "(" + text + ")" };
170
+ }
171
+ _getObjectValueInfo(object, keys_types) {
169
172
  let type;
170
173
  let keys;
171
174
  if (keys_types instanceof Array) {
172
- keys = keys_types;
175
+ keys = [...keys_types];
173
176
  type = [];
174
177
  }
175
178
  else if (keys_types) {
@@ -180,8 +183,7 @@ class SqlValuesCreator {
180
183
  keys = Object.keys(object);
181
184
  type = [];
182
185
  }
183
- const text = this._internalObjectToValues(object, keys, type);
184
- return { columns: keys, text };
186
+ return { keys, type };
185
187
  }
186
188
  _internalObjectToValues(object, keys, type) {
187
189
  const values = [];
@@ -1,8 +1,8 @@
1
1
  /** @public 断言类型 */
2
2
  export type AssertJsType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
3
3
  /** @public */
4
- export type SqlValueData = {
5
- columns: readonly string[];
4
+ export type SqlValuesTextData = {
5
+ columns: string[];
6
6
  text: string;
7
7
  };
8
8
  /** @public */
@@ -2,5 +2,5 @@ import { ChainInsert } from "./insert_chain.ts";
2
2
  /** @public */
3
3
  export declare function insertInto(target: string): ChainInsert;
4
4
  /** @public */
5
- export declare function insertInto(table: string, columns: string[]): ChainInsert;
5
+ export declare function insertInto(table: string, columns: readonly string[]): ChainInsert;
6
6
  //# sourceMappingURL=insert.d.ts.map
@@ -3,7 +3,7 @@ import { ChainModifyReturning } from "./_modify.ts";
3
3
  /** @public */
4
4
  export interface ChainAfterConflict {
5
5
  doNotThing(): ChainInsertReturning;
6
- doUpdate(set: Constructable<string | string[] | Record<string, string>>): ChainInsertReturning;
6
+ doUpdate(set: Constructable<string | readonly string[] | Record<string, string>>): ChainInsertReturning;
7
7
  toString(): string;
8
8
  }
9
9
  /** @public */
@@ -15,7 +15,7 @@ export interface ChainInsert extends ChainInsertAfterValues {
15
15
  * values(["(18, 'hi')", "(17, 'hh')"]) // " VALUES(18, 'hi'), (17, 'hh')"
16
16
  * ```
17
17
  */
18
- values(statement: Constructable<string | string[]>): ChainInsertAfterValues;
18
+ values(statement: Constructable<string | readonly string[]>): ChainInsertAfterValues;
19
19
  select(statement: Constructable<string>): ChainInsertAfterValues;
20
20
  }
21
21
  /** @public */
@@ -11,8 +11,8 @@ export declare function select<T extends TableType>(columns: Constructable<{
11
11
  /** @public */
12
12
  export declare function select<T extends TableType>(columns: Constructable<string | string[]>): ChainSelect<T>;
13
13
  /** @public */
14
- export declare function select<T extends TableType>(columns: Constructable<string | string[] | {
15
- [key in keyof T]: string | boolean;
14
+ export declare function select<T extends TableType>(columns: Constructable<string | readonly string[] | {
15
+ readonly [key in keyof T]: string | boolean;
16
16
  }>): ChainSelect<T>;
17
17
  /**
18
18
  * 生成 ORDER BY 语句
@@ -1,9 +1,9 @@
1
1
  /** @public */
2
- export type ConditionParam = string | string[];
2
+ export type ConditionParam = string | readonly string[];
3
3
  /** @public */
4
4
  export type Constructable<T> = T | (() => T);
5
5
  /** @public */
6
- export type SelectParam = string | string[] | Record<string, string | boolean>;
6
+ export type SelectParam = string | readonly string[] | Record<string, string | boolean>;
7
7
  /**
8
8
  * @public
9
9
  * @example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",