翻译进度
2
分块数量
0
参与人数

适配器

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


The BaseModel class DOES NOT interact with the query builders directly. Instead, it relies on the Model adapter class to construct the query builder instances for different database operation.

This separation allows you swap the adapter with your custom implementation to cover advanced use cases.

Creating a custom adapter

Every custom adapter must adhere to the AdapterContract interface.

import { AdapterContract } from '@ioc:Adonis/Lucid/Orm'
class MyAdapter implements AdapterContract {}

You can assign the adapter to the model as follows:

class User extends BaseModel {
  public static $adapter = new MyAdapter()
}

Methods/Properties

Following is the list of methods/properties that every adapter must have.

modelConstructorClient

Returns the query client for a given model constructor.

import { AdapterContract, ModelAdapterOptions } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public modelConstructorClient(model: typeof BaseModel, options?: ModelAdapterOptions) {
    const connection = options?.connection || model.connection
    return connection ? Database.connection(connection) : Database.connection()
  }
}

modelClient

Returns the query client for a given model instance. The default implementation resolves the client as follows

  • Return the transaction client if the model has $trx property defined
  • Return the query client for a given connection if model instance has $options.connection property defined.
  • Finally, look for the connection property on the model constructor (aka static connection property).
import { AdapterContract } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public modelClient(instance: BaseModel) {
  }
}

query

Return the query builder instance for a given Model constructor. The Model.query method internals calls the query method on the adapter.

import { AdapterContract, ModelAdapterOptions } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public query(model: typeof BaseModel, options?: ModelAdapterOptions) {
    return Database.modelQuery(model)
  }
}

insert

Perform the insert operation for a given model instance. The method receives the model instance and an object of attributes to insert.

import { AdapterContract } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public async insert(instance: BaseModel, attributes: any) {
  }
}

update

Perform the update operation for a given model instance. The method receives the model instance and an object of attributes to update.

import { AdapterContract } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public async update(instance: BaseModel, dirtyAttributes: any) {
  }
}

delete

Perform the delete operation for a given model instance. The method receives only the model instance.

import { AdapterContract } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public async delete(instance: BaseModel) {
  }
}

refresh

Refresh the model instance by performing a select query and hydrating its attributes. The method receives only the model instance.

import { AdapterContract } from '@ioc:Adonis/Lucid/Orm'

class MyAdapter implements AdapterContract {
  public async refresh(instance: BaseModel) {
  }
}

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

讨论数量: 0
发起讨论 只看当前版本


暂无话题~