Skip to content

Documentation / @tenancy-node/db / TablePrefixPlugin

Class: TablePrefixPlugin ​

Defined in: db/src/schema/table-prefix-plugin.ts:34

Agrega el prefijo (tenancy_) a cada tabla de las consultas del paquete, así el código usa nombres cortos y tipados ('tenants') y la base ve tenancy_tenants.

Implements ​

Constructors ​

Constructor ​

new TablePrefixPlugin(prefix): TablePrefixPlugin

Defined in: db/src/schema/table-prefix-plugin.ts:37

Parameters ​

prefix ​

string

Returns ​

TablePrefixPlugin

Properties ​

prefix ​

readonly prefix: string

Defined in: db/src/schema/table-prefix-plugin.ts:37

Methods ​

transformQuery() ​

transformQuery(args): RootOperationNode

Defined in: db/src/schema/table-prefix-plugin.ts:41

This is called for each query before it is executed. You can modify the query by transforming its OperationNode tree provided in args.node and returning the transformed tree. You'd usually want to use an OperationNodeTransformer for this.

If you need to pass some query-related data between this method and transformResult you can use a WeakMap with args.queryId as the key:

ts
import type {
  KyselyPlugin,
  QueryResult,
  RootOperationNode,
  UnknownRow
} from 'kysely'

interface MyData {
  // ...
}
const data = new WeakMap<any, MyData>()

const plugin = {
  transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
    const something: MyData = {}

    // ...

    data.set(args.queryId, something)

    // ...

    return args.node
  },

  async transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>> {
    // ...

    const something = data.get(args.queryId)

    // ...

    return args.result
  }
} satisfies KyselyPlugin

You should use a WeakMap instead of a Map or some other strong references because transformQuery is not always matched by a call to transformResult which would leave orphaned items in the map and cause a memory leak.

Parameters ​

args ​

PluginTransformQueryArgs

Returns ​

RootOperationNode

Implementation of ​

KyselyPlugin.transformQuery


transformResult() ​

transformResult(args): Promise<QueryResult<UnknownRow>>

Defined in: db/src/schema/table-prefix-plugin.ts:45

This method is called for each query after it has been executed. The result of the query can be accessed through args.result. You can modify the result and return the modifier result.

Parameters ​

args ​

PluginTransformResultArgs

Returns ​

Promise<QueryResult<UnknownRow>>

Implementation of ​

KyselyPlugin.transformResult

Publicado bajo la licencia MIT.