33import require from 'require' ;
44import config from 'ember-get-config' ;
55import assert from './assert' ;
6- import { hasEmberData , isDsModel } from 'ember-cli-mirage/utils/ember-data' ;
6+ import {
7+ hasEmberData ,
8+ isDsModel ,
9+ AppSymbolForEmberDataModels ,
10+ } from 'ember-cli-mirage/utils/ember-data' ;
711import { Model , belongsTo , hasMany } from 'miragejs' ;
812import EmberDataSerializer from 'ember-cli-mirage/serializers/ember-data-serializer' ;
913import { _utilsInflectorCamelize as camelize } from 'miragejs' ;
@@ -14,6 +18,19 @@ const { modulePrefix, podModulePrefix } = config;
1418let DsModels , Models ;
1519let DsSerializers , Serializers ;
1620
21+ function _getAppInstance ( ) {
22+ const application = window [ AppSymbolForEmberDataModels ] ;
23+ let appInstance = application . __deprecatedInstance__ ;
24+ // If an appInstance wasn't found (such as when running the tests)
25+ // then instantiate one manually, so we can use it to discover the
26+ // ember-data models in a way that doesn't trigger deprecations.
27+ if ( ! appInstance ) {
28+ application . _buildDeprecatedInstance ( ) ;
29+ appInstance = application . __deprecatedInstance__ ;
30+ }
31+ return appInstance ;
32+ }
33+
1734/**
1835 * Get all ember data models under the app's namespaces
1936 *
@@ -45,8 +62,19 @@ export function getDsModels() {
4562 path . match ( classicModelMatchRegex ) || path . match ( podModelMatchRegex ) ;
4663 if ( matches && matches [ 1 ] ) {
4764 let modelName = matches [ 1 ] ;
65+ let model = undefined ;
66+
67+ // Use the appInstance to lookup the models if provided, to avoid triggering
68+ // the ember-data:deprecate-early-static deprecation in ember-data
69+ const appInstance = _getAppInstance ( ) ;
70+ if ( appInstance ) {
71+ const modelNameExact = path . split ( '/models/' ) [ 1 ] ;
72+ const store = appInstance . lookup ( 'service:store' ) ;
73+ model = store . modelFor ( modelNameExact ) ;
74+ } else {
75+ model = require ( path , null , null , true ) . default ;
76+ }
4877
49- let model = require ( path , null , null , true ) . default ;
5078 if ( isDsModel ( model ) ) {
5179 DsModels [ modelName ] = model ;
5280 }
0 commit comments