创建自定义驱动程序
如果你想使用当前不支持的数据库,你可以实现自己的驱动程序。为此,你需要设计 4 个类:
¥If you want to use database that is not currently supported, you can implement your own driver. To do so, you will need to design 4 classes:
平台
¥Platform
平台是一个提供有关给定驱动程序可用功能的信息的类:
¥Platform is a class that provides information about available features of given driver:
import { Platform } from '@mikro-orm/core';
export class MyCustomPlatform extends Platform {
protected abstract schemaHelper: MyCustomSchemaHelper;
// here you can override default settings
usesPivotTable(): boolean;
supportsTransactions(): boolean;
supportsSavePoints(): boolean;
getNamingStrategy(): { new (): NamingStrategy; };
getIdentifierQuoteCharacter(): string;
getParameterPlaceholder(index?: number): string;
usesReturningStatement(): boolean;
normalizePrimaryKey<T = number | string>(data: IPrimaryKey): T;
denormalizePrimaryKey(data: IPrimaryKey): IPrimaryKey;
getSerializedPrimaryKeyField(field: string): string;
}
SchemaHelper
平台的一部分是 SchemaHelper
,它提供有关如何构建架构的信息。
¥Part of platform is a SchemaHelper
, that provides information about how to build schema.
import { SchemaHelper } from '@mikro-orm/core';
export class MyCustomSchemaHelper extends SchemaHelper {
// here you can override default settings
getIdentifierQuoteCharacter(): string;
getSchemaBeginning(): string;
getSchemaEnd(): string;
getSchemaTableEnd(): string;
getAutoIncrementStatement(meta: EntityMetadata): string;
getPrimaryKeySubtype(meta: EntityMetadata): string;
getTypeDefinition(prop: EntityProperty, types?: Record<string, string>, lengths?: Record<string, number>): string;
getUnsignedSuffix(prop: EntityProperty): string;
supportsSchemaConstraints(): boolean;
supportsSchemaMultiAlter(): boolean;
supportsSequences(): boolean;
quoteIdentifier(field: string): string;
dropTable(meta: EntityMetadata): string;
indexForeignKeys(): boolean;
}
连接
¥Connection
下一部分是连接封装器,它将负责查询数据库:
¥Next part is connection wrapper, that will be responsible for querying the database:
import { Connection } from '@mikro-orm/core';
export class MyCustomConnection extends Connection {
// implement abstract methods
connect(): Promise<void>;
isConnected(): Promise<boolean>;
close(force?: boolean): Promise<void>;
getDefaultClientUrl(): string;
execute(query: string, params?: any[], method?: 'all' | 'get' | 'run'): Promise<QueryResult | any | any[]>;
}
驱动程序
¥Driver
最后一部分是驱动程序,负责使用连接将更改持久保存到数据库。如果你正在构建 SQL 驱动程序,扩展 AbstractSqlDriver
可能会很方便,如果不是,请扩展 DatabaseDriver
抽象类。
¥Last part is driver, that is responsible for using the connection to persist changes to database. If you are building SQL driver, it might be handy to extend AbstractSqlDriver
, if not, extend DatabaseDriver
abstract class.
如果你想要拥有绝对控制权,你可以通过 IDatabaseDriver
接口自己实现整个驱动程序。
¥If you want to have absolute control, you can implement the whole driver yourself via IDatabaseDriver
interface.
import { DatabaseDriver } from '@mikro-orm/core';
export class MyCustomDriver extends DatabaseDriver {
// initialize connection and platform
protected readonly connection = new MyCustomConnection(this.config);
protected readonly platform = new MyCustomPlatform;
// and implement abstract methods
find<T extends AnyEntity>(entityName: string, where: FilterQuery<T>, populate?: string[], orderBy?: Record<string, QueryOrder>, limit?: number, offset?: number): Promise<T[]>;
findOne<T extends AnyEntity>(entityName: string, where: FilterQuery<T> | string, populate: string[]): Promise<T | null>;
nativeInsert<T extends AnyEntityType<T>>(entityName: string, data: EntityData<T>): Promise<QueryResult>;
nativeUpdate<T extends AnyEntity>(entityName: string, where: FilterQuery<T> | IPrimaryKey, data: EntityData<T>): Promise<QueryResult>;
nativeDelete<T extends AnyEntity>(entityName: string, where: FilterQuery<T> | IPrimaryKey): Promise<QueryResult>;
count<T extends AnyEntity>(entityName: string, where: FilterQuery<T>): Promise<number>;
}