diff --git a/example/mobile-models/app.js b/example/mobile-models/app.js new file mode 100644 index 00000000..5a83e9f4 --- /dev/null +++ b/example/mobile-models/app.js @@ -0,0 +1,45 @@ +var models = require('../../lib/models'); + +var asteroid = require('../../'); +var app = asteroid(); + +app.use(asteroid.rest()); + +var dataSource = asteroid.createDataSource('db', {connector: asteroid.Memory}); + +var Application = models.Application(dataSource); + +app.model(Application); + + +var data = {pushSettings: [ + { "platform": "apns", + "apns": { + "pushOptions": { + "gateway": "gateway.sandbox.push.apple.com", + "cert": "credentials/apns_cert_dev.pem", + "key": "credentials/apns_key_dev.pem" + }, + + "feedbackOptions": { + "gateway": "feedback.sandbox.push.apple.com", + "cert": "credentials/apns_cert_dev.pem", + "key": "credentials/apns_key_dev.pem", + "batchFeedback": true, + "interval": 300 + } + }} +]} + +Application.create(data, function(err, data) { + console.log('Created: ', data.toObject()); +}); + + +Application.register('MyApp', 'My first mobile application', 'rfeng', function (err, result) { + console.log(result.toObject()); + + result.resetKeys(function (err, result) { + console.log(result.toObject()); + }); +}); diff --git a/lib/models/application.js b/lib/models/application.js index c4761c5f..a9664b13 100644 --- a/lib/models/application.js +++ b/lib/models/application.js @@ -1,13 +1,30 @@ // Authentication schemes -var AuthenticationScheme = { +var AuthenticationSchemeSchema = { scheme: String, // local, facebook, google, twitter, linkedin, github credential: Object // Scheme-specific credentials } +var APNSSettingSchema = { + pushOptions: {type: { + gateway: String, + cert: String, + key: String + }}, + + feedbackOptions: {type: { + gateway: String, + cert: String, + key: String, + batchFeedback: Boolean, + interval: Number + }} +}; + // Push notification settings -var PushNotificationSetting = { +var PushNotificationSettingSchema = { platform: {type: String, required: true}, // apns, gcm, mpns - configuration: Object // platform-specific configurations + // configuration: {type: Object} // platform-specific configurations + apns: APNSSettingSchema } /** @@ -41,12 +58,12 @@ var ApplicationSchema = { masterKey: String, // Push notification - pushSettings: [PushNotificationSetting], + pushSettings: [PushNotificationSettingSchema], // User Authentication authenticationEnabled: {type: Boolean, default: true}, anonymousAllowed: {type: Boolean, default: true}, - authenticationSchemes: [AuthenticationScheme], + authenticationSchemes: [AuthenticationSchemeSchema], status: {type: String, default: 'sandbox'}, // Status of the application, production/sandbox/disabled @@ -74,9 +91,17 @@ function generateKey(hmacKey, algorithm) { module.exports = function (dataSource) { dataSource = dataSource || new require('jugglingdb').ModelBuilder(); + // var AuthenticationScheme = dataSource.define('AuthenticationScheme', AuthenticationSchemeSchema); + // ApplicationSchema.authenticationSchemes = [AuthenticationScheme]; + + // var PushNotificationSetting = dataSource.define('PushNotificationSetting', PushNotificationSettingSchema); + // ApplicationSchema.pushSettings = [PushNotificationSetting]; var Application = dataSource.define('Application', ApplicationSchema); + // Application.hasMany(AuthenticationScheme, {as: 'authenticationSchemes', foreignKey: 'appId'}); + // Application.hasMany(PushNotificationSetting, {as: 'pushNotificationSettings', foreignKey: 'appId'}); + Application.afterInitialize = function () { var app = this; // use data argument to update object @@ -90,8 +115,17 @@ module.exports = function (dataSource) { }; // Register a new application - Application.register = function (name, description, owner) { - return new Application({name: name, description: description, owner: owner}); + Application.register = function (name, description, owner, cb) { + Application.create({name: name, description: description, owner: owner}, cb); + } + + Application.prototype.resetKeys = function(cb) { + this.clientKey = generateKey('client'); + this.javaScriptKey = generateKey('javaScript'); + this.restApiKey = generateKey('restApi'); + this.windowsKey = generateKey('windows'); + this.masterKey = generateKey('master'); + this.save(cb); } return Application; diff --git a/lib/models/index.js b/lib/models/index.js index f0d994ff..35f6a412 100644 --- a/lib/models/index.js +++ b/lib/models/index.js @@ -1,16 +1,6 @@ -var ModelBuilder = require('jugglingdb').ModelBuilder; +exports.Application = require('./application'); +exports.ACL = require('./acl'); +exports.Role = require('./role'); +exports.Installation = require('./installation'); -var ds = new ModelBuilder(); - -exports.Application = require('./application')(ds); -exports.ACL = require('./acl')(ds); -exports.Role = require('./role')(ds); -exports.Installation = require('./installation')(ds); - -/* -console.log(new exports.Role()); -console.log(new exports.ACL()); -console.log(new exports.Application()); -console.log(new exports.Installation()); -*/