Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

secondthought

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

secondthought - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

.idea/.name

28

index.js

@@ -6,4 +6,5 @@ var r = require("rethinkdb");

var Table = require("./lib/table");
var Sync = require("sync");
var DA = require("deasync");
var SecondThought = function(){

@@ -25,4 +26,4 @@

/*
The first method to call in order to use the API.
The connection information for the DB. This should have {host, db, port}; host and port are optional.
The first method to call in order to use the API.
The connection information for the DB. This should have {host, db, port}; host and port are optional.
*/

@@ -47,6 +48,18 @@ self.connect = function(args, next){

};
self.connectSync = DA(self.connect);
self.execute = function(query, next){
self.openConnection(function(err,conn){
query.run(conn, function(err,res){
conn.close();
next(err,res);
});
});
};
self.executeSync = DA(self.execute);
self.openConnection = function(next){
r.connect(config, next);
};
self.openConnectionSync = DA(self.openConnection);

@@ -64,2 +77,3 @@ self.createDb = function(dbName, next){

};
self.createDbSync = DA(self.createDb);

@@ -76,4 +90,4 @@ self.dropDb = function(dbName, next){

};
self.dropDbSync = DA(self.dropDb);
self.createTable = function(tableName, next){

@@ -90,3 +104,3 @@

};
self.createTableSync = DA(self.createTable);
self.tableExists = function(tableName, next){

@@ -116,2 +130,3 @@

self.install = function(tables, next){

@@ -126,3 +141,4 @@ assert.ok(tables && tables.length > 0, "Be sure to set the tables array on the config");

};
self.installSync = DA(self.install);
return self;

@@ -129,0 +145,0 @@ };

var r = require("rethinkdb");
var assert = require("assert");
var _ = require("underscore")._;
var DA = require("deasync");
/*
This is the main abstraction, which tweaks the RethinkDb Table prototype to be a bit
less verbose - favoring direct method calls over fluent interface.
This is the main abstraction, which tweaks the RethinkDb Table prototype to be a bit
less verbose - favoring direct method calls over fluent interface.
The table returned from this constructor is a RethinkDB table, with methods like
"save" and destroy attached to it.
The table returned from this constructor is a RethinkDB table, with methods like
"save" and destroy attached to it.
*/

@@ -15,41 +16,73 @@ var Table = function(config, tableName){

var table = r.db(config.db).table(tableName);
table.name = tableName;
table.dbName = config.db;
table.get = function(id, next){
table.onConnect(function(err,conn){
r.table(tableName).get(id).run(conn, function(err,res){
conn.close();
if(err) next(err, null);
else next(null,res);
});
});
};
table.getSync = DA(table.get);
//give it some abilities yo
table.first = function(criteria, next){
table.query(criteria, function(err,array){
next(err, _.first(array));
if(err) next(err, null);
else {
next(null, _.first(array));
}
});
};
table.firstSync = DA(table.first);
table.exists = function(criteria, next){
onConnect(function(err,conn){
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.query(criteria,function(err,result){
assert.ok(err === null,err);
conn.close();
next(null,result.length > 0);
if(err) next(err, null);
else {
next(null, result.length > 0);
}
});
});
};
table.existsSync = DA(table.exists);
table.query = function(criteria, next){
onConnect(function(err,conn){
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.filter(criteria).run(conn,function(err,result){
assert.ok(err === null,err);
conn.close();
result.toArray(next);
if(err) next(err, null);
else {
result.toArray(next);
}
});
});
};
table.querySync = DA(table.query);
table.save = function(thing, next){
onConnect(function(err,conn){
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.insert(thing, {conflict : "replace"}).run(conn,function(err,result){
assert.ok(err === null,err);
if(result.generated_keys && result.generated_keys.length > 0){
thing.id = _.first(result.generated_keys);
//assert.ok(err === null,err);
conn.close();
if(err) next(err, null);
else {
if(result.generated_keys && result.generated_keys.length > 0){
//loop the generated keys and assign, then pass it all back
if(_.isArray(thing)){
for(var i = 0;i < result.generated_keys.length; i++){
thing[i] = result.generated_keys[i];
}
}else{
thing.id = _.first(result.generated_keys[0]);
}
}
}
conn.close();
next(err,thing);

@@ -59,9 +92,9 @@ });

};
table.saveSync = DA(table.save);
table.updateOnly = function(updates, id, next){
onConnect(function(err,conn){
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.get(id).update(updates).run(conn,function(err,result){
assert.ok(err === null,err);
conn.close();
r.table(tableName).get(id).update(updates).run(conn,function(err,result){
//assert.ok(err === null,err);
next(null,result.replaced > 0);

@@ -71,8 +104,9 @@ });

};
table.updateOnlySync = DA(table.updateOnly);
table.destroyAll = function(next){
onConnect(function(err,conn){
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.delete().run(conn,function(err,result){
assert.ok(err === null,err);
//assert.ok(err === null,err);
conn.close();

@@ -83,19 +117,22 @@ next(err,result.deleted);

};
table.destroyAllSync = DA(table.destroyAll);
table.destroy = function(id, next){
onConnect(function(err,conn){
assert.ok(err === null,err);
table.get(id).delete().run(conn,function(err,result){
assert.ok(err === null,err);
assert(id, "Need an ID to pass along here");
assert((!_.isObject(id) && !_.isArray(id)), "Need a numeric ID for the destroy call");
table.onConnect(function(err,conn){
r.table(tableName).get(id).delete().run(conn,function(err,result){
//assert.ok(err === null,err);
conn.close();
next(err,true);
if(err) next(err,null);
else next(null, true);
});
});
};
table.destroySync = DA(table.destroy);
table.index = function(att, next){
onConnect(function(err,conn){
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.indexCreate(att).run(conn,function(err,result){
assert.ok(err === null,err);
conn.close();

@@ -106,6 +143,28 @@ next(err,result.created == 1);

};
table.indexSync = DA(table.index);
table.contains = function(args, next){
assert(args.vals && args.field, "Need to have vals (an array) and field specified");
_.isArray(args.vals) || (args.vals = [vals]);
table.onConnect(function(err,conn){
assert.ok(err === null,err);
table.filter(function(row){
return r.expr(args.vals).contains(row(args.field));
}).run(conn, function(err,res){
conn.close();
if(err)next(err,null);
else {
res.toArray(next);
}
});
});
};
table.containsSync = DA(table.contains);
//stole this from https://github.com/rethinkdb/rethinkdb-example-nodejs-chat/blob/master/lib/db.js
var onConnect = function(callback) {
table.onConnect = function(callback) {
r.connect(config, function(err, conn) {
table.conn = conn;
assert.ok(err === null, err);

@@ -112,0 +171,0 @@ conn['_id'] = Math.floor(Math.random()*10001);

@@ -5,3 +5,3 @@ {

"description": "A light wrapper for RethinkDB",
"version": "0.0.5",
"version": "0.0.6",
"private": false,

@@ -11,11 +11,13 @@ "scripts": {

},
"main" : "../lib/db",
"main": "../lib/db",
"dependencies": {
"async": "~0.2.9",
"rethinkdb": "~2.0.0",
"sync": "^0.2.5",
"underscore": "~1.5.1",
"rethinkdb": "~2.0.0",
"async": "~0.2.9"
"deasync": "^0.0.10"
},
"devDependencies": {
"chai": "~1.7.2",
"mocha" : "*"
"mocha": "*"
},

@@ -26,4 +28,11 @@ "repository": {

},
"keywords": ["database", "query", "rethinkdb", "nosql"],
"engines": { "node": ">= 0.10.1" }
"keywords": [
"database",
"query",
"rethinkdb",
"nosql"
],
"engines": {
"node": ">= 0.10.1"
}
}

@@ -13,2 +13,5 @@ # Some Light Abstraction for RethinkDB

In addition, I love Meteor.js and thought I would try to support its synchronous nature as best I could. I know that Meteor uses Fibers, but they can cause problems if you're not completely in control of them.
So, SecondThought uses the `deasync` library to "virtually unblock" the synchronous calls. The thread won't be blocked when you use these sync methods, but the consecutive code calls will. It's expermental, at best.
## Usage

@@ -25,11 +28,11 @@

```javascript
var db = require("second-thought");
db.connect({db : "test"}, function(err,db){
var DB = require("second-thought");
DB.connect({db : "test"}, function(err,db){
//you now have access to all of your tables as properties on your db variable:
//so, assume there's a table called "foo" in your db...
db.foo.save({name : "Mike"}, function(err,saved){
db.foo.save({name : "Mike"}, function(err,newRecord){
//output the generated ID
console.log(saved.id);
console.log(newRecord.id);
});

@@ -40,2 +43,12 @@

You can do this synchronously as well:
```js
var SecondThought = require("second-thought");
var db = SecondThought.connectSync({db : "test"});
var newRecord = db.foo.saveSync({name : "Pete"});
```
You can intermix the sync/async stuff all you like. Each method you see below has an sync counterpart which you can use by attaching `Sync` to the method name.
Each table on your DB object is a full-blown RethinkDB table, so you can step outside the abstraction at any point:

@@ -91,3 +104,7 @@

});
db.foo.contains({field : "name", vals : ["Bob", "Jill"]}, function(err,res){
//res is two records with the names Bob and Jill
});
db.foo.first({email : "rob@tekpub.com"}, function(err,rob){

@@ -94,0 +111,0 @@ //hi Rob

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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