-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
still a work in progress
- Loading branch information
Showing
9 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"name": "@mikro-orm/seeder", | ||
"version": "4.0.0-alpha-0", | ||
"description": "Seeder package for Mikro-ORM.", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git" | ||
}, | ||
"keywords": [ | ||
"orm", | ||
"mongo", | ||
"mongodb", | ||
"mysql", | ||
"mariadb", | ||
"postgresql", | ||
"sqlite", | ||
"sqlite3", | ||
"ts", | ||
"typescript", | ||
"js", | ||
"javascript", | ||
"entity", | ||
"ddd", | ||
"mikro-orm", | ||
"unit-of-work", | ||
"data-mapper", | ||
"identity-map", | ||
"seeder" | ||
], | ||
"author": "Wybren Kortstra", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mikro-orm/mikro-orm/issues" | ||
}, | ||
"homepage": "https://mikro-orm.io", | ||
"engines": { | ||
"node": ">= 10.13.0" | ||
}, | ||
"scripts": { | ||
"build": "yarn clean && yarn compile && yarn copy", | ||
"clean": "rimraf ./dist", | ||
"compile": "tsc -p tsconfig.build.json", | ||
"copy": "ts-node -T ../../scripts/copy.ts" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@mikro-orm/core": "^4.0.7", | ||
"faker": "^4.1.0", | ||
"yargs": "^15.4.1" | ||
}, | ||
"peerDependencies": { | ||
"typescript-mix": "^3.1.3" | ||
}, | ||
"devDependencies": { | ||
"@types/faker": "^4.1.12" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { MikroORM, wrap } from '@mikro-orm/core'; | ||
|
||
export abstract class Factory<C> { | ||
|
||
private amount = 1; | ||
|
||
public abstract model: { new(): C }; | ||
|
||
constructor(private orm: MikroORM) { | ||
} | ||
|
||
public abstract definition(): Record<string, unknown>; | ||
|
||
public make(overrideParameters: Record<string, unknown>): C | C[] { | ||
const objects = [...Array(this.amount)].map(() => { | ||
const o = new this.model(); | ||
Object.assign(o, Object.assign({}, this.definition(), overrideParameters)); | ||
return o; | ||
}); | ||
return this.amount === 1 ? objects[0] : objects; | ||
} | ||
|
||
public async create(overrideParameters: Record<string, unknown>): Promise<C | C[]> { | ||
const objects = [...Array(this.amount)].map(() => { | ||
const o = new this.model(); | ||
wrap(o).assign(Object.assign({}, this.definition(), overrideParameters), | ||
{ em: this.orm.em }); | ||
return o; | ||
}); | ||
await this.orm.em.flush(); | ||
return this.amount === 1 ? objects[0] : objects; | ||
} | ||
|
||
public count(amount: number): Factory<C> { | ||
this.amount = amount; | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Factory } from './factory'; | ||
import { importFiles, loadFiles } from './utils/file.util'; | ||
|
||
export class HasFactory { | ||
|
||
public static async factory<C>(): Promise<Factory<C>> { | ||
if (!(global as any).imported) { | ||
(global as any).factories = new Map(); | ||
const factoryFiles = loadFiles(['./src/database/**/*.factory.ts']); | ||
(await importFiles(factoryFiles)).map((f: any) => { | ||
const factory: Factory<any> = new f(); | ||
(global as any).factories.set(factory.model.name, f); | ||
}); | ||
(global as any).imported = true; | ||
} | ||
if (!(global as any).factories.has(this.name)) { | ||
throw new Error(`Cannot get factory for ${this.name}`); | ||
} | ||
if (! (global as any).orm) { | ||
throw new Error(`MikroORM not found!`); | ||
} | ||
// Need some debugging for now | ||
// eslint-disable-next-line no-console | ||
console.log(`Getting factory for ${this.name}`); | ||
return new (global as any).factories.get(this.name)((global as any).orm); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { Seeder } from './seeder'; | ||
|
||
export const refreshDatabase = async (orm: MikroORM): Promise<void> => { | ||
const generator = orm.getSchemaGenerator(); | ||
await generator.dropSchema(); | ||
orm.config.getLogger().log('info', 'Dropped schema'); | ||
await generator.createSchema(); | ||
orm.config.getLogger().log('info', 'Recreated schema'); | ||
}; | ||
|
||
export const seed = async (orm: MikroORM, seeder: Seeder): Promise<void> => { | ||
(global as any).orm = orm; | ||
await seeder.run(); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { MikroORM } from '@mikro-orm/core'; | ||
|
||
export abstract class Seeder { | ||
|
||
public abstract run(): Promise<void>; | ||
|
||
protected call(orm: MikroORM, seeders: Seeder[]): Promise<void[]> { | ||
return Promise.all(seeders.map(s => s.run())); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as glob from 'glob'; | ||
import * as path from 'path'; | ||
|
||
export const importFiles = async (filePaths: string[]): Promise<any> => { | ||
return await Promise.all(filePaths.map(filePath => import(filePath))); | ||
}; | ||
|
||
export const loadFiles = (filePattern: string[]): string[] => { | ||
return filePattern | ||
.map(pattern => glob.sync(path.resolve(process.cwd(), pattern))) | ||
.reduce((acc, filePath) => acc.concat(filePath), []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*"] | ||
} |