![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@rancid/nestjs-seeder
Advanced tools
An extension library for NestJS to perform seeding. Fork to replace faker with faker-js.
An extension library for NestJS to perform seeding. Fork to replace faker with faker-js.
npm install nestjs-seeder --save-dev
In this example, we'll use @nestjs/mongoose
to define our model. But you could use any class that you want. It's not tied to any database type. The only requirement is that you use ES2015 class.
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Document } from "mongoose";
import { Factory } from "nestjs-seeder";
@Schema()
export class User extends Document {
@Factory(faker => faker.name.findName())
@Prop()
name: string;
}
export const userSchema = SchemaFactory.createForClass(User);
Notice that we use @Factory
decorator to specify the value for this property. This value will be used during the seeding process.
@Factory
decorator supports multiple argument types, for example:
@Factory('male')
gender: string;
@Factory(faker => faker.address.streetAddress())
address: string;
@Factory(() => {
const minAge = 18;
const maxAge = 30;
return Math.round(Math.random() * (maxAge - minAge) + minAge);
})
age: number;
A seeder is a class that implements Seeder
interface. It requires you to implement two methods:
async seed(): Promise<any>
async drop(): Promise<any>
Use seed
method to insert data into the database, and use drop
method to clear the data in the database (collection / table).
To insert the data into the database, you could use the provided DataFactory.createForClass
method. Please see the example below:
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { User } from "../schemas/user.schema";
import { Seeder, DataFactory } from "nestjs-seeder";
@Injectable()
export class UsersSeeder implements Seeder {
constructor(@InjectModel(User.name) private readonly user: Model<User>) {}
async seed(): Promise<any> {
// Generate 10 users.
const users = DataFactory.createForClass(User).generate(10);
// Insert into the database.
return this.user.insertMany(users);
}
async drop(): Promise<any> {
return this.user.deleteMany({});
}
}
Create a seeder file under src
folder in your NestJS project and name it seeder.ts
.
import { seeder } from "nestjs-seeder";
import { MongooseModule } from "@nestjs/mongoose";
import { User, userSchema } from "./schemas/user.schema";
import { UsersSeeder } from "./seeders/users.seeder";
seeder({
imports: [
MongooseModule.forRoot("mongodb://localhost/nestjs-seeder-sample"),
MongooseModule.forFeature([{ name: User.name, schema: userSchema }]),
],
}).run([UsersSeeder]);
Notice that seeder
function accepts NestJS @Module()
decorator metadata such as imports
and providers
.
This will allow you to use NestJS dependency injection and later inject it in your seeder file.
Finally, we call run
method and pass any number of seeders that you want to run. In this case we want to run UsersSeeder
.
If you want to run multiple seeders, you could do:
.run([UsersSeeder, ProductsSeeder])
Add these two script (seed
and seed:refresh
) under the scripts
property in your package.json
file:
"scripts": {
"seed": "node dist/seeder",
"seed:refresh": "node dist/seeder --refresh"
}
NOTE: Don't replace the scripts
. Add both seed
and seed:refresh
scripts after your existing scripts.
With the scripts integrated in the package.json
file, now you could run 2 different commands:
npm run seed
npm run seed:refresh
@Schema()
export class User extends Document {
@Factory(faker => faker.random.arrayElement(["male", "female"]))
@Prop({ required: true })
gender: string;
@Factory((faker, ctx) => faker.name.firstName(ctx.gender === "male" ? 0 : 1))
@Prop({ required: true })
firstName: string;
}
const users = DataFactory.createForClass(User).generate(10, {
zipCode: "10153",
});
@Schema()
export class User extends Document {
// If you pass predefined values to the `generate` function, you will be
// able to access it in the context.
@Factory((faker, ctx) => `${faker.address.streetAddress()} ${ctx.zipCode}`)
@Prop({ required: true })
address: string;
}
nestjs-seeder
is MIT licensed.
FAQs
An extension library for NestJS to perform seeding. Fork to replace faker with faker-js.
The npm package @rancid/nestjs-seeder receives a total of 3 weekly downloads. As such, @rancid/nestjs-seeder popularity was classified as not popular.
We found that @rancid/nestjs-seeder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.