-
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 all commits
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Open questions | ||
|
||
- 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 2k+ lines of new (migrated) code into smaller chunks that will be | ||
easier to review? | ||
|
||
- Should we register LB3 Models for dependency injection into LB4 code? Register | ||
them as repositories, models, services, or something else? | ||
|
||
- Should we implement booting datasources from `datasources.json`? I see two | ||
obstacles: | ||
|
||
1. Datasources must be booted before models are attached. | ||
2. LB3 supports variable replacement in JSON config files using ENV vars but | ||
also `app.get(name)`. This may add unwanted complexity to our compat layer. | ||
|
||
## By the way | ||
|
||
- TypeScript does not support static index signature, which make it difficult to | ||
consume custom model methods. See | ||
https://github.com/Microsoft/TypeScript/issues/6480. | ||
|
||
- I'd like us to extract the Booter contract into a standalone package so that | ||
v3compat and other similar extensions don't have to inherit entire boot and | ||
don't have to lock down a specific semver-major version of boot in extension | ||
dependencies. Instead, they should depend on the Booter contract only, this | ||
contract should not be changed so often (hopefully). |
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,25 @@ | ||
Copyright (c) IBM Corp. 2017,2018. All Rights Reserved. | ||
Node module: @loopback/lb3models | ||
This project is licensed under the MIT License, full text below. | ||
|
||
-------- | ||
|
||
MIT license | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,82 @@ | ||
# @loopback/example-lb3models | ||
|
||
This project demonstrates how to add LoopBack 3 models into a LoopBack 4 | ||
application. | ||
|
||
## Instructions | ||
|
||
1. Create a new LB4 app using `lb4 app`. Add `@loopback/v3compat'` to project | ||
dependencies. | ||
|
||
2. Copy your model files from `common/models` to `legacy/models` directory, for | ||
example: | ||
|
||
```text | ||
legacy/models/coffee-shop.js | ||
legacy/models/coffee-shop.json | ||
``` | ||
|
||
IMPORTANT! These files must live outside your `src` directory, out of sight | ||
of TypeScript compiler. | ||
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. Should we say why here? |
||
|
||
3. Copy your `server/model-config` to `src/legacy/model-config.json`. | ||
|
||
Remove references to LoopBack 3 built-in models like User and ACL. LoopBack 4 | ||
does not support local authentication yet. | ||
|
||
Remove `_meta` section, LoopBack 4 does not support this config option. | ||
|
||
4. Modify your Application class and apply `CompatMixin`. | ||
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. nitpick: might want to mention filename here |
||
|
||
```ts | ||
import {CompatMixin} from '@loopback/v3compat'; | ||
// ... | ||
|
||
export class TodoListApplication extends CompatMixin( | ||
BootMixin(ServiceMixin(RepositoryMixin(RestApplication))), | ||
) { | ||
// ... | ||
} | ||
``` | ||
|
||
5. Register your legacy datasources in Application's constructor. | ||
|
||
```ts | ||
this.v3compat.dataSource('mysqlDs', { | ||
name: 'mysqlDs', | ||
connector: 'mysql', | ||
host: 'demo.strongloop.com', | ||
port: 3306, | ||
database: 'getting_started', | ||
username: 'demo', | ||
password: 'L00pBack', | ||
}); | ||
``` | ||
|
||
## Need help? | ||
|
||
Check out our [Gitter channel](https://gitter.im/strongloop/loopback) and ask | ||
for help with this tutorial. | ||
|
||
## Bugs/Feedback | ||
|
||
Open an issue in [loopback-next](https://github.com/strongloop/loopback-next) | ||
and we'll take a look. | ||
|
||
## Contributions | ||
|
||
- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md) | ||
- [Join the team](https://github.com/strongloop/loopback-next/issues/110) | ||
|
||
## Tests | ||
|
||
Run `npm test` from the root folder. | ||
|
||
## Contributors | ||
|
||
See | ||
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
||
## License | ||
|
||
MIT |
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. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/example-lb3models | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export * from './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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/example-lb3models | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
const application = require('./dist'); | ||
|
||
module.exports = application; | ||
|
||
if (require.main === module) { | ||
// Run the application | ||
const config = { | ||
rest: { | ||
port: +process.env.PORT || 3000, | ||
host: process.env.HOST || 'localhost', | ||
openApiSpec: { | ||
// useful when used with OASGraph to locate your application | ||
setServersFromRequest: true, | ||
}, | ||
}, | ||
}; | ||
application.main(config).catch(err => { | ||
console.error('Cannot start the application.', err); | ||
process.exit(1); | ||
}); | ||
} |
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,8 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/example-lb3models | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
// DO NOT EDIT THIS FILE | ||
// Add any additional (re)exports to src/index.ts instead. | ||
export * from './src'; |
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 @@ | ||
{ | ||
"CoffeeShop": { | ||
"dataSource": "mysqlDs", | ||
"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,28 @@ | ||
'use strict'; | ||
|
||
module.exports = function(CoffeeShop) { | ||
CoffeeShop.status = function(cb) { | ||
var currentDate = new Date(); | ||
var currentHour = currentDate.getHours(); | ||
var OPEN_HOUR = 6; | ||
var CLOSE_HOUR = 20; | ||
console.log('Current hour is %d', currentHour); | ||
var response; | ||
if (currentHour > OPEN_HOUR && currentHour < CLOSE_HOUR) { | ||
response = 'We are open for business.'; | ||
} else { | ||
response = 'Sorry, we are closed. Open daily from 6am to 8pm.'; | ||
} | ||
cb(null, response); | ||
}; | ||
CoffeeShop.remoteMethod('status', { | ||
http: { | ||
path: '/status', | ||
verb: 'get', | ||
}, | ||
returns: { | ||
arg: 'status', | ||
type: '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,22 @@ | ||
{ | ||
"name": "CoffeeShop", | ||
"base": "PersistedModel", | ||
"idInjection": true, | ||
"forceId": false, | ||
"options": { | ||
"validateUpsert": true | ||
}, | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"required": true | ||
}, | ||
"city": { | ||
"type": "string" | ||
} | ||
}, | ||
"validations": [], | ||
"relations": {}, | ||
"acls": [], | ||
"methods": {} | ||
} |
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,65 @@ | ||
{ | ||
"name": "@loopback/example-lb3models", | ||
"version": "0.1.0", | ||
"description": "An example on how to add LoopBack 3 models into a LoopBack 4 application.", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=8.9" | ||
}, | ||
"scripts": { | ||
"build:apidocs": "lb-apidocs", | ||
"build": "lb-tsc es2017 --outDir dist", | ||
"build:watch": "lb-tsc es2017 --outDir dist --watch", | ||
"clean": "lb-clean *example-lb3models*.tgz dist package api-docs", | ||
"lint": "npm run prettier:check && npm run tslint", | ||
"lint:fix": "npm run tslint:fix && npm run prettier:fix", | ||
"prettier:cli": "lb-prettier \"**/*.ts\"", | ||
"prettier:check": "npm run prettier:cli -- -l", | ||
"prettier:fix": "npm run prettier:cli -- --write", | ||
"tslint": "lb-tslint", | ||
"tslint:fix": "npm run tslint -- --fix", | ||
"pretest": "npm run build", | ||
"test": "lb-mocha \"dist/test/*/**/*.js\"", | ||
"test:dev": "lb-mocha --allow-console-logs dist/test/**/*.js && npm run posttest", | ||
"verify": "npm pack && tar xf loopback-todo*.tgz && tree package && npm run clean", | ||
"migrate": "node ./dist/src/migrate", | ||
"prestart": "npm run build", | ||
"start": "node ." | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/strongloop/loopback-next.git" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@loopback/boot": "^1.0.10", | ||
"@loopback/context": "^1.4.1", | ||
"@loopback/core": "^1.1.4", | ||
"@loopback/rest": "^1.5.3", | ||
"@loopback/rest-explorer": "^1.1.6", | ||
"@loopback/v3compat": "^0.1.0", | ||
"loopback-connector-mysql": "^5.3.1" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^1.2.0", | ||
"@loopback/testlab": "^1.0.4", | ||
"@loopback/tslint-config": "^2.0.0", | ||
"@types/lodash": "^4.14.109", | ||
"@types/node": "^10.11.2", | ||
"lodash": "^4.17.10", | ||
"tslint": "^5.12.0", | ||
"typescript": "^3.2.2" | ||
}, | ||
"keywords": [ | ||
"loopback", | ||
"LoopBack", | ||
"example", | ||
"tutorial", | ||
"CRUD", | ||
"models", | ||
"todo" | ||
] | ||
} |
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,73 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<title>@loopback/example-lb3models</title> | ||
|
||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="shortcut icon" type="image/x-icon" href="//v4.loopback.io/favicon.ico"> | ||
|
||
<style> | ||
h3 { | ||
margin-left: 25px; | ||
text-align: center; | ||
} | ||
|
||
a, a:visited { | ||
color: #3f5dff; | ||
} | ||
|
||
h3 a { | ||
margin-left: 10px; | ||
} | ||
|
||
a:hover, a:focus, a:active { | ||
color: #001956; | ||
} | ||
|
||
.power { | ||
position: absolute; | ||
bottom: 25px; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
} | ||
|
||
.info { | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%) | ||
} | ||
|
||
.info h1 { | ||
text-align: center; | ||
margin-bottom: 0 | ||
} | ||
|
||
.info p { | ||
text-align: center; | ||
margin-bottom: 3em; | ||
margin-top: 1em; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div class="info"> | ||
<h1>@loopback/example-lb3models</h1> | ||
|
||
<h3>OpenAPI spec: <a href="/openapi.json">/openapi.json</a></h3> | ||
<h3>API Explorer: <a href="/explorer">/explorer</a></h3> | ||
</div> | ||
|
||
<footer class="power"> | ||
<a href="https://v4.loopback.io" target="_blank"> | ||
<img src="https://loopback.io/images/branding/powered-by-loopback/blue/powered-by-loopback-sm.png" /> | ||
</a> | ||
</footer> | ||
</body> | ||
|
||
</html> | ||
|
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.
IMO the answer ideally should be definitely use async/await style, but re-write them using the tests for inspiration. If you were to reflect on how you migrated strong-remoting and loopback (v3) code over for the spike, do you feel that there were significant changes to warrant a re-write of the tests?