Skip to content

Commit

Permalink
Stable Version 1.1.0.
Browse files Browse the repository at this point in the history
Fixes #2
Fixes #3
Closes #4
Closes #5
  • Loading branch information
jmdobry committed Mar 27, 2015
1 parent 3e3366e commit 4770d0f
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 66 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
##### 1.1.0 - 27 March 2015

###### Backwards compatible API changes
- #4 - Support loading relations in find()
- #5 - Support loading relations in findAll()

###### Backwards compatible bug fixes
- #2 - Should not be saving relations (duplicating data)
- #3 - Should be using removeCircular

##### 1.0.3 - 10 March 2015

Rebuild.
Expand Down
13 changes: 12 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Contributing Guide

First, feel free to contact me with questions. [Mailing List](https://groups.io/org/groupsio/jsdata). [Issues](https://github.com/js-data/js-data-redis/issues).
First, support is handled via the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there.

When submitting issues on GitHub, please include as much detail as possible to make debugging quick and easy.

- good - Your versions of js-data, js-data-redis, etc., relevant console logs/error, code examples that revealed the issue
- better - A [plnkr](http://plnkr.co/), [fiddle](http://jsfiddle.net/), or [bin](http://jsbin.com/?html,output) that demonstrates the issue
- best - A Pull Request that fixes the issue, including test coverage for the issue and the fix

[Github Issues](https://github.com/js-data/js-data-redis/issues).

#### Pull Requests

1. Contribute to the issue that is the reason you'll be developing in the first place
1. Fork js-data-redis
Expand All @@ -10,3 +20,4 @@ First, feel free to contact me with questions. [Mailing List](https://groups.io/
1. (in another terminal) `grunt karma:dev` (runs the tests)
1. Write your code, including relevant documentation and tests
1. Submit a PR and we'll review

5 changes: 2 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/*
* js-data-redis
* http://github.com/js-data/js-data-redis
* https://github.com/js-data/js-data-redis
*
* Copyright (c) 2014-2015 Jason Dobry <http://www.js-data.io/docs/dsredisadapter>
* Licensed under the MIT license. <https://github.com/js-data/js-data-redis/blob/master/LICENSE>
*/
module.exports = function (grunt) {
'use strict';

require('jit-grunt')(grunt, {
coveralls: 'grunt-karma-coveralls'
});
Expand Down Expand Up @@ -49,6 +47,7 @@ module.exports = function (grunt) {
externals: [
'mout/string/underscore',
'mout/random/guid',
'mout/object/omit',
'js-data',
'js-data-schema',
'redis'
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ store.registerAdapter('redis', adapter, { default: true });

### Contributing

First, feel free to contact me with questions. [Mailing List](https://groups.io/org/groupsio/jsdata). [Issues](https://github.com/js-data/js-data-redis/issues).
First, support is handled via the [Mailing List](https://groups.io/org/groupsio/jsdata). Ask your questions there.

When submitting issues on GitHub, please include as much detail as possible to make debugging quick and easy.

- good - Your versions of js-data, js-data-redis, etc., relevant console logs/error, code examples that revealed the issue
- better - A [plnkr](http://plnkr.co/), [fiddle](http://jsfiddle.net/), or [bin](http://jsbin.com/?html,output) that demonstrates the issue
- best - A Pull Request that fixes the issue, including test coverage for the issue and the fix

[Github Issues](https://github.com/js-data/js-data-redis/issues).

#### Pull Requests

1. Contribute to the issue that is the reason you'll be developing in the first place
1. Fork js-data-redis
Expand Down
167 changes: 153 additions & 14 deletions dist/js-data-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,37 @@ module.exports =
/* 0 */
/***/ function(module, exports, __webpack_require__) {

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };

var JSData = __webpack_require__(1);
var redis = __webpack_require__(2);
var underscore = __webpack_require__(3);
var guid = __webpack_require__(4);
var P = JSData.DSUtils.Promise;
var deepMixIn = JSData.DSUtils.deepMixIn;
var forEach = JSData.DSUtils.forEach;
var JSData = _interopRequire(__webpack_require__(1));

var redis = _interopRequire(__webpack_require__(2));

var underscore = _interopRequire(__webpack_require__(3));

var guid = _interopRequire(__webpack_require__(4));

var omit = _interopRequire(__webpack_require__(5));

var DSUtils = JSData.DSUtils;
var P = DSUtils.Promise;
var deepMixIn = DSUtils.deepMixIn;
var removeCircular = DSUtils.removeCircular;
var forEach = DSUtils.forEach;
var contains = DSUtils.contains;

var emptyStore = new JSData.DS();
var filter = emptyStore.defaults.defaultFilter;

function getPath(resourceConfig) {
var getPath = function (resourceConfig) {
if (resourceConfig) {
return resourceConfig.table || underscore(resourceConfig.name);
}
}
};

var Defaults = function Defaults() {
_classCallCheck(this, Defaults);
Expand All @@ -83,6 +95,7 @@ module.exports =
getIds: {
value: function getIds(resourceConfig) {
var _this = this;

return new P(function (resolve, reject) {
return _this.client.SMEMBERS(getPath(resourceConfig), function (err, ids) {
return err ? reject(err) : resolve(ids);
Expand All @@ -93,6 +106,7 @@ module.exports =
GET: {
value: function GET(path) {
var _this = this;

return new P(function (resolve, reject) {
return _this.client.GET(path, function (err, value) {
return err ? reject(err) : resolve(JSON.parse(value));
Expand All @@ -101,9 +115,12 @@ module.exports =
}
},
find: {
value: function find(resourceConfig, id) {
value: function find(resourceConfig, id, options) {
var _this = this;

var fields = [];
return new P(function (resolve, reject) {
options = options || {};
return _this.client.GET("" + getPath(resourceConfig) + "-" + id, function (err, item) {
if (err) {
reject(err);
Expand All @@ -113,13 +130,65 @@ module.exports =
resolve(JSON.parse(item));
}
});
}).then(function (instance) {
if (!options["with"]) {
return instance;
}
var tasks = [];
forEach(resourceConfig.relationList, function (def) {
var relationName = def.relation;
var relationDef = resourceConfig.getResource(relationName);
var __options = DSUtils._(relationDef, options);
if (contains(options["with"], relationName) || contains(options["with"], def.localField)) {
var task = undefined;
var params = {};
if (__options.allowSimpleWhere) {
params[def.foreignKey] = instance[resourceConfig.idAttribute];
} else {
params.where = {};
params.where[def.foreignKey] = {
"==": instance[resourceConfig.idAttribute]
};
}

if (def.type === "hasMany" && params[def.foreignKey]) {
task = _this.findAll(relationDef, params, omit(__options.orig(), ["with"]));
} else if (def.type === "hasOne") {
if (def.localKey && instance[def.localKey]) {
task = _this.find(relationDef, instance[def.localKey], omit(__options.orig(), ["with"]));
} else if (def.foreignKey && params[def.foreignKey]) {
task = _this.findAll(relationDef, params, omit(__options.orig(), ["with"])).then(function (hasOnes) {
return hasOnes.length ? hasOnes[0] : null;
});
}
} else if (instance[def.localKey]) {
task = _this.find(relationDef, instance[def.localKey], omit(__options.orig(), ["with"]));
}

if (task) {
tasks.push(task);
fields.push(def.localField);
}
}
});
if (tasks.length) {
return P.all(tasks).then(function (loadedRelations) {
forEach(fields, function (field, index) {
instance[field] = loadedRelations[index];
});
return instance;
});
}
return instance;
});
}
},
findAll: {
value: function findAll(resourceConfig, params) {
value: function findAll(resourceConfig, params, options) {
var _this = this;
return _this.getIds(resourceConfig).then(function (ids) {

return this.getIds(resourceConfig).then(function (ids) {
options = options || {};
var tasks = [];
var path = getPath(resourceConfig);
forEach(ids, function (id) {
Expand All @@ -128,13 +197,72 @@ module.exports =
return P.all(tasks);
}).then(function (items) {
return filter.call(emptyStore, items, resourceConfig.name, params, { allowSimpleWhere: true });
}).then(function (items) {
if (!options["with"]) {
return items;
}
var topTasks = [];
forEach(items, function (instance) {
var tasks = [];
var fields = [];
forEach(resourceConfig.relationList, function (def) {
var relationName = def.relation;
var relationDef = resourceConfig.getResource(relationName);
var __options = DSUtils._(relationDef, options);
if (contains(options["with"], relationName) || contains(options["with"], def.localField)) {
var task = undefined;
var _params = {};
if (__options.allowSimpleWhere) {
_params[def.foreignKey] = instance[resourceConfig.idAttribute];
} else {
_params.where = {};
_params.where[def.foreignKey] = {
"==": instance[resourceConfig.idAttribute]
};
}

if (def.type === "hasMany" && _params[def.foreignKey]) {
task = _this.findAll(relationDef, _params, omit(__options.orig(), ["with"]));
} else if (def.type === "hasOne") {
if (def.localKey && instance[def.localKey]) {
task = _this.find(relationDef, instance[def.localKey], omit(__options.orig(), ["with"]));
} else if (def.foreignKey && _params[def.foreignKey]) {
task = _this.findAll(relationDef, _params, omit(__options.orig(), ["with"])).then(function (hasOnes) {
return hasOnes.length ? hasOnes[0] : null;
});
}
} else if (instance[def.localKey]) {
task = _this.find(relationDef, instance[def.localKey], omit(__options.orig(), ["with"]));
}

if (task) {
tasks.push(task);
fields.push(def.localField);
}
}
});
if (tasks.length) {
topTasks.push(P.all(tasks).then(function (loadedRelations) {
forEach(fields, function (field, index) {
instance[field] = loadedRelations[index];
});
return instance;
}));
}
});
if (topTasks.length) {
return P.all(topTasks);
}
return items;
});
}
},
create: {
value: function create(resourceConfig, attrs) {
var _this = this;

return new P(function (resolve, reject) {
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || []));
attrs[resourceConfig.idAttribute] = attrs[resourceConfig.idAttribute] || guid();
return _this.client.multi().SET("" + getPath(resourceConfig) + "-" + attrs[resourceConfig.idAttribute], JSON.stringify(attrs)).SADD(getPath(resourceConfig), attrs[resourceConfig.idAttribute]).exec(function (err) {
return err ? reject(err) : resolve(attrs);
Expand All @@ -145,7 +273,9 @@ module.exports =
update: {
value: function update(resourceConfig, id, attrs) {
var _this = this;

return new P(function (resolve, reject) {
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || []));
var path = "" + getPath(resourceConfig) + "-" + id;
return _this.client.GET(path, function (err, value) {
if (err) {
Expand All @@ -166,7 +296,8 @@ module.exports =
updateAll: {
value: function updateAll(resourceConfig, attrs, params) {
var _this = this;
return _this.findAll(resourceConfig, params).then(function (items) {

return this.findAll(resourceConfig, params).then(function (items) {
var tasks = [];
forEach(items, function (item) {
return tasks.push(_this.update(resourceConfig, item[resourceConfig.idAttribute], attrs));
Expand All @@ -178,6 +309,7 @@ module.exports =
destroy: {
value: function destroy(resourceConfig, id) {
var _this = this;

return new P(function (resolve, reject) {
var path = getPath(resourceConfig);
return _this.client.multi().DEL("" + path + "-" + id).SREM(path, id).exec(function (err) {
Expand All @@ -189,7 +321,8 @@ module.exports =
destroyAll: {
value: function destroyAll(resourceConfig, params) {
var _this = this;
return _this.findAll(resourceConfig, params).then(function (items) {

return this.findAll(resourceConfig, params).then(function (items) {
var tasks = [];
forEach(items, function (item) {
return tasks.push(_this.destroy(resourceConfig, item[resourceConfig.idAttribute]));
Expand Down Expand Up @@ -229,5 +362,11 @@ module.exports =

module.exports = require("mout/random/guid");

/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {

module.exports = require("mout/object/omit");

/***/ }
/******/ ]);
Loading

0 comments on commit 4770d0f

Please sign in to comment.