Coder Social home page Coder Social logo

jorgebodega / typeorm-seeding Goto Github PK

View Code? Open in Web Editor NEW

This project forked from w3tecch/typeorm-seeding

67.0 67.0 12.0 2.39 MB

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

Home Page: https://www.npmjs.com/package/@jorgebodega/typeorm-seeding

License: MIT License

JavaScript 0.81% TypeScript 99.19%

typeorm-seeding's Introduction

Hi there! I'm Jorge!

  • πŸ‹ Working as Backend Developer on Acid Tango
  • πŸ“– Learning about DDD and Hexagonal Architecture
  • πŸ‘― Collaborating on typeorm-seeding, typeorm-factory and dotpyle
  • 🍺 Craft beer lover
  • πŸ‰ Former rugby player
  • ⌨️ Holy panda clicky clicky

Connect with me:

jorgebodega | Twitter jorgebodega | LinkedIn


Languages and Tools:

VSCode

Neovim

TypeScript

NodeJS

Postgresql

MongoDB

Gitkraken

Github

Gitlab

Arch Linux

typeorm-seeding's People

Contributors

adrien-may avatar alumni avatar buuug7 avatar clovis1122 avatar davicedraz avatar dennisameling avatar dependabot[bot] avatar eugenio165 avatar ivanli-cn avatar jesster2k10 avatar jleck avatar jorgebodega avatar jsouto18 avatar jthodge avatar mkorobkov avatar oroce avatar owonwo avatar raphaelwoude avatar renovate-bot avatar renovate[bot] avatar semantic-release-bot avatar sfelix-martins avatar stephane-rbn avatar vologab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

typeorm-seeding's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore: update pnpm to v9
  • chore: update wagoid/commitlint-github-action action to v6
  • πŸ” Create all rate-limited PRs at once πŸ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/commitlint.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • wagoid/commitlint-github-action v5
.github/workflows/coverage.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
.github/workflows/quality.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
.github/workflows/release.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • cycjimmy/semantic-release-action v3
nodenv
.node-version
npm
package.json
  • chalk 4.1.2
  • commander 12.0.0
  • glob 10.3.10
  • ora 5.4.1
  • tslib 2.6.2
  • @faker-js/faker 8.4.1
  • @jorgebodega/typeorm-factory 1.4.0
  • @types/glob 8.1.0
  • @types/jest 29.5.12
  • @types/node 20.11.29
  • @typescript-eslint/eslint-plugin 7.3.0
  • @typescript-eslint/parser 7.3.0
  • eslint 8.57.0
  • eslint-config-prettier 9.1.0
  • eslint-import-resolver-typescript 3.6.1
  • eslint-plugin-import 2.29.1
  • jest 29.7.0
  • prettier 3.2.5
  • rimraf 5.0.5
  • sqlite3 5.1.7
  • ts-jest 29.1.2
  • ts-node 10.9.2
  • typeorm 0.3.20
  • typescript 5.4.2
  • typeorm ^0.3.0
  • node >=18 <19 || >=20
  • pnpm 8.15.5

  • Check this box to trigger a request for Renovate to run again on this repository

Swap out Faker for @faker-js/faker

note: sorry in advance, this is a copy / paste from the original project. I'm new to the library and am not sure what the history is or future for the project, i.e. if this repo will become the more popular fork, etc, but either way I wanted to raise the issue here as well!

After the "switcheroo" that was pulled with Faker, I imagine that it would be a good idea to switch Faker out for the accepted "official" fork of the original project.

See: https://fakerjs.dev/

My understanding is that the best way to approach this swap would be to wait for the 6.0.0 release of @faker-js/faker (as of this ticket being created the current package is 6.0.0-alpha.5), then "just" swap out the libraries.

In a very quick test I found that it seems pretty simple to swap out "old faker" for "new faker", although I followed these steps https://github.com/faker-js/faker#typescript-support to make TypeScript happy (this may be unnecessary).

[question] Will this work with typeorm >=0.3

Regarding the brand new version of typeorm: https://github.com/typeorm/typeorm/releases/tag/0.3.0 did you know if it would be easy to upgrade typeorm-seeding to support that last version? I'm not sure if something will break as that version is covered by the semver range

"peerDependencies": {
"typeorm": "^0.2.41"
},

I'm asking this because I won't upgrade typeorm if I can't use typeorm-seeding (and few other packages that rely on typeorm)

Thanks.

(instance instanceof Seeder) not working - src/utils/commandUtils.ts#L26

Example problem demo

https://github.com/AaronNGray/typeorm-seeding-test

Explanation

https://github.com/jorgebodega/typeorm-seeding/blob/next/src/utils/commandUtils.ts#L26

  const seeders: Constructable<Seeder>[] = []
    for (const fileExport in seederFileExports) {
      const seederExport = seederFileExports[fileExport]
      const instance = new seederExport()
      if (instance instanceof Seeder) {
        seeders.push(seederExport)
      }
    }

https://github.com/jorgebodega/typeorm-seeding/blob/next/src/seeder.ts

import type { DataSource } from 'typeorm'

export abstract class Seeder {
  abstract run(dataSource: DataSource): Promise<void>
}

https://github.com/AaronNGray/typeorm-seeding-test/blob/master/src/seeders/CreateUsers.ts

import { DataSource } from 'typeorm'
import { Seeder } from '@jorgebodega/typeorm-seeding'
import { User } from '../entity/User'
import { UserFactory } from '../factories/UserFactory';

export default class CreateUsers implements Seeder {
    async run(dataSource: DataSource): Promise<void> {
        const userFactory = new UserFactory();
        const CreatedUsers = await userFactory.makeMany(10);
        const users: User[] = CreatedUsers;
        console.log("users = ", users);
        await dataSource.createEntityManager().save<User>(users);
    }
}
console.log("Seeder =", Seeder);
console.log("CreateUsers instanceof Seeder =", CreateUsers instanceof Seeder);  // == false

The (instance instanceof Seeder) will never work !

Example :-

https://gist.github.com/AaronNGray/157f22983f21fb3496a087b1d959c9f3

Version 3

Version 3 has been released, with a lot of breaking changes so please be sure you can use it.

Version 3 (latest):

yarn add [-D] @jorgebodega/typeorm-seeding

Version 2 (previous):

yarn add [-D] @jorgebodega/typeorm-seeding@2

Bundle size has been decreased a lot:

345kB vs 3.5kB

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: .github/renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid configuration option: packageRules[0].excludePackagePatters

Allow async .map functions

Hey!
I've been using this library recently and I've noticed that using async map functions are not currently allowed. This would be a nice Quality of Life feature to have on the library!

Driver not Connected

i try to run ts-node -r tsconfig-paths/register ./node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/database/datasource.ts src/database/seeder.ts ( the typeorm-seeding command throw an error βœ– Could not load the data source!, so i use ts-node instead, idk why πŸ€” )
and got this error:
image

i don't know why my fastify app success initialize the connection while the typeorm-seeding package throw the error above, this issue also mentioned on typeorm issue

datasource.ts

import "dotenv/config";
import path from "path";
import { DataSource } from "typeorm";

export const datasource = new DataSource({
  type: "postgres",
  host: process.env.DB_HOST,
  port: process.env.DB_PORT as unknown as number,
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  synchronize: true,
  entities: [path.resolve(__dirname, "../plugins/**/*.entity.{js,ts}")],
});

seeder.ts

import { Seeder } from "@jorgebodega/typeorm-seeding";
import { UserEntity } from "plugins/user/user.entity";
import { DataSource } from "typeorm";
export default class UserSeeder extends Seeder {
  public async run(dataSource: DataSource) {
    const userRepository = dataSource.getRepository(UserEntity);
    const user = userRepository.create({ username: "shinigami", email: "[email protected]", password: "verysecr3t" });
    userRepository.save(user);
  }
}

package version

"@jorgebodega/typeorm-seeding": "^6.0.1"
"typeorm": "^0.3.11"
"pg": "^8.8.0"

I would really appreciate your help.

Seeder do nothing

Library version: ^6.0.1

I create seeder and start it by command:

ts-node-dev ./node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed ./src/database/seeds/1631720650875_create-games.seed.ts -d ormconfig.ts

And seeder do totaly nothing. What i'm doing wrong ?

√ Datasource loaded
√ Seeder imported
i Executing seeders...
query: SELECT * FROM current_schema()
query: CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
query: SELECT version();
√ Finished seeding

1631720650875_create-games.seed.ts:

import { Seeder } from '@jorgebodega/typeorm-seeding';
import {DataSource} from "typeorm";
import {Game} from "../entity/game.entity";

export default class CreateGamesSeed implements Seeder {
	public async run(dataSource: DataSource){
		const game: Game = {
			name: 'test',
			maxPlayerInTeam: 1,
			gameConfig: null,
			teams: [],
			id: '1'
		}
		await dataSource.createEntityManager().save<Game>(game)
	}
}

ormconfig.ts:

import {DataSource} from "typeorm";
import {SnakeNamingStrategy} from "typeorm-naming-strategies";

export const dataSource = new DataSource({
    namingStrategy: new SnakeNamingStrategy(),
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'postgres',
    password: 'postgres',
    database: 'db',
    synchronize: false,
    logging: ["query", "error"],
    entities: ['src/database/entity/**/*.{ts,js}'],
    migrations: ['src/database/migration/**/*.{ts,js}'],
    subscribers: ['src/database/subscriber/**/*.{ts,js}'],
});

Generate SQL commands

Is there a way to generate a SQL file of all the commands instead of running them directly in the db?
Something like the SCHEMA file that RubyOnRails creates.

We are trying to improve the speed of DB preparation for automated testing.
Currently the seed takes >1min to run. So If we could generate the SQL in an upfront step, then we could potentially save some time (30sec)

Any idea how this could be done.

Taking DataSource connection values from configService/Joi

I have my project set up to load the env variables from a joi validation schema into nest.js ConfigService, however, since the DataSource gets called from outside Nest.js' lifecycle there's no way of doing dependency injection.

Any idea how I could get those values without duplicating credentials?

TypeOrmModule.forRootAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) => ({
      type: 'postgres',
      host: configService.get('DB_HOST'),
      port: configService.get('DB_PORT'),
      username: configService.get('DB_USERNAME'),
      password: configService.get('DB_PASSWORD'),
      database: configService.get('DB_DATABASE'),
      autoLoadEntities: true,
      synchronize: true,
    }),
  }),

Running into a weird typescript compiling error

My factory class

export class UserFactory extends Factory<User> {
    protected entity = User;
    protected dataSource = AppDataSource;
    firstName = faker.person.firstName();
    lastName = faker.person.lastName();
    protected attrs(): FactorizedAttrs<User> {
        return {
            firstName: this.firstName,
            lastName: this.lastName,
            email: faker.internet.email({ firstName: this.firstName, lastName: this.lastName, provider: 'test' }),
            username: faker.internet.userName({ firstName: this.firstName, lastName: this.lastName }),
            password: faker.internet.password(),
            // owner: new LazyInstanceAttribute((instance) => new SingleSubfactory(UserFactory, { pets: [instance] })),
        };
    }
}

My seed class

export class CreateUsers implements Seeder {
    public async run(dataSource: DataSource) {
        const CreatedUsers = await new UserFactory().makeMany(10);
        const users: User[] = CreatedUsers;
        await dataSource.manager.save<User>(users);
    }
}

My Entity Class

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ nullable: true })
    firstName?: string;

    @Column({ nullable: true })
    lastName?: string;

    @Length(3)
    @Column({ unique: true })
    username: string;

    @IsEmail()
    @Column({ nullable: false })
    email: string;

    @Column({ nullable: false, select: false })
    password?: string;

    public static create(options: Partial<User>) {
        const user = new User();

        return Object.assign(user, options);
    }
}

My data-source.ts:

const connectionConfig = {
    host: getEnvVar('POSTGRES_HOST'),
    port: Number(getEnvVar('POSTGRES_PORT')),
    username: getEnvVar('POSTGRES_USER'),
    password: getEnvVar('POSTGRES_PASSWORD'),
    database: getEnvVar('POSTGRES_DB'),
};

export const getDataSource = () => {
    return new DataSource({
        type: 'postgres',
        ...connectionConfig,
        synchronize: false,
        logging:
            process.env.NODE_ENV !== 'production' &&
            process.env.DEBUG === 'true',
        entities: [User, Provider, Pet],
        // TODO (task #27) needs to work after build with js files as well
        migrations: [join(__dirname, 'migrations/*.ts')],
        subscribers: [],
    });
};

export const AppDataSource = getDataSource();

export const entityManager = AppDataSource.manager;

The NPM script im running is "seed:run": "ts-node -r tsconfig-paths/register ./../../node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/data-source.ts src/seeds/*.ts"

The Error im getting:

nx run backend:seed:run

> nx run backend:"seed:run"

> [email protected] seed:run
> ts-node -r tsconfig-paths/register ./../../node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/data-source.ts src/seeds/*.ts
- Loading datasource
Error: Unable to open file: "/Users/jesseberman/Development/Den/packages/backend/src/data-source.ts". β¨― Unable to compile TypeScript:
src/entity/User.ts(4,2): error TS1238: Unable to resolve signature of class decorator when called as an expression.
  The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.
src/entity/User.ts(6,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(7,5): error TS2564: Property 'id' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(9,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(12,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(15,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(16,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(17,5): error TS2564: Property 'username' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(19,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(20,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(21,5): error TS2564: Property 'email' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(23,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'Object'.
    at Function.loadDataSource (/Users/jesseberman/Development/Den/node_modules/src/commands/CommandUtils.ts:22:19)
    at async Command.run (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:22:18)
    at async Command.parseAsync (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
    at async bootstrap (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:72:1)
βœ– Could not load the data source!
DataSourceImportationError: Could not load the data source!
    at Command.run (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:30:11)
    at async Command.parseAsync (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
    at async bootstrap (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:72:1) {
  [cause]: Error: Unable to open file: "/Users/jesseberman/Development/Den/packages/backend/src/data-source.ts". β¨― Unable to compile TypeScript:
  src/entity/User.ts(4,2): error TS1238: Unable to resolve signature of class decorator when called as an expression.
    The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.
  src/entity/User.ts(6,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(7,5): error TS2564: Property 'id' has no initializer and is not definitely assigned in the constructor.
  src/entity/User.ts(9,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(12,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(15,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(16,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(17,5): error TS2564: Property 'username' has no initializer and is not definitely assigned in the constructor.
  src/entity/User.ts(19,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(20,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  src/entity/User.ts(21,5): error TS2564: Property 'email' has no initializer and is not definitely assigned in the constructor.
  src/entity/User.ts(23,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
    Argument of type 'undefined' is not assignable to parameter of type 'Object'.
  
      at Function.loadDataSource (/Users/jesseberman/Development/Den/node_modules/src/commands/CommandUtils.ts:22:19)
      at async Command.run (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:22:18)
      at async Command.parseAsync (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
      at async bootstrap (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:72:1)
}
npm ERR! Lifecycle script `seed:run` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: [email protected] 
npm ERR!   at location: /Users/jesseberman/Development/Den/packages/backend 

 β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”

 >  NX   Ran target seed:run for project backend (2s)
 
    βœ–    1/1 failed
    βœ”    0/1 succeeded [0 read from cache]

Bug with noImplicitAny and noUnusedParameters

Bug with noImplicitAny and noUnusedParameters

With noImplicitAny=true or noUnusedParameters=true in tsconfig.json, the CLI fails.

Fix: remove unused parameters and any types

Version affected: 6.0.0

Console output:

> ts-node ./node_modules/@jorgebodega/typeorm-seeding/dist/cli.js -d src/config/postgres.config.ts seeders/*.ts

βœ” Datasource loaded
βœ– Could not load seeders!
SeederImportationError: Could not load seeders!
    at Command.run (/<hidden>/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:40:11)
    at async Command.parseAsync (/<hidden>/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
    at async bootstrap (/<hidden>/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:73:3)

Reccomended version

Hello, If I am using faker for creating data is the recommended version to still use 2 or 3. I was running into trouble using 3.1 and was looking for the correct version

DataSourceImportationError: Could not load the data source!

I need help with running seeder.

// config/database.ts

import { DataSourceOptions } from 'typeorm';
import env from './env';
import { User } from '../../user/User';

export const config: DataSourceOptions = {
    type: 'postgres',
    host: env.POSTGRES_HOST,
    port: env.POSTGRES_LOCAL_PORT,
    username: env.POSTGRES_USER,
    password: env.POSTGRES_PASSWORD,
    database: env.POSTGRES_DB,
    entities: [Book, Author, User],
    synchronize: true
};

// datasource.ts

import { DataSource } from 'typeorm';
import { config as databaseConfig } from './common/config/database';

const AppDataSource = new DataSource(databaseConfig);

export default AppDataSource;

// UserSeeder.ts

import { Seeder } from '@jorgebodega/typeorm-seeding';
import { DataSource } from 'typeorm';
import { User } from './User';
import { faker } from '@faker-js/faker';

export default class UserSeeder extends Seeder {
    public async run(dataSource: DataSource) {
        const users: User[] = [];

        Array.from({ length: 10 }).forEach(() => {
            users.push(this.createRandomUser());
        });

        await dataSource.createEntityManager().save<User>(users);
    }

    private createRandomUser(): User {
        const user = new User();

        user.firstName = faker.name.firstName();
        user.lastName = faker.name.lastName();
        user.email = faker.internet.email();
        user.password = faker.internet.password();
        user.createdAt = faker.date.past();

        return user;
    }
}

Running npx typeorm-seeding seed -d src/datasource.ts /app/src/user/UserSeeder.ts I got following error:

DataSourceImportationError: Could not load the data source!
    at Command.run (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:25:15)
    at async Command.parseAsync (/Users/bla/development/node.js/node_modules/commander/lib/command.js:916:5)
    at async bootstrap (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:61:5) {
  [cause]: Error: Unable to open file: "/Users/bla/development/node.js/src/datasource.ts". Cannot use import statement outside a module
      at CommandUtils.loadDataSource (/Users/bla/development/node.js/node_modules/typeorm/commands/CommandUtils.js:22:19)
      at async Command.run (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:20:22)
      at async Command.parseAsync (/Users/bla/development/node.js/node_modules/commander/lib/command.js:916:5)
      at async bootstrap (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:61:5)
}

Any idea what I'm doing wrong?

Faker library incident

(Referencing the original issue: w3tecch#204)

Looks like the Faker.js library has been either compromised or discontinued in a strange manner.

I believe it would be the best to replace this library, since it is provided as a parameter into entity factories. What are some suitable alternatives?

Could not import seeders!

I am getting this error although I think I wrote everything as stated in the docs:

βœ– Could not import seeders!
Error importing seeders: Seeder RootSeeder does not exist

ormconfig.json:

[
    {
        "name": "default",
        "type": "mysql",
        "host": "localhost",
        "port": 3306,
        "username": "root",
        "password": "mypassword",
        "database": "mydatabase",
        "logging": true,
        "synchronize": true,
        "defaultSeeder": "RootSeeder",
        "entities": [
            "./src/modules/**/infrastructure/typeorm/entities/*.ts"
        ],
        "migrations": [
            "./src/shared/infrastructure/typeorm/migrations/*.ts"
        ],
        "cli": {
            "migrationsDir": "./src/shared/infrastructure/typeorm/migrations"
        },
        "seeds": [
            "./src/shared/infrastructure/typeorm/seeders/*.ts"
        ],
        "factories": [
            "./src/shared/infrastructure/typeorm/factories/*.ts"
        ]
    }
]

UserFactory:

import { Factory, FactorizedAttrs, InstanceAttribute } from "@jorgebodega/typeorm-seeding"
import { faker } from '@faker-js/faker'
import User from "@modules/users/infrastructure/typeorm/entities/User"
export default class UserFactory extends Factory<User> {
    protected entity = User
    protected attrs: FactorizedAttrs<User> = {
        firstName: faker.name.firstName(),
        lastName: faker.name.lastName(),
        email: new InstanceAttribute((instance) =>
            [instance.firstName.toLowerCase(), instance.lastName.toLowerCase(), '@email.com'].join(''),
        ),
        birthdate: faker.date.past(),
        uuid: faker.datatype.uuid(),
        password: faker.internet.password(),
        isActive: faker.datatype.boolean(),
        confirmedEmail: faker.datatype.boolean(),
        gender: faker.datatype.number({
            min: 1,
            max: 2
        })

    }
}

UserSeeder:

import { Seeder } from "@jorgebodega/typeorm-seeding";
import { Connection } from "typeorm";
import UserFactory from "../factories/UserFactory";

export class UserSeeder extends Seeder {
    async run(_connection: Connection) {
        await new UserFactory().createMany(10)
    }
}

RootSeeder:

import { Seeder } from "@jorgebodega/typeorm-seeding"
import { Connection } from "typeorm";
import {UserSeeder} from "./UserSeeder";

export class RootSeeder extends Seeder{
    async run(connection: Connection): Promise<void> {
        await this.call(connection, [UserSeeder])
    }

}

Improve documentation to reflect all changes

Factories have been refactored with new factorized attributes. Documentation should reflect those changes and add more info for different options.

New options for attrs:

  • Same type
  • Function (sync or async) that returns value with correct type
  • InstanceAttribute
  • LazyInstanceAttribute
  • Subfactory

Updated Sample/example code missing

I am updating my project to use typeorm-0.3.17, from older 0.2.35, and found the old typeorm-seeding package was out of date with regard to 0.3.17.

I found https://github.com/jorgebodega/typeorm-seeding, which I needed to frk to get to work with typeorm-0.3.17 https://github.com/AaronNGray/typeorm-seeding (https://www.npmjs.com/package/@aaronngray/typeorm-seeding) and https://github.com/jorgebodega/typeorm-factory.

I am having trouble working out how to use the seeding level, theres documentation for the factory level https://github.com/jorgebodega/typeorm-factory/tree/next/examples but not for the seeding level. The older sample https://github.com/w3tecch/typeorm-seeding/tree/next/sample seems to have been removed and not updated.

So I am having trouble seeing the bigger picture on how to use the seeding level.

Explanation migration from w3tecch/typeorm-seeding to this library

hi i used typeorm-seeding library since the previous version https://github.com/w3tecch/typeorm-seeding, then i found your library and it helped me a lot, but i found a problem when migrating from w3tecch/typeorm-seeding, i think it is better to give instructions regarding migration from previous one because there are some differences in the syntax for example the use of extends instead of implements

in the previous version

export default class UserSeeder implements Seeder { . . . }

when this seeder command is executed the seeder file does not run seeding process and does not display warning information

i miss the changes in documentation to use extends instead of implements

export default class UserSeeder extends Seeder { . . . }

CLI Error: No default seeders found. Windows

Hi. I try to run the following command:

ts-node ./node_modules/@jorgebodega/typeorm-seeding/dist/cli.js -d src/database/dataSource.ts seed "src/database/seeders/user.seeder.ts"

It works fine when i run it from docker container bash. But when i try to run it from windows cmd it throw me the error.

√ Datasource loaded
Γ— Could not load seeders!
Error: No default seeders found
...

Versions i'm using:

"@jorgebodega/typeorm-factory": "^1.2.0",
"@jorgebodega/typeorm-seeding": "^5.0.2",

User factory class

export class UserFactory extends Factory<UserEntity> {
  protected entity = UserEntity;
  protected dataSource = dataSource;  // data source from src/database/dataSource.ts
  protected attrs(): FactorizedAttrs<UserEntity> {
    return {
      refreshTokenHash: '',
      email: faker.internet.email,
      lastName: faker.name.lastName,
      firstName: faker.name.firstName,
      password: async () => await hash('testdata', await genSalt(3)),
      wallet: [],
    };
  }
}

User seeder (src/database/seeders/user.seeder.ts)

export default class UserSeeder extends Seeder {
  async run() {
    await new UserFactory().create();
  }
}

Typeorm data source (src/database/dataSource.ts)

export default new DataSource({
  type: 'postgres',
  host: process.env.DB_HOST,
  port: parseInt(process.env.DB_PORT, 10),
  username: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB,
  autoLoadEntities: true,
  synchronize: false,
  entities: [join(__dirname, '../', '**/*.entity{.ts,.js}')],
  migrations: [join(__dirname, '../', 'database/migrations/**/*{.ts,.js}')],
});

I'm not sure, but maybe the problem is that method resolve return path with double backslashes which is then passed to the Glob.sync method.

const absolutePath = resolve(process.cwd(), args.path as string)

The glob package docs say that you should only use forward slashes to define a path.

I would really appreciate your help.

Move factory logic to a single class

Right now, factory logic is splitted in two files, with definition and operation on their own side.

Following MikroORM structure, which I think is simpler, we could join this on a single class

[question] Will this work with typeorm >=0.3

Regarding the new version of typeorm: https://github.com/typeorm/typeorm/releases/tag/0.3.0 did you know if would be easy to upgrade typeorm-seeding to support that last version? I'm not sure if something will break in typeorm-seeding tho as that version is covered by this semver range:

"peerDependencies": {
"typeorm": "^0.2.41"
},

I'm asking this because I won't upgrade typeorm if I couldn't use typeorm-seeding

Unknown file extension ".ts"

I got past the RootSeeder problem but now I get this:

βœ” ORM Config loaded
βœ” Seeder imported
βœ– Could not run the seed RootSeeder!
Unknown file extension ".ts" for /Users/****/Sites/s2pro.com.br/backend/src/modules/exercises/infrastructure/typeorm/entities/Exercise.ts

Here is my ormconfig.json:

[
    {
        "name": "default",
        "type": "mysql",
        "host": "localhost",
        "port": 3306,
        "username": "root",
        "password": "docker",
        "database": "s2pro",
        "logging": true,
        "synchronize": true,
        "defaultSeeder": "RootSeeder",
        "entities": [
            "./src/modules/**/infrastructure/typeorm/entities/*.ts"
        ],
        "migrations": [
            "./src/shared/infrastructure/typeorm/migrations/*.ts"
        ],
        "cli": {
            "migrationsDir": "./src/shared/infrastructure/typeorm/migrations"
        },
        "seeders": [
            "./src/shared/infrastructure/typeorm/seeders/*.ts"
        ],
        "factories": [
            "./src/shared/infrastructure/typeorm/factories/*.ts"
        ]
    }
]

RootSeeder

import { Seeder } from "@jorgebodega/typeorm-seeding"
import { Connection } from "typeorm";
import {UserSeeder} from "./UserSeeder";

export class RootSeeder extends Seeder{
    async run(connection: Connection): Promise<void> {
        await this.call(connection, [UserSeeder])
    }

}

Any thoughts on this?

SubFactory generating errors. cannot find JoinColumns of undefined

I have this structure

`export class AgentFactory extends Factory {
protected entity = Agent;
protected attrs: FactorizedAttrs = {
user: new Subfactory(UserFactory),
}
}

export class CompanyFactory extends Factory {
protected entity = Company;
protected attrs: FactorizedAttrs = {
name: faker.word.noun(),
description: faker.lorem.lines(3),
city: faker.address.cityName(),
email: faker.internet.email(),
phone: faker.phone.phoneNumber(),
address: faker.address.secondaryAddress(),
rccm: faker.random.alphaNumeric(),
ifu: faker.helpers.slugify(ifu of company ${faker.company.companyName()}),
category: new Subfactory(CompanyCategoryFactory),
account: new Subfactory(AccountFactory),
}
}

export class AgentRoleFactory extends Factory {
protected entity = AgentRole;
protected attrs: FactorizedAttrs = {
name: faker.helpers.arrayElement(["AGENT", "COMPTABLE", "SECRÉTAIRE", "DIRECTEUR TECHNIQUE"]),
description: faker.lorem.lines(3),
}
}

export class CareerFactory extends Factory {
protected entity = Career;
protected attrs: FactorizedAttrs = {
agent: new Subfactory(AgentFactory),
company: new Subfactory(CompanyFactory),
role: new Subfactory(AgentRoleFactory)
}
}`

then when seed will fail each time. How can I solve it

Cannot use import statement outside a module

After upgrading to latest release (7.0.0), I got this error:

/Users/user/Desktop/project/node_modules/@jorgebodega/typeorm-seeding/dist/cli.js:2
import { bootstrap } from './commands/seed.command';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1290:20)
    at Module._compile (node:internal/modules/cjs/loader:1342:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Object.require.extensions.<computed> [as .js] (/Users/user/Desktop/project/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1212:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1028:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at phase4 (/Users/user/Desktop/project/node_modules/ts-node/src/bin.ts:649:14)
    at bootstrap (/Users/user/Desktop/project/node_modules/ts-node/src/bin.ts:95:10)

Error: Cannot find module './typeorm-seeding'

running

yarn ts-node typeorm-seeding -d src/data-source.ts src/seeders/**/*.ts

Throws the error:

yarn run v1.22.19
warning package.json: No license field
$ /Users/user/project/typeorm-seeding-example/node_modules/.bin/ts-node typeorm-seeding -d src/data-source.ts src/seeders/CreateUsers.ts
node:internal/modules/cjs/loader:1080
throw err;
^

Error: Cannot find module './typeorm-seeding'
Require stack:
- /Users/user/project/typeorm-seeding-example/imaginaryUncacheableRequireResolveScript
  at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
  at Function.resolve (node:internal/modules/cjs/helpers:125:19)
  at requireResolveNonCached (/Users/user/project/typeorm-seeding-example/node_modules/ts-node/dist/bin.js:549:16)
  at getProjectSearchDir (/Users/user/project/typeorm-seeding-example/node_modules/ts-node/dist/bin.js:519:40)
  at phase3 (/Users/user/project/typeorm-seeding-example/node_modules/ts-node/dist/bin.js:267:27)
  at bootstrap (/Users/user/project/typeorm-seeding-example/node_modules/ts-node/dist/bin.js:47:30)
  at main (/Users/user/project/typeorm-seeding-example/node_modules/ts-node/dist/bin.js:33:12)
  at Object.<anonymous> (/Users/user/project/typeorm-seeding-example/node_modules/ts-node/dist/bin.js:579:5)
  at Module._compile (node:internal/modules/cjs/loader:1256:14)
  at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
  '/Users/user/project/typeorm-seeding-example/imaginaryUncacheableRequireResolveScript'
  ]
}

Node.js v18.18.2
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I took a look at typeorm-seeding-example and noticed that

ts-node ./node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/data-source.ts src/seeders/**/*.ts

does work. however, I am in a monorepo and strongly prefer not relying on the node_modules path.

What am I doing wrong?

No default seeders found error when running seeder

Hi.

I'm currently upgrading a project from TypeORM v.0.2.45 to v.0.3.7 that had your previous lib as dependency. Since there were a lot of breaking changes and deprecations in v.0.3.0 of TypeORM, we decided to also change the seeder library, but I've encountered some difficulties when I tried to use this lib.

I'm using the v5.0.1 of this lib and v1.2.0 of your factory lib.

I have the following factory class:

export class UserFactory extends Factory<UserEntity> {
  protected entity: Constructable<UserEntity>;

  protected dataSource: DataSource;

  protected attrs(): FactorizedAttrs<UserEntity> {
    return {
      fullName: faker.name.findName(),
      email: faker.internet.email(),
    };
  }
}

And the following seeder class (generate-users.seeder.ts):

export class GenerateUsersSeeder extends Seeder {
  async run(dataSource: DataSource): Promise<void> {
    const userRepo = dataSource.getRepository(UserEntity);
    const users = await new UserFactory().makeMany(10);

    await userRepo.insert(users);
  }
}

Since ormconfig will be removed in TypeORM v0.4.0, we've decided to drop it as well and use the new DataSource interface. The dataSource configuration is as follows:

export const DbDataSource = new DataSource({
  name: 'default',
  type: 'mysql',
  host: '127.0.0.1',
  port: 3307,
  username: 'root',
  password: 'root',
  database: 'db-test',
  timezone: 'Z',
  dateStrings: ['DATE'],
  synchronize: false,
  entities: ['./src/**/*.entity{.ts,.js}'],
  logging: 'all',
  logger: 'advanced-console',
  maxQueryExecutionTime: 180000,
  cache: false,
  migrations: ['./src/database/migrations/**/*{.ts,.js}'],
});

The problem is when I run:

yarn typeorm-seeding seed -d src/database/main-db/db-data-source.ts src/database/seeds/user/generate-users.seeder.ts

I get the following error:

Could not load seeders!
Error: No default seeders found

I would be very much aprecciated if you could help me. If you need any more information, please let me know! It's my first issue in an open source repo :)

Remove context on factories

Right now, there are a lot of different ways to pass custom data to a factory function.

Context has nothing special related to the other ways, and is only creating problems on dev.

Future maintainance

@jorgebodega I left a comment in the pinned issue at the forked repo here: w3tecch#201 (comment) but I will re-paste here as dev shops are probably wondering what to do moving forward. And since you're now added as a maintainer in the original:

@jorgebodega I agree with @nicolasrouanne, any plans to merge the forked repo back into w3tecch now that you're a maintainer? I imagine you could merge it back and cut a major release and go from there. Would like to know going forward as upgrading to the latest typeorm breaks the current version of w3tecch/typeorm-seeding.

Improve documentation for default seeder

Hi! Thank you for the awesome work you do maintaining this library.

Please consider to mention in documentation that seeder classes should be exported without default export if the defaultSeeder configuration is set to the name of the seeder as you show in examples ("RootSeeder").

I just wasted one hour figuring out why my default seeder was not found. Maybe the solution was trivial, but not for me :)
Hope this may help other peoples.

Thank you.

Incorrect usage of `glob.sync` in `src/utils/fileHandling.ts`

Glob patterns allow only / as path separators, independent of platform.

However, process.cwd() and path.resolve() will use \ as path separators on Windows.

Instead of:

return glob.sync(path.resolve(process.cwd(), filePattern), { ignore: '**/node_modules/**' })

a correct usage would be:

return glob.sync(filePattern, { ignore: '**/node_modules/**', absolute: true });

Note: glob also has the cwd option which would default to process.cwd() anyway, so no need pass it.

Later Edit: I understand why the path.resolve was called. If you need absolute path, glob has an option: { absolute: true }.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.