@@ -222,14 +222,14 @@ export class Model {
222222 /**
223223 * Get the constructor for this model.
224224 */
225- get $self ( ) : typeof Model {
225+ $self ( ) : typeof Model {
226226 return this . constructor as typeof Model
227227 }
228228
229229 /**
230230 * Get the store instance.
231231 */
232- get $store ( ) : Store < any > {
232+ $store ( ) : Store < any > {
233233 assert ( this . _store !== undefined , [
234234 'A Vuex Store instance is not injected into the model instance.' ,
235235 'You might be trying to instantiate the model directly. Please use' ,
@@ -239,25 +239,34 @@ export class Model {
239239 return this . _store
240240 }
241241
242+ /**
243+ * Set the store instance.
244+ */
245+ $setStore ( store : Store < any > ) : this {
246+ this . _store = store
247+
248+ return this
249+ }
250+
242251 /**
243252 * Get the entity for this model.
244253 */
245- get $entity ( ) : string {
246- return this . $self . entity
254+ $entity ( ) : string {
255+ return this . $self ( ) . entity
247256 }
248257
249258 /**
250259 * Get the primary key for this model.
251260 */
252- get $primaryKey ( ) : string | string [ ] {
253- return this . $self . primaryKey
261+ $primaryKey ( ) : string | string [ ] {
262+ return this . $self ( ) . primaryKey
254263 }
255264
256265 /**
257266 * Get the model fields for this model.
258267 */
259- get $fields ( ) : ModelFields {
260- return this . $self . schemas [ this . $entity ]
268+ $fields ( ) : ModelFields {
269+ return this . $self ( ) . schemas [ this . $entity ( ) ]
261270 }
262271
263272 /**
@@ -266,9 +275,10 @@ export class Model {
266275 * during hydration through Query operations.
267276 */
268277 $newInstance ( attributes ?: Element , options ?: ModelOptions ) : this {
269- const model = new this . $self ( attributes , options ) as this
278+ const self = this . $self ( )
279+ const model = new self ( attributes , options ) as this
270280
271- model . $setStore ( this . $store )
281+ model . $setStore ( this . $store ( ) )
272282
273283 return model
274284 }
@@ -277,24 +287,15 @@ export class Model {
277287 * Create a new query instance.
278288 */
279289 $query ( ) : Query < this> {
280- return new Query ( this . $store , this )
281- }
282-
283- /**
284- * Set the store instance.
285- */
286- $setStore ( store : Store < any > ) : this {
287- this . _store = store
288-
289- return this
290+ return new Query ( this . $store ( ) , this )
290291 }
291292
292293 /**
293294 * Bootstrap this model.
294295 */
295296 protected $boot ( ) : void {
296- if ( ! this . $self . booted [ this . $entity ] ) {
297- this . $self . booted [ this . $entity ] = true
297+ if ( ! this . $self ( ) . booted [ this . $entity ( ) ] ) {
298+ this . $self ( ) . booted [ this . $entity ( ) ] = true
298299
299300 this . $initializeSchema ( )
300301 }
@@ -304,18 +305,19 @@ export class Model {
304305 * Build the schema by evaluating fields and registry.
305306 */
306307 protected $initializeSchema ( ) : void {
307- this . $self . initializeSchema ( )
308+ this . $self ( ) . initializeSchema ( )
308309 }
309310
310311 /**
311312 * Fill this model by the given attributes. Missing fields will be populated
312313 * by the attributes default value.
313314 */
314315 $fill ( attributes : Element = { } , options : ModelOptions = { } ) : this {
316+ const fields = this . $fields ( )
315317 const fillRelation = options . relations ?? true
316318
317- for ( const key in this . $ fields) {
318- const attr = this . $ fields[ key ]
319+ for ( const key in fields ) {
320+ const attr = fields [ key ]
319321 const value = attributes [ key ]
320322
321323 if ( attr instanceof Relation && ! fillRelation ) {
@@ -349,7 +351,7 @@ export class Model {
349351 * Get the primary key field name.
350352 */
351353 $getKeyName ( ) : string | string [ ] {
352- return this . $primaryKey
354+ return this . $primaryKey ( )
353355 }
354356
355357 /**
@@ -433,10 +435,10 @@ export class Model {
433435 * Get the relation instance for the given relation name.
434436 */
435437 $getRelation ( name : string ) : Relation {
436- const relation = this . $fields [ name ]
438+ const relation = this . $fields ( ) [ name ]
437439
438440 assert ( relation instanceof Relation , [
439- `Relationship [${ name } ] on model [${ this . $entity } ] not found.`
441+ `Relationship [${ name } ] on model [${ this . $entity ( ) } ] not found.`
440442 ] )
441443
442444 return relation
@@ -495,12 +497,12 @@ export class Model {
495497 $toJson ( model ?: Model , options : ModelOptions = { } ) : Element {
496498 model = model ?? this
497499
500+ const fields = model . $fields ( )
498501 const withRelation = options . relations ?? true
499-
500502 const record : Element = { }
501503
502- for ( const key in model . $ fields) {
503- const attr = this . $ fields[ key ]
504+ for ( const key in fields ) {
505+ const attr = fields [ key ]
504506 const value = model [ key ]
505507
506508 if ( ! ( attr instanceof Relation ) ) {
0 commit comments