pantharshit00 / prisma-docs-generator

Prisma generator for automatically generating documentation reference from the Prisma schema.
MIT License
579 stars 27 forks source link

TypeError: Cannot read property 'length' of undefined #38

Closed Blfrg closed 3 years ago

Blfrg commented 3 years ago

Starting with prisma version 2.29.0 to current (2.30.0); The following error now appears when generating docs:

TypeError: Cannot read property 'length' of undefined
    at ModelGenerator.getModelDirective (/api/node_modules/prisma-docs-generator/dist/generator/model.js:196:28)
    at /api/node_modules/prisma-docs-generator/dist/generator/model.js:522:34
    at Array.map (<anonymous>)
    at ModelGenerator.getModels (/api/node_modules/prisma-docs-generator/dist/generator/model.js:518:38)
    at ModelGenerator.getData (/api/node_modules/prisma-docs-generator/dist/generator/model.js:530:26)
    at new ModelGenerator (/api/node_modules/prisma-docs-generator/dist/generator/model.js:35:26)
    at HTMLPrinter.toHTML (/api/node_modules/prisma-docs-generator/dist/printer/index.js:48:26)
    at Object.onGenerate (/api/node_modules/prisma-docs-generator/dist/index.js:51:83)
    at async LineStream.<anonymous> (/api/node_modules/@prisma/generator-helper/dist/generatorHandler.js:33:24)

Reverting back to prisma version 2.28.0 the error no longer appears.

enixlabs commented 3 years ago

Getting same issue even with 2.30.1

connorjones13 commented 3 years ago

Looks like the shape of the data has changed from what this method getModelDirective is expecting.

It seems the idFields property no longer exists and instead there is property on each field called isId. Could probably update that method to iterate over the fields and do something like the below. Good PR opportunity for someone with more time than me.

for (let i = 0; i < model.fields.length; i+=1) {
    const field = model.fields[i];
    if (field.isId) {
      directiveValue.push({ name: '@@id', values: [field] });
    }
    if (field.isUnique) {
      directiveValue.push({
        name: '@@unique',
        values: [field],
      });
    }
}

example model object

{
  name: 'User',
  isEmbedded: false,
  dbName: null,
  fields: [
    {
      name: 'id',
      kind: 'scalar',
      isList: false,
      isRequired: true,
      isUnique: false,
      isId: true,
      isReadOnly: false,
      type: 'Int',
      hasDefaultValue: true,
      default: [Object],
      isGenerated: false,
      isUpdatedAt: false
    },
    {
      name: 'email',
      kind: 'scalar',
      isList: false,
      isRequired: true,
      isUnique: true,
      isId: false,
      isReadOnly: false,
      type: 'String',
      hasDefaultValue: false,
      isGenerated: false,
      isUpdatedAt: false
    },
    {
      name: 'name',
      kind: 'scalar',
      isList: false,
      isRequired: false,
      isUnique: false,
      isId: false,
      isReadOnly: false,
      type: 'String',
      hasDefaultValue: false,
      isGenerated: false,
      isUpdatedAt: false
    },
    {
      name: 'profile',
      kind: 'object',
      isList: false,
      isRequired: false,
      isUnique: false,
      isId: false,
      isReadOnly: false,
      type: 'Profile',
      hasDefaultValue: false,
      relationName: 'ProfileToUser',
      relationFromFields: [],
      relationToFields: [],
      relationOnDelete: 'SetNull',
      isGenerated: false,
      isUpdatedAt: false
    }
  ],
  isGenerated: false,
  primaryKey: null,
  uniqueFields: [],
  uniqueIndexes: []
}
pantharshit00 commented 3 years ago

Hello

SDK had a breaking change so this will require a new release. Stay tuned, I plan to work on it this weekend.

pantharshit00 commented 3 years ago

Sorry for the delay here. This is fixed in https://github.com/pantharshit00/prisma-docs-generator/releases/tag/v0.5.0 https://www.npmjs.com/package/prisma-docs-generator/v/0.5.0