w3cj / prisma-erd-generator-markdown

16 stars 7 forks source link

Breaking changes related to Prisma 5.0: Field shape for Boolean values #46

Open JiveOff opened 1 year ago

JiveOff commented 1 year ago

Hey @w3cj 👋

I encountered an error with the library and it might be related to a breaking change introduced by Prisma 5.

Permalink to genMermaid.ts on the repo.

/app/node_modules/prisma-erd-generator-markdown/dist/helpers/genMermaid.js:63
                const defaultValue = field.default ? `${field.default.name}(${field.default.args.join(', ')})` : '';
                                                                                                 ^
TypeError: Cannot read properties of undefined (reading 'join')
    at /app/node_modules/prisma-erd-generator-markdown/dist/helpers/genMermaid.js:63:98
    at Array.forEach (<anonymous>)
    at /app/node_modules/prisma-erd-generator-markdown/dist/helpers/genMermaid.js:58:21
    at Array.forEach (<anonymous>)
    at genMermaid (/app/node_modules/prisma-erd-generator-markdown/dist/helpers/genMermaid.js:23:22)
    at Object.onGenerate (/app/node_modules/prisma-erd-generator-markdown/dist/generator.js:21:50)
    at LineStream.<anonymous> (/app/node_modules/@prisma/generator-helper/dist/generatorHandler.js:37:38)
    at LineStream.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)

After further investigation, the shape of the fields seem to change depending on their type. For instance, for a string field, you would get an object for the default property (like before), whereas Boolean fields get their default value as a boolean.

In an attempt to fix, I edited the ternary condition to be field.type !== 'Boolean' && field.default and it worked as expected, but I am not sure if it is the right thing to do as I didn't find the appropriate information about generator-related breaking changes for Prisma 5.

Can you have a look ? I can open a quick PR with that change if you want.

Thanks 😁

SQL-Gordster commented 1 year ago

I ran into this same issue, but I found that I could simply make the arg's.join portion of the defaultValue be optional.

const defaultValue = field.default ? `${field.default.name}(${field.default.args?.join(', ')})` : '';