Permalink
Browse files

Lazy-create any needed collections for the in-memory adapter

  • Loading branch information...
mde
mde committed Oct 30, 2012
1 parent c26238f commit 6925ee2ebf2ed744a4b99d84aedff0bfdd61b7ef
Showing with 27 additions and 1 deletion.
  1. +27 −1 lib/adapters/memory/index.js
@@ -38,12 +38,20 @@ utils.mixin(Adapter.prototype, new (function () {
, id = query.byId
, conditions
, sort
- , items = _data[key]
+ , items
, item
, data
, val
, filter
, res = [];
+
+ // Lazy-create the collection
+ if (!_data[key]) {
+ _data[key] = {};
+ }
+
+ items = _data[key];
+
if (id) {
item = _data[key][id];
callback(null, item);
@@ -97,6 +105,12 @@ utils.mixin(Adapter.prototype, new (function () {
var key = query.model.modelName
, id = query.byId
, ids;
+
+ // Lazy-create the collection
+ if (!_data[key]) {
+ _data[key] = {};
+ }
+
if (id) {
ids = [id];
}
@@ -135,6 +149,12 @@ utils.mixin(Adapter.prototype, new (function () {
var key = query.model.modelName
, id = query.byId
, ids;
+
+ // Lazy-create the collection
+ if (!_data[key]) {
+ _data[key] = {};
+ }
+
if (id) {
ids = [id];
}
@@ -164,6 +184,12 @@ utils.mixin(Adapter.prototype, new (function () {
this.insert = function (data, opts, callback) {
var items = Array.isArray(data) ? data.slice() : [data]
, key = items[0].type;
+
+ // Lazy-create the collection
+ if (!_data[key]) {
+ _data[key] = {};
+ }
+
items.forEach(function (item) {
var id = utils.string.uuid();
item.id = id;

0 comments on commit 6925ee2

Please sign in to comment.