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

@dauflo/nest-fixtures

v1.3.1

Published

A module for making fixtures with NestJS. This package is inspired by Doctrine fixtures.

Downloads

49

Readme

NestJS Fixtures

Tired of creating random script to insert data into your database, tired of your coworkers asking you to update the data, this package is made for you! Nest Fixtures allow you to turn all your data schemas into fixtures to populate your database. Currently this package only works for Mongodb databases.

Installation

Before you get started, you'll have to install a few packages. Obviously you need @dauflo/nest-fixtures. You'll also need nest-commander to run your fixtures command.

npm i nest-commander @dauflo/nest-fixtures

How to use

You'll need to create a datafixtures directory inside src, this is where you'll write all your fixtures.

A fixtures file could look like this:

// src/datafixtures/cat.fixtures.ts

import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { Fixtures } from '@dauflo/nest-fixtures'
import { Cat, CatDocument } from 'src/models/cats/cat.schema'

@Injectable()
export class CatFixtures extends Fixtures {
    constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {
        super()
    }

    async load(): Promise<void> {
        // do your logic
    }
}

Congratz! You have you first fixtures, it's now time to populate your database. To do so, head to you app.module.ts file and add the following lines.

// src/app.module.ts

import { FixturesCommand, FixturesModule } from '@dauflo/nest-fixtures'

@Module({
    imports: [
        FixturesModule.forRootAsync(
            'src/datafixtures/*.fixtures.ts',
            'src/models/**/*.schema.ts'
        )
    ],
    providers: [
        FixturesCommand
    ]
})
export class AppModule {}

You can change the directory path schema to match you project directory structure.

Now you need to create a new entrypoint file and update a bit your packages.json

// src/fixtures.ts

import { CommandFactory } from "nest-commander";
import { AppModule } from "./app.module";

async function bootstrap() {
    await CommandFactory.run(AppModule)
}

bootstrap()
"scripts": {
    "fixtures": "npm run build && node dist/fixtures fixtures",
    "fixtures:delete": "npm run build && node dist/fixtures fixtures --delete"
}

You can now run either:

  • npm run fixtures to import data into your database
  • npm run fixtures:delete to purge the database and import data

Fixtures dependencies

You'll sometimes have some fixtures that will require data from other fixtures.

// src/datafixtures/user.fixtures.ts

import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { Fixtures, ReferenceRepository } from '@dauflo/nest-fixtures'
import { User, UserDocument } from 'src/models/users/user.schema'

@Injectable()
export class UserFixtures extends Fixtures {
    constructor(
        private reference: ReferenceRepository,
        @InjectModel(User.name) private userModel: Model<UserDocument>
    ) {
        super()
    }

    async load(): Promise<void> {
        const user = new this.userModel({
            name: 'Alex'
        })

        const userDoc = await user.save()
        this.reference.addReference('user', userDoc)
    }
}
// src/datafixtures/cat.fixtures.ts

import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { Fixtures } from '@dauflo/nest-fixtures'
import { Cat, CatDocument } from 'src/models/cats/cat.schema'

@Injectable()
export class CatFixtures extends Fixtures {
    constructor(
        private reference: ReferenceRepository,
        @InjectModel(Cat.name) private catModel: Model<CatDocument>
    ) {
        super()
    }

    async load(): Promise<void> {
        const user = this.reference.getReference('user')
        new this.catModel({
            name: 'Higgins',
            owner: user
        }).save()
    }

    getDependencies(): string[] {
        return [UserFixtures.name]
    }
}

To create dependencies, just add the getDependencies method and give the fixtures you need.

The ReferenceRepository is a singleton that will keep track of the data passed from fixtures to fixtures.

Discriminator

You can use the mongoose discriminator feature in your fixtures. You just need to add put your discriminators class in a folder named for example discri and add the following definition when importing the fixtures modules:

// src/app.module.ts

import { FixturesCommand, FixturesModule } from '@dauflo/nest-fixtures'

@Module({
    imports: [
        FixturesModule.forRootAsync(
            'src/datafixtures/*.fixtures.ts',
            'src/models/**/*.schema.ts',
            'discri'
        )
    ],
    providers: [
        FixturesCommand
    ]
})
export class AppModule {}

License

This project is licensed under the MIT License - see the LICENSE file for details.