0.3.24 • Published 2 years ago

@technote-space/prisma-seeder-tools v0.3.24

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Prisma Seeder Tools

npm version CI Status CodeFactor License: MIT

Seeder tools for prisma

Table of Contents

Usage

Install

yarn add @technote-space/prisma-seeder-tools

or

npm i @technote-space/prisma-seeder-tools

Use

e.g.

import {PrismaClient} from './client';
import {seed, createDefinition, Seeder} from '@technote-space/prisma-seeder-tools';

class RoomSeeder extends Seeder<PrismaClient> {
  constructor(prisma: PrismaClient) {
    super(prisma);
  }

  public async run(): Promise<void> {
    await this.factory('room').createMany(5);
  }
}
class GuestSeeder extends Seeder<PrismaClient> {
  constructor(prisma: PrismaClient) {
    super(prisma);
  }

  public async run(): Promise<void> {
    await this.factory('guest').createMany(10);
  }
}
class ReservationSeeder extends Seeder<PrismaClient> {
  constructor(prisma: PrismaClient) {
    super(prisma);
  }

  public async run(): Promise<void> {
    const rooms  = await this.factory('room').list();
    const guests = await this.factory('guest').list();
    await [...Array(300)].reduce(async prev => {
      await prev;
      await this.factory('reservation').create({}, rooms.random(), guests.random());
    }, Promise.resolve());
  }
}

(async() => {
  const prisma = new PrismaClient();
  await seed(prisma, [
    createDefinition('guest', faker => ({
      email: `${faker.random.number()}${faker.random.number()}@example.com`,
      name: `${faker.name.lastName()} ${faker.name.firstName()}`,
      phone: faker.phone.phoneNumber(),
    })),
    createDefinition('room', faker => ({
      name: faker.name.firstName() + faker.random.number(),
      number: faker.random.number({ min: 1, max: 10 }),
      price: faker.random.number({ min: 1000, max: 100000 }),
    })),
    createDefinition('reservation', (faker, params) => {
      const room = params[0] as Room;
      const guest = params[1] as Guest;
      const number = faker.random.number({ min: 1, max: room.number });
      const checkin = faker.date.between(faker.date.past(2), faker.date.future(2));
      checkin.setHours(15, 0, 0, 0);
      const nights = faker.random.number({ min: 1, max: 7 });
      const checkout = new Date(checkin.valueOf());
      checkout.setDate(checkin.getDate() + nights);
      checkout.setHours(10, 0, 0, 0);
      const amount = room.price * number * nights;

      return {
        checkin,
        checkout,
        status,
        number,
        amount,
        room: {
          connect: {
            id: room.id,
          },
        },
        guest: {
          connect: {
            id: guest.id,
          },
        },
      };
    }),
  ], [
    new GuestSeeder(prisma),
    new RoomSeeder(prisma),
    new ReservationSeeder(prisma),
  ]);
})();

Author

GitHub (Technote)
Blog

0.3.24

2 years ago

0.3.23

2 years ago

0.3.22

2 years ago

0.3.21

2 years ago

0.3.20

2 years ago

0.3.19

3 years ago

0.3.18

3 years ago

0.3.17

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.10

3 years ago

0.3.9

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago