Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
onechiporenko committed Nov 22, 2021
1 parent 1ccaf87 commit b9628d9
Show file tree
Hide file tree
Showing 29 changed files with 5,405 additions and 5,400 deletions.
489 changes: 251 additions & 238 deletions README.md

Large diffs are not rendered by default.

522 changes: 257 additions & 265 deletions lib/factory.ts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CreateRecordExtraData,
Factory,
FactoryData,
FieldMetaAttr,
Expand All @@ -7,6 +8,10 @@ import {
MetaAttrType,
RelationshipMetaAttr,
SequenceMetaAttr,
field,
hasOne,
hasMany,
sequenceItem,
} from './factory';
import { CRUDOptions, DevInfo, DevInfoItem, Lair } from './lair';

Expand All @@ -15,6 +20,7 @@ import { LairRecord } from './record';
import { Relationships } from './relationships';

export {
CreateRecordExtraData,
CRUDOptions,
DevInfoItem,
DevInfo,
Expand All @@ -29,4 +35,8 @@ export {
FieldMetaAttr,
SequenceMetaAttr,
RelationshipMetaAttr,
field,
hasOne,
hasMany,
sequenceItem,
};
70 changes: 44 additions & 26 deletions lib/lair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Lair {
private factories: { [id: string]: FactoryData } = {};
private relationships: Relationships;
private db: InternalDb = {};
private meta: InternalMetaStore = {};
public meta: InternalMetaStore = {};
private afterCreateQueue: AfterCreateItem[] = [];

private constructor() {
Expand All @@ -121,10 +121,16 @@ export class Lair {
* Register factory instance in the Lair
* Lair works only with registered factories
*/
public registerFactory(factory: Factory, factoryName?: string): void {
const name = factory.name || factoryName;
public registerFactory(
factoryInstanceOrClass: Factory | typeof Factory
): void {
const factory =
factoryInstanceOrClass instanceof Factory
? factoryInstanceOrClass
: new factoryInstanceOrClass();
const name = factory.getFactoryName();
assert(
'Factory name must be defined in the `Factory.create` or it must be provided here as a second argument',
'Factory name must be defined in the `Factory` child-class as a static property "factoryName"',
!!name
);
assert(
Expand All @@ -136,7 +142,6 @@ export class Lair {
this.relationships.addFactory(name);
this.relationships.updateMeta(this.meta);
this.addType(name);
factory.init();
}

/**
Expand Down Expand Up @@ -324,6 +329,9 @@ export class Lair {
);
const meta = this.getMetaFor(factoryName);
keys(data).forEach((attrName) => {
if (attrName === 'id') {
return;
}
if (hasOwnProperty.call(meta, attrName)) {
record[attrName] = this.createAttrValue(
factoryName,
Expand Down Expand Up @@ -410,7 +418,7 @@ export class Lair {
assertLoops(factoryName, relatedChain);
}
const factoryData = this.factories[factoryName];
const { meta, createRelated } = factoryData.factory;
const { meta } = factoryData.factory;
const newRecords = [];
let counter = 1;
for (let i = 0; i < count; i++) {
Expand All @@ -431,24 +439,26 @@ export class Lair {
newRecords.push(record);
this.factories[factoryName].id++;
this.afterCreateQueue.push({ factoryName, id: record.id, extraData });
if (createRelated) {
keys(createRelated).forEach((attrName) => {
const fName = (meta[attrName] as RelationshipMetaAttr).factoryName;
const isHasMany = meta[attrName].type === MetaAttrType.HAS_MANY;
const relatedCount = isHasMany
? getOrCalcValue<number>(createRelated[attrName], record, record.id)
: 1;
const relatedRecords = this.internalCreateRecords(
fName,
relatedCount,
{ factoryName, attrName },
[...relatedChain, factoryName]
);
this.db[factoryName][record.id][attrName] = isHasMany
? relatedRecords
: relatedRecords[0];
});
}
keys(meta).forEach((attrName) => {
const isHasMany = meta[attrName].type === MetaAttrType.HAS_MANY;
const createRelated = meta[attrName].createRelated;
if (!createRelated) {
return;
}
const fName = (meta[attrName] as RelationshipMetaAttr).factoryName;
const relatedCount = isHasMany
? getOrCalcValue<number>(createRelated, record, record.id)
: 1;
const relatedRecords = this.internalCreateRecords(
fName,
relatedCount,
{ factoryName, attrName },
[...relatedChain, factoryName]
);
this.db[factoryName][record.id][attrName] = isHasMany
? relatedRecords
: relatedRecords[0];
});
this.relationships.recalculateRelationshipsForRecord(
factoryName,
this.db[factoryName][record.id]
Expand Down Expand Up @@ -764,8 +774,16 @@ export class Lair {
return newDistIds;
}

private getMetaFor(factoryName: string): Meta {
return this.meta[factoryName];
public getMetaFor(factoryNameOrClass: any): Meta {
const metaForFactoryName = this.meta[factoryNameOrClass];
if (metaForFactoryName) {
return metaForFactoryName;
}
const factoryName = keys(this.factories).find(
(factoryName) =>
this.factories[factoryName].factory === factoryNameOrClass
);
return factoryName ? this.meta[factoryName] : undefined;
}

private getRelatedFactoryNames(factoryName: string): string[] {
Expand Down
4 changes: 3 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function uniq<T>(list: T[]): T[] {
}

export function copy<T>(val: T): T {
return JSON.parse(JSON.stringify(val));
return val === undefined || val === null
? val
: JSON.parse(JSON.stringify(val));
}

export function getOrCalcValue<T>(
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "lair-db",
"version": "2.1.0",
"version": "3.0.0",
"description": "JS database",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"scripts": {
"build": "rm -rf dist && tsc",
"build-publish": "npm run build && rm -rf dist/tests && rm -rf dist/lib/*.js.map && cp dist/lib/* dist/. && rm -rf dist/lib",
"test": "mocha -r ts-node/register tests/**/*.ts",
"lint": "eslint -c .eslintrc.json lib tests",
"lint": "eslint -c .eslintrc.json lib tests --fix",
"test:remap": "remap-istanbul -i dist/coverage/coverage.json -o ./dist/coverage/coverage -t lcovonly",
"test:cov": "istanbul cover --dir ./dist/coverage ./node_modules/mocha/bin/_mocha -- dist/tests/**/*.js --report lcovonly -R spec --bail -- && npm run test:remap",
"docs": "typedoc --out ./docs lib"
Expand Down Expand Up @@ -45,5 +45,8 @@
"ts-node": "^10.2.1",
"typedoc": "^0.22.4",
"typescript": "^4.4.3"
},
"dependencies": {
"reflect-metadata": "^0.1.13"
}
}
Loading

0 comments on commit b9628d9

Please sign in to comment.