mikro-orm
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -22,3 +22,3 @@ import { Collection as MongoCollection, Db, FilterQuery } from 'mongodb'; | ||
}, limit?: number, offset?: number): Promise<T[]>; | ||
findOne<T extends BaseEntity>(entityName: string, where: FilterQuery<T>, populate?: string[]): Promise<T>; | ||
findOne<T extends BaseEntity>(entityName: string, where: FilterQuery<T> | string, populate?: string[]): Promise<T>; | ||
merge<T extends BaseEntity>(entityName: string, data: any): T; | ||
@@ -25,0 +25,0 @@ getReference<T extends BaseEntity>(entityName: string, id: string): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const mongodb_1 = require("mongodb"); | ||
const BaseEntity_1 = require("./BaseEntity"); | ||
@@ -65,3 +66,5 @@ const EntityRepository_1 = require("./EntityRepository"); | ||
async findOne(entityName, where, populate = []) { | ||
// TODO support also where: ObjectID | ||
if (where instanceof mongodb_1.ObjectID) { | ||
where = where.toHexString(); | ||
} | ||
if (Utils_1.Utils.isString(where) && this.identityMap[`${entityName}-${where}`]) { | ||
@@ -72,5 +75,8 @@ // TODO populate missing references and rehydrate | ||
if (Utils_1.Utils.isString(where)) { | ||
where = { _id: where }; | ||
where = { _id: new mongodb_1.ObjectID(where) }; | ||
} | ||
const data = await this.getCollection(entityName).find(where).limit(1).next(); | ||
if (!data) { | ||
return null; | ||
} | ||
const entity = this.merge(entityName, data); | ||
@@ -81,3 +87,3 @@ await this.processPopulate(entity, populate); | ||
merge(entityName, data) { | ||
if (!data.id && !data._id) { | ||
if (!data || (!data.id && !data._id)) { | ||
throw new Error('You cannot merge entity without id!'); | ||
@@ -84,0 +90,0 @@ } |
@@ -9,3 +9,3 @@ import { FilterQuery } from 'mongodb'; | ||
persist(entity: T, flush?: boolean): Promise<void>; | ||
findOne(where: FilterQuery<T>, populate?: string[]): Promise<T>; | ||
findOne(where: FilterQuery<T> | string, populate?: string[]): Promise<T>; | ||
find(where: FilterQuery<T>, populate?: string[], orderBy?: { | ||
@@ -12,0 +12,0 @@ [k: string]: 1 | -1; |
@@ -1,2 +0,2 @@ | ||
import { Collection as MongoCollection, Db, FilterQuery } from 'mongodb'; | ||
import { Collection as MongoCollection, Db, FilterQuery, ObjectID } from 'mongodb'; | ||
import { BaseEntity, EntityMetadata } from './BaseEntity'; | ||
@@ -77,4 +77,7 @@ import { EntityRepository } from './EntityRepository'; | ||
async findOne<T extends BaseEntity>(entityName: string, where: FilterQuery<T>, populate: string[] = []): Promise<T> { | ||
// TODO support also where: ObjectID | ||
async findOne<T extends BaseEntity>(entityName: string, where: FilterQuery<T> | string, populate: string[] = []): Promise<T> { | ||
if (where instanceof ObjectID) { | ||
where = where.toHexString(); | ||
} | ||
if (Utils.isString(where) && this.identityMap[`${entityName}-${where}`]) { | ||
@@ -86,6 +89,11 @@ // TODO populate missing references and rehydrate | ||
if (Utils.isString(where)) { | ||
where = { _id: where }; | ||
where = { _id: new ObjectID(where as string) }; | ||
} | ||
const data = await this.getCollection(entityName).find(where).limit(1).next(); | ||
const data = await this.getCollection(entityName).find(where as FilterQuery<T>).limit(1).next(); | ||
if (!data) { | ||
return null; | ||
} | ||
const entity = this.merge(entityName, data) as T; | ||
@@ -98,3 +106,3 @@ await this.processPopulate(entity, populate); | ||
merge<T extends BaseEntity>(entityName: string, data: any): T { | ||
if (!data.id && !data._id) { | ||
if (!data || (!data.id && !data._id)) { | ||
throw new Error('You cannot merge entity without id!'); | ||
@@ -101,0 +109,0 @@ } |
@@ -14,3 +14,3 @@ import { FilterQuery } from 'mongodb'; | ||
async findOne(where: FilterQuery<T>, populate: string[] = []): Promise<T> { | ||
async findOne(where: FilterQuery<T> | string, populate: string[] = []): Promise<T> { | ||
return this.em.findOne<T>(this.entityName, where, populate); | ||
@@ -17,0 +17,0 @@ } |
{ | ||
"name": "mikro-orm", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Simple typescript mongo ORM for node.js based on data-mapper, unit-of-work and identity-map patterns", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -128,7 +128,5 @@ # mikro-orm | ||
- cascade persist in collections | ||
- immediate flush issue when persisting new entities with references | ||
- aggregate support? | ||
- improve populating in EM#find() method | ||
- rehydrate and populate missing references when fetching already loaded entities from db | ||
- support for string id (now we require object id) in EM/repositories | ||
- add query logging | ||
@@ -135,0 +133,0 @@ - add nativeUpdate and nativeDelete (without hooks support), allow only entities in EM#remove |
@@ -142,2 +142,18 @@ import { ObjectID } from 'bson'; | ||
test('findOne by id', async () => { | ||
const authorRepository = orm.em.getRepository<Author>(Author.name); | ||
const jon = new Author('Jon Snow', 'snow@wall.st'); | ||
await authorRepository.persist(jon); | ||
orm.em.clear(); | ||
let author = await authorRepository.findOne(jon._id); | ||
expect(author).not.toBeNull(); | ||
expect(author.name).toBe('Jon Snow'); | ||
orm.em.clear(); | ||
author = await authorRepository.findOne(jon.id); | ||
expect(author).not.toBeNull(); | ||
expect(author.name).toBe('Jon Snow'); | ||
}); | ||
test('many to many relation', async () => { | ||
@@ -144,0 +160,0 @@ const author = new Author('Jon Snow', 'snow@wall.st'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117359
2946
143