-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ObserverMixin to PersistedModelClass typings
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
coverage | ||
dist |
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 |
---|---|---|
|
@@ -12,3 +12,4 @@ npm-debug.log | |
.travis.yml | ||
.nyc_output | ||
dist | ||
types/__test__.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,67 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: loopback-datasource-juggler | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
// A test file to verify types described by our .d.ts files. | ||
// The code in this file is only compiled, we don't run it via Mocha. | ||
|
||
import { | ||
DataSource, | ||
KeyValueModel, | ||
ModelBase, | ||
ModelBaseClass, | ||
PersistedModel, | ||
PersistedModelClass, | ||
} from '..'; | ||
|
||
const db = new DataSource('db', {connector: 'memory'}); | ||
|
||
//------- | ||
// ModelBase should provide ObserverMixin APIs as static methods | ||
//------- | ||
// | ||
(function() { | ||
const Data = db.createModel('Data'); | ||
|
||
// An operation hook can be installed | ||
Data.observe('before save', async ctx => {}); | ||
|
||
// ModelBaseClass can be assigned to `typeof ModelBase` | ||
const modelTypeof: typeof ModelBase = Data; | ||
const modelCls: ModelBaseClass = modelTypeof; | ||
}); | ||
|
||
//------- | ||
// PersistedModel should provide ObserverMixin APIs as static methods | ||
//------- | ||
(function () { | ||
const Product = db.createModel<PersistedModelClass>( | ||
'Product', | ||
{name: String}, | ||
{strict: true} | ||
); | ||
|
||
// It accepts async function | ||
Product.observe('before save', async ctx => {}); | ||
|
||
// It accepts callback-based function | ||
Product.observe('before save', (ctx, next) => { | ||
next(new Error('test error')); | ||
}); | ||
|
||
// PersistedModelClass can be assigned to `typeof PersistedModel` | ||
const modelTypeof: typeof PersistedModel = Product; | ||
const modelCls: PersistedModelClass = modelTypeof; | ||
}); | ||
|
||
//------- | ||
// KeyValueModel should provide ObserverMixin APIs as static methods | ||
//------- | ||
(function () { | ||
const kvdb = new DataSource({connector: 'kv-memory'}); | ||
const CacheItem = kvdb.createModel<typeof KeyValueModel>('CacheItem'); | ||
|
||
// An operation hook can be installed | ||
CacheItem.observe('before save', async ctx => {}); | ||
}); |
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