This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
appbase-mongo.js
65 lines (54 loc) · 1.82 KB
/
appbase-mongo.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
/* vim: set sw=2 ts=2 nocin si: */
var mongoose = require("mongoose"), utils = require("./utils");
mongoose.connect('mongodb://localhost/net9-auth');
mongoose.model('App', new mongoose.Schema({
name: { type: String, index: true },
clientid: { type: String, index: true },
secret: String,
desc: String,
owners: [String]
}));
var App = mongoose.model('App');
exports.getAllByUser = function (username, callback) {
App.find({ owners: username }, function (err, arr) {
callback(true, arr.map(function (app) { return app.toObject(); }));
});
};
exports.checkByName = function (appname, callback) {
App.count({ name: appname }, function (err, count) {
callback(count !== 0);
});
};
exports.create = function (appinfo, callback) {
var newApp = new App(appinfo);
newApp.save(function (err) {
if (err) callback(false, err);
else callback(true, newApp.toObject());
});
};
exports.getByID = function (clientid, callback) {
App.findOne({ clientid: clientid }, function (err, app) {
if (app === null) callback(false, 'app-not-found');
else callback(true, app.toObject());
});
};
exports.deleteByID = function (clientid, callback) {
App.remove({ clientid: clientid }, function (err) {
callback(err ? false : true, err);
});
};
exports.authenticate = function (clientid, secret, callback) {
App.findOne({ clientid: clientid }, function (err, app) {
if (app === null) callback(false, 'no-such-app-clientid');
else if (app.secret !== secret) callback(false, 'wrong-secret');
else callback(true, app.toObject());
});
};
exports.update = function (appinfo, callback) {
App.findOne({ clientid: appinfo.clientid }, function (err, app) {
utils.merge(app, appinfo).save(function (err) {
if (err) callback(false, err);
else callback(true, app.toObject());
});
});
};