Skip to content

Commit

Permalink
Merge 4a5c378 into 099ea56
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianhoitz committed Feb 7, 2019
2 parents 099ea56 + 4a5c378 commit 323b8bc
Show file tree
Hide file tree
Showing 41 changed files with 1,987 additions and 771 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,4 +1,6 @@
node_modules
.vscode
.idea
lib/
coverage
coverage
package-lock.json
7 changes: 7 additions & 0 deletions .prettierrc
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true,
"arrowParens": "always"
}
19 changes: 19 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,19 @@
version: '3'
services:
mongo:
image: mongo
ports:
- '27017:27017'

mysql:
image: mysql:5.6
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 'secret'
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_PORT: 3306

ports:
- '3306:3306'
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"author": "Marc J. Schmidt <marc@marcjschmidt.de>",
"license": "MIT",
"scripts": {
"test": "jest --coverage",
"test": "docker-compose up -d && jest --coverage",
"bootstrap": "lerna bootstrap --hoist --no-ci",
"tsc": "lerna run tsc",
"publish": "lerna publish"
Expand All @@ -19,9 +19,13 @@
"jest": "^23.6.0",
"jest-extended": "^0.11.0",
"lerna": "^3.4.3",
"prettier": "^1.16.4",
"reflect-metadata": "^0.1.12",
"ts-jest": "^23.10.4",
"ts-node": "^6.0.0",
"tslint": "^5.12.1",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^2.9.2"
},
"engines": {
Expand Down
90 changes: 71 additions & 19 deletions packages/core/src/decorators.ts
@@ -1,14 +1,22 @@
import {Types} from "./mapper";
import {ClassType, getClassName} from "./utils";
import {AddValidator, PropertyValidator, PropertyValidatorError} from "./validation";
import { Types } from './mapper';
import { ClassType, getClassName } from './utils';
import {
AddValidator,
PropertyValidator,
PropertyValidatorError,
} from './validation';

export const RegisteredEntities: { [name: string]: ClassType<any> } = {};

export function Entity<T>(name: string, collectionName?: string) {
return (target: ClassType<T>) => {
RegisteredEntities[name] = target;
Reflect.defineMetadata('marshal:entityName', name, target);
Reflect.defineMetadata('marshal:collectionName', collectionName || (name + 's'), target);
Reflect.defineMetadata(
'marshal:collectionName',
collectionName || name + 's',
target
);
};
}

Expand All @@ -33,7 +41,12 @@ export function ID() {

export function ParentReference<T>() {
return (target: Object, property: string) => {
Reflect.defineMetadata('marshal:parentReference', true, target, property);
Reflect.defineMetadata(
'marshal:parentReference',
true,
target,
property
);
};
}

Expand All @@ -53,7 +66,7 @@ export function OnLoad(options: { fullLoad?: boolean } = {}) {
return (target: Object, property: string) => {
addMetadataArray('marshal:onLoad', target, {
property: property,
options: options
options: options,
});
};
}
Expand All @@ -80,10 +93,9 @@ export function ExcludeToPlain() {
}

export function registerProperty(target: Object, property: string) {
addMetadataArray('marshal:properties', target, property)
addMetadataArray('marshal:properties', target, property);
}


export function Type(type: Types) {
return (target: Object, property: string) => {
Reflect.defineMetadata('marshal:dataType', type, target, property);
Expand All @@ -108,26 +120,49 @@ export function MapType() {
export function ClassCircular<T>(classType: () => ClassType<T>) {
return (target: Object, property: string) => {
Type('class')(target, property);
Reflect.defineMetadata('marshal:dataTypeValue', classType, target, property);
Reflect.defineMetadata('marshal:dataTypeValueCircular', true, target, property);
Reflect.defineMetadata(
'marshal:dataTypeValue',
classType,
target,
property
);
Reflect.defineMetadata(
'marshal:dataTypeValueCircular',
true,
target,
property
);
};
}

export function Class<T>(classType: ClassType<T>) {
return (target: Object, property: string) => {
if (!classType) {
throw new Error(`${getClassName(target)}::${property} has @Class but argument is empty. Use @ClassCircular(() => YourClass) to work around circular dependencies.`);
throw new Error(
`${getClassName(
target
)}::${property} has @Class but argument is empty. Use @ClassCircular(() => YourClass) to work around circular dependencies.`
);
}

Type('class')(target, property);
Reflect.defineMetadata('marshal:dataTypeValue', classType, target, property);
Reflect.defineMetadata(
'marshal:dataTypeValue',
classType,
target,
property
);
};
}

export function ClassMap<T>(classType: ClassType<T>) {
return (target: Object, property: string) => {
if (!classType) {
throw new Error(`${getClassName(target)}::${property} has @ClassMap but argument is empty. Use @ClassMap(() => YourClass) to work around circular dependencies.`);
throw new Error(
`${getClassName(
target
)}::${property} has @ClassMap but argument is empty. Use @ClassMap(() => YourClass) to work around circular dependencies.`
);
}

Class(classType)(target, property);
Expand All @@ -145,7 +180,11 @@ export function ClassMapCircular<T>(classType: () => ClassType<T>) {
export function ClassArray<T>(classType: ClassType<T>) {
return (target: Object, property: string) => {
if (!classType) {
throw new Error(`${getClassName(target)}::${property} has @ClassArray but argument is empty. Use @ClassArrayCircular(() => YourClass) to work around circular dependencies.`);
throw new Error(
`${getClassName(
target
)}::${property} has @ClassArray but argument is empty. Use @ClassArrayCircular(() => YourClass) to work around circular dependencies.`
);
}

Class(classType)(target, property);
Expand All @@ -165,7 +204,7 @@ function concat(...decorators: ((target: Object, property: string) => void)[]) {
for (const decorator of decorators) {
decorator(target, property);
}
}
};
}

export function MongoIdType() {
Expand All @@ -186,7 +225,11 @@ export function BinaryType() {

export function StringType() {
class Validator implements PropertyValidator {
async validate<T>(value: any, target: ClassType<T>, property: string): Promise<PropertyValidatorError | void> {
async validate<T>(
value: any,
target: ClassType<T>,
property: string
): Promise<PropertyValidatorError | void> {
if ('string' !== typeof value) {
return new PropertyValidatorError('No String given');
}
Expand All @@ -202,7 +245,11 @@ export function AnyType() {

export function NumberType() {
class Validator implements PropertyValidator {
async validate<T>(value: any, target: ClassType<T>, property: string): Promise<PropertyValidatorError | void> {
async validate<T>(
value: any,
target: ClassType<T>,
property: string
): Promise<PropertyValidatorError | void> {
value = parseFloat(value);

if (!Number.isFinite(value)) {
Expand All @@ -222,6 +269,11 @@ export function EnumType(type: any, allowLabelsAsValue = false) {
return (target: Object, property: string) => {
Type('enum')(target, property);
Reflect.defineMetadata('marshal:dataTypeValue', type, target, property);
Reflect.defineMetadata('marshal:enum:allowLabelsAsValue', allowLabelsAsValue, target, property);
}
Reflect.defineMetadata(
'marshal:enum:allowLabelsAsValue',
allowLabelsAsValue,
target,
property
);
};
}

0 comments on commit 323b8bc

Please sign in to comment.