Giter Site home page Giter Site logo

mogzol / prisma-generator-typescript-interfaces Goto Github PK

View Code? Open in Web Editor NEW
6.0 6.0 3.0 215 KB

Generate zero-dependency Typescript interfaces from Prisma schema

Home Page: https://www.npmjs.com/package/prisma-generator-typescript-interfaces

License: MIT License

TypeScript 98.98% JavaScript 0.52% Shell 0.49%

prisma-generator-typescript-interfaces's People

Contributors

mogzol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

prisma-generator-typescript-interfaces's Issues

Error: Namespace ‘path to prisma’.Prisma has no exported member ‘[Type Name]’

Any help here is appreciated! Running into an issue when attempt to deploy. I'm running locally to resolve issues and this is what I've found:

The error:
When I run the build command the full error is:
“Namespace ‘“[local path]/node_modules/.pnpm/@[email protected][email protected]/node_modules/.prisma/client/default".Prisma' has no exported member ‘Address Create Input’
I followed these steps to initialize:

- Npx only allow ppm
- pnpm install // to install dependencies
- Pnpm generate // doesn’t throw errors
- Pnpm migrate dev // doesn’t throw errors
- Pnpm build // runs tsc -p tsconfig.json
    - This throws the error that the types aren’t found

My schema initialization looks like this:

generator client {
  provider = "prisma-client-js"
  output   = "../node_modules/.prisma/client"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator typescriptInterfaces {
  provider  = "prisma-generator-typescript-interfaces"
  output    = "../src/shared/models/entities.ts"
  modelType = "type"
  enumType  = "enum"
  prettier = true
}

When I remove the output line - I still get the same error.

An example of how it’s used:

import { Prisma } from '@prisma/client';
import { DBContext } from '../../db.context';
import { AddressCreateInputDto } from '../../../types/DTOs/address.dtos';
import { injectable } from 'inversify';
import { Address } from '../../../shared/models/entities'; //generator output
@injectable()
export class AddressRepo {
  constructor(private readonly _db: DBContext) {
    this._db = _db;
  }
  public async create(address: AddressCreateInputDto): Promise<Address> {
    const newAddress = await this._db.prisma.address.create({ data: address as Prisma.AddressCreateInput });
    return newAddress;
  }

The generator output:

export type Address = {
  id: string;
  name: string;
… // rest of the fields
}
```;


So I know the types exist in the code base. Is there an extra step needed to ensure prisma can find the generated types?

Types instead of interfaces?

I prefer types to interfaces, could that be an option?

generator typescriptInterfaces {
  provider = "prisma-generator-typescript-interfaces"
  typeOrInterface = "type|interface"
}

-ish? 🤞

Add support for omitting relations

First of all awesome package, this is exactly what I needed!

Just one small feature request, can you add an option which allows you to omit the relation property from the type/interface. This is so that the type/interface generated exactly matches the database model.

Eg.. If I have a schema

generator typescriptSimpleTypes {
    provider          = "prisma-generator-typescript-interfaces"
    output            = "../src/models/schema.ts"
    decimalType       = "number"
    modelType         = "type"
    optionalRelations = "false"
    omitRelations = "true"  // Include this
}

model A {
 id String
 name String
 bId String

 b B
}

model B {
 id String
 prop String
 
 as A[]
}

to output

type ModelA {
  id: string
  name: string
  bId: string
}

type ModelB {
  id: string
  prop: string
}

Let me know what you think! I could look to help out at some stage!

It crashes on Mac M1 :(

The error I am getting:

import: delegate library support not built-in '' (X11) @ error/import.c/ImportImageCommand/1302.
import: delegate library support not built-in '' (X11) @ error/import.c/ImportImageCommand/1302.
import: delegate library support not built-in '' (X11) @ error/import.c/ImportImageCommand/1302.
/api/node_modules/.bin/prisma-generator-typescript-interfaces: line 4: //: is a directory
/api/node_modules/.bin/prisma-generator-typescript-interfaces: line 5: const: command not found
/api/node_modules/.bin/prisma-generator-typescript-interfaces: line 6: syntax error near unexpected token `='
/api/node_modules/.bin/prisma-generator-typescript-interfaces: line 6: `    String: () => "string",'

Node version: v18.17.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.