All Projects → w3tecch → Typeorm Seeding

w3tecch / Typeorm Seeding

Licence: mit
🌱 A delightful way to seed test data into your database.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typeorm Seeding

Mongo Seeding
The ultimate solution for populating your MongoDB database.
Stars: ✭ 375 (-25.15%)
Mutual labels:  cli, database, seed
Csv2db
The CSV to database command line loader
Stars: ✭ 102 (-79.64%)
Mutual labels:  cli, database
Generator Ngx Rocket
🚀 Extensible Angular 11+ enterprise-grade project generator
Stars: ✭ 1,329 (+165.27%)
Mutual labels:  cli, seed
Webtau
Webtau (short for web test automation) is a testing API, command line tool and a framework to write unit, integration and end-to-end tests. Test across REST-API, Graph QL, Browser, Database, CLI and Business Logic with consistent set of matchers and concepts. REPL mode speeds-up tests development. Rich reporting cuts down investigation time.
Stars: ✭ 156 (-68.86%)
Mutual labels:  cli, database
Mssql Cli
A command-line client for SQL Server with auto-completion and syntax highlighting
Stars: ✭ 1,061 (+111.78%)
Mutual labels:  cli, database
App
Reusable framework for micro services & command line tools
Stars: ✭ 66 (-86.83%)
Mutual labels:  cli, database
Dynein
DynamoDB CLI written in Rust.
Stars: ✭ 126 (-74.85%)
Mutual labels:  cli, database
Faker
Faker is a pure Elixir library for generating fake data.
Stars: ✭ 673 (+34.33%)
Mutual labels:  database, seed
Klepto
Klepto is a tool for copying and anonymising data
Stars: ✭ 193 (-61.48%)
Mutual labels:  cli, database
Ldb
A C++ REPL / CLI for LevelDB
Stars: ✭ 201 (-59.88%)
Mutual labels:  cli, database
Ronin
Ronin is a Ruby platform for vulnerability research and exploit development. Ronin allows for the rapid development and distribution of code, Exploits or Payloads, Scanners, etc, via Repositories.
Stars: ✭ 220 (-56.09%)
Mutual labels:  cli, database
Things.sh
Simple read-only comand-line interface to your Things 3 database
Stars: ✭ 492 (-1.8%)
Mutual labels:  cli, database
Express Typescript Boilerplate
A delightful way to building a RESTful API with NodeJs & TypeScript by @w3tecch
Stars: ✭ 2,293 (+357.68%)
Mutual labels:  database, seed
Mongoaudit
🔥 A powerful MongoDB auditing and pentesting tool 🔥
Stars: ✭ 1,174 (+134.33%)
Mutual labels:  cli, database
Polluter
The easiest solution to seed database with Go
Stars: ✭ 146 (-70.86%)
Mutual labels:  database, seed
Dksnap
Docker Snapshots for Development and Test Data
Stars: ✭ 122 (-75.65%)
Mutual labels:  cli, database
Autoserver
Create a full-featured REST/GraphQL API from a configuration file
Stars: ✭ 188 (-62.48%)
Mutual labels:  cli, database
Lev
The complete REPL & CLI for managing LevelDB instances.
Stars: ✭ 295 (-41.12%)
Mutual labels:  cli, database
Bogus
📇 A simple and sane fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Stars: ✭ 5,083 (+914.57%)
Mutual labels:  database, seed
Instapy Cli
✨ Python library and CLI to upload photo and video on Instagram. W/o a phone!
Stars: ✭ 498 (-0.6%)
Mutual labels:  cli

logo

TypeORM Seeding

NPM package Build Status Dependency Sematic-Release

A delightful way to seed test data into your database. Inspired by the awesome framework laravel in PHP and of the repositories from pleerock Made with ❤️ by Gery Hirschfeld and contributors


❯ Table of contents

❯ Introduction

Isn't it exhausting to create some sample data for your database, well this time is over!

How does it work? Just create a entity factory for your entities (models) and a seed script.

Entity

First create your TypeORM entities.

// user.entity.ts
@Entity()
export class User {
  @PrimaryGeneratedColumn('uuid') id: string
  @Column({ nullable: true }) name: string
  @Column({ type: 'varchar', length: 100, nullable: false }) password: string
  @OneToMany((type) => Pet, (pet) => pet.user) pets: Pet[]

  @BeforeInsert()
  async setPassword(password: string) {
    const salt = await bcrypt.genSalt()
    this.password = await bcrypt.hash(password || this.password, salt)
  }
}

// pet.entity.ts
@Entity()
export class Pet {
  @PrimaryGeneratedColumn('uuid') id: string
  @Column() name: string
  @Column() age: number
  @ManyToOne((type) => User, (user) => user.pets)
  @JoinColumn({ name: 'user_id' })
  user: User
}

Factory

Then for each entity define a factory. The purpose of a factory is to create new entites with generate data.

Note: Factories can also be used to generate data for testing.

// user.factory.ts
define(User, (faker: typeof Faker) => {
  const gender = faker.random.number(1)
  const firstName = faker.name.firstName(gender)
  const lastName = faker.name.lastName(gender)

  const user = new User()
  user.name = `${firstName} ${lastName}`
  user.password = faker.random.word()
  return user
})

// pet.factory.ts
define(Pet, (faker: typeof Faker) => {
  const gender = faker.random.number(1)
  const name = faker.name.firstName(gender)

  const pet = new Pet()
  pet.name = name
  pet.age = faker.random.number()
  pet.user = factory(User)() as any
  return pet
})

Seeder

And last but not least, create a seeder. The seeder can be called by the configured cli command seed:run. In this case it generates 10 pets with a owner (User).

Note: seed:run must be configured first. Go to CLI Configuration.

// create-pets.seed.ts
export default class CreatePets implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await factory(Pet)().createMany(10)
  }
}

❯ Installation

Before using this TypeORM extension please read the TypeORM Getting Started documentation. This explains how to setup a TypeORM project.

After that install the extension with npm or yarn.

npm i typeorm-seeding
# or
yarn add typeorm-seeding

Optional, install the type definitions of the Faker library.

npm install -D @types/faker

Configuration

To configure the path to your seeds and factories change the TypeORM config file (ormconfig.js or ormconfig.json).

The default paths are src/database/{seeds,factories}/**/*{.ts,.js}

ormconfig.js

module.exports = {
  ...
  seeds: ['src/seeds/**/*{.ts,.js}'],
  factories: ['src/factories/**/*{.ts,.js}'],
}

.env

TYPEORM_SEEDING_FACTORIES=src/factories/**/*{.ts,.js}
TYPEORM_SEEDING_SEEDS=src/seeds/**/*{.ts,.js}

CLI Configuration

Add the following scripts to your package.json file to configure the seed cli commands.

"scripts": {
  "seed:config": "ts-node ./node_modules/typeorm-seeding/dist/cli.js config"
  "seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"
  ...
}

To execute the seed run npm run seed:run in the terminal.

Note: More CLI options are here

Add the following TypeORM cli commands to the package.json to drop and sync the database.

"scripts": {
  ...
  "schema:drop": "ts-node ./node_modules/typeorm/cli.js schema:drop",
  "schema:sync": "ts-node ./node_modules/typeorm/cli.js schema:sync",
  ...
}

CLI Options

Option Default Description
--seed or -s null Option to specify a seeder class to run individually.
--connection or -c null Name of the typeorm connection. Required if there are multiple connections.
--configName or -n ormconfig.js Name to the typeorm config file.
--root or -r process.cwd() Path to the typeorm config file.

❯ Basic Seeder

A seeder class only contains one method by default run. Within this method, you may insert data into your database. For manually insertion use the Query Builder or use the Entity Factory

Note. The seeder files will be executed alphabetically.

import { Factory, Seeder } from 'typeorm-seeding'
import { Connection } from 'typeorm'
import { User } from '../entities'

export default class CreateUsers implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await connection
      .createQueryBuilder()
      .insert()
      .into(User)
      .values([
        { firstName: 'Timber', lastName: 'Saw' },
        { firstName: 'Phantom', lastName: 'Lancer' },
      ])
      .execute()
  }
}

❯ Using Entity Factory

Of course, manually specifying the attributes for each entity seed is cumbersome. Instead, you can use entity factories to conveniently generate large amounts of database records.

For all entities we want to create, we need to define a factory. To do so we give you the awesome faker library as a parameter into your factory. Then create your "fake" entity and return it. Those factory files should be in the src/database/factories folder and suffixed with .factory like src/database/factories/user.factory.ts.

Types Description
Entity TypeORM Entity like the user or the pet in the samples.
Context Argument to pass some static data into the factory function.
EntityFactory This object is used to make new filled entities or create it into the database.

define

The define function creates a new entity factory.

define: <Entity, Context>(entity: Entity, factoryFn: FactoryFunction<Entity, Context>) => void;
import Faker from 'faker'
import { define } from 'typeorm-seeding'
import { User } from '../entities'

define(User, (faker: typeof Faker, context: { roles: string[] }) => { ... })

factory

Factory retrieves the defined factory function and returns the EntityFactory to start creating new enities.

factory: (entity: Entity) => (context?: Context) => EntityFactory<Entity, Context>
factory(Pet)()
factory(Pet)({ name: 'Balou' })

EntityFactory

map

Use the .map() function to alter the generated value before they get persisted.

map(mapFunction: (entity: Entity) => Promise<Entity>): EntityFactory<Entity, Context>
await factory(User)()
  .map(async (user: User) => {
    const pets: Pet[] = await factory(Pet)().createMany(2)
    const petIds = pets.map((pet: Pet) => pet.Id)
    await user.pets().attach(petIds)
  })
  .createMany(5)

make & makeMany

Make and makeMany executes the factory functions and return a new instance of the given entity. The instance is filled with the generated values from the factory function, but not saved in the database.

overrideParams - Override some of the attributes of the entity.

make(overrideParams: EntityProperty<Entity> = {}): Promise<Entity>
await factory(User)().make()
await factory(User)().makeMany(10)

// override the email
await factory(User)().make({ email: '[email protected]' })
await factory(User)().makeMany(10, { email: '[email protected]' })

create & createMany

the create and createMany method is similar to the make and makeMany method, but at the end the created entity instance gets persisted in the database.

overrideParams - Override some of the attributes of the entity.

create(overrideParams: EntityProperty<Entity> = {}): Promise<Entity>
await factory(User)().create()
await factory(User)().createMany(10)

// override the email
await factory(User)().create({ email: '[email protected]' })
await factory(User)().createMany(10, { email: '[email protected]' })

❯ Seeding Data in Testing

The entity factories can also be used in testing. To do so call the useSeeding function, which loads all the defined entity factories.

Choose your test database wisley. We suggest to run your test in a sqlite in memory database.

{
  "type": "sqlite",
  "name": "memory",
  "database": ":memory:"
}

However, if the test database is not in memory, than use the --runInBand flag to disable parallelizes runs.

describe("UserService", () => {
  let connection: Connection

  beforeAll(async (done) => {
    connection = await useRefreshDatabase({ connection: 'memory' })
    await useSeeding()

    const user = await factory(User)().make()
    const createdUser = await factory(User)().create()

    await runSeeder(CreateUserSeed)
    done()
  })

  afterAll(async (done) => {
    await tearDownDatabase()
    done()
  })

  test('Should ...', () => { ... })
})

useSeeding

Loads the defined entity factories.

useSeeding(options: ConfigureOption = {}): Promise<void>

runSeeder

Runs the given seeder class.

runSeeder(seed: SeederConstructor): Promise<void>

useRefreshDatabase

Connects to the database, drops it and recreates the schema.

useRefreshDatabase(options: ConfigureOption = {}): Promise<Connection>

tearDownDatabase

Closes the open database connection.

tearDownDatabase(): Promise<void>

ConfigureOption

interface ConfigureOption {
  root?: string // path to the orm config file. Default = process.cwd()
  configName?: string // name of the config file. eg. ormconfig.js
  connection?: string // name of the database connection.
}

❯ Example Projects

Please fill free to add your open-source project here. This helps others to better understand the seeding technology.

Project Description
copa-ch/copa-backend 🚀 Nest application written in TypeScript for the COPA project. This app manages your tournaments and generates the schedules.

❯ Changelog

Changelog

❯ License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].