Open Nik-Novak opened 6 months ago
return prisma.$extends({
query:{
$allOperations({ model, operation, args, query }) {
/* your custom logic for modifying all Prisma Client operations here */
return query(args)
}
},
model: {
$allModels:{
polish<T>(
this:T,
valueToPolish:Prisma.Args<T, 'create'>['data'], //corresponding model input type
schemaName = Prisma.getExtensionContext(this).$name
) :Prisma.Result<T, undefined, 'findFirst'>/* corresponding model return type */ {
let ctx = Prisma.getExtensionContext(this);
if(Array.isArray(valueToPolish))//@ts-expect-error
return valueToPolish.map(v=>this.polish(v, schemaName))
if(typeof valueToPolish !== 'object' || valueToPolish === null)
return valueToPolish;
let schemaReference = Prisma.dmmf.datamodel.models.find(m=>m.name==schemaName) || Prisma.dmmf.datamodel.types.find(t=>t.name==schemaName);
let polishedData = {} as Prisma.Result<T, undefined, 'findFirst'>;
schemaReference?.fields.forEach(field=>{
let value = valueToPolish[field.name];
if(value==undefined) // SOURCE FROM DBNAME such as __v in the case that v doesnt exist
value=(field.dbName && valueToPolish[field.dbName])
if(field.kind === 'object') {
//@ts-expect-error
value = this.polish(value, field.type); //Prisma.getExtensionContext(this) this is helpful too, in case of differing order of definition
}
//@ts-expect-error
polishedData[field.name] = value;
});
return polishedData;
}
},
},
})
}
Here's a solution to add a model extension polish method. Typing could be improved
I love the concept, honestly it's severely needed.
This doesn't seem to be able to support mongodb schemas with composite types.