-
Notifications
You must be signed in to change notification settings - Fork 76
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
Use modelName instead of typeKey when available, but preserve existing ID generation behavior #64
Merged
nolanlawson
merged 3 commits into
pouchdb-community:master
from
rsutphin:record_type_name
Jun 9, 2015
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Adapter } from 'ember-pouch/index'; | ||
import PouchDB from 'pouchdb'; | ||
|
||
function createDb() { | ||
return new PouchDB('ember-pouch-test'); | ||
} | ||
|
||
export default Adapter.extend({ | ||
init() { | ||
this._super(...arguments); | ||
this.set('db', createDb()); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import DS from 'ember-data'; | ||
|
||
export default DS.Model.extend({ | ||
rev: DS.attr('string'), | ||
|
||
flavor: DS.attr('string') | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { module, test } from 'qunit'; | ||
import startApp from '../../helpers/start-app'; | ||
|
||
import Ember from 'ember'; | ||
/* globals PouchDB */ | ||
|
||
var App; | ||
|
||
/* | ||
* Tests basic CRUD behavior for an app using the ember-pouch adapter. | ||
*/ | ||
|
||
module('adapter:pouch [integration]', { | ||
beforeEach: function (assert) { | ||
var done = assert.async(); | ||
|
||
// TODO: do this in a way that doesn't require duplicating the name of the | ||
// test database here and in dummy/app/adapters/application.js. Importing | ||
// the adapter directly doesn't work because of what seems like a resolver | ||
// issue. | ||
(new PouchDB('ember-pouch-test')).destroy().then(() => { | ||
App = startApp(); | ||
done(); | ||
}); | ||
}, | ||
|
||
afterEach: function (assert) { | ||
Ember.run(App, 'destroy'); | ||
} | ||
}); | ||
|
||
function db() { | ||
return adapter().get('db'); | ||
} | ||
|
||
function adapter() { | ||
// the default adapter in the dummy app is an ember-pouch adapter | ||
return App.__container__.lookup('adapter:application'); | ||
} | ||
|
||
function store() { | ||
return App.__container__.lookup('store:main'); | ||
} | ||
|
||
test('can find all', function (assert) { | ||
assert.expect(3); | ||
|
||
var done = assert.async(); | ||
Ember.RSVP.Promise.resolve().then(() => { | ||
return db().bulkDocs([ | ||
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor' } }, | ||
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean' } }, | ||
{ _id: 'burritoShake_2_X', data: { consistency: 'smooth' } } | ||
]); | ||
}).then(() => { | ||
return store().find('taco-soup'); | ||
}).then((found) => { | ||
assert.equal(found.get('length'), 2, 'should have found the two taco soup items only'); | ||
assert.deepEqual(found.mapBy('id'), ['A', 'B'], | ||
'should have extracted the IDs correctly'); | ||
assert.deepEqual(found.mapBy('flavor'), ['al pastor', 'black bean'], | ||
'should have extracted the attributes also'); | ||
done(); | ||
}).catch((error) => { | ||
console.error('error in test', error); | ||
assert.ok(false, 'error in test:' + error); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('can find one', function (assert) { | ||
assert.expect(2); | ||
|
||
var done = assert.async(); | ||
Ember.RSVP.Promise.resolve().then(() => { | ||
return db().bulkDocs([ | ||
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor' } }, | ||
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean' } }, | ||
]); | ||
}).then(() => { | ||
return store().find('taco-soup', 'D'); | ||
}).then((found) => { | ||
assert.equal(found.get('id'), 'D', | ||
'should have found the requested item'); | ||
assert.deepEqual(found.get('flavor'), 'black bean', | ||
'should have extracted the attributes also'); | ||
done(); | ||
}).catch((error) => { | ||
console.error('error in test', error); | ||
assert.ok(false, 'error in test:' + error); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('create a new record', function (assert) { | ||
assert.expect(1); | ||
|
||
var done = assert.async(); | ||
Ember.RSVP.Promise.resolve().then(() => { | ||
var newSoup = store().createRecord('taco-soup', { id: 'E', flavor: 'balsamic' }); | ||
return newSoup.save(); | ||
}).then((saved) => { | ||
return db().get('tacoSoup_2_E'); | ||
}).then((newDoc) => { | ||
assert.equal(newDoc.data.flavor, 'balsamic', 'should have saved the attribute'); | ||
done(); | ||
}).catch((error) => { | ||
console.error('error in test', error); | ||
assert.ok(false, 'error in test:' + error); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('update an existing record', function (assert) { | ||
assert.expect(1); | ||
|
||
var done = assert.async(); | ||
Ember.RSVP.Promise.resolve().then(() => { | ||
return db().bulkDocs([ | ||
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor' } }, | ||
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean' } }, | ||
]); | ||
}).then(() => { | ||
return store().find('taco-soup', 'C'); | ||
}).then((found) => { | ||
found.set('flavor', 'pork'); | ||
return found.save(); | ||
}).then((saved) => { | ||
return db().get('tacoSoup_2_C'); | ||
}).then((updatedDoc) => { | ||
assert.equal(updatedDoc.data.flavor, 'pork', 'should have updated the attribute'); | ||
done(); | ||
}).catch((error) => { | ||
console.error('error in test', error); | ||
assert.ok(false, 'error in test:' + error); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('delete an existing record', function (assert) { | ||
assert.expect(1); | ||
|
||
var done = assert.async(); | ||
Ember.RSVP.Promise.resolve().then(() => { | ||
return db().bulkDocs([ | ||
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor' } }, | ||
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean' } }, | ||
]); | ||
}).then(() => { | ||
return store().find('taco-soup', 'C'); | ||
}).then((found) => { | ||
return found.destroyRecord(); | ||
}).then(() => { | ||
return db().get('tacoSoup_2_C'); | ||
}).then((doc) => { | ||
assert.ok(!doc, 'document should no longer exist'); | ||
}, (result) => { | ||
assert.equal(result.status, 404, 'document should no longer exist'); | ||
done(); | ||
}).catch((error) => { | ||
console.error('error in test', error); | ||
assert.ok(false, 'error in test:' + error); | ||
done(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we get a problem here when ED transitions to dasherized types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I initially used
type.modelName || type.typeKey
instead ofgetRecordTypeName
in this method for that reason. But the serialized data comes back with a camelized key (see line 155 below), and I figured it wouldn't hurt to use the camelized version in both places.ED has supported either dasherized or camelized names for lookups (
modelFor
,serializerFor
, etc) for a while — have you seen something that indicates that it is going to stop supporting camelized ones at some point?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this some more — these uses are conceptually different from
recordTypeName
, so it's probably best not to use it here. E.g., if you overridegetRecordTypeName
to provide your own scheme, the serializer is still going to return the data with a camelized key. And if the serializer changes in the future, it shouldn't affect the type name used in the record IDs. So I'll change this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 4b33181.