Skip to content

asnowc/pg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSR version

SQL 生成器

API 文档

抽象类

DbQuery 抽象类

class YourQuery extends DbQuery {
  execute(sql: QueryInput | MultipleQueryInput): Promise<void> {
    // implement
  }

  query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
  query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
  query<T = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
  query<T = any>(sql: QueryInput | MultipleQueryInput): Promise<QueryRowsResult<T>> {
    // implement
  }
  multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: StringLike): Promise<T> {
    // implement
  }
  /**
   * 执行多语句的方法
   * @deprecated 不建议使用。改用 query()
   */
  abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
}
const db: DbQuery = new YourQuery();
declare const db: DbQuery;

type Row = { name: string; age: number };
const sqlText = "SELECT * FROM user";

const rows: Row[] = await db.queryRows<Row>(sqlText);
const count: number = await db.queryCount(sqlText);
const rows: Map<string, Row> = await db.queryMap<Row>(sqlText, "name");

DbQueryPool 抽象类

class YourPool extends DbQueryPool {
  // implement
}
const pool: DbQueryPool = new YourPool();
普通查询
const conn = await pool.connect();
try {
  await conn.queryRows(sqlText);
} finally {
  conn.release();
}

或者,使用 using 语法更优雅 (推荐)

using conn = await pool.connect();
await conn.queryRows(sqlText);
事务查询
const conn = pool.begin();
try {
  await conn.queryRows(sqlText);
  await conn.queryRows(sqlText);
  await conn.commit();
} catch (e) {
  await conn.rollback();
  throw e;
}

或者,使用 using 语法更优雅 (推荐)

await using conn = pool.begin();

await conn.queryRows(sqlText);
await conn.queryRows(sqlText);
await conn.commit();
游标查询
const cursor = await pool.cursor(sqlText);

let rows = await cursor.read(20);
while (rows.length) {
  console.log(rows);
  rows = await cursor.read(20);
  if (conditions) {
    await cursor.close(); // 提前关闭游标
    break;
  }
}

或者使用 for await of 更优雅 (推荐)

const cursor = await pool.cursor(sqlText);
for await (const element of cursor) {
  console.log(element);
  if (conditions) break; //提前关闭游标
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors