@asla/yoursql 0.8.7 → 0.8.8

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,7 +1,7 @@
1
1
  import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
2
  import { DbQuery } from "./DbQuery.ts";
3
- import type { MultipleQueryResult, QueryRowsResult } from "./DbQuery.ts";
4
- import type { DbConnection, TransactionMode } from "./interfaces.ts";
3
+ import type { MultipleQueryResult, QueryRowsResult, DbQueryBase } from "./DbQueryBase.ts";
4
+ import type { StringLike, TransactionMode } from "./interfaces.ts";
5
5
  /**
6
6
 
7
7
  /**
@@ -10,15 +10,11 @@ import type { DbConnection, TransactionMode } from "./interfaces.ts";
10
10
  */
11
11
  export declare class DbPoolConnection extends DbQuery {
12
12
  #private;
13
- constructor(conn: DbConnection, onRelease: () => void);
13
+ constructor(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void);
14
14
  begin(mode?: TransactionMode): Promise<void>;
15
15
  query<T = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
16
- query<T = any>(sql: {
17
- toString(): string;
18
- }): Promise<QueryRowsResult<T>>;
19
- multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: {
20
- toString(): string;
21
- }): Promise<T>;
16
+ query<T = any>(sql: StringLike): Promise<QueryRowsResult<T>>;
17
+ multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: StringLike): Promise<T>;
22
18
  rollback(): Promise<void>;
23
19
  commit(): Promise<void>;
24
20
  get released(): boolean;
@@ -42,9 +42,10 @@ class DbPoolConnection extends DbQuery {
42
42
  }
43
43
  /** 调用 release() 时,如果事务未提交,则抛出异常 */
44
44
  release() {
45
- if (this.#conn) {
45
+ const conn = this.#conn;
46
+ if (conn) {
46
47
  this.#conn = undefined;
47
- this.#onRelease();
48
+ this.#onRelease(conn);
48
49
  }
49
50
  }
50
51
  //implement
@@ -1,8 +1,8 @@
1
1
  import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
2
  import { DbQuery } from "./DbQuery.ts";
3
- import type { MultipleQueryResult, QueryRowsResult } from "./DbQuery.ts";
3
+ import type { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
4
4
  import type { DbPoolConnection } from "./DbPoolConnection.ts";
5
- import type { DbTransaction, TransactionMode } from "./interfaces.ts";
5
+ import type { DbTransaction, StringLike, TransactionMode } from "./interfaces.ts";
6
6
  /** @public */
7
7
  export type DbPoolTransactionOption = {
8
8
  errorRollback?: boolean;
@@ -21,13 +21,9 @@ export declare class DbPoolTransaction extends DbQuery implements DbTransaction
21
21
  savePoint(savePoint: string): Promise<void>;
22
22
  rollbackTo(savePoint: string): Promise<void>;
23
23
  query<T extends object = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
24
- query<T extends object = any>(sql: {
25
- toString(): string;
26
- }): Promise<QueryRowsResult<T>>;
24
+ query<T extends object = any>(sql: StringLike): Promise<QueryRowsResult<T>>;
27
25
  multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlStatementDataset<T>): Promise<T>;
28
- multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: {
29
- toString(): string;
30
- }): Promise<T>;
26
+ multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: StringLike): Promise<T>;
31
27
  get released(): boolean;
32
28
  [Symbol.asyncDispose](): Promise<void>;
33
29
  }
@@ -1,45 +1,22 @@
1
1
  import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
- /** @public */
3
- export interface SingleQueryResult {
4
- rowCount: number;
5
- rows?: any[];
6
- }
7
- /** @public */
8
- export interface QueryRowsResult<T = any> extends SingleQueryResult {
9
- rowCount: number;
10
- rows: T[];
11
- }
12
- /** @public */
13
- export type MultipleQueryResult = SingleQueryResult[];
14
- /** @public */
15
- export type QueryResult = MultipleQueryResult | SingleQueryResult;
2
+ import { StringLike } from "./interfaces.ts";
3
+ import { MultipleQueryResult, DbQueryBase, QueryRowsResult } from "./DbQueryBase.ts";
16
4
  /**
17
5
  * SQL 查询相关操作
18
6
  * @public
19
7
  */
20
- export declare abstract class DbQuery {
21
- /** 单语句查询,不应查询条语句,否则返回错误值 */
22
- abstract query<T = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
23
- /** 单语句查询,不应查询条语句,否则返回错误值 */
24
- abstract query<T = any>(sql: {
25
- toString(): string;
26
- }): Promise<QueryRowsResult<T>>;
27
- /** 多语句查询 */
28
- abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlStatementDataset<T>): Promise<T>;
29
- /** 多语句查询 */
30
- abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: {
31
- toString(): string;
32
- }): Promise<T>;
8
+ export declare abstract class DbQuery implements DbQueryBase {
9
+ abstract query<T = any>(sql: StringLike): Promise<QueryRowsResult<T>>;
10
+ abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: StringLike): Promise<T>;
33
11
  /** 单语句查询受影响的行 */
34
- queryCount(sql: string | {
35
- toString(): string;
36
- }): Promise<number>;
37
- /** 单语句查询,不应查询条语句,否则返回错误值 */
12
+ queryCount(sql: string | StringLike): Promise<number>;
13
+ /** 单语句查询,不应查询多语句,否则返回错误值 */
38
14
  queryRows<T = any>(sql: SqlStatementDataset<T>): Promise<T[]>;
39
- /** 单语句查询,不应查询条语句,否则返回错误值 */
40
- queryRows<T = any>(sql: {
41
- toString(): string;
42
- }): Promise<T[]>;
15
+ /** 单语句查询,不应查询多语句,否则返回错误值 */
16
+ queryRows<T = any>(sql: StringLike): Promise<T[]>;
17
+ /** 单语句查询,只返回第一行。如果查询没有返回行,则抛出异常。 */
18
+ queryFirstRow<T = any>(sql: SqlStatementDataset<T>): Promise<T>;
19
+ queryFirstRow<T = any>(sql: StringLike): Promise<T>;
43
20
  /**
44
21
  * 查询行
45
22
  * 不应查询单条语句,否则返回错误值
@@ -47,22 +24,18 @@ export declare abstract class DbQuery {
47
24
  multipleQueryRows<T extends any[] = any[]>(sql: SqlStatementDataset<T>): Promise<T[]>;
48
25
  /**
49
26
  * 查询行
50
- * 不应查询条语句,否则返回错误值
27
+ * 不应查询单语句,否则返回错误值
51
28
  */
52
- multipleQueryRows<T extends any[] = any[]>(sql: {
53
- toString(): string;
54
- }): Promise<T[]>;
29
+ multipleQueryRows<T extends any[] = any[]>(sql: StringLike): Promise<T[]>;
55
30
  /**
56
31
  * 指定某一列为key,返回 key 到 row 的映射
57
- * 单语句查询,不应查询条语句,否则返回错误值
32
+ * 单语句查询,不应查询多语句,否则返回错误值
58
33
  */
59
34
  queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: SqlStatementDataset<T>, key: K): Promise<Map<T[K], T>>;
60
35
  /**
61
36
  * 指定某一列为key,返回 key 到 row 的映射
62
- * 单语句查询,不应查询条语句,否则返回错误值
37
+ * 单语句查询,不应查询多语句,否则返回错误值
63
38
  */
64
- queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: {
65
- toString(): string;
66
- }, key: K): Promise<Map<T[K], T>>;
39
+ queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: StringLike, key: K): Promise<Map<T[K], T>>;
67
40
  }
68
41
  //# sourceMappingURL=DbQuery.d.ts.map
@@ -14,6 +14,13 @@ class DbQuery {
14
14
  queryRows(sql) {
15
15
  return this.query(sql.toString()).then((res) => res.rows);
16
16
  }
17
+ queryFirstRow(sql) {
18
+ return this.query(sql.toString()).then(({ rows, rowCount }) => {
19
+ if (rows.length === 0)
20
+ throw new Error("Query did not return any rows");
21
+ return rows[0];
22
+ });
23
+ }
17
24
  multipleQueryRows(sql) {
18
25
  return this.multipleQuery(sql.toString()).then((res) => res.map((item) => item.rows ?? []));
19
26
  }
@@ -0,0 +1,26 @@
1
+ import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
+ import { StringLike } from "./interfaces.ts";
3
+ /** @public */
4
+ export interface SingleQueryResult {
5
+ rowCount: number;
6
+ rows?: any[];
7
+ }
8
+ /** @public */
9
+ export interface QueryRowsResult<T = any> extends SingleQueryResult {
10
+ rowCount: number;
11
+ rows: T[];
12
+ }
13
+ /** @public */
14
+ export type MultipleQueryResult = SingleQueryResult[];
15
+ /** @public */
16
+ export type QueryResult = MultipleQueryResult | SingleQueryResult;
17
+ /** @public */
18
+ export interface DbQueryBase {
19
+ /** 单语句查询,不应查询多语句,否则返回错误值 */
20
+ query<T = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
21
+ /** 单语句查询,不应查询多语句,否则返回错误值 */
22
+ query<T = any>(sql: StringLike): Promise<QueryRowsResult<T>>;
23
+ /** 多语句查询 */
24
+ multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: StringLike): Promise<T>;
25
+ }
26
+ //# sourceMappingURL=DbQueryBase.d.ts.map
@@ -59,8 +59,10 @@ export interface DbQueryPool extends DbQuery {
59
59
  totalCount: number;
60
60
  begin(mode?: TransactionMode): DbTransaction;
61
61
  cursor<T extends {}>(sql: SqlStatementDataset<T>): Promise<DbCursor<T>>;
62
- cursor<T>(sql: {
63
- toString(): string;
64
- }, option?: DbCursorOption): Promise<DbCursor<T>>;
62
+ cursor<T>(sql: StringLike, option?: DbCursorOption): Promise<DbCursor<T>>;
65
63
  }
64
+ /** @public */
65
+ export type StringLike = {
66
+ toString(): string;
67
+ } | string;
66
68
  //# sourceMappingURL=interfaces.d.ts.map
@@ -1,5 +1,6 @@
1
1
  export * from "./errors.ts";
2
2
  export * from "./interfaces.ts";
3
+ export * from "./DbQueryBase.ts";
3
4
  export * from "./DbQuery.ts";
4
5
  export * from "./DbCursor.ts";
5
6
  export * from "./DbPoolConnection.ts";
@@ -20,6 +20,8 @@ export declare class DbTable<T extends TableType> {
20
20
  [key: string]: string | boolean;
21
21
  }>, as?: string): ChainSelect<Record<string, any>>;
22
22
  /** 选择单表 */
23
+ select<R extends {} = Record<string, any>>(columns: Constructable<string | string[]>, as?: string): ChainSelect<R>;
24
+ /** 选择单表 */
23
25
  select<R extends {}>(columns: Constructable<{
24
26
  [key in keyof R]: boolean | string;
25
27
  } | string | string[]>, as?: string): ChainSelect<R>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.8.7",
3
+ "version": "0.8.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",