mikro-orm
Advanced tools
Comparing version 0.6.4 to 0.6.5
@@ -39,7 +39,5 @@ import { Collection as MongoCollection, Db, FilterQuery } from 'mongodb'; | ||
canPopulate(entityName: string, property: string): boolean; | ||
/** | ||
* @todo improve this for find() operations | ||
*/ | ||
private processPopulate; | ||
private populateMany; | ||
private runHooks; | ||
} |
@@ -59,5 +59,5 @@ "use strict"; | ||
const entity = this.merge(entityName, data); | ||
await this.processPopulate(entity, populate); | ||
ret.push(entity); | ||
} | ||
await this.processPopulate(ret, populate); | ||
return ret; | ||
@@ -163,14 +163,37 @@ } | ||
} | ||
/** | ||
* @todo improve this for find() operations | ||
*/ | ||
async processPopulate(entity, populate) { | ||
if (entity instanceof BaseEntity_1.BaseEntity) { | ||
for (const field of populate) { | ||
if (entity[field] instanceof Collection_1.Collection && !entity[field].isInitialized()) { | ||
await entity[field].init(); | ||
} | ||
if (entity[field] instanceof BaseEntity_1.BaseEntity && !entity[field].isInitialized()) { | ||
await entity[field].init(); | ||
} | ||
} | ||
return; | ||
} | ||
if (entity.length === 0) { | ||
return; | ||
} | ||
for (const field of populate) { | ||
if (entity[field] instanceof Collection_1.Collection && !entity[field].isInitialized()) { | ||
await entity[field].init(); | ||
await this.populateMany(entity, field); | ||
} | ||
} | ||
async populateMany(entities, field) { | ||
if (entities[0][field] instanceof Collection_1.Collection) { | ||
for (const entity of entities) { | ||
if (entity[field] instanceof Collection_1.Collection && !entity[field].isInitialized()) { | ||
await entity[field].init(); | ||
} | ||
} | ||
if (entity[field] instanceof BaseEntity_1.BaseEntity && !entity[field].isInitialized()) { | ||
await entity[field].init(); | ||
} | ||
return; | ||
} | ||
const children = entities.filter(e => e[field] instanceof BaseEntity_1.BaseEntity && !e[field].isInitialized()); | ||
if (children.length === 0) { | ||
return; | ||
} | ||
// preload everything in one call (this will update already existing references in IM) | ||
const ids = Utils_1.Utils.unique(children.map(e => e[field].id)); | ||
await this.find(entities[0][field].constructor.name, { _id: { $in: ids } }); | ||
} | ||
@@ -177,0 +200,0 @@ runHooks(type, entity) { |
@@ -8,2 +8,3 @@ import { BaseEntity } from './BaseEntity'; | ||
static equals(a: any, b: any): boolean; | ||
static unique<T = string>(items: T[]): T[]; | ||
static diff(a: any, b: any): any; | ||
@@ -10,0 +11,0 @@ /** |
@@ -22,2 +22,5 @@ "use strict"; | ||
} | ||
static unique(items) { | ||
return [...new Set(items)]; | ||
} | ||
static diff(a, b) { | ||
@@ -24,0 +27,0 @@ const ret = {}; |
@@ -68,6 +68,7 @@ import { Collection as MongoCollection, Db, FilterQuery, ObjectID } from 'mongodb'; | ||
const entity = this.merge<T>(entityName, data); | ||
await this.processPopulate(entity, populate); | ||
ret.push(entity); | ||
} | ||
await this.processPopulate(ret, populate); | ||
return ret; | ||
@@ -201,15 +202,46 @@ } | ||
/** | ||
* @todo improve this for find() operations | ||
*/ | ||
private async processPopulate(entity: BaseEntity, populate: string[]): Promise<void> { | ||
private async processPopulate(entity: BaseEntity | BaseEntity[], populate: string[]): Promise<void> { | ||
if (entity instanceof BaseEntity) { | ||
for (const field of populate) { | ||
if (entity[field] instanceof Collection && !entity[field].isInitialized()) { | ||
await (entity[field] as Collection<BaseEntity>).init(); | ||
} | ||
if (entity[field] instanceof BaseEntity && !entity[field].isInitialized()) { | ||
await (entity[field] as BaseEntity).init(); | ||
} | ||
} | ||
return; | ||
} | ||
if (entity.length === 0) { | ||
return; | ||
} | ||
for (const field of populate) { | ||
if (entity[field] instanceof Collection && !entity[field].isInitialized()) { | ||
await (entity[field] as Collection<BaseEntity>).init(); | ||
await this.populateMany(entity, field); | ||
} | ||
} | ||
private async populateMany(entities: BaseEntity[], field: string): Promise<void> { | ||
if (entities[0][field] instanceof Collection) { | ||
for (const entity of entities) { | ||
if (entity[field] instanceof Collection && !entity[field].isInitialized()) { | ||
await (entity[field] as Collection<BaseEntity>).init(); | ||
} | ||
} | ||
if (entity[field] instanceof BaseEntity && !entity[field].isInitialized()) { | ||
await (entity[field] as BaseEntity).init(); | ||
} | ||
return; | ||
} | ||
const children = entities.filter(e => e[field] instanceof BaseEntity && !e[field].isInitialized()); | ||
if (children.length === 0) { | ||
return; | ||
} | ||
// preload everything in one call (this will update already existing references in IM) | ||
const ids = Utils.unique(children.map(e => e[field].id)); | ||
await this.find<BaseEntity>(entities[0][field].constructor.name, { _id: { $in: ids } }); | ||
} | ||
@@ -216,0 +248,0 @@ |
@@ -28,2 +28,6 @@ import * as fastEqual from 'fast-deep-equal'; | ||
static unique<T = string>(items: T[]): T[] { | ||
return [...new Set(items)]; | ||
} | ||
static diff(a: any, b: any): any { | ||
@@ -30,0 +34,0 @@ const ret = {} as any; |
{ | ||
"name": "mikro-orm", | ||
"version": "0.6.4", | ||
"version": "0.6.5", | ||
"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", |
@@ -138,4 +138,5 @@ # mikro-orm | ||
```typescript | ||
const booksRepository = orm.em.getRepository<Book>(Book.name); | ||
// with sorting, limit and offset parameters, populating author references | ||
const booksRepository = orm.em.getRepository<Book>(Book.name); | ||
const books = await booksRepository.find({ author: '...' }, ['author'], { title: -1 }, 2, 1); | ||
@@ -191,33 +192,31 @@ console.log(books); // Book[] | ||
for (const author of authors) { | ||
console.log(author.name); // Jon Snow | ||
console.log(author.name); // Jon Snow | ||
await author.books.init(); // init all books | ||
for (const book of author.books) { | ||
console.log(book.title); // initialized | ||
console.log(book.author.isInitialized()); // true | ||
console.log(book.author.id); | ||
console.log(book.author.name); // Jon Snow | ||
console.log(book.publisher); // just reference | ||
console.log(book.publisher.isInitialized()); // false | ||
console.log(book.publisher.id); | ||
console.log(book.publisher.name); // undefined | ||
} | ||
// collection needs to be initialized before you can work with it | ||
author.books.add(book); | ||
console.log(author.books.contains(book)); // true | ||
author.books.remove(book); | ||
console.log(author.books.contains(book)); // false | ||
author.books.add(book); | ||
console.log(author.books.count()); // 1 | ||
author.books.removeAll(); | ||
console.log(author.books.contains(book)); // false | ||
console.log(author.books.count()); // 0 | ||
console.log(author.books.getItems()); // 0 | ||
console.log(author.books.getIdentifiers()); // array of ObjectID | ||
console.log(author.books.getIdentifiers('id')); // array of string | ||
await author.books.init(); // init all books | ||
for (const book of author.books) { | ||
console.log(book.title); // initialized | ||
console.log(book.author.isInitialized()); // true | ||
console.log(book.author.id); | ||
console.log(book.author.name); // Jon Snow | ||
console.log(book.publisher); // just reference | ||
console.log(book.publisher.isInitialized()); // false | ||
console.log(book.publisher.id); | ||
console.log(book.publisher.name); // undefined | ||
} | ||
// collection needs to be initialized before you can work with it | ||
author.books.add(book); | ||
console.log(author.books.contains(book)); // true | ||
author.books.remove(book); | ||
console.log(author.books.contains(book)); // false | ||
author.books.add(book); | ||
console.log(author.books.count()); // 1 | ||
author.books.removeAll(); | ||
console.log(author.books.contains(book)); // false | ||
console.log(author.books.count()); // 0 | ||
console.log(author.books.getItems()); // 0 | ||
console.log(author.books.getIdentifiers()); // array of ObjectID | ||
console.log(author.books.getIdentifiers('id')); // array of string | ||
``` | ||
@@ -229,5 +228,5 @@ | ||
- aggregate support? | ||
- improve populating in EM#find() method | ||
- improve populating of collections in EM#find() method | ||
- add nativeUpdate and nativeDelete (without hooks support), allow only entities in EM#remove | ||
- remove references on other entities when deleting entity (e.g. from M:N collection) | ||
- cascade remove references on other entities when deleting entity (e.g. from M:N collection) | ||
@@ -234,0 +233,0 @@ ## TODO docs |
@@ -59,2 +59,11 @@ import { ObjectID } from 'bson'; | ||
const authorRepository = orm.em.getRepository<Author>(Author.name); | ||
const booksRepository = orm.em.getRepository<Book>(Book.name); | ||
const books = await booksRepository.findAll(['author']); | ||
expect(books[0].author.isInitialized()).toBe(true); | ||
orm.em.clear(); | ||
const noBooks = await booksRepository.find({ title: 'not existing' }, ['author']); | ||
expect(noBooks.length).toBe(0); | ||
orm.em.clear(); | ||
const jon = await authorRepository.findOne({ name: 'Jon Snow' }, ['books', 'favouriteBook']); | ||
@@ -109,3 +118,2 @@ const authors = await authorRepository.findAll(['books', 'favouriteBook']); | ||
const booksRepository = orm.em.getRepository<Book>(Book.name); | ||
const booksByTitleAsc = await booksRepository.find({ author: jon._id }, [], { title: 1 }); | ||
@@ -112,0 +120,0 @@ expect(booksByTitleAsc[0].title).toBe('My Life on The Wall, part 1'); |
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
139820
3347
236