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

Update examples to work with latest loopback versions #78

Merged
merged 1 commit into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
node_modules
npm-debug.log
services/*/dist*
.vscode
.idea
package-lock.json
dist/
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock: false
package-lock=false
5 changes: 3 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
api-docs
dist/
api-docs/
node_modules/
*.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Make sure you have the following installed:
# install loopback4-example-microservices
git clone https://github.com/strongloop/loopback4-example-microservices
cd loopback4-example-microservices
npm run build
npm i
```

## Basic use
Expand Down
File renamed without changes.
21 changes: 0 additions & 21 deletions bin/start

This file was deleted.

3 changes: 0 additions & 3 deletions bin/stop

This file was deleted.

10 changes: 8 additions & 2 deletions bin/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ let spawn = require('child_process').spawn;
let fs = Promise.promisifyAll(require('fs'));
let path = require('path');

let cmd = path.resolve(__dirname, '..', 'node_modules', '.bin', '_mocha');
let args = ['--compilers', 'ts:ts-node/register,tsx:ts-node/register'];
let cmd = path.resolve(
__dirname,
'..',
'node_modules',
'.bin',
/^win/.test(process.platform) ? '_mocha.cmd' : '_mocha',
);
let args = ['--require', 'ts-node/register'];

let services = path.resolve('services');
return fs.readdirAsync(services).then(folders => {
Expand Down
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@
"description": "How to use LoopBack.next and some recommended best practices.",
"main": "facade/index.js",
"scripts": {
"build": "bin/build",
"postinstall": "bin/install",
"build:account":"cd ./services/account && npm run build",
"build:customer":"cd ./services/customer && npm run build",
"build:transaction":"cd ./services/transaction && npm run build",
"build:facade":"cd ./services/facade && npm run build",
"build": "npm run build:account && npm run build:customer && npm run build:transaction && npm run build:facade",
"lint": "npm run prettier:check && npm run tslint",
"lint:fix": "npm run prettier:fix && npm run tslint:fix",
"prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"",
"prettier:check": "npm run prettier:cli -- -l",
"prettier:fix": "npm run prettier:cli -- --write",
"tslint": "lb-tslint",
"tslint:fix": "npm run tslint -- --fix",
"restart": "npm run stop && npm run build && npm run start",
"start": "bin/start",
"stop": "bin/stop",
"test": "node bin/test.js",
"start": "concurrently --kill-others \"npm run start:account\" \"npm run start:customer\" \"npm run start:transaction\" \"npm run start:facade\"",
"start:account":"cd ./services/account && npm run start",
"start:customer":"cd ./services/customer && npm run start",
"start:transaction":"cd ./services/transaction && npm run start",
"start:facade":"cd ./services/facade && npm run start",
"test": "npm run test:account && npm run test:customer && npm run test:transaction",
"test:account":"cd ./services/account && npm run test",
"test:customer":"cd ./services/customer && npm run test",
"test:transaction":"cd ./services/transaction && npm run test",
"test:concurrently": "concurrently --kill-others \"npm run test:account\" \"npm run test:customer\" \"npm run test:transaction\"",
"test:ts": "./bin/test",
"posttest": "npm run lint"
},
"engines": {
Expand All @@ -39,9 +51,10 @@
"bluebird": "^3.5.0",
"mocha": "^5.0.3",
"ts-node": "^3.1.0",
"typescript": "^2.4.1"
"typescript": "^2.6.2"
},
"devDependencies": {
"@loopback/build": "^0.1.1"
"@loopback/build": "^0.3.2",
"concurrently": "^3.5.1"
}
}
2 changes: 1 addition & 1 deletion services/account-without-juggler/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock: false
package-lock=false
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {api} from '@loopback/core';
import {api} from '@loopback/rest';
import {def} from './AccountController.api';
import {AccountRepository} from '../repositories/account';
import {inject} from '@loopback/context';
Expand Down
29 changes: 19 additions & 10 deletions services/account-without-juggler/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import {Application} from '@loopback/core';
import {Application, ApplicationConfig} from '@loopback/core';
import {AccountController} from './controllers/AccountController';
import {AccountRepository} from './repositories/account';
import {RestBindings, RestComponent, RestServer} from '@loopback/rest';

class AccountMicroservice extends Application {
private _startTime: Date;

constructor() {
super();

constructor(options?: ApplicationConfig) {
options = Object.assign(
{},
{
components: [RestComponent],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support binding components via the constructor anymore.
Please bind the component through app.component(RestComponent) instead.
Alternatively, the application could extend from RestApplication instead, which already has RestComponent and RestServer registered.

rest: {
port: 3001,
},
},
options,
);
super(options);
const app = this;
app.controller(AccountController);
app.bind('http.port').to(3001);
app.bind('repositories.account').toClass(AccountRepository);
app.controller(AccountController);
}

async start() {
Expand All @@ -20,12 +29,12 @@ class AccountMicroservice extends Application {
}

async info() {
const port: Number = await this.get('http.port');

const rest = await this.getServer(RestServer);
const port: Number = await rest.get(RestBindings.PORT);
return {
appName: 'account-without-juggler',
appName: 'account',
uptime: Date.now() - this._startTime.getTime(),
url: 'http://127.0.0.1:' + port,
url: `http://127.0.0.1:${port}`,
};
}
}
Expand Down
23 changes: 16 additions & 7 deletions services/account-without-juggler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
"node": ">=8"
},
"dependencies": {
"@loopback/core": "^0.2.1",
"@loopback/repository": "^0.2.1",
"@loopback/rest": "^0.3.0",
"mysql": "^2.13.0",
"mysql-promise": "^4.1.0",
"@loopback/core": "^4.0.0-alpha.10",
"@loopback/repository": "^4.0.0-alpha.5",
"loopback-datasource-juggler": "^3.4.1"
"mysql-promise": "^4.1.0"
},
"devDependencies": {
"@loopback/testlab": "^4.0.0-alpha.6",
"@types/node": "^7.0.12",
"@loopback/testlab": "^0.4.0",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.47",
"debug": "^2.6.8",
"lodash": "^4.17.4",
"mocha": "^3.4.2",
"mocha": "^4.0.1",
"ts-node": "^3.0.4",
"tslint": "^5.4.3",
"typescript": "^2.4.1"
Expand All @@ -33,6 +34,14 @@
"account",
"microservice"
],
"repository": {
"type": "git",
"url": "git+https://github.com/strongloop/loopback4-example-microservices.git"
},
"bugs": {
"url": "https://github.com/strongloop/loopback4-example-microservices/issues"
},
"homepage": "https://github.com/strongloop/loopback4-example-microservices#readme",
"author": "IBM",
"license": "MIT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Entity, model, ModelDefinition} from '@loopback/repository';
@model(require('./account/model-definition'))
export class Account extends Entity {
static definition = new ModelDefinition(
require('./account/model-definition'),
require('./account/model-definition').properties,
);
static modelName = 'Account';

Expand Down
2 changes: 1 addition & 1 deletion services/account-without-juggler/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--compilers ts:ts-node/register
--require ts-node/register
--recursive
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// test/unit/account-controller.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

import 'mocha';
import {AccountController} from '../../controllers/AccountController';
import {expect} from '@loopback/testlab';
Expand Down
5 changes: 1 addition & 4 deletions services/account-without-juggler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
},
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2017",
"dom"
],
"lib": ["es2017", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"target": "es2017",
Expand Down
2 changes: 1 addition & 1 deletion services/account/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock: false
package-lock=false
4 changes: 4 additions & 0 deletions services/account/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
api-docs/
node_modules/
*.json
6 changes: 6 additions & 0 deletions services/account/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "all"
}
40 changes: 40 additions & 0 deletions services/account/bin/generate-example-file-db
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
DIRECTORY="./dist"
TEST="./dist/test/unit/test.data.json"
DATA="./dist/src/datasources/data.json"

if [ -d "$DIRECTORY" ]; then
echo "Generating the example data files inside dist folder for tests and app..."
cat >$TEST <<EOF
{
"ids": {
"Account": 5
},
"models": {
"Account": {
"CHK52321122": "{\"id\":\"CHK52321122\",\"customerNumber\":\"000343223\",\"balance\":85.84,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":398.93,\"minimumBalance\":10}",
"CHK54520000": "{\"id\":\"CHK54520000\",\"customerNumber\":\"003499223\",\"balance\":99.99,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":500.93,\"minimumBalance\":10}",
"CHK52321199": "{\"id\":\"CHK52321199\",\"customerNumber\":\"000343223\",\"balance\":109.89,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":100.93,\"minimumBalance\":10}",
"CHK99999999": "{\"id\":\"CHK99999999\",\"customerNumber\":\"0002444422\",\"balance\":100.89,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":100.93,\"minimumBalance\":10}"
}
}
}
EOF

cat >$DATA <<EOF
{
"ids": {
"Account": 5
},
"models": {
"Account": {
"CHK52321122": "{\"id\":\"CHK52321122\",\"customerNumber\":\"000343223\",\"balance\":85.84,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":398.93,\"minimumBalance\":10}",
"CHK54520000": "{\"id\":\"CHK54520000\",\"customerNumber\":\"003499223\",\"balance\":99.99,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":500.93,\"minimumBalance\":10}",
"CHK52321199": "{\"id\":\"CHK52321199\",\"customerNumber\":\"000343223\",\"balance\":109.89,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":100.93,\"minimumBalance\":10}",
"CHK99999999": "{\"id\":\"CHK99999999\",\"customerNumber\":\"0002444422\",\"balance\":100.89,\"branch\":\"Foster City\",\"type\":\"Checking\",\"avgBalance\":100.93,\"minimumBalance\":10}"
}
}
}
EOF
fi

Loading