Nestjs Prisma extension
Hi everyone
I'm trying to fill the
- I think my custom
- Typescript is complaining because I didn't fill the code on the
This is my prisma service:
I'm trying to fill the
codecode field with a custom nanoid()nanoid() with only uppercase letters and numbers, but when I do the $extends$extends with my logic, I have two problems:- I think my custom
nanoid()nanoid() isn't working- Typescript is complaining because I didn't fill the code on the
.create.create This is my prisma service:
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(PrismaService.name);
#nanoid: (size?: number) => string = undefined;
constructor() {
super();
this.#nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 4);
}
async onModuleInit() {
this.logger.log(`CODE: ${this.#nanoid()}`);
this.$extends({
model: {
customer: {
create: ({ args, query }) => {
args.data = {
...args.data,
code: this.#nanoid(),
};
query(args);
},
},
},
}).$extends(
createSoftDeleteExtension({
models: {
User: true,
Customer: true,
},
defaultConfig: {
field: 'deletedAt',
createValue: (deleted) => {
if (deleted) {
return new Date();
}
return null;
},
},
}),
);
await this.$connect();
}
async onModuleDestroy() {
await this.$disconnect();
}
}@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(PrismaService.name);
#nanoid: (size?: number) => string = undefined;
constructor() {
super();
this.#nanoid = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 4);
}
async onModuleInit() {
this.logger.log(`CODE: ${this.#nanoid()}`);
this.$extends({
model: {
customer: {
create: ({ args, query }) => {
args.data = {
...args.data,
code: this.#nanoid(),
};
query(args);
},
},
},
}).$extends(
createSoftDeleteExtension({
models: {
User: true,
Customer: true,
},
defaultConfig: {
field: 'deletedAt',
createValue: (deleted) => {
if (deleted) {
return new Date();
}
return null;
},
},
}),
);
await this.$connect();
}
async onModuleDestroy() {
await this.$disconnect();
}
}