-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
RFC: use LB3 models in an LB4 app [SPIKE - DO NOT MERGE] #2274
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3302a9f
feat(v3compat): initial commit
bajtos eb868ee
feat(v3compat): model registration and JavaScript DAO API
bajtos ec441a2
feat(v3compat): add minimum strong-remoting infrastructure
bajtos 4ddb7ab
feat(v3compat): define remoting metadata for some CRUD methods
bajtos 2893a49
docs: add working notes for the spike
bajtos fa06c1c
feat(v3compat): rest adapter
bajtos f9843eb
feat(v3compat): request body parameters
bajtos cee5f84
feat(v3compat): bind datasources in Context
bajtos d1348e8
feat(v3compat): boot LB3 models
bajtos 8309591
fix(v3compat): fail when attaching a model to an unknown datasource
bajtos cb4a147
feat(example-lb3models): initial commit
bajtos e0413f7
feat(v3compat): support callback-based remote methods
bajtos 63f46b5
feat(v3compat): use a more sensible default root for LB3 model booter
bajtos de3529b
docs(v3compat): add developer and spike documentation
bajtos 0465a94
fix: make "npm test" pass
bajtos 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 |
---|---|---|
|
@@ -6,32 +6,36 @@ How to test code migrated from strong-remoting and loopback (v3)? Do we want to | |
copy existing tests over? Migrate them to async/await style? Don't bother with | ||
testing at all, use few acceptance-level tests only? | ||
|
||
How to split 1k+ lines of new (migrated) code into smaller chunks that will be | ||
How to split 2k+ lines of new (migrated) code into smaller chunks that will be | ||
easier to review? | ||
|
||
TODO: | ||
|
||
1. expose remote methods via REST | ||
2. boot | ||
Should we register LB3 Models for dependency injection into LB4 code? Register | ||
them as repositories, models, services, or something else? | ||
|
||
## Should have/next iterations: | ||
|
||
- PersistedModel - all CRUD APIs | ||
- register Models and DataSources for dependency injection? | ||
- allow LB3 models to be attached to LB4 dataSources? | ||
- pick up models/methods added after app start | ||
- hasUpdateOnlyProps (different request body schema for "create" method) | ||
- set options from HTTP context (`http: 'optionsFromRequest'`) | ||
- `{rest: {after: convertNullToNotFoundError}}` | ||
- mixins | ||
- case-insensitive URL paths (?) (/Todo is same as /todo) | ||
- model-sources and mixin-sources in model-config.json | ||
|
||
On aside | ||
|
||
## Model | ||
- https://github.com/Microsoft/TypeScript/issues/6480 | ||
- extract the Booter contract into a standalone package so that v3compat does | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain what 'Booter contract' is in this case? EDIT: I kind of have an idea what it is (i.e. a booter class). |
||
not have to inherit entire boot | ||
|
||
### Model | ||
|
||
- beforeRemote/afterRemote/afterRemoteError | ||
- disableRemoteMethodByName(name) | ||
|
||
## HttpContext | ||
### HttpContext | ||
|
||
- method | ||
- req | ||
- res | ||
|
@@ -40,13 +44,15 @@ TODO: | |
- methodString | ||
- result | ||
|
||
## SharedMethod | ||
### SharedMethod | ||
|
||
- isMethodEnabled(sharedMethod) | ||
- resolve(resolver) | ||
- findMethodByName | ||
- disableMethodByName | ||
|
||
### Application | ||
|
||
- app.connector(name, connector) | ||
- app.connectors.{name} | ||
|
||
|
@@ -74,10 +80,12 @@ TODO: | |
|
||
### Remoting features | ||
|
||
- respond with a Buffer, respond with a ReadableStream | ||
- respond with a Buffer, respond with a ReadableStream | ||
|
||
## Won't have | ||
## WILL NOT HAVE | ||
|
||
- allow LB3 models to be attached to LB4 dataSources. This won't work | ||
because LB3 requires all datasources to share the same ModelBuilder | ||
- CLS-based context | ||
- global registry: loopback.createModel, loopback.findModel, etc. | ||
- Model.checkAccess(token, modelId, sharedMethod, ctx, cb) | ||
|
@@ -102,6 +110,7 @@ TODO: | |
- fillCustomChangeProperties | ||
|
||
built-in models | ||
|
||
- Access-token | ||
- Acl | ||
- Application | ||
|
@@ -114,13 +123,16 @@ built-in models | |
- User | ||
|
||
SharedClass | ||
|
||
- find: was already deprecated | ||
- disableMethod: was already deprecated | ||
|
||
SharedMethod | ||
|
||
- prototype.invoke(scope, args, remotingOptions, ctx, cb) | ||
|
||
HttpContext | ||
|
||
- ~~typeRegistry~~ | ||
- ~~supportedTypes~~ | ||
- invoke | ||
|
@@ -134,6 +146,11 @@ HttpContext | |
- (etc.) | ||
|
||
Remoting | ||
|
||
- XML | ||
- JSON API | ||
- piping retval of remote function into response | ||
|
||
Booting | ||
|
||
- datasources |
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,6 @@ | ||
{ | ||
"Todo": { | ||
"dataSource": "db", | ||
"public": true | ||
} | ||
} |
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,11 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/v3compat | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
module.exports = function(Todo) { | ||
// A dummy custom method to verify that model JS file was correctly processed | ||
Todo.findByTitle = function(title) { | ||
return this.find({where: {title}}); | ||
}; | ||
}; |
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,11 @@ | ||
{ | ||
"name": "Todo", | ||
"base": "PersistedModel", | ||
"strict": false, | ||
"properties": { | ||
"title": { | ||
"type": "string", | ||
"required": true | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# v3compat/boot | ||
|
||
Source code migrated from | ||
[loopback-boot](https://github.com/strongloop/loopback-boot). |
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,6 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/v3compat | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export * from './lb3-model-booter'; |
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,121 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/v3compat | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {BootBindings, Booter, discoverFiles} from '@loopback/boot'; | ||
import {Application, CoreBindings, inject} from '@loopback/core'; | ||
import * as debugFactory from 'debug'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import {promisify} from 'util'; | ||
import {Lb3Application} from '../core'; | ||
|
||
const fileExists = promisify(fs.exists); | ||
|
||
const debug = debugFactory('loopback:v3compat:model-booter'); | ||
|
||
const DefaultOptions = { | ||
root: './legacy', | ||
}; | ||
|
||
export class Lb3ModelBooter implements Booter { | ||
options: Lb3ModelBooterOptions; | ||
|
||
root: string; | ||
modelDefinitionsGlob: string; | ||
modelConfigFile: string; | ||
|
||
discoveredDefinitionFiles: string[]; | ||
|
||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) | ||
public app: Application & {v3compat: Lb3Application}, | ||
@inject(BootBindings.PROJECT_ROOT) | ||
public projectRoot: string, | ||
@inject(`${BootBindings.BOOT_OPTIONS}#v3compat`) | ||
options: Partial<Lb3ModelBooterOptions> = {}, | ||
) { | ||
this.options = Object.assign({}, DefaultOptions, options); | ||
} | ||
|
||
async configure?(): Promise<void> { | ||
this.root = path.join(this.projectRoot, this.options.root); | ||
this.modelDefinitionsGlob = '/models/*.json'; | ||
this.modelConfigFile = 'model-config.json'; | ||
} | ||
|
||
async discover?(): Promise<void> { | ||
debug( | ||
'Discovering LB3 model definitions in %j using glob %j', | ||
this.root, | ||
this.modelDefinitionsGlob, | ||
); | ||
|
||
const allFiles = await discoverFiles(this.modelDefinitionsGlob, this.root); | ||
this.discoveredDefinitionFiles = allFiles.filter( | ||
f => f[0] !== '_' && path.extname(f) === '.json', | ||
); | ||
debug(' -> %j', allFiles); | ||
} | ||
|
||
async load?(): Promise<void> { | ||
for (const f of this.discoveredDefinitionFiles) await this.loadModel(f); | ||
|
||
await this.configureModels(); | ||
} | ||
|
||
private async loadModel(jsonFile: string) { | ||
const basename = path.basename(jsonFile, path.extname(jsonFile)); | ||
const sourceDir = path.dirname(jsonFile); | ||
// TODO: support additional extensions like `.ts` and `.coffee` | ||
const sourceFile = path.join(sourceDir, `${basename}.js`); | ||
const sourceExists = await fileExists(sourceFile); | ||
|
||
debug( | ||
'Loading model from %j (%j)', | ||
path.relative(this.projectRoot, jsonFile), | ||
sourceExists | ||
? path.relative(this.projectRoot, sourceFile) | ||
: '<no source code>', | ||
); | ||
|
||
const definition = require(jsonFile); | ||
const script = sourceExists ? require(sourceFile) : undefined; | ||
|
||
debug(' creating a new model %j', definition.name); | ||
const modelCtor = this.app.v3compat.registry.createModel(definition); | ||
|
||
if (typeof script === 'function') { | ||
debug( | ||
' customizing model %j using %j', | ||
definition.name, | ||
path.relative(this.projectRoot, sourceFile), | ||
); | ||
script(modelCtor); | ||
} else if (sourceExists) { | ||
debug( | ||
' skipping model file %s - `module.exports` is not a function', | ||
sourceFile, | ||
); | ||
} | ||
} | ||
|
||
private async configureModels() { | ||
const configFile = path.join(this.root, this.modelConfigFile); | ||
debug('Loading model-config from %j', configFile); | ||
|
||
const config = require(configFile); | ||
for (const modelName in config) { | ||
if (modelName === '_meta') continue; | ||
const modelConfig = config[modelName]; | ||
debug(' configuring %j with %j', modelName, modelConfig); | ||
const modelCtor = this.app.v3compat.registry.getModel(modelName); | ||
this.app.v3compat.model(modelCtor, modelConfig); | ||
} | ||
} | ||
} | ||
|
||
export interface Lb3ModelBooterOptions { | ||
root: string; | ||
} |
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,11 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/v3compat | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Component} from '@loopback/core'; | ||
import {Lb3ModelBooter} from './boot'; | ||
|
||
export class CompatComponent implements Component { | ||
booters = [Lb3ModelBooter]; | ||
} |
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
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
Oops, something went wrong.
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.
Good questions; LB3 Datasources were easy to register for DI into LB4 app, but LB3 models are tricky. IMO LB3 models are synonymous with LB4 repositories or services.