Skip to content

Commit

Permalink
fix(model): delete method should be synchronous (#76) (#77)
Browse files Browse the repository at this point in the history
fix #76

In addition, tests are revised to reflect the synchronous behavior.
  • Loading branch information
cuebit committed Jun 8, 2021
1 parent e115503 commit 4fb4673
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export class Model {
/**
* Delete the model from the database.
*/
async $delete(): Promise<boolean> {
$delete(): boolean {
const key = this.$getKeyName()

return isArray(key)
Expand All @@ -500,21 +500,21 @@ export class Model {
/**
* Delete the model from the database by ID.
*/
protected async $deleteByKeyName(key: string): Promise<boolean> {
return !!(await this.$query().destroy(this[key]))
protected $deleteByKeyName(key: string): boolean {
return !!this.$query().destroy(this[key])
}

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

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

return (await query.delete()).length > 0
return query.delete().length > 0
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/feature/model/deletes_delete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('feature/model/deletes_delete', () => {

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

const result = await user.$delete()
const result = user.$delete()

expect(result).toBe(true)

Expand Down
2 changes: 1 addition & 1 deletion test/feature/model/deletes_delete_composite_key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('feature/model/deletes_delete_composite_key', () => {

const user = store.$repo(User).where('idA', 1).where('idB', 2).first()!

const result = await user.$delete()
const result = user.$delete()

expect(result).toBe(true)

Expand Down
4 changes: 2 additions & 2 deletions test/feature/relations/has_many_save.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('feature/relations/has_many_save', () => {
it('generates missing foreign key', async () => {
const store = createStore()

await store.$repo(User).save({
store.$repo(User).save({
id: 1,
name: 'John Doe',
posts: [
Expand All @@ -76,7 +76,7 @@ describe('feature/relations/has_many_save', () => {
it('can insert a record with missing relational key', async () => {
const store = createStore()

await store.$repo(User).save({
store.$repo(User).save({
id: 1,
name: 'John Doe'
})
Expand Down

0 comments on commit 4fb4673

Please sign in to comment.