summaryrefslogtreecommitdiff
path: root/examples/lib/okpush/db.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-10-05 17:49:19 -0400
committerJules Laplace <jules@okfoc.us>2016-10-05 17:49:19 -0400
commitae843591da0fea1a57684f26737be0b484718809 (patch)
tree547be3ae5d69c3568626a2f9ce9f35e034c82ed6 /examples/lib/okpush/db.js
parent95df0e83fdb7f564d64b6c916e674e0db46c116d (diff)
okpush template stuff
Diffstat (limited to 'examples/lib/okpush/db.js')
-rw-r--r--examples/lib/okpush/db.js62
1 files changed, 48 insertions, 14 deletions
diff --git a/examples/lib/okpush/db.js b/examples/lib/okpush/db.js
index 5a66b01..67d1ba8 100644
--- a/examples/lib/okpush/db.js
+++ b/examples/lib/okpush/db.js
@@ -1,29 +1,63 @@
-// db
+var mongoose = require('mongoose')
+var _ = require('lodash')
+var db, PushToken
function init (config) {
- var db = mongoose.connect(config.mongodbUrl);
- mongoose.connection.on('error', errorHandler);
+ db = mongoose.connect(config.mongodbUrl)
+ mongoose.connection.on('error', errorHandler)
- var pushAssociationSchema = new db.Schema({
- user: {
- type: 'String',
- required: true
- },
+ var pushTokenSchema = new db.Schema({
type: {
type: 'String',
required: true,
enum: ['ios', 'android'],
- lowercase: true
+ lowercase: true,
},
token: {
type: 'String',
- required: true
+ required: true,
}
- });
+ })
- PushAssociation = db.model('PushAssociation', pushAssociationSchema);
+ PushToken = db.model('PushToken', pushTokenSchema)
}
function errorHandler (error) {
- console.error('ERROR: ' + error);
-}; \ No newline at end of file
+ console.error('ERROR: ' + error)
+}
+function add (deviceType, token) {
+ var pushItem = new PushToken({ type: deviceType, token: token })
+ pushItem.save()
+}
+function getAll (cb) {
+ var wcb = wrap(cb)
+ PushToken.find(wcb)
+}
+function removeDevice () {
+ PushToken.remove({token: token}, function (err) {
+ if (err) console.log(err)
+ })
+}
+function removeDevices (tokens) {
+ PushAssociation.remove({token: {$in: tokens}}, function (err) {
+ if (err) console.log(err)
+ })
+}
+function wrap (cb) {
+ return function (err, items) {
+ if (err) return cb(err, null)
+
+ var items = _.map(items, function (item) {
+ return _.pick(item, ['type', 'token'])
+ })
+
+ return callback(null, items)
+ }
+}
+module.exports = {
+ init: init,
+ add: add,
+ getAll: getAll,
+ removeDevice: removeDevice,
+ removeDevices: removeDevices,
+} \ No newline at end of file