Skip to content

Commit 46a39d9

Browse files
author
Kyle Jones
committed
Initial release version.
1 parent 1675943 commit 46a39d9

File tree

8 files changed

+140
-32
lines changed

8 files changed

+140
-32
lines changed

.editorconfig

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,10 @@ charset = utf-8
1111
trim_trailing_whitespace = true
1212
insert_final_newline = true
1313
indent_style = space
14-
indent_size = 2
15-
16-
[*.js]
17-
indent_style = space
18-
indent_size = 2
14+
indent_size = 4
1915

2016
[*.hbs]
2117
insert_final_newline = false
22-
indent_style = space
23-
indent_size = 2
24-
25-
[*.css]
26-
indent_style = space
27-
indent_size = 2
28-
29-
[*.html]
30-
indent_style = space
31-
indent_size = 2
3218

3319
[*.{diff,md}]
3420
trim_trailing_whitespace = false

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
# Ember-cli-multi-store-service
22

3-
This README outlines the details of collaborating on this Ember addon.
3+
This is a fairly simple Ember CLI addon for managing multiple Ember Data stores. Normally one store is all that an application needs, but sometimes life throws you curve balls.
44

5-
## Installation
5+
## Why would I need multiple stores?
66

7-
* `git clone` this repository
8-
* `npm install`
9-
* `bower install`
7+
- Loading fixture data in an easy to swap out area
8+
- Managing the state of multiple isolated databases
9+
- Working with historical data snapshots
10+
- Crazy situations I've never thought of
1011

11-
## Running
12+
## Are there any rules I need to follow to use this?
1213

13-
* `ember server`
14-
* Visit your app at http://localhost:4200.
14+
Not too many, but I have a few recommendations based on experience using multiple stores:
1515

16-
## Running Tests
16+
- Stop using the injected store.
17+
- Either pass down the store that you want to use explicitly as an attr, or inject this service (`service:multi-store`) and grab the store you need by name as needed.
18+
- Seriously, don't inject the store into anything else like adapters or serializers
19+
- At this point the store that you use will pass itself to adapters and serializers, build them in a stateless fashion that only use the correct store passed in.
20+
- Each store from this service has a `name` attribute to be used for directing requests as needed at call time.
21+
- Use this in moderation
22+
- I know I'm not selling this well, but it should really only be useful when you need total data isolation and/or expect to see multiple conflicting versions of the same objects (same id and type).
1723

18-
* `npm test` (Runs `ember try:testall` to test your addon against multiple Ember versions)
19-
* `ember test`
20-
* `ember test --server`
24+
## How does this play with the Ember Inspector?
2125

22-
## Building
26+
Not too bad, at least as far as I've tested it. There is a `switchInspectorStore()` method on the service that will let you change which store you're viewing information on. You'll have to change views away from the data section to see the change as it's not dynamically watching for the store change. This also uses an undocumented and private API for the inspector. Don't be surprised if it stops working. Instead just open a new issue and I'll take a look at it
2327

24-
* `ember build`
28+
## How about some usage documentation?
2529

26-
For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/).
30+
I'll do that soon enough.

addon/.gitkeep

Whitespace-only changes.

addon/services/multi-store.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import Ember from 'ember';
2+
import DS from 'ember-data';
3+
import getOwner from 'ember-getowner-polyfill';
4+
5+
export default Ember.Service.extend({
6+
/**
7+
* This is an observable array for keeping track of stores.
8+
*
9+
* @member {Ember.NativeArray}
10+
*/
11+
storeNames: null,
12+
13+
/**
14+
* Init function, just initialize the list of store names.
15+
*/
16+
init() {
17+
Ember.set(this, 'storeNames', Ember.A());
18+
this._super(...arguments);
19+
},
20+
21+
/**
22+
* Check is a store name is registered.
23+
*
24+
* @function isStoreRegistered
25+
* @param {string} name - The name of the store
26+
* @returns {boolean}
27+
*/
28+
isStoreRegistered(name) {
29+
return Ember.get(this, 'storeNames').indexOf(name) !== -1;
30+
},
31+
32+
/**
33+
* Register a new store name.
34+
*
35+
* @function registerStore
36+
* @param {string} name - The name of the store
37+
* @returns {boolean}
38+
*/
39+
registerStore(name) {
40+
const storeNames = Ember.get(this, 'storeNames');
41+
42+
if (storeNames.indexOf(name) === -1) {
43+
getOwner(this).register(`store:${name}`,
44+
DS.Store.extend({
45+
name: name
46+
})
47+
);
48+
storeNames.pushObject(name);
49+
return true;
50+
}
51+
return false;
52+
},
53+
54+
/**
55+
* Unregister a store name.
56+
*
57+
* @function unregisterStore
58+
* @param {string} name - The name of the store
59+
* @returns {boolean}
60+
*/
61+
unregisterStore(name) {
62+
const storeNames = Ember.get(this, 'storeNames');
63+
64+
if (storeNames.indexOf(name) !== -1) {
65+
getOwner(this).unregister(`store:${name}`);
66+
storeNames.removeObject(name);
67+
return true;
68+
}
69+
return false;
70+
},
71+
72+
/**
73+
* Get a registered store by name.
74+
*
75+
* @function getStore
76+
* @param {string} name - The name of the store
77+
* @returns {boolean}
78+
*/
79+
getStore(name) {
80+
return getOwner(this).lookup(`store:${name}`);
81+
},
82+
83+
/**
84+
* Change the store that the Ember Inspector uses.
85+
*
86+
* NOTE: This will probably stop working on some Ember Data update, but it should only be used for debugging anyway.
87+
*
88+
* @function switchInspectorStore
89+
* @param {string} [name] - The name of the store
90+
*/
91+
switchInspectorStore(name) {
92+
const store = name ?
93+
this.getStore(name) :
94+
getOwner(this).lookup('service:store');
95+
96+
let dataAdapter = getOwner(this).lookup('data-adapter:main');
97+
dataAdapter.set('store', store);
98+
}
99+
});

app/.gitkeep

Whitespace-only changes.

app/services/multi-store.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-cli-multi-store-service/services/multi-store';

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ember-cli-multi-store-service",
33
"version": "0.0.0",
4-
"description": "The default blueprint for ember-cli addons.",
4+
"description": "A service for managing multiple Ember Data stores in an Ember CLI app.",
55
"directories": {
66
"doc": "doc",
77
"test": "tests"
@@ -34,13 +34,17 @@
3434
"ember-disable-prototype-extensions": "^1.1.0",
3535
"ember-disable-proxy-controllers": "^1.0.1",
3636
"ember-export-application-global": "^1.0.4",
37+
"ember-getowner-polyfill": "1.0.0",
3738
"ember-load-initializers": "^0.5.0",
3839
"ember-resolver": "^2.0.3",
3940
"ember-try": "^0.1.2",
4041
"loader.js": "^4.0.0"
4142
},
4243
"keywords": [
43-
"ember-addon"
44+
"ember-addon",
45+
"ember-data-addon",
46+
"ember-data",
47+
"store"
4448
],
4549
"dependencies": {
4650
"ember-cli-babel": "^5.1.5"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('service:multi-store', 'Unit | Service | multi store', {});
4+
5+
test('it exists', function(assert) {
6+
let service = this.subject();
7+
assert.ok(service);
8+
});
9+
10+
test('it starts empty', function(assert) {
11+
let service = this.subject();
12+
assert.deepEqual([], service.get('storeNames'));
13+
assert.notOk(service.getStore('foo'));
14+
});

0 commit comments

Comments
 (0)