P
Prismathis hour
vpjm

Dmmf in frontend

Hi, I need to determine whether a database field is an array. To achieve this, I’ve created a function that uses Prisma's DMMF. Additionally, I want to associate multilingual labels with each field in the database. For this, I’ve defined a typed object that maps each field to its translated labels. These two pieces of information — whether a field is an array and its translations — need to be accessible from the frontend. What would be the best way to structure and expose this data to the client ? Thanks a lot for your help!
2 Replies
Prisma AI Help
Prisma AI Helpthis hour
Skipping coffee for instant knowledge! Let's brew that AI answer now. To determine if a field is an array and expose field translations to the frontend, you can use Prisma's DMMF (Data Model Meta Format) which contains schema metadata. Accessing DMMF You can access DMMF through the Prisma client:
const prisma = new PrismaClient()
// @ts-ignore
console.log(prisma._dmmf)
const prisma = new PrismaClient()
// @ts-ignore
console.log(prisma._dmmf)
Or through the Prisma object:
import { Prisma } from '@prisma/client';
console.log(Prisma.dmmf);
import { Prisma } from '@prisma/client';
console.log(Prisma.dmmf);
Note that this is an internal API that's not officially documented and might change in future versions Link to discussion. Checking if a Field is an Array The DMMF contains an isList property for each field that indicates whether it's an array:
const prismaModels = Prisma.dmmf.datamodel.models;
for (const model of prismaModels) {
for (const field of model.fields) {
// field.isList will be true if the field is an array
console.log(`${model.name}.${field.name} is array: ${field.isList}`);
}
}
const prismaModels = Prisma.dmmf.datamodel.models;
for (const model of prismaModels) {
for (const field of model.fields) {
// field.isList will be true if the field is an array
console.log(`${model.name}.${field.name} is array: ${field.isList}`);
}
}
Adding Multilingual Labels For multilingual labels, there's no built-in solution yet. There's an open feature request for column labels in Prisma (Link to issue), but for now, you'll need to create your own solution. Exposing to Frontend Here's a suggested approach: 1. Create a server endpoint that exposes the necessary metadata:
// Server-side
app.get('/api/schema-metadata', (req, res) => {
const metadata = {
fields: {},
translations: yourTranslationsObject // Your custom translations
};

// Extract field types from DMMF
Prisma.dmmf.datamodel.models.forEach(model => {
metadata.fields[model.name] = {};
model.fields.forEach(field => {
metadata.fields[model.name][field.name] = {
isArray: field.isList,
type: field.type
};
});
});

res.json(metadata);
});
// Server-side
app.get('/api/schema-metadata', (req, res) => {
const metadata = {
fields: {},
translations: yourTranslationsObject // Your custom translations
};

// Extract field types from DMMF
Prisma.dmmf.datamodel.models.forEach(model => {
metadata.fields[model.name] = {};
model.fields.forEach(field => {
metadata.fields[model.name][field.name] = {
isArray: field.isList,
type: field.type
};
});
});

res.json(metadata);
});
2. Fetch this data in your frontend application and use it as needed. Remember that using DMMF directly is not part of the official API, so consider generating this metadata during build time if possible to avoid runtime dependencies on internal APIs. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
vpjm
vpjmOPthis hour
That's the solution I was thinking of, but I'm not sure — maybe there's a better way to do it

Did you find this page helpful?