-
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
Changes from 1 commit
3302a9f
eb868ee
ec441a2
4ddb7ab
2893a49
fa06c1c
f9843eb
cee5f84
d1348e8
8309591
cb4a147
e0413f7
63f46b5
de3529b
0465a94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# @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. | ||
|
||
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. Edit your boot configuration in `src/application.ts` and add the following | ||
section: | ||
|
||
```ts | ||
{ | ||
v3compat: { | ||
// from "/dist/src/application.ts" to "/legacy" | ||
root: '../../legacy', | ||
}, | ||
} | ||
``` | ||
|
||
6. Register your legacy datasources in Application's constructor. | ||
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. To clarify, at this point, our compat lb3-model booter would have configured and loaded the legacy models for us right? How do we attach legacy datasource(s) to a legacy model after? |
||
|
||
```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 |
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'; |
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); | ||
}); | ||
} |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"CoffeeShop": { | ||
"dataSource": "mysqlDs", | ||
"public": true | ||
} | ||
} |
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', | ||
}, | ||
}); | ||
}; |
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": {} | ||
} |
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" | ||
] | ||
} |
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> | ||
|
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.
Should we say why here?