Skip to content

Commit

Permalink
Merge pull request miragejs#2542 from miragejs/ember-data-v5-compat-d…
Browse files Browse the repository at this point in the history
…iscoverEmberDataModels

feat: access Ember Data models via Store service
  • Loading branch information
SergeAstapov authored Oct 4, 2023
2 parents 7c0b1d2 + fec9ce8 commit 7d49476
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
12 changes: 7 additions & 5 deletions packages/ember-cli-mirage/addon/ember-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ let DsSerializers, Serializers;
* @method getDsModels
* @private
* @hide
* @param {StoreService} store
* @return {Object} models
*/
export function getDsModels() {
export function getDsModels(store) {
if (DsModels) {
return DsModels;
}
Expand All @@ -46,7 +47,7 @@ export function getDsModels() {
if (matches && matches[1]) {
let modelName = matches[1];

let model = require(path, null, null, true).default;
let model = store.modelFor(modelName);
if (isDsModel(model)) {
DsModels[modelName] = model;
}
Expand All @@ -60,14 +61,15 @@ export function getDsModels() {
* Get all mirage models for each of the ember-data models
*
* @method discoverEmberDataModels
* @param {StoreService} store
* @return {Object} models
*/
export function discoverEmberDataModels() {
if (Models) {
export function discoverEmberDataModels(store) {
if (Models || !store) {
return Models;
}

let emberDataModels = getDsModels();
let emberDataModels = getDsModels(store);
Models = {};

Object.keys(emberDataModels).forEach((modelName) => {
Expand Down
1 change: 1 addition & 0 deletions packages/ember-cli-mirage/addon/start-mirage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function startMirage(owner, { env, makeServer } = {}) {

let options = Object.assign(modules, {
environment,
store: owner.lookup('service:store'),
});

options.inflector = { singularize, pluralize };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export default function (config) {
let finalConfig = {
...config,
// Remove discoverEmberDataModels if you do not want ember-cli-mirage to auto discover the ember models
models: { ...discoverEmberDataModels(), ...config.models },
models: {
...discoverEmberDataModels(config.store),
...config.models
},
// uncomment to opt into ember-cli-mirage to auto discover ember serializers
// serializers: applyEmberDataSerializers(config.serializers),
routes,
Expand Down
8 changes: 4 additions & 4 deletions packages/ember-cli-mirage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"devDependencies": {
"@babel/eslint-parser": "^7.22.15",
"@babel/plugin-proposal-decorators": "^7.23.0",
"@ember-data/model": "~4.11.3",
"@ember-data/store": "~4.11.3",
"@ember-data/tracking": "~4.11.3",
"@ember-data/model": "~5.3.0",
"@ember-data/store": "~5.3.0",
"@ember-data/tracking": "~5.3.0",
"@ember/jquery": "^2.0.0",
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
Expand All @@ -77,7 +77,7 @@
"ember-cli-moment-shim": "^3.8.0",
"ember-cli-sri": "^2.1.1",
"ember-cli-terser": "^4.0.2",
"ember-data": "~4.11.3",
"ember-data": "~5.3.0",
"ember-fetch": "^8.1.2",
"ember-load-initializers": "^2.1.2",
"ember-moment": "^9.0.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/ember-cli-mirage/tests/dummy/config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = async function () {
name: 'ember-lts-4.4',
npm: {
devDependencies: {
'ember-data': '~4.4.0',
'ember-source': '~4.4.0',
},
},
Expand All @@ -32,6 +33,7 @@ module.exports = async function () {
name: 'ember-lts-4.8',
npm: {
devDependencies: {
'ember-data': '~4.8.0',
'ember-source': '~4.8.0',
},
},
Expand All @@ -40,6 +42,7 @@ module.exports = async function () {
name: 'ember-lts-4.12',
npm: {
devDependencies: {
'ember-data': '~4.12.0',
'ember-source': '~4.12.0',
},
},
Expand Down
5 changes: 4 additions & 1 deletion packages/ember-cli-mirage/tests/dummy/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { createServer } from 'miragejs';
export default function (config) {
let finalConfig = {
...config,
models: { ...discoverEmberDataModels(), ...config.models },
models: {
...discoverEmberDataModels(config.store),
...config.models,
},
serializers: applyEmberDataSerializers(config.serializers),
routes,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ module('Unit | Model | ember data model discover', function (hooks) {
let server;

hooks.beforeEach(function () {
const store = this.owner.lookup('service:store');
server = createServer({
models: discoverEmberDataModels({}),
models: discoverEmberDataModels(store),
});
});

Expand Down

0 comments on commit 7d49476

Please sign in to comment.