-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.datastore.js
64 lines (59 loc) · 1.52 KB
/
lib.datastore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var mysql = require('mysql');
var http = require('http');
var fs = require('fs');
var mongodb = require('mongodb');
var qs = require('querystring');
var _ = require('underscore');
function datastore(options) {
this.options = _.extend({
host: "127.0.0.1",
port: 27017,
database: "fleetwit"
},options);
this.collections = {};
}
datastore.prototype.init = function(callback) {
var scope = this;
this.server = new mongodb.Server(this.options.host, this.options.port, {});
this.db = new mongodb.Db(this.options.database, this.server, {w:1});
this.db.open(function (error, client) {
if (error) {
throw error;
}
scope.instance = client;
callback();
});
}
datastore.prototype.open = function(collectionName, callback) {
var scope = this;
if (!this.collections[collectionName]) {
this.collections[collectionName] = new mongodb.Collection(this.instance, collectionName);
}
callback(this.collections[collectionName]);
}
datastore.prototype.getUser = function(collectionName, uid, callback) {
var scope = this;
this.open(collectionName, function(collection) {
collection.find({
uid: uid
}, {
limit:1
}).toArray(function(err, docs) {
//console.dir(docs);
if (docs.length == 0) {
// No userdata, we create it.
collection.insert({
uid: uid,
surveydata: {},
facebookdata: {},
twitterdata: {}
}, function(err, docs) {
callback(collection);
});
} else {
callback(collection);
}
});
});
}
exports.datastore = datastore;