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

typeorm-stubs

v1.2.1

Published

Stubs generator for Typeorm entities

Downloads

1,779

Readme

typeorm-stubs

Stubs generator for Typeorm entities. It uses typeorm metadata to get the type of the column and generate a random value.

Contents

Usage

Simple usage

You may generate a single stub or an array of stubs from Typeorm entity like this:

@Entity()
export class MyEntity {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @CreateDateColumn()
  public createdAt: Date;
  
  @Column({ type: 'varchar', nullable: false })
  public name: string;
  
  @Column({ type: 'integer', nullable: false })
  public index: number;
  
  @Column({ type: 'boolean', nullable: false })
  public isMain: boolean;
}

This code generates a single stub:

const stub = new Stub().createOne(MyEntity);

It gives you an object like this:

const stub: MyEntity = {
  id: '125a1c28-2938-4996-95f0-d768cbc3c15e',
  createdAt: new Date('2022-10-09T04:43:05.976Z'),
  name: 'Proin interdum adipiscing vel tortor.',
  index: 487,
  isMain: true,
}

For an array of stubs use this code:

const stubs = new Stub().createMany(MyEntity, 5);

It gives you 5 stubs in array. Param count is optional and can be omitted, then random count of stubs will be generated.

Deep generation

You may create a stub from entity with relations, e.g.:

@Entity()
export class MyEntity {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @CreateDateColumn()
  public createdAt: Date;
  
  @Column({ type: 'varchar', nullable: false })
  public name: string;
  
  @Column({ type: 'integer', nullable: false })
  public index: number;
  
  @Column({ type: 'boolean', nullable: false })
  public isMain: boolean;
  
  @Column({ type: 'uuid', nullable: true })
  public relationId?: string;
  
  @ManyToOne(() => RelationEntity)
  @JoinColumn({ name: 'relationId' })
  public relation?: RelationEntity;
}

@Entity()
export class RelationEntity {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @CreateDateColumn()
  public createdAt: Date;
  
  @Column({ type: 'varchar', nullable: false })
  public description: string;
  
  @OneToMany(() => DeepRelation)
  public deepRelations: DeepRelation[];
}

@Entity()
export class DeepRelation {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @CreateDateColumn()
  public createdAt: Date;

  @Column({ type: 'integer', nullable: false })
  public index: number;

  @Column({ type: 'uuid', nullable: true })
  public parentId?: string;

  @ManyToOne(() => RelationEntity)
  @JoinColumn({ name: 'relationId' })
  public parent?: RelationEntity;
}

It gives:

const stub: MyEntity = {
  id: '125a1c28-2938-4996-95f0-d768cbc3c15e',
  createdAt: new Date('2022-10-09T04:43:05.976Z'),
  name: 'Proin interdum adipiscing vel tortor.',
  index: 487,
  isMain: true,
  relationId: '3631150a-c6be-4fab-9896-182e67056efe',
  relation: {
    id: '3631150a-c6be-4fab-9896-182e67056efe',
    createdAt: new Date('2022-10-09T04:43:05.976Z'),
    description: 'Sit ut, mattis cursus. porttitor feugiat sit malesuada vitae.',
    deepRelations: [
      {
        id: '7d8b2f6b-bd52-424a-adcf-770a979690d1',
        createdAt: new Date('2022-10-09T04:43:05.976Z'),
        index: 15,
        parentId: '3631150a-c6be-4fab-9896-182e67056efe',
      },
      {
        id: '02abe928-08e5-412a-957b-7a45382df9fc',
        createdAt: new Date('2022-10-09T04:43:05.976Z'),
        index: 8,
        parentId: '3631150a-c6be-4fab-9896-182e67056efe',
      },
      {
        id: '74a1ded5-e9d9-4fd4-bb1e-161d4d0df412',
        createdAt: new Date('2022-10-09T04:43:05.976Z'),
        index: 593,
        parentId: '3631150a-c6be-4fab-9896-182e67056efe',
      },
      {
        id: '8baa6530-3fa3-4778-ba11-f675d7d653a4',
        createdAt: new Date('2022-10-09T04:43:05.976Z'),
        index: 967,
        parentId: '3631150a-c6be-4fab-9896-182e67056efe',
      },
    ],
  },
}

Foreign keys will be mapped to an entity if it's possible.

Note that circular dependencies will be omitted, and entities won't be generated twice.

A large number of elements in the array makes the generation slower. To improve performance you can pass deep: false in the options, this will disable all deep generation for stubs, e.g.:

const stub = new Stub().createMany(MyEntity, 536, {
  deep: false,
});

Null defaults

By passing nullDefaults: true in the options, the stub will return null for any column that is flagged as nullable, e.g. given the entity:

@Entity()
export class MyEntity {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @CreateDateColumn()
  public createdAt: Date;
  
  @Column({ type: 'varchar', nullable: false })
  public name: string;
  
  @Column({ type: 'integer', nullable: true })
  public index: number;
  
  @Column({ type: 'boolean', nullable: true })
  public isMain: boolean;
  
}

Then calling:

const stubs = new Stub().createOne(MyEntity, { nullDefaults: true });

Will give:

const stub: MyEntity = {
  id: '125a1c28-2938-4996-95f0-d768cbc3c15e',
  createdAt: new Date('2022-10-09T04:43:05.976Z'),
  name: 'Proin interdum adipiscing vel tortor.',
  index: null,
  isMain: null
}

Generator overriding

You may override a stub generator for specific types, e.g. for string. You should create a new class implementing StubGenerator interface and pass it to Stub:

class MyGenerator implements StubGenerator {
  public generateString(_column: ColumnMetadataArgs): string {
    return 'overrided!';
  }
}

const stub = new Stub(MyGenerator).createOne(MyEntity);

It gives:

const stub: MyEntity = {
  id: '125a1c28-2938-4996-95f0-d768cbc3c15e',
  createdAt: new Date('2022-10-09T04:43:05.976Z'),
  name: 'overrided!',
  index: 487,
  isMain: true,
}

Todos

  • "level" option for deep generation
  • Tests
  • Overriding by column type
  • Generating PostgreSQL arrays, JSONs