Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config changes #213

Merged
merged 1 commit into from Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -136,10 +136,13 @@ EmberPouch supports both `hasMany` and `belongsTo` relationships.

To be more in line with the normal ember data way of saving `hasMany` - `belongsTo` relationships, ember-pouch now has an option to not save the child ids on the `hasMany` side. This prevents the extra need to save the `hasMany` side as explained below. For a more detailed explanation please read the [relational-pouch documentation](https://github.com/pouchdb-community/relational-pouch#dont-save-hasmany)

This new mode can be selected for a `hasMany` relationship by specifying the option `dontsave: true` on the relationship. An application wide setting named `ENV.emberpouch.dontsavehasmany` can also be set to `true` to make all `hasMany` relationships behave this way.
This new mode can be disabled for a `hasMany` relationship by specifying the option `save: false` on the relationship. An application wide setting named `ENV.emberPouch.saveHasMany` can also be set to `false` to make all `hasMany` relationships behave the old way.

Using this mode does impose a slight runtime overhead, since this will use `db.find` and database indexes to search for the child ids. The indexes are created automatically for you. But large changes to the model might require you to clean up old, unused indexes.

!Important
This mode is the default from version 5 onwards. Before that it was called `dontsave` and `dontsavehasmany`

### Saving child ids

When you do save child ids on the `hasMany` side, you have to follow the directions below to make sure the data is saved correctly.
Expand Down
8 changes: 4 additions & 4 deletions addon/adapters/pouch.js
Expand Up @@ -4,7 +4,8 @@ import { pluralize } from 'ember-inflector';
//import BelongsToRelationship from 'ember-data/-private/system/relationships/state/belongs-to';

import {
extractDeleteRecord
extractDeleteRecord,
shouldSaveRelationship
} from '../utils';

const {
Expand Down Expand Up @@ -181,7 +182,6 @@ export default DS.RESTAdapter.extend({
}

let config = getOwner(this).resolveRegistration('config:environment');
let dontsavedefault = config['emberpouch'] && config['emberpouch']['dontsavehasmany'];
// else it's new, so update
this._schema.push(schemaDef);
// check all the subtypes
Expand All @@ -198,10 +198,10 @@ export default DS.RESTAdapter.extend({
let includeRel = true;
rel.options = rel.options || {};
if (typeof(rel.options.async) === "undefined") {
rel.options.async = config.emberpouch && !Ember.isEmpty(config.emberpouch.async) ? config.emberpouch.async : true;//default true from https://github.com/emberjs/data/pull/3366
rel.options.async = config.emberPouch && !Ember.isEmpty(config.emberPouch.async) ? config.emberPouch.async : true;//default true from https://github.com/emberjs/data/pull/3366
}
let options = Object.create(rel.options);
if (rel.kind === 'hasMany' && (options.dontsave || typeof(options.dontsave) === 'undefined' && dontsavedefault)) {
if (rel.kind === 'hasMany' && !shouldSaveRelationship(self, rel)) {
let inverse = type.inverseFor(rel.key, store);
if (inverse) {
if (inverse.kind === 'belongsTo') {
Expand Down
19 changes: 7 additions & 12 deletions addon/serializers/pouch.js
@@ -1,9 +1,12 @@
import Ember from 'ember';
import DS from 'ember-data';

import {
shouldSaveRelationship
} from '../utils';

const {
get,
getOwner
get
} = Ember;
const keys = Object.keys || Ember.keys;
const assign = Object.assign || Ember.assign;
Expand All @@ -12,18 +15,10 @@ var Serializer = DS.RESTSerializer.extend({

init: function() {
this._super(...arguments);

let config = getOwner(this).resolveRegistration('config:environment');
this.dontsavedefault = config['emberpouch'] && config['emberpouch']['dontsavehasmany'];
},

_getDontsave(relationship) {
return !Ember.isEmpty(relationship.options.dontsave) ? relationship.options.dontsave : this.dontsavedefault;
},

shouldSerializeHasMany: function(snapshot, key, relationship) {
let dontsave = this._getDontsave(relationship);
let result = !dontsave;
let result = shouldSaveRelationship(this, relationship);
return result;
},

Expand Down Expand Up @@ -89,7 +84,7 @@ var Serializer = DS.RESTSerializer.extend({
let relationships = this._super(...arguments);

modelClass.eachRelationship((key, relationshipMeta) => {
if (relationshipMeta.kind === 'hasMany' && this._getDontsave(relationshipMeta) && !!relationshipMeta.options.async) {
if (relationshipMeta.kind === 'hasMany' && !shouldSaveRelationship(this, relationshipMeta) && !!relationshipMeta.options.async) {
relationships[key] = { links: { related: key } };
}
});
Expand Down
19 changes: 17 additions & 2 deletions addon/utils.js
@@ -1,6 +1,21 @@
import Ember from 'ember';

// ember-data doesn't like getting a json response of {deleted: true}
function extractDeleteRecord() {
export function extractDeleteRecord() {
return null;
}

export { extractDeleteRecord };
//should this take a config?
export function shouldSaveRelationship(container, relationship) {
if (typeof relationship.options.save !== "undefined")
return relationship.options.save;

if (relationship.kind === 'belongsTo') return true;

//TODO: save default locally? probably on container?
let config = Ember.getOwner(container).resolveRegistration('config:environment');
let saveDefault = config['emberPouch'] && config['emberPouch']['saveHasMany'];
//default is false if not specified

return saveDefault;
}
4 changes: 2 additions & 2 deletions tests/dummy/app/adapter.js
Expand Up @@ -4,9 +4,9 @@ import config from 'dummy/config/environment';
export default Adapter.extend({
_init(store, type) {
type.eachRelationship((name, rel) => {
rel.options.async = config.emberpouch.async;
rel.options.async = config.emberPouch.async;
if (rel.kind === 'hasMany') {
rel.options.dontsave = config.emberpouch.dontsavehasmany;
rel.options.save = config.emberPouch.saveHasMany;
}
});
this._super(...arguments);
Expand Down
8 changes: 4 additions & 4 deletions tests/dummy/app/adapters/application.js
Expand Up @@ -6,14 +6,14 @@ import Ember from 'ember';
const { assert, isEmpty } = Ember;

function createDb() {
let localDb = config.emberpouch.localDb;
let localDb = config.emberPouch.localDb;

assert('emberpouch.localDb must be set', !isEmpty(localDb));
assert('emberPouch.localDb must be set', !isEmpty(localDb));

let db = new PouchDB(localDb);

if (config.emberpouch.remote) {
let remoteDb = new PouchDB(config.emberpouch.remoteDb);
if (config.emberPouch.remote) {
let remoteDb = new PouchDB(config.emberPouch.remoteDb);

db.sync(remoteDb, {
live: true,
Expand Down
8 changes: 4 additions & 4 deletions tests/dummy/app/adapters/taco-salad.js
Expand Up @@ -6,14 +6,14 @@ import Ember from 'ember';
const { assert, isEmpty } = Ember;

function createDb() {
let localDb = config.emberpouch.localDb;
let localDb = config.emberPouch.localDb;

assert('emberpouch.localDb must be set', !isEmpty(localDb));
assert('emberPouch.localDb must be set', !isEmpty(localDb));

let db = new PouchDB(localDb);

if (config.emberpouch.remote) {
let remoteDb = new PouchDB(config.emberpouch.remoteDb);
if (config.emberPouch.remote) {
let remoteDb = new PouchDB(config.emberPouch.remoteDb);

db.sync(remoteDb, {
live: true,
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/config/environment.js
Expand Up @@ -5,7 +5,7 @@ module.exports = function(environment) {
let ENV = {
modulePrefix: 'dummy',
environment,
emberpouch: { localDb: 'ember-pouch-test' },
emberPouch: { localDb: 'ember-pouch-test' },
rootURL: '/',
locationType: 'auto',
EmberENV: {
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/adapters/pouch-basics-test.js
Expand Up @@ -23,7 +23,7 @@ function promiseToRunLater(timeout) {


function savingHasMany() {
return !config.emberpouch.dontsavehasmany;
return config.emberPouch.saveHasMany;
}

function getDocsForRelations() {
Expand Down Expand Up @@ -442,25 +442,25 @@ test('delete cascade null', function (assert) {
let syncAsync = function() {
module('async', {
beforeEach: function() {
config.emberpouch.async = true;
config.emberPouch.async = true;
}
}, () => { allTests(); asyncTests(); });
module('sync', {
beforeEach: function() {
config.emberpouch.async = false;
config.emberPouch.async = false;
}
}, allTests);
};

module('dont save hasMany', {
beforeEach: function() {
config.emberpouch.dontsavehasmany = true;
config.emberPouch.saveHasMany = false;
}
}, syncAsync);

module('save hasMany', {
beforeEach: function() {
config.emberpouch.dontsavehasmany = false;
config.emberPouch.saveHasMany = true;
}
}, syncAsync);
});