Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Database from Model #98

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions docs/guide/repository/deleting-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,3 @@ You can also run a delete statement on a set of records. In this example, we wil
```js
store.$repo(User).where('active', false).delete()
```

## Deleting data by model instance method

You may delete a specific record with the model `$delete` instance method.

```js
const user = store.$repo(User).find(1)

user.$delete()
```
2 changes: 1 addition & 1 deletion src/database/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Database {

if (attr instanceof Relation) {
attr.getRelateds().forEach((m) => {
this.register(m.$setDatabase(this))
this.register(m)
})
}
}
Expand Down
71 changes: 0 additions & 71 deletions src/model/Model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { isNullish, isArray, assert } from '../support/Utils'
import { Element, Item, Collection } from '../data/Data'
import { Database } from '../database/Database'
import { Query } from '../query/Query'
import { NonEnumerable } from './decorators/NonEnumerable'
import { Attribute } from './attributes/Attribute'
import { Attr } from './attributes/types/Attr'
import { String as Str } from './attributes/types/String'
Expand Down Expand Up @@ -54,12 +51,6 @@ export class Model {
*/
protected static booted: Record<string, boolean> = {}

/**
* The database instance.
*/
@NonEnumerable
protected _database!: Database

/**
* Create a new model instance.
*/
Expand Down Expand Up @@ -246,28 +237,6 @@ export class Model {
return this.constructor as typeof Model
}

/**
* Get the database instance.
*/
$database(): Database {
assert(this._database !== undefined, [
'A Vuex Store instance is not injected into the model instance.',
'You might be trying to instantiate the model directly. Please use',
'`repository.make` method to create a new model instance.'
])

return this._database
}

/**
* Set the database instance.
*/
$setDatabase(database: Database): this {
this._database = database

return this
}

/**
* Get the entity for this model.
*/
Expand Down Expand Up @@ -298,18 +267,9 @@ export class Model {
const self = this.$self()
const model = new self(attributes, options) as this

model.$setDatabase(this.$database())

return model
}

/**
* Create a new query instance.
*/
$query(): Query<this> {
return new Query(this.$database(), this)
}

/**
* Bootstrap this model.
*/
Expand Down Expand Up @@ -486,37 +446,6 @@ export class Model {
return this.$toJson(this, { relations: false })
}

/**
* Delete the model from the database.
*/
$delete(): boolean {
const key = this.$getKeyName()

return isArray(key)
? this.$deleteByCompositeKeyName(key)
: this.$deleteByKeyName(key)
}

/**
* Delete the model from the database by ID.
*/
protected $deleteByKeyName(key: string): boolean {
return !!this.$query().destroy(this[key])
}

/**
* Delete the model from the database by composite key.
*/
protected $deleteByCompositeKeyName(keys: string[]): boolean {
const query = this.$query()

keys.forEach((key) => {
query.where(key, this[key])
})

return query.delete().length > 0
}

/**
* Serialize this model, or the given model, as POJO.
*/
Expand Down
5 changes: 1 addition & 4 deletions src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ export class Query<M extends Model = Model> {
* Create a new query instance from the given relation.
*/
protected newQueryForRelation(relation: Relation): Query<Model> {
return new Query(
this.database,
relation.getRelated().$setDatabase(this.database)
)
return new Query(this.database, relation.getRelated())
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Repository<M extends Model = Model> {
initialize(model?: ModelConstructor<M>): this {
// If there's a model passed in, just use that and return immediately.
if (model) {
this.model = model.newRawInstance().$setDatabase(this.database)
this.model = model.newRawInstance()

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

return this
}
Expand Down
31 changes: 0 additions & 31 deletions test/feature/model/deletes_delete.spec.ts

This file was deleted.

34 changes: 0 additions & 34 deletions test/feature/model/deletes_delete_composite_key.spec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions test/unit/model/Model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ describe('unit/model/Model', () => {
@Attr() id!: number
}

it('throws when accessing the database but it is not injected', () => {
expect(() => new User().$database()).toThrow()
})
Comment on lines -11 to -13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The database is no longer injected into the model so we no longer need this test.


it('ignores unkown field when filling the model', () => {
const user = new User({ id: 1, name: 'John Doe' })

Expand Down
4 changes: 0 additions & 4 deletions test/unit/repository/Repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('unit/repository/Repository', () => {
const user = store.$repo(User).make()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing expectations of injected databases into the Model.


expect(user).toBeInstanceOf(User)
expect(user.$database()).toBe(store.$database)
assertModel(user, { id: null, name: 'John Doe' })
})

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

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

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

expect(user).toBeInstanceOf(User)
expect(user.$database()).toBe(store.$database)
assertModel(user, { id: 1, name: 'Jane Doe' })
})

Expand Down