-
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
[WIP] [RFC] feat: Add REST mapping for repository interfaces #740
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.tgz | ||
dist* | ||
package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) IBM Corp. 2018. All Rights Reserved. | ||
Node module: @loopback/repository-rest | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# @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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"content": [ | ||
"./index.ts", | ||
"./src/**/*.ts" | ||
], | ||
"codeSectionDepth": 4, | ||
"assets": { | ||
"/": "/docs", | ||
"/docs": "/docs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export * from './dist8/src'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
module.exports = require('@loopback/dist-util').loadDist(__dirname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rest | ||
// 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@loopback/repository-rest", | ||
"version": "0.1.0", | ||
"description": "Repository REST APIs for LoopBack", | ||
"engines": { | ||
"node": ">=8.9" | ||
}, | ||
"main": "index", | ||
"scripts": { | ||
"build:all-dist": "npm run build:dist8 && npm run build:dist10", | ||
"build:apidocs": "lb-apidocs", | ||
"build": "lb-tsc", | ||
"build:dist8": "lb-tsc es2017", | ||
"build:dist10": "lb-tsc es2018", | ||
"clean": "lb-clean loopback-repository-rest*.tgz dist* package api-docs", | ||
"pretest": "npm run build", | ||
"test": "lb-mocha \"DIST/test/unit/**/*.js\" \"DIST/test/integration/**/*.js\" \"DIST/test/acceptance/**/*.js\"", | ||
"verify": "npm pack && tar xf loopback-repository-rest*.tgz && tree package && npm run clean" | ||
}, | ||
"author": "IBM", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@loopback/build": "^0.7.3", | ||
"@loopback/testlab": "^0.12.2" | ||
}, | ||
"dependencies": { | ||
"@loopback/context": "^0.12.8", | ||
"@loopback/core": "^0.11.9", | ||
"@loopback/dist-util": "^0.3.7", | ||
"@loopback/repository": "^0.16.5", | ||
"@loopback/rest": "^0.22.2" | ||
}, | ||
"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" | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/repository-rest/src/controllers/crud-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: @loopback/repository-rest | ||
// 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, requestBody} 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( | ||
@requestBody() dataObject: DataObject<T>, | ||
@param({name: 'options', required: false, in: 'query'}) options?: Options, | ||
): Promise<T> { | ||
return this.repository.create(dataObject, options); | ||
} | ||
|
||
@post(`/`) | ||
createAll( | ||
@requestBody() 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( | ||
@requestBody() 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); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
packages/repository-rest/src/controllers/entity-crud-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: @loopback/repository-rest | ||
// 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, requestBody} from '@loopback/rest'; | ||
|
||
import {CrudController} from './crud-controller'; | ||
|
||
/** | ||
* Base controller class to expose CrudRepository operations to REST | ||
*/ | ||
export abstract class EntityCrudController< | ||
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. Same here, I'd call it EntityRestController |
||
T extends Entity, | ||
ID | ||
> extends CrudController<T> { | ||
constructor(protected repository: EntityCrudRepository<T, ID>) { | ||
super(repository); | ||
} | ||
|
||
@put(`/save`) | ||
save( | ||
@requestBody() 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( | ||
@requestBody() 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( | ||
@requestBody() 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', schema: {type: 'string'}}) 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, | ||
@requestBody() 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, | ||
@requestBody() 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', schema: {type: 'string'}}) id: ID, | ||
@param({name: 'options', required: false, in: 'query'}) options?: Options, | ||
): Promise<boolean> { | ||
return this.repository.exists(id, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rest | ||
// 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export * from './controllers'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would rename this to RestController; that way we can keep the idea of a CrudController open as a base class for a RestController, a GrpcController, etc.