Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
implements #137
Browse files Browse the repository at this point in the history
  • Loading branch information
arafato committed Mar 17, 2018
1 parent 7715fd7 commit b622ce5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
22 changes: 22 additions & 0 deletions lib/actions/table/MergeEntity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const AzuriteTableResponse = require('./../../model/table/AzuriteTableResponse'),
tableStorageManager = require('./../../core/table/TableStorageManager'),
ODataMode = require('./../../core/Constants').ODataMode,
N = require('./../../core/HttpHeaderNames');

class MergeEntity {
constructor() {
}

process(request, res) {
tableStorageManager.mergeEntity(request)
.then((response) => {
response.addHttpProperty(N.ETAG, response.proxy.etag);
res.set(response.httpProps);
res.status(204).send();
});
}
}

module.exports = new MergeEntity;
3 changes: 2 additions & 1 deletion lib/core/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ const Operations = {
QUERY_TABLE: 'QueryTable',
QUERY_ENTITY: 'QueryEntity',
UPDATE_ENTITY: 'UpdateEntity',
INSERT_OR_REPLACE_ENTITY: 'InsertOrReplaceEntity'
INSERT_OR_REPLACE_ENTITY: 'InsertOrReplaceEntity',
MERGE_ENTITY: 'MergeEntity'
}
}

Expand Down
16 changes: 16 additions & 0 deletions lib/core/table/TableStorageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ class TableStorageManager {
return BbPromise.resolve(new AzuriteTableResponse({ proxy: proxy }));
}

mergeEntity(request) {
const coll = this.db.getCollection(request.tableName),
entity = EntityGenerator.generateEntity(request.payload, request.tableName),
res = coll.findOne({ partitionKey: request.partitionKey, rowKey: request.rowKey });

// A property cannot be removed with a Merge Entity operation (in contrast to an update operation).
for (const key of Object.keys(entity.attribs)) {
if (entity[key]) {
res.attribs[key] = entity.attribs[key];
}
}
res.odata = entity.odata;
coll.update(res);
return new EntityProxy(res);
}

_getTable(name) {
const coll = this.db.getCollection(Tables.Tables);
const result = coll.chain()
Expand Down
8 changes: 7 additions & 1 deletion lib/middleware/table/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const BbPromise = require('bluebird'),
queryTable = require('./../../actions/table/QueryTable'),
queryEntities = require('./../../actions/table/QueryEntities'),
updateEntity = require('./../../actions/table/UpdateEntity'),
insertOrReplaceEntity = require('./../../actions/table/InsertOrReplaceEntity'),
mergeEntity = require('./../../actions/table/MergeEntity'),
createTable = require('./../../actions/table/CreateTable');

module.exports = (req, res) => {
Expand Down Expand Up @@ -54,5 +56,9 @@ actions[Operations.UPDATE_ENTITY] = (request, res) => {
}

actions[Operations.INSERT_OR_REPLACE_ENTITY] = (request, res) => {
updateEntity.process(request, res);
insertOrReplaceEntity.process(request, res);
}

actions[Operations.MERGE_ENTITY] = (request, res) => {
mergeEntity.process(request, res);
}
7 changes: 7 additions & 0 deletions lib/middleware/table/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ validations[Operations.UPDATE_ENTITY] = (valContext) => {
validations[Operations.INSERT_OR_REPLACE_ENTITY] = (valContext) => {
valContext
.run(TableExistsVal);
}

validations[Operations.MERGE_ENTITY] = (valContext) => {
valContext
.run(TableExistsVal)
.run(EntityExistsVal)
.run(EntityIfMatchVal);
}
5 changes: 5 additions & 0 deletions lib/routes/table/EntityRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ module.exports = (app) => {
req.azuriteOperation = Operations.DELETE_ENTITY;
req.azuriteRequest = new AzuriteTableRequest({ req: req });
next();
})
.merge((req, res, next) => {
req.azuriteOperation = Operations.MERGE_ENTITY;
req.azuriteRequest = new AzuriteTableRequest({ req: req, payload: req.payload });
next();
});
}

0 comments on commit b622ce5

Please sign in to comment.