Skip to content
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
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/lb3models/LICENSE
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.
94 changes: 94 additions & 0 deletions examples/lb3models/README.md
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.
Copy link
Contributor

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?


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`.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
6 changes: 6 additions & 0 deletions examples/lb3models/index.d.ts
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';
26 changes: 26 additions & 0 deletions examples/lb3models/index.js
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);
});
}
8 changes: 8 additions & 0 deletions examples/lb3models/index.ts
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';
6 changes: 6 additions & 0 deletions examples/lb3models/legacy/model-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"CoffeeShop": {
"dataSource": "mysqlDs",
"public": true
}
}
28 changes: 28 additions & 0 deletions examples/lb3models/legacy/models/coffee-shop.js
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',
},
});
};
22 changes: 22 additions & 0 deletions examples/lb3models/legacy/models/coffee-shop.json
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": {}
}
65 changes: 65 additions & 0 deletions examples/lb3models/package.json
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"
]
}
73 changes: 73 additions & 0 deletions examples/lb3models/public/index.html
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>

Loading