From 11ab4bd6b8c9a3aca4e1d323de401d8edbac35c2 Mon Sep 17 00:00:00 2001 From: Nora Date: Sun, 19 Jan 2020 23:25:08 -0500 Subject: [PATCH] fix(example-todo): use latest cli code --- examples/todo/.dockerignore | 5 ++++ examples/todo/Dockerfile | 28 +++++++++++++++++++ .../__tests__/acceptance/todo.acceptance.ts | 4 +-- examples/todo/src/__tests__/helpers.ts | 4 +-- .../services/geocoder.service.integration.ts | 6 ++-- .../unit/controllers/todo.controller.unit.ts | 4 +-- .../todo/src/controllers/todo.controller.ts | 18 ++++++------ examples/todo/src/models/todo.model.ts | 1 + .../todo/src/services/geocoder.service.ts | 15 +++++----- 9 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 examples/todo/.dockerignore create mode 100644 examples/todo/Dockerfile diff --git a/examples/todo/.dockerignore b/examples/todo/.dockerignore new file mode 100644 index 000000000000..7aecc7e3dda5 --- /dev/null +++ b/examples/todo/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +/dist +# Cache used by TypeScript's incremental build +*.tsbuildinfo diff --git a/examples/todo/Dockerfile b/examples/todo/Dockerfile new file mode 100644 index 000000000000..bca90e201a53 --- /dev/null +++ b/examples/todo/Dockerfile @@ -0,0 +1,28 @@ +# Check out https://hub.docker.com/_/node to select a new base image +FROM node:10-slim + +# Set to a non-root built-in user `node` +USER node + +# Create app directory (with user `node`) +RUN mkdir -p /home/node/app + +WORKDIR /home/node/app + +# Install app dependencies +# A wildcard is used to ensure both package.json AND package-lock.json are copied +# where available (npm@5+) +COPY --chown=node package*.json ./ + +RUN npm install + +# Bundle app source code +COPY --chown=node . . + +RUN npm run build + +# Bind to all network interfaces so that it can be mapped to the host OS +ENV HOST=0.0.0.0 PORT=3000 + +EXPOSE ${PORT} +CMD [ "node", "." ] diff --git a/examples/todo/src/__tests__/acceptance/todo.acceptance.ts b/examples/todo/src/__tests__/acceptance/todo.acceptance.ts index 0e6d616d3d8f..9f2a11daf616 100644 --- a/examples/todo/src/__tests__/acceptance/todo.acceptance.ts +++ b/examples/todo/src/__tests__/acceptance/todo.acceptance.ts @@ -14,7 +14,7 @@ import { import {TodoListApplication} from '../../application'; import {Todo} from '../../models/'; import {TodoRepository} from '../../repositories/'; -import {GeocoderService} from '../../services'; +import {Geocoder} from '../../services'; import { aLocation, getProxiedGeoCoderConfig, @@ -40,7 +40,7 @@ describe('TodoApplication', () => { before(async function() { // eslint-disable-next-line no-invalid-this this.timeout(30 * 1000); - const service = await app.get('services.GeocoderService'); + const service = await app.get('services.Geocoder'); available = await isGeoCoderServiceAvailable(service); }); diff --git a/examples/todo/src/__tests__/helpers.ts b/examples/todo/src/__tests__/helpers.ts index 72aa7026de3e..2608aabf7957 100644 --- a/examples/todo/src/__tests__/helpers.ts +++ b/examples/todo/src/__tests__/helpers.ts @@ -8,7 +8,7 @@ import {merge} from 'lodash'; import path from 'path'; import GEO_CODER_CONFIG from '../datasources/geocoder.datasource.config.json'; import {Todo} from '../models/index'; -import {GeocoderService, GeoPoint} from '../services/geocoder.service'; +import {Geocoder, GeoPoint} from '../services/geocoder.service'; /* ============================================================================== @@ -74,7 +74,7 @@ export async function givenCachingProxy() { return proxy; } -export async function isGeoCoderServiceAvailable(service: GeocoderService) { +export async function isGeoCoderServiceAvailable(service: Geocoder) { try { await service.geocode(aLocation.address); return true; diff --git a/examples/todo/src/__tests__/integration/services/geocoder.service.integration.ts b/examples/todo/src/__tests__/integration/services/geocoder.service.integration.ts index 745f7e67ad9b..176dd8a7cb99 100644 --- a/examples/todo/src/__tests__/integration/services/geocoder.service.integration.ts +++ b/examples/todo/src/__tests__/integration/services/geocoder.service.integration.ts @@ -5,7 +5,7 @@ import {expect} from '@loopback/testlab'; import {GeocoderDataSource} from '../../../datasources/geocoder.datasource'; -import {GeocoderService, GeocoderServiceProvider} from '../../../services'; +import {Geocoder, GeocoderProvider} from '../../../services'; import { aLocation, getProxiedGeoCoderConfig, @@ -22,7 +22,7 @@ describe('GeoLookupService', function() { before(async () => (cachingProxy = await givenCachingProxy())); after(() => cachingProxy.stop()); - let service: GeocoderService; + let service: Geocoder; before(givenGeoService); let available = true; @@ -42,6 +42,6 @@ describe('GeoLookupService', function() { async function givenGeoService() { const config = getProxiedGeoCoderConfig(cachingProxy); const dataSource = new GeocoderDataSource(config); - service = await new GeocoderServiceProvider(dataSource).value(); + service = await new GeocoderProvider(dataSource).value(); } }); diff --git a/examples/todo/src/__tests__/unit/controllers/todo.controller.unit.ts b/examples/todo/src/__tests__/unit/controllers/todo.controller.unit.ts index 50f575291b06..fba6760124ee 100644 --- a/examples/todo/src/__tests__/unit/controllers/todo.controller.unit.ts +++ b/examples/todo/src/__tests__/unit/controllers/todo.controller.unit.ts @@ -13,12 +13,12 @@ import { import {TodoController} from '../../../controllers'; import {Todo} from '../../../models/index'; import {TodoRepository} from '../../../repositories'; -import {GeocoderService} from '../../../services'; +import {Geocoder} from '../../../services'; import {aLocation, givenTodo} from '../../helpers'; describe('TodoController', () => { let todoRepo: StubbedInstanceWithSinonAccessor; - let geoService: GeocoderService; + let geoService: Geocoder; let geocode: sinon.SinonStub; diff --git a/examples/todo/src/controllers/todo.controller.ts b/examples/todo/src/controllers/todo.controller.ts index 32ac22f34fbb..1acbcd2e21ac 100644 --- a/examples/todo/src/controllers/todo.controller.ts +++ b/examples/todo/src/controllers/todo.controller.ts @@ -18,12 +18,12 @@ import { } from '@loopback/rest'; import {Todo} from '../models'; import {TodoRepository} from '../repositories'; -import {GeocoderService} from '../services'; +import {Geocoder} from '../services'; export class TodoController { constructor( - @repository(TodoRepository) protected todoRepo: TodoRepository, - @inject('services.GeocoderService') protected geoService: GeocoderService, + @repository(TodoRepository) protected todoRepository: TodoRepository, + @inject('services.Geocoder') protected geoService: Geocoder, ) {} @post('/todos', { @@ -52,7 +52,7 @@ export class TodoController { // https://gis.stackexchange.com/q/7379 todo.remindAtGeo = `${geo[0].y},${geo[0].x}`; } - return this.todoRepo.create(todo); + return this.todoRepository.create(todo); } @get('/todos/{id}', { @@ -67,7 +67,7 @@ export class TodoController { @param.path.number('id') id: number, @param.query.boolean('items') items?: boolean, ): Promise { - return this.todoRepo.findById(id); + return this.todoRepository.findById(id); } @get('/todos', { @@ -86,7 +86,7 @@ export class TodoController { @param.query.object('filter', getFilterSchemaFor(Todo)) filter?: Filter, ): Promise { - return this.todoRepo.find(filter); + return this.todoRepository.find(filter); } @put('/todos/{id}', { @@ -100,7 +100,7 @@ export class TodoController { @param.path.number('id') id: number, @requestBody() todo: Todo, ): Promise { - await this.todoRepo.replaceById(id, todo); + await this.todoRepository.replaceById(id, todo); } @patch('/todos/{id}', { @@ -121,7 +121,7 @@ export class TodoController { }) todo: Partial, ): Promise { - await this.todoRepo.updateById(id, todo); + await this.todoRepository.updateById(id, todo); } @del('/todos/{id}', { @@ -132,6 +132,6 @@ export class TodoController { }, }) async deleteTodo(@param.path.number('id') id: number): Promise { - await this.todoRepo.deleteById(id); + await this.todoRepository.deleteById(id); } } diff --git a/examples/todo/src/models/todo.model.ts b/examples/todo/src/models/todo.model.ts index 93ed89c900a6..fc0461434390 100644 --- a/examples/todo/src/models/todo.model.ts +++ b/examples/todo/src/models/todo.model.ts @@ -10,6 +10,7 @@ export class Todo extends Entity { @property({ type: 'number', id: true, + generated: false, }) id?: number; diff --git a/examples/todo/src/services/geocoder.service.ts b/examples/todo/src/services/geocoder.service.ts index fe1c6113ba9f..b9da8d7f97ea 100644 --- a/examples/todo/src/services/geocoder.service.ts +++ b/examples/todo/src/services/geocoder.service.ts @@ -1,11 +1,11 @@ -// Copyright IBM Corp. 2018. All Rights Reserved. +// Copyright IBM Corp. 2018,2020. All Rights Reserved. // Node module: @loopback/example-todo // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {getService, juggler} from '@loopback/service-proxy'; import {inject, Provider} from '@loopback/core'; -import {GeocoderDataSource} from '../datasources/geocoder.datasource'; +import {getService} from '@loopback/service-proxy'; +import {GeocoderDataSource} from '../datasources'; export interface GeoPoint { /** @@ -19,17 +19,18 @@ export interface GeoPoint { x: number; } -export interface GeocoderService { +export interface Geocoder { geocode(address: string): Promise; } -export class GeocoderServiceProvider implements Provider { +export class GeocoderProvider implements Provider { constructor( + // geocoder must match the name property in the datasource json file @inject('datasources.geocoder') - protected dataSource: juggler.DataSource = new GeocoderDataSource(), + protected dataSource: GeocoderDataSource = new GeocoderDataSource(), ) {} - value(): Promise { + value(): Promise { return getService(this.dataSource); } }