Skip to content

Commit 6e9f4f0

Browse files
committed
refactor: make model $ getters to be functions (#34)
close #34
1 parent 5ac7af2 commit 6e9f4f0

File tree

15 files changed

+92
-67
lines changed

15 files changed

+92
-67
lines changed

src/database/Database.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ export class Database {
6565
* Register the given model.
6666
*/
6767
register<M extends Model>(model: M): void {
68-
if (!this.models[model.$entity]) {
69-
this.models[model.$entity] = model
68+
const entity = model.$entity()
69+
70+
if (!this.models[entity]) {
71+
this.models[entity] = model
7072

7173
this.createModule(model)
7274

@@ -80,8 +82,10 @@ export class Database {
8082
* Register all related models.
8183
*/
8284
private registerRelatedModels<M extends Model>(model: M): void {
83-
for (const name in model.$fields) {
84-
const attr = model.$fields[name]
85+
const fields = model.$fields()
86+
87+
for (const name in fields) {
88+
const attr = fields[name]
8589

8690
if (attr instanceof Relation) {
8791
attr.getRelateds().forEach((m) => {
@@ -118,10 +122,11 @@ export class Database {
118122
* Create sub module.
119123
*/
120124
private createModule<M extends Model>(model: M): void {
121-
const preserveState = !!this.store.state[this.connection][model.$entity]
125+
const entity = model.$entity()
126+
const preserveState = !!this.store.state[this.connection][entity]
122127

123128
this.store.registerModule(
124-
[this.connection, model.$entity],
129+
[this.connection, entity],
125130
{
126131
namespaced: true,
127132
state: this.createState(),
@@ -151,6 +156,6 @@ export class Database {
151156
* Create schema from the given model.
152157
*/
153158
private createSchema<M extends Model>(model: M): Normalizr.Entity {
154-
return (this.schemas[model.$entity] = new Schema(model).one())
159+
return (this.schemas[model.$entity()] = new Schema(model).one())
155160
}
156161
}

src/interpreter/Interpreter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ export class Interpreter<M extends Model> {
4343
* Get the schema from the database.
4444
*/
4545
private getSchema(): Normalizr.Entity {
46-
return this.store.$database.getSchema(this.model.$entity)
46+
return this.store.$database.getSchema(this.model.$entity())
4747
}
4848
}

src/model/Model.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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)) {

src/model/decorators/attributes/relations/BelongsTo.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export function BelongsTo(
1010
ownerKey?: string
1111
): PropertyDecorator {
1212
return (target, propertyKey) => {
13-
target.$self.setRegistry(propertyKey, () =>
14-
target.$self.belongsTo(related(), foreignKey, ownerKey)
13+
const self = target.$self()
14+
15+
self.setRegistry(propertyKey, () =>
16+
self.belongsTo(related(), foreignKey, ownerKey)
1517
)
1618
}
1719
}

src/model/decorators/attributes/relations/HasMany.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export function HasMany(
1010
localKey?: string
1111
): PropertyDecorator {
1212
return (target, propertyKey) => {
13-
target.$self.setRegistry(propertyKey, () =>
14-
target.$self.hasMany(related(), foreignKey, localKey)
13+
const self = target.$self()
14+
15+
self.setRegistry(propertyKey, () =>
16+
self.hasMany(related(), foreignKey, localKey)
1517
)
1618
}
1719
}

src/model/decorators/attributes/relations/HasOne.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export function HasOne(
1010
localKey?: string
1111
): PropertyDecorator {
1212
return (target, propertyKey) => {
13-
target.$self.setRegistry(propertyKey, () =>
14-
target.$self.hasOne(related(), foreignKey, localKey)
13+
const self = target.$self()
14+
15+
self.setRegistry(propertyKey, () =>
16+
self.hasOne(related(), foreignKey, localKey)
1517
)
1618
}
1719
}

src/model/decorators/attributes/types/Attr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { PropertyDecorator } from '../../Contracts'
55
*/
66
export function Attr(value?: any): PropertyDecorator {
77
return (target, propertyKey) => {
8-
target.$self.setRegistry(propertyKey, () => target.$self.attr(value))
8+
const self = target.$self()
9+
10+
self.setRegistry(propertyKey, () => self.attr(value))
911
}
1012
}

src/model/decorators/attributes/types/Bool.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export function Bool(
88
options: TypeOptions = {}
99
): PropertyDecorator {
1010
return (target, propertyKey) => {
11-
target.$self.setRegistry(propertyKey, () => {
12-
const attr = target.$self.boolean(value)
11+
const self = target.$self()
12+
13+
self.setRegistry(propertyKey, () => {
14+
const attr = self.boolean(value)
1315

1416
if (options.nullable) {
1517
attr.nullable()

src/model/decorators/attributes/types/Num.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export function Num(
88
options: TypeOptions = {}
99
): PropertyDecorator {
1010
return (target, propertyKey) => {
11-
target.$self.setRegistry(propertyKey, () => {
12-
const attr = target.$self.number(value)
11+
const self = target.$self()
12+
13+
self.setRegistry(propertyKey, () => {
14+
const attr = self.number(value)
1315

1416
if (options.nullable) {
1517
attr.nullable()

src/model/decorators/attributes/types/Str.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export function Str(
88
options: TypeOptions = {}
99
): PropertyDecorator {
1010
return (target, propertyKey) => {
11-
target.$self.setRegistry(propertyKey, () => {
12-
const attr = target.$self.string(value)
11+
const self = target.$self()
12+
13+
self.setRegistry(propertyKey, () => {
14+
const attr = self.string(value)
1315

1416
if (options.nullable) {
1517
attr.nullable()

0 commit comments

Comments
 (0)