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

Commit

Permalink
implements #140
Browse files Browse the repository at this point in the history
  • Loading branch information
arafato committed Mar 17, 2018
1 parent ad4cd6b commit e81f7fd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
21 changes: 21 additions & 0 deletions lib/actions/table/InsertOrMergeEntity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

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

class InsertOrMergeEntity {
constructor() {
}

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

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

Expand Down
37 changes: 25 additions & 12 deletions lib/core/table/TableStorageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,13 @@ class TableStorageManager {
}

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 });
const proxy = this._insertOrMergeEntity(request.partitionKey, request.rowKey, request.tableName, request.payload);
return BbPromise.resolve(new AzuriteTableResponse({ proxy: proxy }));
}

// 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.attribs[key]) {
res.attribs[key] = entity.attribs[key];
}
}
res.odata = entity.odata;
coll.update(res);
return BbPromise.resolve(new AzuriteTableResponse({ proxy: new EntityProxy(res) }));
insertOrMergeEntity(request) {
const proxy = this._insertOrMergeEntity(request.partitionKey, request.rowKey, request.tableName, request.payload);
return BbPromise.resolve(new AzuriteTableResponse({ proxy: proxy }));
}

_getTable(name) {
Expand Down Expand Up @@ -208,6 +202,25 @@ class TableStorageManager {
const entityProxy = new EntityProxy(coll.insert(entity));
return entityProxy;
}

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

if (res !== null) {
// 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.attribs[key]) {
res.attribs[key] = entity.attribs[key];
}
}
res.odata = entity.odata;
coll.update(res);
return new EntityProxy(res);
}
return this._createOrUpdateEntity(partitionKey, rowKey, tableName, rawEntity);
}
}


Expand Down
5 changes: 5 additions & 0 deletions lib/middleware/table/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const BbPromise = require('bluebird'),
updateEntity = require('./../../actions/table/UpdateEntity'),
insertOrReplaceEntity = require('./../../actions/table/InsertOrReplaceEntity'),
mergeEntity = require('./../../actions/table/MergeEntity'),
insertOrMergeEntity = require('./../../actions/table/InsertOrMergeEntity'),
createTable = require('./../../actions/table/CreateTable');

module.exports = (req, res) => {
Expand Down Expand Up @@ -61,4 +62,8 @@ actions[Operations.INSERT_OR_REPLACE_ENTITY] = (request, res) => {

actions[Operations.MERGE_ENTITY] = (request, res) => {
mergeEntity.process(request, res);
}

actions[Operations.INSERT_OR_MERGE_ENTITY] = (request, res) => {
insertOrMergeEntity.process(request, res);
}
5 changes: 5 additions & 0 deletions lib/middleware/table/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ validations[Operations.MERGE_ENTITY] = (valContext) => {
.run(TableExistsVal)
.run(EntityExistsVal)
.run(EntityIfMatchVal);
}

validations[Operations.INSERT_OR_MERGE_ENTITY] = (valContext) => {
valContext
.run(TableExistsVal);
}
4 changes: 3 additions & 1 deletion lib/routes/table/EntityRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ module.exports = (app) => {
next();
})
.merge((req, res, next) => {
req.azuriteOperation = Operations.MERGE_ENTITY;
req.azuriteRequest = new AzuriteTableRequest({ req: req, payload: req.payload });
req.azuriteOperation = req.azuriteRequest.httpProps[N.IF_MATCH]
? Operations.MERGE_ENTITY
: Operations.INSERT_OR_MERGE_ENTITY;
next();
});
}

0 comments on commit e81f7fd

Please sign in to comment.