Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat(repository): allow populating clean entities
- Loading branch information
Showing
with
10 additions
and
8 deletions.
-
+10
−8
src/repository.js
|
@@ -162,22 +162,23 @@ export class Repository { |
|
|
* Get new populated entity or entities based on supplied data including associations |
|
|
* |
|
|
* @param {{}|[{}]} data|[data] The data to populate with |
|
|
* @param {boolean} [clean] Mark the entities as clean or not |
|
|
* |
|
|
* @return {Entity|[Entity]} |
|
|
*/ |
|
|
populateEntities(data) { |
|
|
populateEntities(data, clean) { |
|
|
if (!data) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
if (!Array.isArray(data)) { |
|
|
return this.getPopulatedEntity(data); |
|
|
return this.getPopulatedEntity(data, null, clean); |
|
|
} |
|
|
|
|
|
let collection = []; |
|
|
|
|
|
data.forEach(source => { |
|
|
collection.push(this.getPopulatedEntity(source)); |
|
|
collection.push(this.getPopulatedEntity(source, null, clean)); |
|
|
}); |
|
|
|
|
|
return collection; |
|
@@ -186,12 +187,13 @@ export class Repository { |
|
|
/** |
|
|
* Populate a (new) entity including associations |
|
|
* |
|
|
* @param {{}} data The data to populate with |
|
|
* @param {Entity} [entity] optional. if not set, a new entity is returned |
|
|
* @param {{}} data The data to populate with |
|
|
* @param {Entity} [entity] optional. if not set, a new entity is returned |
|
|
* @param {boolean} [clean] Mark the entities as clean or not |
|
|
* |
|
|
* @return {Entity} |
|
|
*/ |
|
|
getPopulatedEntity(data, entity) { |
|
|
getPopulatedEntity(data, entity, clean) { |
|
|
entity = entity || this.getNewEntity(); |
|
|
let entityMetadata = entity.getMeta(); |
|
|
let populatedData = {}; |
|
@@ -219,10 +221,10 @@ export class Repository { |
|
|
|
|
|
let repository = this.entityManager.getRepository(entityMetadata.fetch('associations', key).entity); |
|
|
|
|
|
populatedData[key] = repository.populateEntities(value); |
|
|
populatedData[key] = repository.populateEntities(value, true); |
|
|
} |
|
|
|
|
|
return entity.setData(populatedData); |
|
|
return entity.setData(populatedData, clean); |
|
|
} |
|
|
|
|
|
/** |
|
|