#How to substitute few lines of code only for development
7 messages · Page 1 of 1 (latest)
I have this code ```typescript
import { Prisma, PrismaClient } from '@prisma/client'
import { createSoftDeleteExtension } from 'prisma-extension-soft-delete';
import { withNestedOperations } from "prisma-extension-nested-operations";
const softDeleteFieldName = 'deletedAt';
// Get all models that have field softDeleteFieldName
const modelsWithSoftDeleteField = Prisma.dmmf.datamodel.models.filter((model) =>
model.fields.some((field) => field.name === softDeleteFieldName)
)
const prisma = new PrismaClient();
export default prisma.$extends({
query: {
$allModels: {
$allOperations: withNestedOperations({
async $rootOperation(params) {
// update root params here
const result = params.query(params.args);
// update root result here
return result;
},
async $allNestedOperations(params) {
// update nested params here
if ((params.operation == "update") || (params.operation == "updateMany")) {
params.args.data.updatedAt = new Date();
}
const result = await params.query(params.args);
// update nested result here
return result;
},
}),
},
},
}).$extends(
createSoftDeleteExtension({
// Select all models with softDeleteFieldName to use soft delete extension
models: Object.fromEntries(modelsWithSoftDeleteField.map((model) => [model.name, {
field: softDeleteFieldName,
allowToOneUpdates: true,
createValue: (deleted: boolean) => {
if (deleted) return new Date();
return null;
},
}])),
})
);
I want to replace this
const prisma = new PrismaClient();
with this
const client = new PGlite(undefined);
const migration = await readFile('./prisma/ddl.sql', 'utf8')
await client.exec(migration)
const adapter = new PrismaPGlite(client);
const prisma = new PrismaClient({ adapter });
so that the above code will become
import { Prisma, PrismaClient } from '@prisma/client'
import { PGlite } from '@electric-sql/pglite';
import { PrismaPGlite } from 'pglite-prisma-adapter';
import { createSoftDeleteExtension } from 'prisma-extension-soft-delete';
import { withNestedOperations } from "prisma-extension-nested-operations";
import { readFile } from 'fs/promises';
const softDeleteFieldName = 'deletedAt';
// Get all models that have field `softDeleteFieldName`
const modelsWithSoftDeleteField = Prisma.dmmf.datamodel.models.filter((model) =>
model.fields.some((field) => field.name === softDeleteFieldName)
)
const client = new PGlite(undefined);
const migration = await readFile('./prisma/ddl.sql', 'utf8')
await client.exec(migration)
const adapter = new PrismaPGlite(client);
const prisma = new PrismaClient({ adapter });
// const prisma = new PrismaClient();
export default prisma.$extends({
query: {
$allModels: {
$allOperations: withNestedOperations({
async $rootOperation(params) {
// update root params here
const result = params.query(params.args);
// update root result here
return result;
},
async $allNestedOperations(params) {
// update nested params here
if ((params.operation == "update") || (params.operation == "updateMany")) {
params.args.data.updatedAt = new Date();
}
const result = await params.query(params.args);
// update nested result here
return result;
},
}),
},
},
}).$extends(
createSoftDeleteExtension({
models: Object.fromEntries(modelsWithSoftDeleteField.map((model) => [model.name, {
field: softDeleteFieldName,
allowToOneUpdates: true,
createValue: (deleted: boolean) => {
if (deleted) return new Date();
return null;
},
}])),
})
);
you can write a rollup/vite plugin
that checks process.env.NODE_ENV and if that's equal to development, do code transforms
I found a solution: https://github.com/ikeq/vite-plugin-filter-replace
Apply filename based replacements. Contribute to ikeq/vite-plugin-filter-replace development by creating an account on GitHub.