Skip to content

Commit a779ac3

Browse files
authored
feat: remove database from model (#98)
1 parent 092c04d commit a779ac3

File tree

9 files changed

+4
-161
lines changed

9 files changed

+4
-161
lines changed

docs/guide/repository/deleting-data.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,3 @@ You can also run a delete statement on a set of records. In this example, we wil
6262
```js
6363
store.$repo(User).where('active', false).delete()
6464
```
65-
66-
## Deleting data by model instance method
67-
68-
You may delete a specific record with the model `$delete` instance method.
69-
70-
```js
71-
const user = store.$repo(User).find(1)
72-
73-
user.$delete()
74-
```

src/database/Database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class Database {
8989

9090
if (attr instanceof Relation) {
9191
attr.getRelateds().forEach((m) => {
92-
this.register(m.$setDatabase(this))
92+
this.register(m)
9393
})
9494
}
9595
}

src/model/Model.ts

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { isNullish, isArray, assert } from '../support/Utils'
22
import { Element, Item, Collection } from '../data/Data'
3-
import { Database } from '../database/Database'
4-
import { Query } from '../query/Query'
5-
import { NonEnumerable } from './decorators/NonEnumerable'
63
import { Attribute } from './attributes/Attribute'
74
import { Attr } from './attributes/types/Attr'
85
import { String as Str } from './attributes/types/String'
@@ -55,12 +52,6 @@ export class Model {
5552
*/
5653
protected static booted: Record<string, boolean> = {}
5754

58-
/**
59-
* The database instance.
60-
*/
61-
@NonEnumerable
62-
protected _database!: Database
63-
6455
/**
6556
* Create a new model instance.
6657
*/
@@ -263,28 +254,6 @@ export class Model {
263254
return this.constructor as typeof Model
264255
}
265256

266-
/**
267-
* Get the database instance.
268-
*/
269-
$database(): Database {
270-
assert(this._database !== undefined, [
271-
'A Vuex Store instance is not injected into the model instance.',
272-
'You might be trying to instantiate the model directly. Please use',
273-
'`repository.make` method to create a new model instance.'
274-
])
275-
276-
return this._database
277-
}
278-
279-
/**
280-
* Set the database instance.
281-
*/
282-
$setDatabase(database: Database): this {
283-
this._database = database
284-
285-
return this
286-
}
287-
288257
/**
289258
* Get the entity for this model.
290259
*/
@@ -315,18 +284,9 @@ export class Model {
315284
const self = this.$self()
316285
const model = new self(attributes, options) as this
317286

318-
model.$setDatabase(this.$database())
319-
320287
return model
321288
}
322289

323-
/**
324-
* Create a new query instance.
325-
*/
326-
$query(): Query<this> {
327-
return new Query(this.$database(), this)
328-
}
329-
330290
/**
331291
* Bootstrap this model.
332292
*/
@@ -503,37 +463,6 @@ export class Model {
503463
return this.$toJson(this, { relations: false })
504464
}
505465

506-
/**
507-
* Delete the model from the database.
508-
*/
509-
$delete(): boolean {
510-
const key = this.$getKeyName()
511-
512-
return isArray(key)
513-
? this.$deleteByCompositeKeyName(key)
514-
: this.$deleteByKeyName(key)
515-
}
516-
517-
/**
518-
* Delete the model from the database by ID.
519-
*/
520-
protected $deleteByKeyName(key: string): boolean {
521-
return !!this.$query().destroy(this[key])
522-
}
523-
524-
/**
525-
* Delete the model from the database by composite key.
526-
*/
527-
protected $deleteByCompositeKeyName(keys: string[]): boolean {
528-
const query = this.$query()
529-
530-
keys.forEach((key) => {
531-
query.where(key, this[key])
532-
})
533-
534-
return query.delete().length > 0
535-
}
536-
537466
/**
538467
* Serialize this model, or the given model, as POJO.
539468
*/

src/query/Query.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ export class Query<M extends Model = Model> {
8383
* Create a new query instance from the given relation.
8484
*/
8585
protected newQueryForRelation(relation: Relation): Query<Model> {
86-
return new Query(
87-
this.database,
88-
relation.getRelated().$setDatabase(this.database)
89-
)
86+
return new Query(this.database, relation.getRelated())
9087
}
9188

9289
/**

src/repository/Repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class Repository<M extends Model = Model> {
4949
initialize(model?: ModelConstructor<M>): this {
5050
// If there's a model passed in, just use that and return immediately.
5151
if (model) {
52-
this.model = model.newRawInstance().$setDatabase(this.database)
52+
this.model = model.newRawInstance()
5353

5454
return this
5555
}
@@ -59,7 +59,7 @@ export class Repository<M extends Model = Model> {
5959
// In this case, we'll check if the user has set model to the `use`
6060
// property and instantiate that.
6161
if (this.use) {
62-
this.model = (this.use.newRawInstance() as M).$setDatabase(this.database)
62+
this.model = this.use.newRawInstance() as M
6363

6464
return this
6565
}

test/feature/model/deletes_delete.spec.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/feature/model/deletes_delete_composite_key.spec.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/unit/model/Model.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ describe('unit/model/Model', () => {
88
@Attr() id!: number
99
}
1010

11-
it('throws when accessing the database but it is not injected', () => {
12-
expect(() => new User().$database()).toThrow()
13-
})
14-
1511
it('ignores unkown field when filling the model', () => {
1612
const user = new User({ id: 1, name: 'John Doe' })
1713

test/unit/repository/Repository.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe('unit/repository/Repository', () => {
2222
const user = store.$repo(User).make()
2323

2424
expect(user).toBeInstanceOf(User)
25-
expect(user.$database()).toBe(store.$database)
2625
assertModel(user, { id: null, name: 'John Doe' })
2726
})
2827

@@ -33,8 +32,6 @@ describe('unit/repository/Repository', () => {
3332
const user = store.$repo(User, connection).make()
3433

3534
expect(user).toBeInstanceOf(User)
36-
expect(user.$database()).toBe(store.$databases[connection])
37-
expect(user.$database().started).toBe(true)
3835
assertModel(user, { id: null, name: 'John Doe' })
3936

4037
// Fetches the same atabase on 2nd call.
@@ -51,7 +48,6 @@ describe('unit/repository/Repository', () => {
5148
})
5249

5350
expect(user).toBeInstanceOf(User)
54-
expect(user.$database()).toBe(store.$database)
5551
assertModel(user, { id: 1, name: 'Jane Doe' })
5652
})
5753

0 commit comments

Comments
 (0)