Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
feat(ModelDescription): Improved support of multiple definitions
Browse files Browse the repository at this point in the history
Array & Object field types may have several definitions. Support was subject to some quirks. Fixes #53
  • Loading branch information
Gerkin committed Jan 16, 2019
1 parent 660a9e1 commit 84c0459
Show file tree
Hide file tree
Showing 18 changed files with 573 additions and 360 deletions.
4 changes: 2 additions & 2 deletions .vscode/csak-timelog.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"starttime": "9/16/2018, 7:56:26 PM",
"totaltimesec": 203820,
"lasttime": "1/16/2019, 3:24:40 PM"
"totaltimesec": 205260,
"lasttime": "1/16/2019, 3:51:02 PM"
}
70 changes: 35 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,41 @@
"testMatch": null
},
"devDependencies": {
"@babel/core": "7.2.2",
"@babel/preset-env": "7.2.3",
"@types/express": "4.16.0",
"@types/jest": "23.3.10",
"@types/lodash": "4.14.118",
"@types/logform": "1.2.0",
"@types/node": "10.12.10",
"@types/request-promise": "4.1.42",
"commitizen": "3.0.4",
"conventional-changelog-cli": "2.0.11",
"coveralls": "3.0.2",
"cz-conventional-changelog": "2.1.0",
"express": "4.16.4",
"intercept-stdout": "0.1.2",
"jest": "23.6.0",
"jest-extended": "0.11.0",
"rimraf": "2.6.2",
"rollup": "0.68.0",
"rollup-plugin-babel": "4.0.3",
"rollup-plugin-commonjs": "9.2.0",
"rollup-plugin-jscc": "1.0.0",
"rollup-plugin-json": "3.1.0",
"rollup-plugin-node-resolve": "4.0.0",
"rollup-plugin-sourcemaps": "0.4.2",
"rollup-plugin-terser": "4.0.0",
"rollup-plugin-typescript2": "0.18.0",
"semantic-release": "15.13.3",
"ts-jest": "23.10.5",
"tslint": "5.11.0",
"tslint-config-standard": "8.0.1",
"typedoc": "0.14.0",
"typescript": "3.1.6",
"validate-commit-msg": "2.14.0",
"validate-npm-package-name": "3.0.0",
"xmlhttprequest": "1.8.0"
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.12",
"@types/lodash": "^4.14.119",
"@types/logform": "^1.2.0",
"@types/node": "^10.12.18",
"@types/request-promise": "^4.1.42",
"commitizen": "^3.0.5",
"conventional-changelog-cli": "^2.0.11",
"coveralls": "^3.0.2",
"cz-conventional-changelog": "^2.1.0",
"express": "^4.16.4",
"intercept-stdout": "^0.1.2",
"jest": "^23.6.0",
"jest-extended": "^0.11.0",
"rimraf": "^2.6.3",
"rollup": "^1.1.0",
"rollup-plugin-babel": "^4.3.0",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-jscc": "^1.0.0",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^4.0.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-terser": "^4.0.2",
"rollup-plugin-typescript2": "^0.18.1",
"semantic-release": "^15.13.3",
"ts-jest": "^23.10.5",
"tslint": "^5.12.1",
"tslint-config-standard": "^8.0.1",
"typedoc": "^0.14.1",
"typescript": "^3.2.2",
"validate-commit-msg": "^2.14.0",
"validate-npm-package-name": "^3.0.0",
"xmlhttprequest": "^1.8.0"
},
"dependencies": {
"lodash": "^4.17.11",
Expand Down
45 changes: 29 additions & 16 deletions src/adapters/base/adapter-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@ import AAdapterEntity = _Adapter.Base.AAdapterEntity;
import AAdapter = _Adapter.Base.AAdapter;
import { QueryLanguage } from '../../types/queryLanguage';
import { IEntityAttributes } from '../../types/entity';
import { isNumber, isString } from 'lodash';

/**
* Generic constructor for a certain type
*/
export type Constructor<TClass> = new ( ...args: any[] ) => TClass;

const getNum = ( ...params: Array<string | string[]> ) => {
const flatten = _.flattenDeep( params ) as string[];
const [fullMatch, sign, val] = flatten;
if ( '∞' === val ) {
if ( '-' === sign ) {
return -Infinity;
} else {
return Infinity;
}
} else {
return parseInt( fullMatch, 10 );
}
};

const validations = {
type: {
int( key: string, val: string | number ) {
Expand All @@ -42,6 +29,22 @@ const validations = {
/^([[\]])((-)?(\d+|)),((-)?(\d+|))([[\]])$/
);
if ( rangeMatch ) {
// Cast a number component to a number
const getNum = ( ...params: Array<string | string[]> ) => {
const flatten = _.flattenDeep( params ) as string[];
const [fullMatch, sign, val] = flatten;
if ( '∞' === val ) {
if ( '-' === sign ) {
return -Infinity;
} else {
return Infinity;
}
} else {
return parseInt( fullMatch, 10 );
}
};


const lower = getNum( rangeMatch.splice( 2, 3 ) );
const upper = getNum( rangeMatch.splice( 2, 3 ) );
const isInRangeLower = '[' === rangeMatch[1] ? val >= lower : val > lower;
Expand All @@ -55,16 +58,26 @@ const validations = {
return val;
},
};
const validateOption = (

/**
* Check if a validation matches
*
* @param key - The name of the option checked
* @param val - The value to match against
*/
const validateOption = <TVal>(
key: string,
val: string | number,
val: TVal,
config: { type: string; rng?: string }
): any => {
const valTypes: any = validations.type;
if ( valTypes[config.type] ) {
val = valTypes[config.type]( key, val );
}
if ( config.rng ) {
if ( !isString( val ) && !isNumber( val ) ){
throw new TypeError( `A range operator to match against the option ${key} must be a string or a number, not a ${typeof val}` );
}
val = validations.rng( key, val, config.rng );
}
return val;
Expand Down
8 changes: 4 additions & 4 deletions src/adapters/base/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ export namespace Adapter.Base {
* @param options - Hash of options that forms the query
* @param query - Query function to loop
*/
private static async iterateLimit(
private static async iterateLimit<TRet>(
options: _QueryLanguage.IQueryOptions,
query: (
options: _QueryLanguage.IQueryOptions
) => Promise<IEntityProperties | undefined>
) {
const foundEntities: IEntityProperties[] = [];
) => Promise<TRet | undefined>
): Promise<TRet[]> {
const foundEntities: TRet[] = [];
const localOptions = _.assign( {}, options );
let origSkip = localOptions.skip;

Expand Down
14 changes: 4 additions & 10 deletions src/entities/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export abstract class Entity<TEntity extends IEntityAttributes> extends Sequenti
this._lastDataSource = null;
this.idHash = {};

// ### Load datas from source
// If we construct our Entity from a datastore entity (that can happen internally in Diaspora), set it to `sync` state
// ### Load datas from source
// If we construct our Entity from a datastore entity (that can happen internally in Diaspora), set it to `sync` state
if ( source instanceof AAdapterEntity ) {
// ### Load datas from source
// If we construct our Entity from a datastore entity (that can happen internally in Diaspora), set it to `sync` state
Expand All @@ -104,21 +104,15 @@ export abstract class Entity<TEntity extends IEntityAttributes> extends Sequenti
const sourceDModel = _.difference( _.keys( this._attributes ), modelAttrsKeys );
if ( 0 !== sourceDModel.length ) {
// Later, add a criteria for schemaless models
throw new Error(
`Source has unknown keys: ${JSON.stringify(
sourceDModel
)} in ${JSON.stringify( source )}`
);
throw new Error( `Source has unknown keys: ${JSON.stringify( sourceDModel )} in ${JSON.stringify( source )}` );
}

// ### Load events
_.forEach(
this.model.modelDesc.lifecycleEvents,
( eventFunctions, eventName ) => {
// Iterate on each event functions. `_.castArray` will ensure we iterate on an array if a single function is provided.
_.forEach( _.castArray( eventFunctions ), eventFunction => {
this.on( eventName, eventFunction );
} );
_.forEach( _.castArray( eventFunctions ), eventFunction => this.on( eventName, eventFunction ) );
}
);
}
Expand Down
Loading

0 comments on commit 84c0459

Please sign in to comment.