Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

List API improvements

  • Loading branch information...
commit c8b8012e281a1b0dc44245ba4ac501dc8c418c6d 1 parent e0f5f45
@1602 authored
View
4 lib/abstract-class.js
@@ -66,7 +66,7 @@ AbstractClass.prototype._initProperties = function (data, applySetters) {
var type = properties[attr].type;
if (BASE_TYPES.indexOf(type.name) === -1) {
- if (typeof this[_attr] !== 'object') {
+ if (typeof this[_attr] !== 'object' && this[_attr]) {
try {
this[_attr] = JSON.parse(this[_attr] + '');
} catch (e) {
@@ -149,7 +149,7 @@ AbstractClass.whatTypeName = function (propName) {
AbstractClass._forDB = function (data) {
var res = {};
Object.keys(data).forEach(function (propName) {
- if (this.whatTypeName(propName) === 'JSON') {
+ if (this.whatTypeName(propName) === 'JSON' || data[propName] instanceof Array) {
res[propName] = JSON.stringify(data[propName]);
} else {
res[propName] = data[propName];
View
90 lib/list.js
@@ -25,8 +25,12 @@ function List(data, type, parent) {
}
data.forEach(function (item, i) {
- data[i] = new Item(item, parent);
- list[data[i].id] = data[i];
+ data[i] = new Item(item, list);
+ Object.defineProperty(list, data[i].id, {
+ writable: true,
+ enumerable: false,
+ value: data[i]
+ });
if (list.nextid <= data[i].id) {
list.nextid = data[i].id + 1;
}
@@ -44,20 +48,81 @@ function List(data, type, parent) {
}
+var _;
+try {
+ _ = require('underscore');
+} catch (e) {
+ _ = false;
+}
+
+if (_) {
+ var _import = [
+ // collection methods
+ 'each',
+ 'map',
+ 'reduce',
+ 'reduceRight',
+ 'find',
+ 'filter',
+ 'reject',
+ 'all',
+ 'any',
+ 'include',
+ 'invoke',
+ 'pluck',
+ 'max',
+ 'min',
+ 'sortBy',
+ 'groupBy',
+ 'sortedIndex',
+ 'shuffle',
+ 'toArray',
+ 'size',
+ // array methods
+ 'first',
+ 'initial',
+ 'last',
+ 'rest',
+ 'compact',
+ 'flatten',
+ 'without',
+ 'union',
+ 'intersection',
+ 'difference',
+ 'uniq',
+ 'zip',
+ 'indexOf',
+ 'lastIndexOf',
+ 'range'
+ ];
+
+ _import.forEach(function (name) {
+ List.prototype[name] = function () {
+ var args = [].slice.call(arguments);
+ args.unshift(this.items);
+ return _[name].apply(_, args);
+ };
+ });
+}
+
List.prototype.toObject = function () {
return this.items;
};
+List.prototype.toJSON = function () {
+ return this.items;
+};
+
+List.prototype.toString = function () {
+ return JSON.stringify(this.items);
+};
+
List.prototype.autoincrement = function () {
return this.nextid++;
};
List.prototype.push = function (obj) {
var item = new ListItem(obj, this);
- if (this.ItemType) {
- item.__proto__ = this.ItemType.prototype;
- }
- item.id = this.autoincrement();
this.items.push(item);
return item;
};
@@ -99,5 +164,18 @@ function ListItem(data, parent) {
configurable: true,
value: parent
});
+ if (!this.id) {
+ this.id = parent.autoincrement();
+ }
+ if (parent.ItemType) {
+ this.__proto__ = parent.ItemType.prototype;
+ if (parent.ItemType !== ListItem) {
+ parent.ItemType.apply(this);
+ }
+ }
+
+ this.save = function (c) {
+ parent.parent.save(c);
+ };
}
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "jugglingdb",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
- "version": "0.1.17",
+ "version": "0.1.18",
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
"contributors": [
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },
View
2  test/common_test.js
@@ -17,7 +17,7 @@ var schemas = {
database: ':memory:'
},
neo4j: { url: 'http://localhost:7474/' },
- mongoose: { url: 'mongodb://travis:test@localhost:27017/myapp' },
+ // mongoose: { url: 'mongodb://travis:test@localhost:27017/myapp' },
mongodb: { url: 'mongodb://travis:test@localhost:27017/myapp' },
redis2: {},
memory: {}
Please sign in to comment.
Something went wrong with that request. Please try again.