-
Notifications
You must be signed in to change notification settings - Fork 364
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
Add ObserverMixin to ModelBase typings #1823
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,90 @@ | ||
// 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 => {}); | ||
|
||
// Context is typed and provides `Model` property | ||
Data.observe('before save', async ctx => { | ||
console.log(ctx.Model.modelName); | ||
}); | ||
|
||
// ModelBaseClass can be assigned to `typeof ModelBase` | ||
// Please note that both `ModelBaseClass` and typeof ModelBase` | ||
// are different ways how to describe a class constructor of a model. | ||
// In this test we are verifying that the value returned by `createModel` | ||
// can be assigned to both types. | ||
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')); | ||
}); | ||
|
||
// ctx.Model is a PersistedModel class constructor | ||
Product.observe('before save', async ctx => { | ||
await ctx.Model.findOne(); | ||
}); | ||
|
||
// PersistedModelClass can be assigned to `typeof PersistedModel` | ||
// Please note that both `PersistedModelClass` and typeof PersistedModel` | ||
// are different ways how to describe a class constructor of a model. | ||
// In this test we are verifying that the value returned by `createModel` | ||
// can be assigned to both types. | ||
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 => {}); | ||
|
||
// ctx.Model is a KeyValueModel class constructor | ||
CacheItem.observe('before save', async ctx => { | ||
await ctx.Model.expire('key', 100); | ||
}); | ||
}); |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment that
ModelBaseClass
is an alias oftypeof ModelBase
and we are testing the alias here. thx.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See 6c38347, is my comment good enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is great. thx. But a tiny typo:
verifying that the value retunred