Configure DrizzlePlanetScaleModule
mainThe DrizzlePlanetScaleModule can be initialized using three different NestJS patterns. You can provide a tag to uniquely identify the database instance, which is used later for injection.
Method 1: Static Registration
Use DrizzlePlanetScaleModule.register() to pass a configuration object directly.
Method 2: Asynchronous Registration with useFactory
Use DrizzlePlanetScaleModule.registerAsync() with a useFactory function. This is useful for loading configuration from environment variables or other services.
Method 3: Asynchronous Registration with useClass
Use DrizzlePlanetScaleModule.registerAsync() with a useClass to delegate configuration to a dedicated service class.
import { Module } from '@nestjs/common';
import * as schema from '../db/schema';
import { DrizzlePlanetScaleModule } from '@knaadh/nestjs-drizzle-planetscale';
@Module({
imports: [
// Method #1: Pass options object
DrizzlePlanetScaleModule.register({
tag: 'DB_DEV',
planetscale: {
config: {
username: 'PLANETSCALE_USERNAME',
password: 'PLANETSCALE_PASSWORD',
host: 'PLANETSCALE_HOST',
},
},
config: { schema: { ...schema } },
}),
// Method #2: useFactory()
DrizzlePlanetScaleModule.registerAsync({
tag: 'DB_PROD',
useFactory() {
return {
planetscale: {
config: {
username: 'PLANETSCALE_USERNAME',
password: 'PLANETSCALE_PASSWORD',
host: 'PLANETSCALE_HOST',
},
},
config: { schema: { ...schema } },
};
},
}),
// Method #3: useClass()
DrizzlePlanetScaleModule.registerAsync({
tag: 'DB_STAGING',
useClass: DBConfigService,
}),
],
})
export class AppModule {}
// Example of a configuration class for Method #3
export class DBConfigService {
create = () => {
return {
planetscale: {
config: {
username: 'PLANETSCALE_USERNAME',
password: 'PLANETSCALE_PASSWORD',
host: 'PLANETSCALE_HOST',
},
},
config: { schema: { ...schema } },
};
};
}