更新项:
1. 实体的pick和omit增加对toPickData方法的支持 2. database和PostgresClient增加同一的类型IDatabase
This commit is contained in:
161
src/database.ts
161
src/database.ts
@ -4,41 +4,66 @@ import { DeleteBuilder, IDeleteBuilder, IInsertBuilder, InsertBuilder, ISelectBu
|
||||
import { Class } from "./types";
|
||||
import { formatSQL } from "./util";
|
||||
|
||||
interface IPostgresClient {
|
||||
/**
|
||||
* 数据库基本操作
|
||||
*/
|
||||
export interface IDatabase {
|
||||
/**
|
||||
* 执行sql语句
|
||||
* @param sql sql语句
|
||||
* @param args sql参数
|
||||
*/
|
||||
query<R>(sql: string, args?: any[]): Promise<QueryResult<R[]>>;
|
||||
|
||||
/** 开启事务 */
|
||||
trans(): Promise<void>;
|
||||
query<R extends QueryResultRow = any>(sql: string, args?: any[] | Record<string, any>): Promise<QueryResult<R>>
|
||||
|
||||
/**
|
||||
* 查询实体
|
||||
* @param Entity 实体
|
||||
* @param alias 别名
|
||||
*/
|
||||
select<E extends BasicEntity>(Entity: Class<E>, alias?: string): ISelectBuilder<E>;
|
||||
select<E extends BasicEntity>(Entity: Class<E>, alias?: string): ISelectBuilder<E>
|
||||
|
||||
/**
|
||||
* 进行数据库插入
|
||||
* @param Entity 实体
|
||||
*/
|
||||
insert<E extends BasicEntity>(Entity: Class<E>): IInsertBuilder<E>;
|
||||
insert<E extends BasicEntity>(Entity: Class<E>): IInsertBuilder<E>
|
||||
|
||||
/**
|
||||
* 进行实体更新
|
||||
* @param Entity 实体
|
||||
*/
|
||||
update<E extends BasicEntity>(Entity: Class<E>): IUpdateBuilder<E>;
|
||||
update<E extends BasicEntity>(Entity: Class<E>): IUpdateBuilder<E>
|
||||
|
||||
/**
|
||||
* 进行实体删除
|
||||
* @param Entity 实体
|
||||
*/
|
||||
del<E extends BasicEntity>(Entity: Class<E>): IDeleteBuilder<E>;
|
||||
delete<E extends BasicEntity>(Entity: Class<E>): IDeleteBuilder<E>
|
||||
}
|
||||
|
||||
interface IPostgresClient extends IDatabase {
|
||||
/** 开启事务 */
|
||||
trans(): Promise<void>;
|
||||
}
|
||||
|
||||
interface IPostgresDatabase extends IDatabase {
|
||||
/**
|
||||
* 配置数据库
|
||||
* @param loader 配置加载器
|
||||
*/
|
||||
config(loader: () => IPostgresConfig): void;
|
||||
/**
|
||||
* 连接数据库
|
||||
* @param callback 回调
|
||||
*/
|
||||
connect<R>(callback: (client: IPostgresClient) => R | Promise<R>): Promise<R>
|
||||
/**
|
||||
* 执行事务操作
|
||||
* @param callback 回调
|
||||
*/
|
||||
transaction<R>(callback: (client: IPostgresClient) => R | Promise<R>): Promise<R>
|
||||
/** 关闭数据库 */
|
||||
close(): void;
|
||||
}
|
||||
|
||||
interface IPostgresConfig {
|
||||
@ -58,7 +83,7 @@ class PostgresClient implements IPostgresClient {
|
||||
this.#client = client;
|
||||
}
|
||||
|
||||
public query<R>(sql: string, args?: any[] | Record<string, any>): Promise<QueryResult<R[]>> { return this.#client.query(args ? formatSQL(sql, args) : sql); }
|
||||
public query<R extends QueryResultRow = any>(sql: string, args?: any[] | Record<string, any>): Promise<QueryResult<R>> { return this.#client.query(args ? formatSQL(sql, args) : sql); }
|
||||
|
||||
|
||||
public async trans() {
|
||||
@ -69,86 +94,41 @@ class PostgresClient implements IPostgresClient {
|
||||
public get transOn() { return this.#transOn; }
|
||||
|
||||
public release() { this.#client.release(); }
|
||||
|
||||
public select<E extends BasicEntity>(Entity: Class<E>, alias?: string): ISelectBuilder<E> { return new SelectBuilder(this.query.bind(this), Entity, alias); }
|
||||
|
||||
public insert<E extends BasicEntity>(Entity: Class<E>): IInsertBuilder<E> { return new InsertBuilder(this.query.bind(this), Entity); }
|
||||
|
||||
public update<E extends BasicEntity>(Entity: Class<E>): IUpdateBuilder<E> { return new UpdateBuilder(this.query.bind(this), Entity); }
|
||||
|
||||
public del<E extends BasicEntity>(Entity: Class<E>): IDeleteBuilder<E> { return new DeleteBuilder(this.query.bind(this), Entity); }
|
||||
public delete<E extends BasicEntity>(Entity: Class<E>): IDeleteBuilder<E> { return new DeleteBuilder(this.query.bind(this), Entity); }
|
||||
};
|
||||
|
||||
/** 数据库操作相关 */
|
||||
export namespace database {
|
||||
|
||||
let pool: Pool | null = null;
|
||||
let confLoader!: () => IPostgresConfig
|
||||
|
||||
function getPool() {
|
||||
if (!pool) {
|
||||
const conf = confLoader();
|
||||
pool = new Pool({
|
||||
host: conf.host,
|
||||
port: conf.port,
|
||||
user: conf.user,
|
||||
password: conf.password,
|
||||
database: conf.database,
|
||||
max: conf.max,
|
||||
ssl: false,
|
||||
});
|
||||
}
|
||||
return pool;
|
||||
let pool: Pool | null = null;
|
||||
let confLoader!: () => IPostgresConfig
|
||||
function getPool() {
|
||||
if (!pool) {
|
||||
const conf = confLoader();
|
||||
pool = new Pool({
|
||||
host: conf.host,
|
||||
port: conf.port,
|
||||
user: conf.user,
|
||||
password: conf.password,
|
||||
database: conf.database,
|
||||
max: conf.max,
|
||||
ssl: false,
|
||||
});
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置数据库
|
||||
* @param loader 配置加载器
|
||||
*/
|
||||
export function config(loader: () => IPostgresConfig) {
|
||||
|
||||
export const database: IPostgresDatabase = {
|
||||
config(loader: () => IPostgresConfig) {
|
||||
confLoader = loader;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 直接执行sql语句
|
||||
* @param sql sql语句
|
||||
* @param args sql参数
|
||||
*/
|
||||
export function query<R extends QueryResultRow>(sql: string, args?: any[] | Record<string, any>) { return getPool().query<R>(args ? formatSQL(sql, args) : sql); }
|
||||
|
||||
/**
|
||||
* 查询实体
|
||||
* @param Entity 实体
|
||||
* @param alias 别名
|
||||
*/
|
||||
export function select<E extends BasicEntity>(Entity: Class<E>, alias?: string): ISelectBuilder<E> { return new SelectBuilder(database.query, Entity, alias); }
|
||||
|
||||
/**
|
||||
* 进行数据库插入
|
||||
* @param Entity 实体
|
||||
*/
|
||||
export function insert<E extends BasicEntity>(Entity: Class<E>): IInsertBuilder<E> { return new InsertBuilder(database.query, Entity); }
|
||||
|
||||
/**
|
||||
* 进行实体更新
|
||||
* @param Entity 实体
|
||||
*/
|
||||
export function update<E extends BasicEntity>(Entity: Class<E>): IUpdateBuilder<E> { return new UpdateBuilder(database.query, Entity); }
|
||||
|
||||
/**
|
||||
* 进行实体删除
|
||||
* @param Entity 实体
|
||||
*/
|
||||
export function del<E extends BasicEntity>(Entity: Class<E>): IDeleteBuilder<E> { return new DeleteBuilder(database.query, Entity); }
|
||||
|
||||
/**
|
||||
* 连接数据库
|
||||
* @param callback 回调
|
||||
*/
|
||||
export async function connect<R>(callback: (client: PostgresClient) => R | Promise<R>): Promise<R> {
|
||||
async connect(callback) {
|
||||
const conn = await getPool().connect();
|
||||
const client = new PostgresClient(conn);
|
||||
let ret: R;
|
||||
let ret: any;
|
||||
try {
|
||||
ret = await callback(client);
|
||||
if (client.transOn) await client.query("commit");
|
||||
@ -162,21 +142,22 @@ export namespace database {
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 执行事务操作
|
||||
* @param callback 回调
|
||||
*/
|
||||
export function transaction<R>(callback: (client: PostgresClient) => R | Promise<R>): Promise<R> {
|
||||
return connect(async (client) => {
|
||||
async transaction(callback) {
|
||||
return this.connect(async (client) => {
|
||||
await client.trans();
|
||||
return callback(client);
|
||||
});
|
||||
};
|
||||
},
|
||||
|
||||
/** 关闭数据库 */
|
||||
export function close() {
|
||||
close() {
|
||||
if (pool) pool.end().catch((err) => console.error(err));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
query(sql, args) { return getPool().query(args ? formatSQL(sql, args) : sql); },
|
||||
select(Entity, alias) { return new SelectBuilder(this.query.bind(this), Entity, alias); },
|
||||
insert(Entity) { return new InsertBuilder(this.query.bind(this), Entity); },
|
||||
update(Entity) { return new UpdateBuilder(this.query.bind(this), Entity); },
|
||||
delete(Entity) { return new DeleteBuilder(this.query.bind(this), Entity); },
|
||||
};
|
Reference in New Issue
Block a user