New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rods

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rods - npm Package Compare versions

Comparing version 0.1.1-beta to 1.0.0-beta

lib/model.js

85

lib/index.js

@@ -1,2 +0,3 @@

var table = require('./table');
var async = require('async');
var model = require('./model');

@@ -20,4 +21,4 @@ var rods = function(knex, opts) {

this.table = function(name, opts) {
var t = table.new(name);
this.model = function(name, opts) {
var t = model.new(name);
t._table = name;

@@ -54,4 +55,8 @@ t._knex = knex;

} else {
data._isNew = false;
callback(null, new self(data));
if (data) {
data._isNew = false;
callback(null, new self(data));
} else {
callback(null, null);
}
}

@@ -82,7 +87,11 @@ });

} else {
var objs = data.map(function(x) {
x._isNew = false;
return new self(x);
})
callback(null, objs);
if (!data) {
callback(null, null);
} else {
var objs = data.map(function(x) {
x._isNew = false;
return new self(x);
})
callback(null, objs);
}
}

@@ -94,5 +103,7 @@ });

var self = this;
var ret = knex.select().from(self._table);
var ret = knex.select(self._table + '.*').from(self._table);
ret.__self = self;
ret.__populate = [];
ret.exec = exec;
ret.populate = populate;
return ret;

@@ -103,8 +114,16 @@ }

var self = this;
var ret = knex.first().from(self._table);
var ret = knex.first(self._table + '.*').from(self._table);
ret.__self = self;
ret.__populate = [];
ret.exec = exec;
ret.populate = populate;
return ret;
}
function populate(_to, _from, _with, multi) {
var m = multi || false;
this.__populate.push({_to: _to, _from: _from, _with: _with, multi: m});
return this;
}
function exec(callback) {

@@ -118,10 +137,22 @@ var self = this;

if (Array.isArray(data)) {
var objs = data.map(function(x) {
var objs = []
async.eachSeries(data, function(x, cb) {
x._isNew = false;
return new self.__self(x);
})
callback(null, objs);
var obj = new self.__self(x);
_populate(obj, self.__populate, function(err) {
if (err) return cb(err);
objs.push(obj);
cb();
});
}, function(err) {
if (err) return callback(err);
callback(null, objs);
});
} else {
data._isNew = false;
callback(null, new self.__self(data));
var obj = new self.__self(data);
_populate(obj, self.__populate, function(err) {
if (err) return callback(err);
callback(null, obj);
});
}

@@ -132,2 +163,22 @@ }

function _populate(obj, populate, callback) {
async.eachSeries(populate, function(x, cb) {
if (x.multi) {
x._from.fetch(x._with(obj), function(err, data) {
if (err) return cb(err);
obj[x._to] = data;
cb();
});
} else {
x._from.get(x._with(obj), function(err, data) {
if (err) return cb(err);
obj[x._to] = data;
cb();
});
}
}, function(err) {
callback(err);
});
}
return this;

@@ -134,0 +185,0 @@ }

20

package.json
{
"name": "rods",
"version": "0.1.1-beta",
"version": "1.0.0-beta",
"description": "a micro ORM using knex",
"main": "lib/index.js",
"keywords": ["knex", "orm", "sql"],
"scripts": {
"test": "mocha"
},
"keywords": [
"knex",
"orm",
"sql"
],
"author": "Bob Rupp <bob@rupp.io>",

@@ -16,3 +23,10 @@ "license": "MIT",

},
"homepage": "https://github.com/hitgeek/rods#readme"
"homepage": "https://github.com/hitgeek/rods#readme",
"devDependencies": {
"knex": "^0.11.10",
"sqlite3": "^3.1.4"
},
"dependencies": {
"async": "^2.0.1"
}
}

@@ -24,3 +24,3 @@ #rods

###Mapping Tables
###Mapping Models
```js

@@ -30,6 +30,6 @@

//map tables using the table name
//map models using the table name
//tables should already exists
//see knex documentation for Schema Building and Migrations
db.user = rods.table('users');
db.user = rods.model('users');
```

@@ -137,2 +137,34 @@

###Population
Population provides a convient syntax for populating foreign references with associated objects. Population does individual queries for each reference, so it may not be the most efficient option.
Population must be used with `.first()` and `select()`
```.populate(to, from, with, multi)```
to: name of property to be assigned
from: the db.table object to get/fetch from
with: the query to use inside of get/fetch
multi: true/false (true=the property is an array) (optional defaults to false)
```js
//In this example user can have multiple groups. user_group is a cross reference table between user & group
//Notice how the results of the 1st populate are used in the second populate
db.user
.first()
.populate('user_groups', db.user_group, function(x) {
return {user_id: x.id}; //query that will be used for db.user_group.fetch
}, true)
.populate('groups', db.group, function(x) {
return x.user_groups.map(x => x.group_id); //query used for db.group.fetch
}, true)
.exec(function(err, u) {
assert.equal(u.groups[0].name, 'admins');
done();
});
```
###Options

@@ -139,0 +171,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc