mikro-orm
Advanced tools
Comparing version 0.8.2 to 0.9.0
@@ -66,2 +66,5 @@ "use strict"; | ||
} | ||
if (props[prop] && props[prop].reference === ReferenceType.SCALAR) { | ||
this[prop] = em.validator.validateProperty(props[prop], this[prop], this); | ||
} | ||
this[prop] = data[prop]; | ||
@@ -68,0 +71,0 @@ }); |
@@ -6,2 +6,3 @@ import { Collection as MongoCollection, Db, FilterQuery } from 'mongodb'; | ||
import { Options } from './MikroORM'; | ||
import { Validator } from './Validator'; | ||
export declare class EntityManager { | ||
@@ -14,2 +15,3 @@ private db; | ||
}; | ||
readonly validator: Validator; | ||
private readonly unitOfWork; | ||
@@ -16,0 +18,0 @@ private readonly repositoryMap; |
@@ -11,2 +11,3 @@ "use strict"; | ||
const Collection_1 = require("./Collection"); | ||
const Validator_1 = require("./Validator"); | ||
class EntityManager { | ||
@@ -18,2 +19,3 @@ constructor(db, options) { | ||
this.identityMap = {}; | ||
this.validator = new Validator_1.Validator(this.options.strict); | ||
this.unitOfWork = new UnitOfWork_1.UnitOfWork(this); | ||
@@ -20,0 +22,0 @@ this.repositoryMap = {}; |
@@ -23,2 +23,3 @@ import 'reflect-metadata'; | ||
entitiesDirs: string[]; | ||
strict: boolean; | ||
logger?: Function; | ||
@@ -25,0 +26,0 @@ baseDir?: string; |
@@ -52,2 +52,3 @@ "use strict"; | ||
this.removeUnknownProperties(ret, meta); | ||
this.em.validator.validate(ret.entity, ret.payload, meta); | ||
if (Object.keys(ret.payload).length === 0) { | ||
@@ -54,0 +55,0 @@ return null; |
@@ -87,2 +87,6 @@ import { getMetadataStorage, getEntityManager } from './MikroORM'; | ||
if (props[prop] && props[prop].reference === ReferenceType.SCALAR) { | ||
this[prop] = em.validator.validateProperty(props[prop], this[prop], this) | ||
} | ||
this[prop] = data[prop]; | ||
@@ -89,0 +93,0 @@ }); |
@@ -9,2 +9,3 @@ import { Collection as MongoCollection, Db, FilterQuery, ObjectID } from 'mongodb'; | ||
import { Collection } from './Collection'; | ||
import { Validator } from './Validator'; | ||
@@ -15,2 +16,3 @@ export class EntityManager { | ||
public readonly identityMap: { [k: string]: BaseEntity } = {}; | ||
public readonly validator = new Validator(this.options.strict); | ||
@@ -17,0 +19,0 @@ private readonly unitOfWork = new UnitOfWork(this); |
@@ -88,2 +88,3 @@ import 'reflect-metadata'; | ||
entitiesDirs: string[]; | ||
strict: boolean; | ||
logger?: Function; | ||
@@ -90,0 +91,0 @@ baseDir?: string; |
@@ -65,2 +65,3 @@ import { Utils } from './Utils'; | ||
this.removeUnknownProperties(ret, meta); | ||
this.em.validator.validate(ret.entity, ret.payload, meta); | ||
@@ -67,0 +68,0 @@ if (Object.keys(ret.payload).length === 0) { |
{ | ||
"name": "mikro-orm", | ||
"version": "0.8.2", | ||
"version": "0.9.0", | ||
"description": "Simple typescript mongo ORM for node.js based on data-mapper, unit-of-work and identity-map patterns", | ||
@@ -71,5 +71,5 @@ "main": "dist/index.js", | ||
"ts-jest": "^22.4.6", | ||
"ts-node": "^6.1.0", | ||
"ts-node": "^7.0.0", | ||
"tslint": "^5.10.0" | ||
} | ||
} |
@@ -427,1 +427,2 @@ # mikro-orm | ||
- lifecycle hooks | ||
- property type validation |
@@ -22,2 +22,5 @@ import { | ||
@Property() | ||
age: number; | ||
@Property() | ||
born: Date; | ||
@@ -24,0 +27,0 @@ |
import { UnitOfWork } from '../lib/UnitOfWork'; | ||
import { Author } from './entities/Author'; | ||
import { EntityManager, MikroORM } from '../lib'; | ||
import { MikroORM } from '../lib'; | ||
import { initORM, wipeDatabase } from './bootstrap'; | ||
@@ -12,15 +12,40 @@ | ||
let orm: MikroORM; | ||
let uow: UnitOfWork; | ||
beforeAll(async () => orm = await initORM()); | ||
beforeAll(async () => { | ||
orm = await initORM(); | ||
uow = new UnitOfWork(orm.em); | ||
}); | ||
beforeEach(async () => wipeDatabase(orm.em)); | ||
test('should load entities', async () => { | ||
const Mock = jest.fn<EntityManager>(() => ({ | ||
entityFactory: jest.fn(), | ||
connection: jest.fn(), | ||
})); | ||
const em = new Mock(); | ||
const uow = new UnitOfWork(em); | ||
const a = new Author('test2', 'mail2@test.com'); | ||
uow.addToIdentityMap(a); | ||
test('entity validation when persisting [not strict]', async () => { | ||
// number instead of string will throw | ||
const author = new Author('test', 'test'); | ||
Object.assign(author, { name: 111, email: 222 }); | ||
await expect(uow.persist(author)).rejects.toThrowError(`Validation error: trying to set Author.name of type 'string' to '111' of type 'number'`); | ||
// string date with unknown format will throw | ||
Object.assign(author, { name: '333', email: '444', born: 'asd' }); | ||
await expect(uow.persist(author)).rejects.toThrowError(`Validation error: trying to set Author.born of type 'date' to 'asd' of type 'string'`); | ||
// string date with correct format will be auto-corrected | ||
Object.assign(author, { name: '333', email: '444', born: '2018-01-01' }); | ||
let changeSet = await uow.persist(author); | ||
expect(typeof changeSet.payload.name).toBe('string'); | ||
expect(typeof changeSet.payload.email).toBe('string'); | ||
expect(changeSet.payload.born instanceof Date).toBe(true); | ||
// string number with correct format will be auto-corrected | ||
Object.assign(author, { age: '21' }); | ||
changeSet = await uow.persist(author); | ||
expect(typeof changeSet.payload.age).toBe('number'); | ||
expect(changeSet.payload.age).toBe(21); | ||
// string instead of number with will throw | ||
Object.assign(author, { age: 'asd' }); | ||
await expect(uow.persist(author)).rejects.toThrowError(`Validation error: trying to set Author.age of type 'number' to 'asd' of type 'string'`); | ||
Object.assign(author, { age: new Date() }); | ||
await expect(uow.persist(author)).rejects.toThrowError(/Validation error: trying to set Author\.age of type 'number' to '.*' of type 'date'/); | ||
Object.assign(author, { age: false }); | ||
await expect(uow.persist(author)).rejects.toThrowError(`Validation error: trying to set Author.age of type 'number' to 'false' of type 'boolean'`); | ||
}); | ||
@@ -27,0 +52,0 @@ |
162471
74
3662
428