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

Multiple functionality #10

Closed
wants to merge 17 commits into from
Closed
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
33 changes: 32 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 @@ -148,6 +149,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 +184,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 +228,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 +252,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 +269,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 +299,8 @@ function PgRestify(config, postInitFunction) {

res.status(200);

res.pgRestifyResponseBody = result;

return next();

}
Expand All @@ -288,6 +314,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