From bb114c57ce676b4bd950beb6601fa192b989e01c Mon Sep 17 00:00:00 2001 From: Joren l'Ami Date: Fri, 26 Jan 2018 12:24:33 +0100 Subject: [PATCH] emberpouch -> emberPouch dontsave -> save dontsavehasmany -> saveHasMany --- README.md | 5 ++++- addon/adapters/pouch.js | 8 ++++---- addon/serializers/pouch.js | 19 +++++++------------ addon/utils.js | 19 +++++++++++++++++-- tests/dummy/app/adapter.js | 4 ++-- tests/dummy/app/adapters/application.js | 8 ++++---- tests/dummy/app/adapters/taco-salad.js | 8 ++++---- tests/dummy/config/environment.js | 2 +- .../integration/adapters/pouch-basics-test.js | 10 +++++----- 9 files changed, 48 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index da534b6..8681b70 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/addon/adapters/pouch.js b/addon/adapters/pouch.js index 580d185..38e44f1 100644 --- a/addon/adapters/pouch.js +++ b/addon/adapters/pouch.js @@ -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 { @@ -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 @@ -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') { diff --git a/addon/serializers/pouch.js b/addon/serializers/pouch.js index f584891..a82840b 100644 --- a/addon/serializers/pouch.js +++ b/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; @@ -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; }, @@ -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 } }; } }); diff --git a/addon/utils.js b/addon/utils.js index c60af40..da4118f 100644 --- a/addon/utils.js +++ b/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 }; \ No newline at end of file +//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; +} diff --git a/tests/dummy/app/adapter.js b/tests/dummy/app/adapter.js index 1971394..ea82e9f 100644 --- a/tests/dummy/app/adapter.js +++ b/tests/dummy/app/adapter.js @@ -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); diff --git a/tests/dummy/app/adapters/application.js b/tests/dummy/app/adapters/application.js index 1825349..9a7d3a2 100644 --- a/tests/dummy/app/adapters/application.js +++ b/tests/dummy/app/adapters/application.js @@ -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, diff --git a/tests/dummy/app/adapters/taco-salad.js b/tests/dummy/app/adapters/taco-salad.js index 2a2bc0e..e2fb7bd 100644 --- a/tests/dummy/app/adapters/taco-salad.js +++ b/tests/dummy/app/adapters/taco-salad.js @@ -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, diff --git a/tests/dummy/config/environment.js b/tests/dummy/config/environment.js index 127319f..efcdba4 100644 --- a/tests/dummy/config/environment.js +++ b/tests/dummy/config/environment.js @@ -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: { diff --git a/tests/integration/adapters/pouch-basics-test.js b/tests/integration/adapters/pouch-basics-test.js index b51b1c7..fc924c5 100644 --- a/tests/integration/adapters/pouch-basics-test.js +++ b/tests/integration/adapters/pouch-basics-test.js @@ -23,7 +23,7 @@ function promiseToRunLater(timeout) { function savingHasMany() { - return !config.emberpouch.dontsavehasmany; + return config.emberPouch.saveHasMany; } function getDocsForRelations() { @@ -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); });