Skip to content

Commit

Permalink
Merge 2d40b75 into 69c46aa
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostanos committed Nov 21, 2017
2 parents 69c46aa + 2d40b75 commit 7a24cbe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function PgRestify(config, postInitFunction) {
this.convertFieldToColumn = config.convertFieldToColumn || defaultConvertFieldToColumn;
this.convertColumnToField = config.convertColumnToField || defaultConvertColumnToField;
this.hooks = config.hooks || new exports.Hooks();
this.hooks.parent = this;
this.tableIdColumns = config.tableIdColumns || {};
var self = this;

Expand All @@ -38,7 +39,7 @@ function PgRestify(config, postInitFunction) {

self.executeWithHooks(req, res, next, 'getList', function(dbClient, next) {

// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already generated content
// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already generated response body
if (res.pgRestifyResponseBody){
return next();
}
Expand Down Expand Up @@ -116,6 +117,9 @@ function PgRestify(config, postInitFunction) {
if (req.pgRestifyWhere){
query = query.where(req.pgRestifyWhere);
}
if (req.query.groupBy){
query = query.groupBy(req.query.groupBy.split(',').map(self.convertFieldToColumn));
}
query = query
.orderBy(orderBy)
.limit(pageSize)
Expand Down Expand Up @@ -148,6 +152,11 @@ function PgRestify(config, postInitFunction) {

self.executeWithHooks(req, res, next, 'getCount', function(dbClient, next) {

// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already generated response body
if (res.pgRestifyResponseBody){
return next();
}

var table = self.convertResourceToTable(req.params.resource);

self.validateTable(table, function(err) {
Expand Down Expand Up @@ -178,6 +187,11 @@ function PgRestify(config, postInitFunction) {

self.executeWithHooks(req, res, next, 'get', function(dbClient, next) {

// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already generated response body
if (res.pgRestifyResponseBody){
return next();
}

var table = self.convertResourceToTable(req.params.resource);
var id = req.params.id;

Expand Down Expand Up @@ -217,6 +231,11 @@ function PgRestify(config, postInitFunction) {

self.executeWithHooks(req, res, next, 'post', function(dbClient, next) {

// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already did the magic and generated response body
if (res.pgRestifyResponseBody){
return next();
}

var table = self.convertResourceToTable(req.params.resource);
var data = self.convertFieldsToColumns(req.body);

Expand All @@ -236,6 +255,9 @@ function PgRestify(config, postInitFunction) {
var location = ((req.isSecure()) ? 'https' : 'http') +
'://' + req.headers.host + req.url + '/' + result.rows[0][self.tableIdColumns[table] || 'id'];
res.setHeader('location', location);

res.pgRestifyResponseBody = result;

res.status(201);

return next();
Expand All @@ -250,6 +272,11 @@ function PgRestify(config, postInitFunction) {

self.executeWithHooks(req, res, next, 'put', function(dbClient, next) {

// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already generated response body
if (res.pgRestifyResponseBody){
return next();
}

var table = self.convertResourceToTable(req.params.resource);
var id = req.params.id;
var data = self.convertFieldsToColumns(req.body);
Expand All @@ -275,6 +302,8 @@ function PgRestify(config, postInitFunction) {

res.status(200);

res.pgRestifyResponseBody = result;

return next();

}
Expand All @@ -288,6 +317,11 @@ function PgRestify(config, postInitFunction) {

self.executeWithHooks(req, res, next, 'delete', function(dbClient, next) {

// Skip if res.pgRestifyResponseBody is not empty, we assume preHook already generated response body
if (res.pgRestifyResponseBody){
return next();
}

var table = self.convertResourceToTable(req.params.resource);
var id = req.params.id;

Expand Down
27 changes: 27 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,35 @@ curl -X POST \

**Response**
```
# Header:
HTTP 201 Created
location: http://127.0.0.1:8080/api/generic/user-alert-messages/1
# Body (JSON pg SQL INSERT result) example:
{
"command": "INSERT",
"rowCount": 1,
"oid": 0,
"rows": [
{
"id": 1
}
],
"fields": [
{
"name": "id",
"tableID": 17558,
"columnID": 1,
"dataTypeID": 23,
"dataTypeSize": 4,
"dataTypeModifier": -1,
"format": "text"
}
],
"_parsers": [
null
],
"rowAsArray": false
}
```

---
Expand Down

0 comments on commit 7a24cbe

Please sign in to comment.