# @technote-space/prisma-seeder-tools

> [![npm version](https://badge.fury.io/js/%40technote-space%2Fprisma-seeder-tools.svg)](https://badge.fury.io/js/%40technote-space%2Fprisma-seeder-tools) [![CI Status](https://github.com/technote-space/prisma-seeder-tools/workflows/CI/badge.svg)](https://g

Latest version **0.3.24** (published 2022-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @technote-space/prisma-seeder-tools
pnpm add @technote-space/prisma-seeder-tools
yarn add @technote-space/prisma-seeder-tools
bun add @technote-space/prisma-seeder-tools
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.24 |
| Published | 2022-01-19 |
| First published | 2021-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | yes |
| GitHub stars | 4 |
| Author | Technote |
| Maintainers | technote |
| Keywords | github, github actions |

## Links

- npm: https://www.npmjs.com/package/@technote-space/prisma-seeder-tools
- Repository: https://github.com/technote-space/prisma-seeder-tools
- Issues: https://github.com/technote-space/prisma-seeder-tools/issues
- npm.io page: https://npm.io/package/@technote-space/prisma-seeder-tools

## Dependencies (1)

- [faker](https://npm.io/package/faker.md) ^6.6.6

## Recent versions

- 0.3.24 (latest) — 2022-01-19
- 0.3.23 — 2022-01-17
- 0.3.22 — 2022-01-04
- 0.3.21 — 2022-01-03
- 0.3.20 — 2021-12-21
- 0.3.19 — 2021-10-20
- 0.3.18 — 2021-10-13
- 0.3.17 — 2021-09-07
- 0.3.16 — 2021-09-04
- 0.3.15 — 2021-08-24
- 0.3.14 — 2021-08-18
- 0.3.13 — 2021-08-14
- 0.3.12 — 2021-08-05
- 0.3.11 — 2021-07-20
- 0.3.10 — 2021-07-16
- … 12 more at https://npm.io/package/@technote-space/prisma-seeder-tools/versions

## README

# Prisma Seeder Tools

[![npm version](https://badge.fury.io/js/%40technote-space%2Fprisma-seeder-tools.svg)](https://badge.fury.io/js/%40technote-space%2Fprisma-seeder-tools)
[![CI Status](https://github.com/technote-space/prisma-seeder-tools/workflows/CI/badge.svg)](https://github.com/technote-space/prisma-seeder-tools/actions)
[![CodeFactor](https://www.codefactor.io/repository/github/technote-space/prisma-seeder-tools/badge)](https://www.codefactor.io/repository/github/technote-space/prisma-seeder-tools)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/technote-space/prisma-seeder-tools/blob/master/LICENSE)

Seeder tools for prisma

## Table of Contents

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<details>
<summary>Details</summary>

- [Usage](#usage)
  - [Install](#install)
  - [Use](#use)
- [Author](#author)

</details>
<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Usage

### Install

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

or

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

### Use

e.g.

```tsx
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)](https://github.com/technote-space)  
[Blog](https://technote.space)

---
_Source: https://npm.io/package/@technote-space/prisma-seeder-tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
