I have a NestJS application with Drizzle. Currently everything is functional and fine, but the issue that I've run into is that I need to get the NestJS ConfigService injected into a custom column that will encrypt/decrypt the contents automatically, like so:
export const encryptedColumn = customType<{ data: string }>({
dataType() {
return "text";
},
fromDriver(value: unknown) {
//Encryption goes here
},
toDriver(value: string) {
//Decryption goes here
}
});
You get the idea. The fun part is that I need the ConfigService to grab the key for encryption/decryption within the fromDriver and toDriver functions. The current Drizzle integration in NestJS is straightforward at this point, just a simple module:
@Module({
providers: [DrizzleProvider],
exports: [DrizzleProvider],
})
export class DrizzleModule {}
Along with a simple provider that gets injected all over the app as needed:
import * as DbSchema from './schema';
export const DrizzleModuleProvider = 'DrizzleModuleProvider';
export const DrizzleProvider = {
provide: DrizzleModuleProvider,
useFactory: async () => {
const pool = new Pool({
connectionString: process.env.DB_CONN_STRING_GOES_HERE,
ssl: {
rejectUnauthorized: false
},
});
const db = drizzle(pool, {
schema: DbSchema,
});
return db;
}
};
In this case, the schema.ts file is just a big batch of imports from various Drizzle entity declarations in files spread throughout various modules. The fun part is that those entity files have nothing to do with NestJS, at least not directly. They're just simple classes with exported table declarations that get bundled up in the schema.ts file.
So how might I properly get the ConfigService injected into a custom column definition? Wrapping those definitions in an @Injectable() class probably isn't going to work too well since the Drizzle entities aren't using NestJS themselves, so it can't really be injected. This also means that I couldn't pass the ConfigService through a closure to the column definition since the entities don't have access to it in the first place.
Any suggestions on how to do this cleanly without rewriting swaths of the application, or perhaps in the least-hacky fashion?