camouflage
Advanced tools
Comparing version 0.8.6 to 0.8.7
@@ -23,3 +23,3 @@ "use strict"; | ||
} else { | ||
throw new Error('Unsupported type or bad variable. ' + | ||
throw new Error('Unsupported type or bad variable. ' + | ||
'Remember, non-persisted objects must start with an underscore (_). Got:', property); | ||
@@ -76,3 +76,3 @@ } | ||
class BaseDocument { | ||
constructor() { | ||
constructor() { | ||
this._schema = { // Defines document structure/properties | ||
@@ -96,7 +96,7 @@ _id: { type: DB().nativeIdType() }, // Native ID to backend database | ||
get id() { | ||
return this._values._id; | ||
return this._values._id; | ||
} | ||
set id(id) { | ||
this._values._id = id; | ||
this._values._id = id; | ||
} | ||
@@ -166,3 +166,3 @@ | ||
// TODO: This should probably be in Document, not BaseDocument | ||
if (value !== null && value !== undefined && | ||
if (value !== null && value !== undefined && | ||
value.documentClass && value.documentClass() === 'embedded') { | ||
@@ -230,6 +230,6 @@ value.validate(); | ||
var value = that._values[key]; | ||
if (that._schema[key].type === Date && isNumber(value)) { | ||
that._values[key] = new Date(value); | ||
} else if (value !== null && value !== undefined && | ||
} else if (value !== null && value !== undefined && | ||
value.documentClass && value.documentClass() === 'embedded') { | ||
@@ -314,6 +314,2 @@ // TODO: This should probably be in Document, not BaseDocument | ||
// Load all 1-level-deep references | ||
// First, find all unique keys needed to be loaded... | ||
var keys = []; | ||
// TODO: Bad assumption: Not all documents in the database will have the same schema... | ||
@@ -323,103 +319,56 @@ // Hmm, if this is true, thats an error on the user. Right? | ||
_.keys(anInstance._schema).forEach(function(key) { | ||
var keyDefs = _.keys(anInstance._schema).filter(function(key) { | ||
// Handle array of references (ex: { type: [MyObject] }) | ||
if (isArray(anInstance._schema[key].type) && | ||
return (isArray(anInstance._schema[key].type) && | ||
anInstance._schema[key].type.length > 0 && | ||
isDocument(anInstance._schema[key].type[0])) { | ||
keys.push(key); | ||
isDocument(anInstance._schema[key].type[0])) | ||
|| ((isString(anInstance[key]) || DB().isNativeId(anInstance[key])) && | ||
isDocument(anInstance._schema[key].type)); | ||
}).map(function(key){ | ||
var type = anInstance._schema[key].type; | ||
var isKeyForArray = isArray(type); | ||
if(isKeyForArray){ | ||
type = type[0] | ||
} | ||
// Handle anInstance[key] being a string id, a native id, or a Document instance | ||
else if ((isString(anInstance[key]) || DB().isNativeId(anInstance[key])) && | ||
isDocument(anInstance._schema[key].type)) { | ||
keys.push(key); | ||
} | ||
return { | ||
key: key, | ||
type: type, | ||
isArray: isKeyForArray | ||
}; | ||
}); | ||
// ...then get all ids for each type of reference to be loaded... | ||
// ids = { | ||
// houses: { | ||
// 'abc123': ['ak23lj', '2kajlc', 'ckajl32'], | ||
// 'l2jo99': ['28dsa0'] | ||
// }, | ||
// friends: { | ||
// '1039da': ['lj0adf', 'k2jha'] | ||
// } | ||
//} | ||
var ids = {}; | ||
keys.forEach(function(k) { | ||
ids[k] = {}; | ||
documents.forEach(function(d) { | ||
ids[k][DB().toCanonicalId(d.id)] = [].concat(d[k]); // Handles values and arrays | ||
var cache = new Map(); | ||
function loadFromCache(type, id){ | ||
var typeCache = cache.get(type); | ||
if(!typeCache){ | ||
typeCache = new Map(); | ||
cache.set(type, typeCache); | ||
} | ||
var loadPromise = typeCache.get(id); | ||
if(!loadPromise){ | ||
loadPromise = type.loadById(id, { populate: false }); | ||
typeCache.set(id, loadPromise); | ||
} | ||
return loadPromise; | ||
} | ||
// Also, initialize document member arrays | ||
// to assign to later if needed | ||
if (isArray(d[k])) { | ||
d[k] = []; | ||
} | ||
}); | ||
}); | ||
// TODO: Is this really the most efficient | ||
// way to do this? Maybe make a master list | ||
// of all objects that need to be loaded (separated | ||
// by type), load those, and then search through | ||
// ids to see where dereferenced objects should | ||
// go? | ||
// ...then for each array of ids, load them all... | ||
var loadPromises = []; | ||
_.keys(ids).forEach(function(key) { | ||
var keyIds = []; | ||
_.keys(ids[key]).forEach(function(k) { | ||
// Before adding to list, we convert id to the | ||
// backend database's native ID format. | ||
keyIds = keyIds.concat(ids[key][k]); | ||
}); | ||
// Only want to load each reference once | ||
keyIds = _.unique(keyIds); | ||
// Handle array of references (like [MyObject]) | ||
var type = null; | ||
if (isArray(anInstance._schema[key].type)) { | ||
type = anInstance._schema[key].type[0]; | ||
} else { | ||
type = anInstance._schema[key].type; | ||
} | ||
// Bulk load dereferences | ||
var p = type.loadMany({ '_id': { $in: keyIds } }, { populate: false }) | ||
.then(function(dereferences) { | ||
// Assign each dereferenced object to parent | ||
_.keys(ids[key]).forEach(function(k) { | ||
// TODO: Replace with documents.find when able | ||
// Find the document to assign the derefs to | ||
var doc; | ||
documents.forEach(function(d) { | ||
if (DB().toCanonicalId(d.id) === k) doc = d; | ||
keyDefs.forEach(function(keyDef) { | ||
documents.forEach(function(d) { | ||
if(keyDef.isArray && d[keyDef.key]){ | ||
var idList = d[keyDef.key]; | ||
idList.forEach(function(id){ | ||
loadPromises.push(loadFromCache(keyDef.type, id).then(function(data){ | ||
d[keyDef.key].push(data); | ||
})); | ||
}); | ||
// For all ids to be dereferenced, find the | ||
// deref and assign or push it | ||
ids[key][k].forEach(function(id) { | ||
// TODO: Replace with dereferences.find when able | ||
// Find the right dereference | ||
var deref = dereferences.find(function(d) { | ||
return DB().toCanonicalId(d.id) === DB().toCanonicalId(id); | ||
}); | ||
if (isArray(anInstance._schema[key].type)) { | ||
if(!doc[key]){ | ||
doc[key] = []; | ||
} | ||
doc[key].push(deref); | ||
} else { | ||
doc[key] = deref; | ||
} | ||
}); | ||
}); | ||
d[keyDef.key] = []; | ||
d[keyDef.key].idList = idList; | ||
} else { | ||
var id = d[keyDef.key]; | ||
loadPromises.push(loadFromCache(keyDef.type, id).then(function(data){ | ||
d[keyDef.key] = data; | ||
})); | ||
} | ||
}); | ||
loadPromises.push(p); | ||
}); | ||
@@ -426,0 +375,0 @@ |
{ | ||
"name": "camouflage", | ||
"version": "0.8.6", | ||
"version": "0.8.7", | ||
"description": "A class-based ES6 ODM for Mongo-like databases.", | ||
@@ -5,0 +5,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
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
253698
5112