npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dans98/prismabox

v2.3.0

Published

Typebox generator for prisma schema

Downloads

2

Readme

prismabox

Generate versatile typebox schemes from your prisma schema.

Currently does not support mongoDB composite types

Install it in your project,

npm i -D prismabox
pnpm i -D prismabox
bun add -D prismabox

then add

generator prismabox {
  provider = "prismabox"
  // you can optionally specify the output location. Defaults to ./prismabox
  output = "./myCoolPrismaboxDirectory"
  // if you want, you can customize the imported variable name that is used for the schemes. Defaults to "Type" which is what the standard typebox package offers
  typeboxImportVariableName = "t"
  // you also can specify the dependency from which the above import should happen. This is useful if a package re-exports the typebox package and you would like to use that
  typeboxImportDependencyName = "elysia"
  // by default the generated schemes do not allow additional properties. You can allow them by setting this to true
  additionalProperties = true
  // optionally enable the data model generation. See the data model section below for more info
  inputModel = true
}

to your prisma.schema. You can modify the settings to your liking, please see the respective comments for info on what the option does.

There are additional config options available which are mostly irrelevant to the average user. Please see config.ts for all available options.

Annotations

Prismabox offers annotations to adjust the output of models and fields.

| Annotation | Example | Description | ---|---|--- | @prismabox.hide | - | Hides the field or model from the output | | @prismabox.hidden | - | Alias for @prismabox.hide | | @prismabox.input.hide | - | Hides the field or model from the output only in the input model | | @prismabox.options | @prismabox.options{ min: 10, max: 20 } | Uses the provided options for the field or model in the generated schema. Be careful to use valid JS/TS syntax! |

For a more detailed list of available annotations, please see annotations.ts

A schema using annotations could look like this:

/// The post model
model Post {
  id        Int      @id @default(autoincrement())
  /// @prismabox.hidden
  createdAt DateTime @default(now())
  title     String   @unique

  User   User? @relation(fields: [userId], references: [id])
  /// @prismabox.options{max: 10}
  /// this is the user id
  userId Int?
}

/// @prismabox.hidden
enum Account {
  PASSKEY
  PASSWORD
}

Please note that you cannot use multiple annotations in one line! Each needs to be in its own!

Generated Schemes

The generator will output schema objects based on the models:

// the plain object without any relations
export const PostPlain = ...

// only the relations of a model
export const PostRelations = ...

// a composite model of the two, providing the full type
export const Post = ...

Input models

To simplify the validation of input data, prismabox is able to generate schemes specifically for input data. These are called "InputModels" and need to be explicitly enabled in the generator settings (inputModel = true) because they expect some conventions/field naming patterns to work properly.

If you want to see the specifics on how the model behaves, see here and here.

  1. Foreign Ids need to end in Id (case is ignored, e.g. userId or userid will work)
  2. createdAt will be detected and ignored if it follows exactly this pattern: createdAt DateTime @default(now())
  3. updatedAt will be detected and ignored if it follows exactly this pattern: updatedAt DateTime @updatedAt
  4. Hide annotations marked for imports (@prismabox.input.hide) are respected.

If enabled, the generator will additonally output more schemes for each model which can be used for creating/updating entities. The model will only allow editing fields of the entity itself. For relations, only connecting/disconnecting is allowed, but changing/creating related entities is not possible.