-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for operation hooks. Signed-off-by: Hage Yaapa <hage.yaapa@in.ibm.com>
- Loading branch information
Hage Yaapa
committed
Mar 5, 2020
1 parent
cd89ad4
commit 7444da9
Showing
3 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/repository/src/__tests__/acceptance/operation-hooks.acceptance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/repository | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {expect} from '@loopback/testlab'; | ||
import {DataSource} from 'loopback-datasource-juggler'; | ||
import {DefaultCrudRepository, juggler} from '../..'; | ||
import {Product, ProductRelations} from '../fixtures/models/product.model'; | ||
|
||
// This test shows the recommended way how to use @loopback/repository | ||
// together with existing connectors when building LoopBack applications | ||
describe('Operation hooks', () => { | ||
let repo: ProductRepository; | ||
beforeEach(givenProductRepository); | ||
|
||
const beforeSave = 'before save'; | ||
const afterSave = 'after save'; | ||
const expectedArray = [beforeSave, afterSave]; | ||
|
||
it('supports operation hooks', async () => { | ||
await repo.create({slug: 'pencil'}); | ||
expect(repo.hooksCalled).to.eql(expectedArray); | ||
}); | ||
|
||
function givenProductRepository() { | ||
const db = new DataSource({ | ||
connector: 'memory', | ||
}); | ||
|
||
repo = new ProductRepository(db); | ||
} | ||
|
||
class ProductRepository extends DefaultCrudRepository< | ||
Product, | ||
typeof Product.prototype.id, | ||
ProductRelations | ||
> { | ||
constructor(dataSource: juggler.DataSource) { | ||
super(Product, dataSource); | ||
} | ||
|
||
hooksCalled: string[] = []; | ||
|
||
definePersistedModel(entityClass: typeof Product) { | ||
const modelClass = super.definePersistedModel(entityClass); | ||
modelClass.observe(beforeSave, async ctx => { | ||
this.hooksCalled.push(beforeSave); | ||
}); | ||
|
||
modelClass.observe(afterSave, async ctx => { | ||
this.hooksCalled.push(afterSave); | ||
}); | ||
return modelClass; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters