Comparing version 0.11.3 to 0.11.4
105
lib/Set.js
@@ -13,4 +13,6 @@ !function(){ | ||
, init: function(options){ | ||
/** | ||
* class constructor | ||
*/ | ||
, init: function init(options){ | ||
Object.defineProperty(this, '_primaryKeys', {value: options.primaryKeys}); | ||
@@ -24,6 +26,8 @@ Object.defineProperty(this, '_name', {value: options.name}); | ||
Object.defineProperty(this, '_resource', {value: options.resource}); | ||
Array.prototype.constructor.call(this); | ||
} | ||
/* | ||
/** | ||
* returns the resource which was used to load this set from the db | ||
@@ -36,3 +40,9 @@ */ | ||
, push: function(item) { | ||
/** | ||
* add an row to the set, checks if an row is already in the | ||
* set, doesn't add it twice | ||
* | ||
* @param <Object> row | ||
*/ | ||
, push: function(row) { | ||
// we need unique items by primary key | ||
@@ -42,3 +52,3 @@ var key = ''; | ||
this._primaryKeys.forEach(function(keyName){ | ||
key += item[keyName]; | ||
key += row[keyName]; | ||
}.bind(this)); | ||
@@ -48,3 +58,3 @@ | ||
// this value was added before | ||
item._mappingIds.forEach(function(id){ | ||
row._mappingIds.forEach(function(id){ | ||
this._maps._primary[key]._mappingIds.push(id); | ||
@@ -55,4 +65,4 @@ }.bind(this)); | ||
else { | ||
this._maps._primary[key] = item; | ||
return Array.prototype.push.call(this, item); | ||
this._maps._primary[key] = row; | ||
return Array.prototype.push.call(this, row); | ||
} | ||
@@ -62,2 +72,5 @@ } | ||
/** | ||
* returns the first row of the set | ||
*/ | ||
, first: function(){ | ||
@@ -67,3 +80,9 @@ return this[0]; | ||
, last: function(){ | ||
/** | ||
* returns the last row of the set | ||
* | ||
* @retuns <Object> if there is at least on row or undefined | ||
*/ | ||
, last: function() { | ||
return this.length ? this[this.length-1] : undefined; | ||
@@ -73,3 +92,10 @@ } | ||
, getColumnValues: function(column){ | ||
/** | ||
* returns an array containing all the values of one column | ||
* | ||
* @param <String> optional name of the column to return, defaults to «id» | ||
* | ||
* @retuns <Array> | ||
*/ | ||
, getColumnValues: function(column) { | ||
column = column || 'id'; | ||
@@ -83,8 +109,21 @@ | ||
, getIds: function(){ | ||
return this.map(function(item){ return item.id;}); | ||
/** | ||
* returns an array containing the all ids of the set | ||
* | ||
* @retuns <Array> | ||
*/ | ||
, getIds: function() { | ||
return this.getColumnValues(); | ||
} | ||
, getByColumnValue: function(column, value){ | ||
/** | ||
* returns all rows that have a specoific value in a specifi column | ||
* | ||
* @param <String> the column to filter | ||
* @param <Mixed> the value to filter for | ||
*/ | ||
, getByColumnValue: function(column, value) { | ||
if (!this._maps[column]) this.createMap(column); | ||
@@ -95,9 +134,17 @@ return this._maps[column] ? this._maps[column][value] : undefined; | ||
, createMap: function(column){ | ||
if (!this._maps[column]){ | ||
/** | ||
* creates a map of the values for a column, so that rows can be | ||
* accessed by the value of a specific column. if the column is | ||
* not unique mulltiple rows may be returned | ||
* | ||
* @param <String> column name to create the map for | ||
*/ | ||
, createMap: function(column) { | ||
if (!this._maps[column]) { | ||
this._maps[column] = {}; | ||
this.forEach(function(item){ | ||
if (!this._maps[column][item[column]]) this._maps[column][item[column]] = []; | ||
this._maps[column][item[column]].push(item); | ||
this.forEach(function(row) { | ||
if (!this._maps[column][row[column]]) this._maps[column][row[column]] = []; | ||
this._maps[column][row[column]].push(row); | ||
}.bind(this)); | ||
@@ -108,2 +155,8 @@ } | ||
/** | ||
* log or return the rows as an array | ||
* | ||
* @param <Boolean> if true the values are not logged but returned | ||
*/ | ||
, dir: function(returnResult) { | ||
@@ -120,2 +173,18 @@ var result = []; | ||
/** | ||
* return the values as plain array, the contained rows are still orm | ||
* model objects, in opposite top the toJSON method | ||
* | ||
* @returns <Array> array | ||
*/ | ||
, toArray: function() { | ||
return this.slice(); | ||
} | ||
/** | ||
* returns a plain js array containing plain js objects representing | ||
* the sets contents | ||
*/ | ||
, toJSON: function() { | ||
@@ -122,0 +191,0 @@ return this.map(function(item){ |
{ | ||
"name" : "ee-orm" | ||
, "description" : "ORM for postgres and mysql. Loads and saves referenced entites, executes complex queries, supports joins, transactions, complex database clusters, connection pooling and much more. No conventions. MIT Licence" | ||
, "version" : "0.11.3" | ||
, "version" : "0.11.4" | ||
, "homepage" : "https://github.com/eventEmitter/ee-orm" | ||
@@ -6,0 +6,0 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" |
26
test.js
@@ -16,3 +16,3 @@ | ||
orm.load(function(err) { | ||
var db = orm.public | ||
var db = orm.ee_orm_test_postgres | ||
, start | ||
@@ -27,17 +27,13 @@ , count = 0 | ||
var filters = {}; | ||
var dbQuery = db | ||
.product(['*'], {id_shop: 1, id: 18}) | ||
.getArticle(['*'], filters.article) | ||
.fetchColor('*') | ||
.fetchType('*') | ||
.getVariant(['*'], filters.variant) | ||
.getSale_variant(['*'], filters.sale_variant) | ||
.getSale(['*'], filters.sale); | ||
dbQuery.findOne(function(err, product){ log(product); | ||
if(err) return log(err); | ||
if(product) product.setDebugMode().save(); | ||
}); | ||
db.event().find().then(function(list) { | ||
list = list.toArray(); | ||
log.error(list.length); | ||
log.wtf(list instanceof Array, [1,2].concat(list)); | ||
list.push({}); | ||
list.length = 1; | ||
log.warn(list, list.length); | ||
}).catch(log); | ||
/* | ||
@@ -44,0 +40,0 @@ db.shop(['*'], {id: 1}) |
@@ -872,3 +872,16 @@ | ||
describe('[SET Methods]', function() { | ||
it('the toArray method should return the values as an plain array', function(done) { | ||
db.event('*').order('id').find().then(function(list) { | ||
list = list.toArray(); | ||
list.length = 1; | ||
expect('[{"id":2,"id_venue":2,"title":"Mapping Test","startdate":"1970-01-01T00:00:00.000Z","enddate":null,"canceled":null,"created":null,"updated":null,"deleted":null}]', done)(null, list); | ||
}).catch(done); | ||
}); | ||
}); | ||
describe('[Model Extending]', function() { | ||
@@ -875,0 +888,0 @@ it('should work', function(done) { |
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
300298
5690