Skip to content

Commit

Permalink
feat: Add REST mapping for repository interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Nov 16, 2017
1 parent 3f124f3 commit 8b0fe3d
Show file tree
Hide file tree
Showing 17 changed files with 701 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/repository-rest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.tgz
dist*
package
1 change: 1 addition & 0 deletions packages/repository-rest/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 25 additions & 0 deletions packages/repository-rest/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2017. All Rights Reserved.
Node module: @loopback/repository
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.
15 changes: 15 additions & 0 deletions packages/repository-rest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @loopback/repository-rest

This module provides base controllers to map repository interfaces to REST APIs

## Tests

run 'npm test' from the root folder.

## Contributors

See [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
11 changes: 11 additions & 0 deletions packages/repository-rest/docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"content": [
"./index.ts",
"./src/i**/*.ts"
],
"codeSectionDepth": 4,
"assets": {
"/": "/docs",
"/docs": "/docs"
}
}
6 changes: 6 additions & 0 deletions packages/repository-rest/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './dist/src';
9 changes: 9 additions & 0 deletions packages/repository-rest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const nodeMajorVersion = +process.versions.node.split('.')[0];
module.exports = nodeMajorVersion >= 7 ?
require('./dist/src') :
require('./dist6/src');
7 changes: 7 additions & 0 deletions packages/repository-rest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// NOTE(bajtos) This file is used by VSCode/TypeScriptServer at dev time only
export * from './src';
49 changes: 49 additions & 0 deletions packages/repository-rest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@loopback/repository-rest",
"version": "4.0.0-alpha.1",
"description": "Repository REST APIs for LoopBack",
"engines": {
"node": ">=6"
},
"main": "index",
"scripts": {
"acceptance": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'",
"build": "npm run build:dist && npm run build:dist6",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
"build:dist6": "lb-tsc es2015",
"build:apidocs": "lb-apidocs",
"clean": "rm -rf loopback-context*.tgz dist* package",
"prepare": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"test": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/acceptance/**/*.js' 'DIST/test/integration/**/*.js'",
"integration": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/integration/**/*.js'",
"unit": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'",
"verify": "npm pack && tar xf loopback-juggler*.tgz && tree package && npm run clean"
},
"author": "IBM",
"license": "MIT",
"devDependencies": {
"@loopback/build": "^4.0.0-alpha.5",
"@loopback/testlab": "^4.0.0-alpha.14"
},
"dependencies": {
"@loopback/context": "^4.0.0-alpha.20",
"@loopback/core": "^4.0.0-alpha.22",
"@loopback/repository": "^4.0.0-alpha.16",
"@loopback/rest": "^4.0.0-alpha.9"
},
"files": [
"README.md",
"index.js",
"index.d.ts",
"dist/src",
"dist6/src",
"api-docs",
"src"
],
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git"
}
}
85 changes: 85 additions & 0 deletions packages/repository-rest/src/controllers/crud-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
CrudRepository,
ValueObject,
Entity,
DataObject,
Options,
Filter,
Where,
} from '@loopback/repository';

import {post, get, param} from '@loopback/rest';

/**
* Base controller class to expose CrudRepository operations to REST
*/
export abstract class CrudController<T extends ValueObject | Entity> {
constructor(protected repository: CrudRepository<T>) {}

@post(`/`)
create(
@param({name: '', in: 'body'})
dataObject: DataObject<T>,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<T> {
return this.repository.create(dataObject, options);
}

@post(`/`)
createAll(
@param({name: '', in: 'body'})
dataObjects: DataObject<T>[],
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<T[]> {
return this.repository.createAll(dataObjects, options);
}

@get(`/`)
find(
@param({name: 'filter', required: false, in: 'query'})
filter?: Filter,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<T[]> {
return this.repository.find(filter, options);
}

@post(`/updateAll`)
updateAll(
@param({name: '', in: 'body'})
dataObject: DataObject<T>,
@param({name: 'where', required: false, in: 'query'})
where?: Where,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<number> {
return this.repository.updateAll(dataObject, where, options);
}

@post(`/deleteAll`)
deleteAll(
@param({name: 'where', required: false, in: 'query'})
where?: Where,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<number> {
return this.repository.deleteAll(where, options);
}

@get(`/count`)
count(
@param({name: 'where', required: false, in: 'query'})
where?: Where,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<number> {
return this.repository.count(where, options);
}
}
156 changes: 156 additions & 0 deletions packages/repository-rest/src/controllers/entity-crud-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
Entity,
DataObject,
Options,
Filter,
EntityCrudRepository,
} from '@loopback/repository';

import {post, put, patch, get, del, param} from '@loopback/rest';

import {CrudController} from './crud-controller';

/**
* Base controller class to expose CrudRepository operations to REST
*/
export abstract class EntityCrudController<
T extends Entity,
ID
> extends CrudController<T> {
constructor(protected repository: EntityCrudRepository<T, ID>) {
super(repository);
}

@put(`/save`)
save(
@param({name: '', in: 'body'})
entity: DataObject<T>,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<T | null> {
return this.repository.save(entity, options);
}

@post(`/update`, {
responses: {
'200': {
description: 'The instance is updated successfully',
schema: {type: 'boolean'},
},
},
})
update(
@param({name: '', in: 'body'})
entity: DataObject<T>,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<boolean> {
return this.repository.update(entity, options);
}

@post(`/delete`, {
responses: {
'200': {
description: 'The instance is deleted successfully',
schema: {type: 'boolean'},
},
},
})
delete(
@param({name: '', in: 'body'})
entity: DataObject<T>,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<boolean> {
return this.repository.update(entity, options);
}

@get(`/{id}`)
findById(
@param({name: 'id', in: 'path'})
id: ID,
@param({name: 'filter', required: false, in: 'query'})
filter?: Filter,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<T> {
return this.repository.findById(id, filter, options);
}

@patch(`/{id}`, {
responses: {
'200': {
description: 'The instance is updated successfully',
schema: {type: 'boolean'},
},
},
})
updateById(
@param({name: 'id', in: 'path'})
id: ID,
@param({name: '', in: 'body'})
data: DataObject<T>,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<boolean> {
return this.repository.updateById(id, data, options);
}

@put(`/{id}`, {
responses: {
'200': {
description: 'The instance is replaced successfully',
schema: {type: 'boolean'},
},
},
})
replaceById(
@param({name: 'id', in: 'path'})
id: ID,
@param({name: '', in: 'body'})
data: DataObject<T>,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<boolean> {
return this.repository.replaceById(id, data, options);
}

@del(`{id}`, {
responses: {
'200': {
description: 'The instance is deleted successfully',
schema: {type: 'boolean'},
},
},
})
deleteById(
@param({name: 'id', in: 'path'})
id: ID,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<boolean> {
return this.repository.deleteById(id, options);
}

@get(`/{id}/exists`, {
responses: {
'200': {
description: 'The id exists for an instance',
schema: {type: 'boolean'},
},
},
})
exists(
@param({name: 'id', in: 'path'})
id: ID,
@param({name: 'options', required: false, in: 'query'})
options?: Options,
): Promise<boolean> {
return this.repository.exists(id, options);
}
}
7 changes: 7 additions & 0 deletions packages/repository-rest/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './crud-controller';
export * from './entity-crud-controller';
6 changes: 6 additions & 0 deletions packages/repository-rest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './controllers';
Loading

0 comments on commit 8b0fe3d

Please sign in to comment.