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

prisma-loader

v1.0.13

Published

prisma-loader helps initializing a db with a Prisma model and a YAML file

Downloads

36

Readme

prisma-loader

prisma-loader is a utility to load your database with initial data (a.k.a «fixture»). You specify the initial data in a YAML file. Use it to get your database into a predictable state in your CI/CD pipeline or before every test run during testing.

What is Prisma?

Prisma is an open-source database toolkit, that replaces traditional ORMs and makes database access easy with an auto-generated query builder for TypeScript & Node.js.

👉 prisma.io/docs/

Usage from the Command Line

yarn add prisma-loader -D
npx prisma-loader path/to/data.yml [more.yml files.yml]

Let's suppose we want to load two users into our database. Let's also assume, that our Prisma Schema has a type «user» already defined (see example schema.prisma).

# initial-data.yml
user:
  - name: Admin
    email: [email protected]
    password: test
    role: ADMIN
  - name: Student
    email: [email protected]
    password: test
    role: STUDENT

This will generate two users in our database. You can define further types and initial data within the same YAML file:

# initial-data.yml
user:
  - name: Admin
    ...

school:
  - name: School One
    ...

If your database already contains data, e.g. from a previous initialization run, then you may want to first delete this data. You can do this by specifying which types to delete in the YAML file:

# initial-data.yml
delete:
  - user
  - school
  - ...

user:
  - name: Admin
    ...

school:
  - name: School One
    ...

Watch out, depending on how the data types reference each other you may run into problems when deleting data, as Prisma does not yet support cascading deletes (see this issue). List those types first, which no other types references to as a required relation.

Usage in your code

You can also use this tool from within your code, e.g. to load data before every test run

import { loadFixture } from "prisma-loader";

loadFixture("examples/example.yml");

This will execute the data load specified in the YAML file.

Usage in Cypress

If you would like to use this to load predefined data before every test run with cypress.io, then you need to set up cypress as follows:

// in cypress/plugins/index.js
const { loadFixture } = require("prisma-loader");

module.exports = (on, config) => {
  on("task", {
    async prismaLoader(fixtureFile) {
      const file = `${config.fixturesFolder}/${fixtureFile}`;
      await loadFixture(file);
      return null;
    },
  });
}

This makes available a cypress task, which can be used as follows:

describe("Some test", () => {
  beforeEach(() => {
    // test-data.yml will be loaded from cypress/fixtures/...
    cy.task("prismaLoader", "test-data.yml");
  });

  it("Already has the data loaded", {
    ...
  });