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

@panter/prisma-inputs

v0.0.7

Published

Prisma Inputs is a TypeScript library that provides utility functions and types for working with Prisma input schemas. It offers a set of mapper functions that help you map and transform a model that can be used in a form to match the structure expected

Downloads

301

Readme

Prisma Inputs

Prisma Inputs is a TypeScript library that provides utility functions and types for working with Prisma input schemas. It offers a set of mapper functions that help you map and transform a model that can be used in a form to match the structure expected by Prisma's create, update, connect and disconnect methods.

Installation

You can install Prisma Inputs using npm or yarn:

yarn add @panter/prisma-inputs

! Important NOTE !

This package is not transpiled jet. If you want to use it with next js you need to add this to your next.config.js:

const nextConfig = {
  transpilePackages: ["@panter/prisma-inputs"]
  ...
}

What does prisma-inputs do?

Before diving into the code, let's understand the transformation with a simple example. Consider a model for a User that has an associated address.

type UserModel = {
  name: string;
  email: string;
  address: {
    street: string;
    city: string;
    zip: string;
  };
};

Suppose you have an instance of this model:

const user: UserModel = {
  name: "John Doe",
  email: "[email protected]",
  address: {
    id: "1",
    street: "123 Main St",
    city: "Anytown",
    zip: "12345"
  }
};

Now, let's say you want to update the user's address in your database using Prisma. Prisma expects the input for relations in a specific format, using create, update, connect, and disconnect.

After Mapping Using Prisma Inputs, the transformed model ready for a Prisma update might look like:

const prismaUpdateInput = {
  name: "John Doe",
  email: "[email protected]",
  address: {
    update: {
      street: { set: "123 Main St" },
      city: { set: "Anytown" },
      zip: { set: "12345" }
    }
  }
};

In this example, the address field of the UserModel is transformed to match Prisma's expected input format for updating relations. The PrismaInputSchema and the provided utility functions handle this transformation seamlessly.

Usage

To use Prisma Inputs, you need to import the necessary functions and types from the library. Here's an example of how you can use Prisma Inputs to define an input schema and map input data:

PrismaInputSchema

import {
  PrismaInputSchema,
  PropertyMapper,
  OneRelationMapper,
  reference,
  relation,
  object,
  mapFromPrismaSchema,
} from '@panter/prisma-inputs';

// Create an input schema
const addressCreateSchema: PrismaInputSchema<
  AddressCreateWithoutPersonInput,
  InferPrismaModel<Partial<AddressCreateWithoutPersonInput>>
> = {
  mapper: object(),
  properties: {
    address: property(),
  },
};

// Update an input schema
const addressUpdateSchema: PrismaInputSchema<
  AddressUpdateInput,
  InferPrismaModel<Partial<AddressUpdateInput>>
> = {
  mapper: object(),
  properties: {
    address: property(),
  },
};

// create a new address
const createAddressInput = mapFromPrismaSchema({
  schema: addressCreateSchema,
  value: { address: 'otherstreet' },
});
console.log(createAddressInput); // { address: 'otherstreet' }

// update new address
const newAddress = { id: '1', address: 'streetname' };
const updateAddressInput = mapFromPrismaSchema({
  schema: addressUpdateSchema,
  value: newAddress,
});
console.log(updateAddressInput); // { address: { set: 'streetname' } }

// use address schema in a relation
const userCreateSchema: PrismaInputSchema<PersonCreateInput> = {
  mapper: object(),
  properties: {
    name: property(),
    addresses: manyRelation(() => ({
      create: () => addressCreateSchema,
      update: () => addressUpdateSchema,
    })),
    organisation: reference(),
  },
};

const createPersonInput = mapFromPrismaSchema({
  schema: userCreateSchema,
  value: { organisation: { id: '1' }, addresses: [{ address: 'streetname' }] },
});

/**
{
  organisation: { connect: { id: '1' } },
  addresses: { create: [{ address: 'streetname' }] },
}
 */
console.log(createPersonInput);

PrismaSchemaBuilder

To define an input schema builder, you can use the prismaSchema function. It takes three parameters: unionProps, createProps, and updateProps. These parameters are functions that define the properties of the input schema.

Here's an example of how to define an input schema for the Simple model:

const simpleSchema = prismaSchemaBuilder<SimpleCreateInput, SimpleUpdateInput>({
  props: {
    name: property(),
  },
  create: {},
  update: { secondName: property() },
});

const personSchema = prismaSchemaBuilder<PersonCreateInput, PersonUpdateInput>({
  props: {
    name: property(),
    addresses: manyRelation(() => addressSchema.relation()),
    addressesIds: manyReference(),
    organisation: reference(),
    organisationId: reference(),
  },
  create: {},
  update: {},
});

const addressSchema = prismaSchemaBuilder<
  AddressCreateWithoutPersonInput,
  AddressUpdateInput
>({
  props: {
    address: property(),
  },
  create: {},
  update: {},
});

const organisationCreateMapper: PrismaInputSchema<
  PrismaInput<OrganisationCreateInput>
> = {
  mapper: object(),
  properties: {
    description: property(),
    person: manyReference(),
    personIds: manyReference(),
    simple: relation(() => simpleSchema.relation()),
    simpleId: reference(),
    simples: manyRelation(() => simpleSchema.relation()),
    simplesIds: manyReference(),
  },
};

Using the schemas to map

describe('mapFromPrismaSchema()', () => {
  it('should map using the schema', () => {
    const createSchema = personSchema.createSchema;
    expect(createSchema).not.toBeUndefined();
    if (!createSchema) {
      return;
    }

    const resultCreate = mapFromPrismaSchema({
      schema: createSchema,
      value: { addressesIds: [{ id: '1' }] },
    });

    expect(resultCreate).toEqual({
      addresses: { connect: [{ id: '1' }] },
    });
  });
});