From ae843591da0fea1a57684f26737be0b484718809 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 5 Oct 2016 17:49:19 -0400 Subject: okpush template stuff --- examples/lib/okpush/db.js | 62 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 14 deletions(-) (limited to 'examples/lib/okpush/db.js') 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 -- cgit v1.2.3-70-g09d2