Skip to content

Commit

Permalink
feat: update to latest hoodie
Browse files Browse the repository at this point in the history
  • Loading branch information
arun-prasad-r authored and gr2m committed Apr 11, 2017
1 parent bde9eb8 commit 67fb969
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 25 deletions.
30 changes: 21 additions & 9 deletions addon/adapters/hoodie.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import DS from 'ember-data';
import Ember from 'ember';

const {
inject: { service }
get,
inject: {
service
},
String: {
camelize
}
} = Ember;

export default DS.Adapter.extend({
Expand All @@ -15,22 +21,25 @@ export default DS.Adapter.extend({

findRecord(store, type, id) {
return this._next(() => {
return this._storeForType(type).find(id);
return this._hoodieStore().find(id);
});
},

createRecord(store, type, snapshot) {
var props = this.serialize(snapshot);
if (snapshot.id) { props.id = snapshot.id; }
if (snapshot.id) {
props.id = snapshot.id;
}
props.type = camelize(type.modelName);
return this._next(() => {
return this._storeForType(type).add(props);
return this._hoodieStore().add(props);
});
},

updateRecord(store, type, snapshot) {
var props = this.serialize(snapshot);
return this._next(() => {
return this._storeForType(type).update(snapshot.id, props);
return this._hoodieStore().update(snapshot.id, props);
});
},

Expand All @@ -40,22 +49,25 @@ export default DS.Adapter.extend({
// https://github.com/hoodiehq/hoodie-store-client/issues/95
deleteRecord(store, type, snapshot) {
return this._next(() => {
return this.get('hoodie.store').remove(snapshot.id);
return this._hoodieStore().remove(snapshot.id);
});
},

findAll(store, type) {
return this._next(() => {
return this._storeForType(type).findAll();
let isOfType = function(m){
return m.type === type.modelName;
};
return this._hoodieStore().findAll(isOfType);
});
},

query() {
throw new Error('not implemented');
},

_storeForType(type) {
return this.get('hoodie.store')(type.modelName);
_hoodieStore() {
return get(this, 'hoodie.store');
},

// This is used to synchronize and single-file hoodie operations.
Expand Down
45 changes: 36 additions & 9 deletions addon/services/hoodie-account.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Ember from 'ember';

const {
computed,
get,
inject: { service }
inject: {
service
}
} = Ember;

// I kinda just guessed at these...
Expand All @@ -28,6 +28,8 @@ const proxiedFunctions = [

export default Ember.Service.extend({
hoodie: service(),
// isSignedIn: false,
// username: '',

init() {
this._super(...arguments);
Expand All @@ -38,15 +40,40 @@ export default Ember.Service.extend({
let props = eventInvalidations[event];
account.on(event, this._notifyProperties.bind(this, props));
}
this.set('account', account);

let update = () => {
this._updateAccountProperties();
};

this._updateAccountProperties();

this.get('hoodie.account').on('signin', update);
this.get('hoodie.account').on('signout', update);
this.get('hoodie.account').on('unauthenticate', update);
this.get('hoodie.account').on('reauthenticate', update);
this.get('hoodie.account').on('update', update);
},

isSignedIn: computed(function() {
return get(this, 'hoodie.account').isSignedIn();
}),
_updateAccountProperties: function() {
this.account.get('session').then(session => {
let signedIn = session ? true : false;
this.set('isSignedIn', signedIn);
});
this.account.get('username').then(username => {
this.set('username', username);
});
this.account.get('session.invalid').then(hasInvalidSession => {
this.set('hasInvalidSession', hasInvalidSession);
});
this.account.profile.get().then(profile => {
this.set('profile', profile);
});
},

username: computed(function() {
return get(this, 'hoodie.account.username');
}),
_hoodieAccount() {
return get(this, 'hoodie.account');
},

_notifyProperties(props) {
props.forEach((prop) => {
Expand Down
9 changes: 5 additions & 4 deletions addon/services/hoodie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* globals Hoodie */
/* globals Hoodie, PouchDB */
import Ember from 'ember';

const {
Service,
get,
Expand All @@ -11,8 +10,10 @@ export default Service.extend({
init() {
this._super(...arguments);
const appConfig = Ember.getOwner(this).application.resolveRegistration('config:environment');
const hoodieConfig = appConfig.hoodie ? appConfig.hoodie.client : {};
const hoodie = new Hoodie(hoodieConfig);
const hoodie = new Hoodie({
url : appConfig.hoodie.client.url,
PouchDB : PouchDB
});
set(this, 'hoodie', hoodie);
// for debug only
window.hoodie = hoodie;
Expand Down
24 changes: 21 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,31 @@ function hoodieMiddleware(config) {
}

var Hapi = require('hapi');
var hoodie = require('hoodie');
var hoodie = require('hoodie').register;
var proxy = require('http-proxy-middleware');
var PouchDB = require('pouchdb');

config.app.use('/hoodie', proxy({target: 'http://localhost:' + appConfig.hoodie.server.port}));

var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: appConfig.hoodie.server.port
});

server.register({
register: hoodie,
options: appConfig.hoodie.server
options: {
PouchDB: PouchDB,
// paths: {
// data: '.hoodie'
// },
adminPassword: appConfig.hoodie.server.adminPassword,
client: {
url: "http://localhost:"+ appConfig.hoodie.server.port
}
}
// options: appConfig.hoodie.server
}, function (error) {
if (error) {
throw error;
Expand Down Expand Up @@ -60,8 +72,13 @@ module.exports = {
srcDir: 'dist',
destDir: '/'
});
var pouchdbPackage = path.join(path.dirname(require.resolve('pouchdb')), '..', 'dist');
var pouchdbTree = new Funnel(pouchdbPackage, {
files: ['pouchdb.js'],
destDir: 'pouchdb'
});
if (tree) {
return mergeTrees([tree, hoodieTree]);
return mergeTrees([tree, hoodieTree, pouchdbTree], {overwrite: true});
} else {
return hoodieTree;
}
Expand All @@ -71,6 +88,7 @@ module.exports = {
this._super.apply(this, arguments);

app.import('vendor/hoodie.js');
app.import('vendor/pouchdb/pouchdb.js');
},

serverMiddleware: hoodieMiddleware
Expand Down

0 comments on commit 67fb969

Please sign in to comment.