@dudousxd/nestjs-filter-drizzle
Version:
Drizzle ORM adapter for @dudousxd/nestjs-filter.
67 lines • 2.93 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var DrizzleFilterModule_1;
import { FILTER_ADAPTER_IMPL } from '@dudousxd/nestjs-filter';
import { Global, Module, } from '@nestjs/common';
import { DrizzleAdapter } from './drizzle.adapter.js';
function adapterProvider(options) {
const { connection, db, ...rest } = options;
const { imports: _imports, ...adapterOptions } = rest;
if (db !== undefined) {
return { provide: FILTER_ADAPTER_IMPL, useValue: new DrizzleAdapter(db, adapterOptions) };
}
return {
provide: FILTER_ADAPTER_IMPL,
useFactory: (instance) => new DrizzleAdapter(instance, adapterOptions),
inject: [connection],
};
}
let DrizzleFilterModule = DrizzleFilterModule_1 = class DrizzleFilterModule {
/**
* Registers the Drizzle adapter as the application-wide filter adapter.
* The `connection` token must be resolvable from this module: provide it
* globally (the usual `@Global()` database module), or pass the module that
* exports it in `imports`.
*/
static forRoot(options) {
return {
module: DrizzleFilterModule_1,
imports: options.imports ?? [],
providers: [adapterProvider(options)],
exports: [FILTER_ADAPTER_IMPL],
};
}
};
DrizzleFilterModule = DrizzleFilterModule_1 = __decorate([
Global(),
Module({})
], DrizzleFilterModule);
export { DrizzleFilterModule };
/**
* The adapter as a descriptor, for `FilterModule.forRoot({ adapter })`.
*
* A function, not a constant, because the database token is the app's own —
* the same argument `DrizzleFilterModule.forRoot` takes. Preferred over the
* module form: one module owns the adapter token instead of two, so there is
* nothing for the container to disambiguate. The `connection` token must be
* provided globally (FilterModule's own providers resolve it).
*
* ```ts
* FilterModule.forRoot({ adapter: drizzleAdapter({ connection: DRIZZLE, schema }) })
* ```
*/
export const drizzleAdapter = (options) => {
const { connection, db, ...adapterOptions } = options;
if (db !== undefined) {
return { useFactory: () => new DrizzleAdapter(db, adapterOptions), inject: [] };
}
return {
useFactory: (instance) => new DrizzleAdapter(instance, adapterOptions),
inject: [connection],
};
};
//# sourceMappingURL=module.js.map