Skip to content

MySQL Adapter API ​

@toshihiko/mysql-adapter 使用 mysql2/promise 实现 Adapter 契约。本页的构造与执行部分适用于应用;查询快照和 SQL Builder 部分面向 Adapter 扩展开发者。

应用配置 ​

typescript
type MySQLValue =
  | string
  | number
  | bigint
  | boolean
  | Date
  | null
  | undefined
  | Blob
  | Buffer
  | Uint8Array
  | Raw
  | ({} | null | undefined)[]
  | { [key: string]: MySQLValue };

type MySQLValues =
  | readonly MySQLValue[]
  | Readonly<Record<string, MySQLValue>>;

type MySQLShowSql = false | true | ((sql: string) => void);

interface MySQLAdapterOptions extends Omit<
  PoolOptions,
  'database' | 'password' | 'user'
> {
  readonly cache?: CacheSource;
  readonly database?: string;
  readonly password?: string;
  readonly package?: string;
  readonly pool?: MySQLPool;
  readonly showSql?: MySQLShowSql;
  readonly user?: string;
  readonly username?: string;
}
typescript
const database = new Toshihiko('mysql', {
  database: 'app',
  host: '127.0.0.1',
  password: 'secret',
  username: 'root',
});
配置类型默认值说明
databasestring'toshihiko'数据库名和 Cache 命名空间
hoststring'localhost'MySQL 主机名或 IP
portnumber3306MySQL 端口
userstring''MySQL 用户名
usernamestring—user 的兼容写法;同时提供时优先使用
passwordstring''MySQL 密码
poolMySQLPool—注入现有 Pool;省略时按其余配置创建 Pool
showSqlfalse | true | ((sql: string) => void)falseSQL 日志开关或日志函数
cacheCache | CacheOptions—现有 Cache 实例,或包含 module、path、name 及其构造参数的模块配置;Model 默认继承
packagestring—兼容字段;运行时驱动始终是 mysql2
其他配置mysql2.PoolOptions由 mysql2 决定原样传给 mysql2.createPool()

MySQLAdapterOptions 继承 mysql2 的 PoolOptions,所以还可以设置 connectionLimit、charset、ssl、connectTimeout 等驱动配置。

业务代码通过 database.execute()、Model、Query 与事务方法工作,不需要取得 MySQLAdapter 实例。

原始执行 ​

typescript
type MySQLExecuteArguments =
  | readonly [sql: string, values?: MySQLValues]
  | readonly [
      connection: MySQLConnection | null,
      sql: string,
      values?: MySQLValues,
    ];

type MySQLQueryExecuteArguments = readonly [
  sql: string,
  values?: MySQLValues,
];

database.execute(
  ...args: MySQLExecuteArguments
): Promise<MySQLQueryResult>
query.execute(
  ...args: MySQLQueryExecuteArguments
): Promise<MySQLQueryResult>

数组值通常使用 execute();对象值或包含 ?? 的 SQL 使用 query()。showSql 会在执行前收到格式化 SQL。

事务 ​

typescript
const connection = await User.beginTransaction();
try {
  await User.conn(connection).execute('UPDATE ...', values);
  await User.commit(connection);
} catch (error) {
  await User.rollback(connection);
  throw error;
}

事务开始失败、提交或回滚结束后,连接都会按契约释放。

扩展实现 ​

MySQLAdapter 为 Adapter 作者公开 find()、count()、updateByQuery()、deleteByQuery()、insert() 与 update()。它们接收 Adapter Query 快照 或 Model/Field 数据,不接收应用的可变 Query 实例。

typescript
class MySQLAdapter extends Adapter<
  MySQLAdapterOptions,
  MySQLModel,
  MySQLConnection,
  MySQLField,
  DataValue,
  MySQLQuery,
  AdapterExecuteSpec<
    MySQLExecuteArguments,
    MySQLQueryExecuteArguments,
    MySQLQueryResult
  >
> {
  readonly database: string;
  readonly mysql: MySQLPool;
  readonly package: 'mysql2';
  readonly username: string;
  readonly format: (sql: string, values?: MySQLValues) => string;

  find(query: MySQLQuery, options?: AdapterFindOptions):
    Promise<AdapterRow | readonly AdapterRow[] | null>;
  count(query: MySQLQuery): Promise<number>;
  updateByQuery(query: MySQLQuery): Promise<MySQLMutationResult>;
  deleteByQuery(query: MySQLQuery): Promise<MySQLMutationResult>;
  insert(model: MySQLModel, connection: MySQLConnection | null,
    data: readonly AdapterData<MySQLField, DataValue>[]): Promise<AdapterRow>;
  update(model: MySQLModel, connection: MySQLConnection | null,
    primaryKey: DataRow,
    data: readonly AdapterData<MySQLField, DataValue>[]):
    Promise<MySQLMutationResult>;
  beginTransaction(): Promise<MySQLConnection>;
  commit(connection: MySQLConnection): Promise<void>;
  rollback(connection: MySQLConnection): Promise<void>;
}

findWithCache() 会先读取主键、命中缓存并并发补齐 miss;更新与删除在执行 SQL 前使相关主键缓存失效。

MySQLSqlBuilder ​

参数化方法的完整公开签名为:

typescript
interface MySQLStatement {
  readonly sql: string;
  readonly values: readonly MySQLValue[];
}

compileFieldWhere(model: MySQLModel, key: string, condition: DataValue, logic?: string): MySQLStatement
compileArrayWhere(model: MySQLModel, condition: readonly DataRow[], logic?: string): MySQLStatement
compileWhere(model: MySQLModel, condition: DataRow | readonly DataRow[], logic?: string): MySQLStatement
compileSet(model: MySQLModel, update: DataRow): MySQLStatement
compileValue(field: MySQLField, value: DataValue): MySQLStatement
compileFind(model: MySQLModel, options?: MySQLQueryOptions): MySQLStatement
compileUpdate(model: MySQLModel, options?: MySQLQueryOptions): MySQLStatement
compileDelete(model: MySQLModel, options?: MySQLQueryOptions): MySQLStatement
compileSql(type: string, model: MySQLModel, options?: MySQLQueryOptions): MySQLStatement

执行 SQL 时应优先使用参数化的 compile*() 方法。make*() 方法返回格式化字符串,主要用于兼容和检查。

导出类型 ​

软件包导出 MySQLAdapterOptions、MySQLConnection、MySQLPool、MySQLQueryResult、MySQLMutationResult、MySQLValue、MySQLValues、MySQLExecuteArguments、MySQLStatement,以及 Adapter 边界所需的 MySQLModel、MySQLQuery 与 MySQLField。

Released under the MIT License.