Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅 Await promise #27

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules
lib
coverage/

# IDEs and editors
Expand Down
1 change: 1 addition & 0 deletions lib/decorators/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './post-generation.decorator';
17 changes: 17 additions & 0 deletions lib/decorators/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./post-generation.decorator"), exports);
1 change: 1 addition & 0 deletions lib/decorators/post-generation.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function PostGeneration(): (target: any, propertyKey: string) => void;
10 changes: 10 additions & 0 deletions lib/decorators/post-generation.decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostGeneration = void 0;
const factory_storage_1 = require("../factory-storage");
function PostGeneration() {
return function (target, propertyKey) {
factory_storage_1.FactoryStorage.storage.addFactoryPostGenerator(target.constructor.name, propertyKey);
};
}
exports.PostGeneration = PostGeneration;
10 changes: 10 additions & 0 deletions lib/factory-storage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export declare class FactoryStorage {
private static instance;
factoryPostGenerators: {
[factoryName: string]: string[];
};
private constructor();
static get storage(): FactoryStorage;
addFactoryPostGenerator(factoryName: string, fnName: string): void;
getPostGenerators(factoryName: string): string[];
}
26 changes: 26 additions & 0 deletions lib/factory-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FactoryStorage = void 0;
class FactoryStorage {
constructor() {
this.factoryPostGenerators = {};
}
static get storage() {
if (!this.instance) {
this.instance = new FactoryStorage();
}
return this.instance;
}
addFactoryPostGenerator(factoryName, fnName) {
if (!this.factoryPostGenerators[factoryName]) {
this.factoryPostGenerators[factoryName] = [fnName];
}
else {
this.factoryPostGenerators[factoryName].push(fnName);
}
}
getPostGenerators(factoryName) {
return this.factoryPostGenerators[factoryName];
}
}
exports.FactoryStorage = FactoryStorage;
12 changes: 12 additions & 0 deletions lib/factory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ObjectLiteral } from 'typeorm';
import { Constructable } from './types';
export declare abstract class Factory<T extends ObjectLiteral> {
abstract get entity(): Constructable<T>;
constructor();
create(values?: Partial<T>): Promise<T>;
createMany(count: number, values?: Partial<T>): Promise<T[]>;
protected getOrCreate(): string[];
private getExistingEntity;
private createEntity;
private static getEntityValue;
}
61 changes: 61 additions & 0 deletions lib/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Factory = void 0;
const typeorm_1 = require("typeorm");
const subfactory_1 = require("./subfactory");
const sequence_1 = require("./sequence");
const factory_storage_1 = require("./factory-storage");
class Factory {
constructor() { }
async create(values = {}) {
if (this.getOrCreate().length !== 0) {
const existingEntity = await this.getExistingEntity(values);
if (existingEntity) {
return existingEntity;
}
}
const entity = await this.createEntity(values);
const savedEntity = await (0, typeorm_1.getRepository)(this.entity).save(entity);
const storage = factory_storage_1.FactoryStorage.storage;
const postGenerators = storage.getPostGenerators(this.constructor.name);
if (postGenerators && postGenerators.length !== 0) {
await Promise.all(postGenerators.map(async (fnName) => this[fnName](savedEntity)));
}
return savedEntity;
}
async createMany(count, values = {}) {
const entities = await Promise.all(Array.from({ length: count }).map(() => this.createEntity(values)));
return (0, typeorm_1.getRepository)(this.entity).save(entities);
}
getOrCreate() {
return [];
}
async getExistingEntity(values) {
const whereClauses = {};
this.getOrCreate().forEach((key) => {
whereClauses[key] = values[key] ? values[key] : this[key];
});
return (0, typeorm_1.getRepository)(this.entity).findOne({ where: whereClauses });
}
async createEntity(values) {
const entity = new this.entity();
await Promise.all(Object.entries(this).map(async ([key, value]) => {
const _value = Object.prototype.hasOwnProperty.call(values, key) ? values[key] : value;
const entityValue = await Factory.getEntityValue(_value);
Object.assign(entity, { [key]: entityValue });
}));
return entity;
}
static async getEntityValue(value) {
if (value instanceof subfactory_1.SubFactory) {
return value.factory.create(value.values);
}
else if (value instanceof sequence_1.Sequence) {
return value.nextValue;
}
else {
return value;
}
}
}
exports.Factory = Factory;
6 changes: 6 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './factory';
export * from './sequence';
export * from './subfactory';
export * from './types';
export * from './factory-storage';
export * from './decorators';
22 changes: 22 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./factory"), exports);
__exportStar(require("./sequence"), exports);
__exportStar(require("./subfactory"), exports);
__exportStar(require("./types"), exports);
__exportStar(require("./factory-storage"), exports);
__exportStar(require("./decorators"), exports);
7 changes: 7 additions & 0 deletions lib/sequence.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SequenceFn } from './types';
export declare class Sequence {
fn: SequenceFn;
private currentIndex;
constructor(fn: SequenceFn);
get nextValue(): any;
}
15 changes: 15 additions & 0 deletions lib/sequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sequence = void 0;
class Sequence {
constructor(fn) {
this.currentIndex = 0;
this.fn = fn;
}
get nextValue() {
const value = this.fn(this.currentIndex);
this.currentIndex++;
return value;
}
}
exports.Sequence = Sequence;
8 changes: 8 additions & 0 deletions lib/subfactory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FactoryClass } from './types';
import { Factory } from './factory';
import { ObjectLiteral } from 'typeorm';
export declare class SubFactory<T extends ObjectLiteral> {
factory: Factory<T>;
values: Partial<T> | undefined;
constructor(factory: FactoryClass<T>, values?: Partial<T>);
}
10 changes: 10 additions & 0 deletions lib/subfactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubFactory = void 0;
class SubFactory {
constructor(factory, values) {
this.factory = new factory();
this.values = values;
}
}
exports.SubFactory = SubFactory;
5 changes: 5 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ObjectLiteral } from 'typeorm';
import { Factory } from './factory';
export type FactoryClass<T extends ObjectLiteral> = new () => Factory<T>;
export type Constructable<T> = new (...args: any[]) => T;
export type SequenceFn = (i: number) => any;
2 changes: 2 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Loading