Skip to content

Commit

Permalink
Fix update on empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
ianpogi5 committed Aug 28, 2020
1 parent 90aacc4 commit 571f2a3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const OPTIONS = ['-sharedDb'];
process.env.DDB_ENDPOINT = `http://localhost:${PORT}`;
process.env.DDB_REGION = 'localhost';
process.env.DDB_TABLE = 'kdc-model.localhost';
// process.env.DEBUG = 'ddb:update';

const setup = async () => {
const dynamoDB = new DynamoDB({
Expand Down
19 changes: 13 additions & 6 deletions src/helpers/db/ops/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ const update = async function update(fields) {
if (!fields) throw new Error('Missing fields to update');
if (!Array.isArray(fields)) throw new Error('fields is not an array');

const fieldsUpdated = [];
const data = this.toObject();
let SET = '';
let REMOVE = '';
const ExpressionAttributeNames = {};
const ExpressionAttributeValues = {};

let updateCtr = 0;
// Fields that need to be set
fields.forEach((f) => {
if (!this.isDirty(f)) return;
if (data[f] == null) return;
if (!data[f]) return;
if (SET !== '') SET += `, #${f} = :${f}`;
else SET = `SET #${f} = :${f}`;
ExpressionAttributeNames[`#${f}`] = f;
ExpressionAttributeValues[`:${f}`] = data[f];
updateCtr += 1;
fieldsUpdated.push(f);
});

// Fields that need to be removed
Expand All @@ -35,21 +35,28 @@ const update = async function update(fields) {
if (REMOVE === '') REMOVE += ` REMOVE #${f}`;
else REMOVE += `, #${f}`;
ExpressionAttributeNames[`#${f}`] = f;
updateCtr += 1;
fieldsUpdated.push(f);
});

// Nothing to update
if (updateCtr === 0) return;
if (fieldsUpdated.length === 0) return;

const params = {
Key: { ...this.pKey() },
UpdateExpression: `${SET}${REMOVE}`,
ExpressionAttributeNames,
ExpressionAttributeValues,
};

if (Object.keys(ExpressionAttributeValues).length !== 0)
params.ExpressionAttributeValues = ExpressionAttributeValues;

debug(params);
await this.db('update', params);

// remove updated fields from dirtyFields
this._dirtyFields = this._dirtyFields.filter(
(f) => fieldsUpdated.indexOf(f) < 0
);
};

export default update;
30 changes: 29 additions & 1 deletion tests/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Update', () => {
});
await dog.create();

expect.assertions(13);
expect.assertions(22);
try {
await dog.update();
} catch (error) {
Expand All @@ -44,6 +44,12 @@ describe('Update', () => {
expect(error.message).toBe('fields is not an array');
}

try {
await dog.update(['hello']);
} catch (error) {
expect(error).toBeUndefined();
}

const params = { Key: { pk: 'Retriever#1', sk: 'DOG#Retriever' } };
let obj = await DDB.db('get', params);
expect(obj.Item).toBeDefined();
Expand All @@ -52,13 +58,16 @@ describe('Update', () => {

dog.owner = 'me';
dog.birthDate = 'yesterday';
expect(dog.dirtyFields).toHaveLength(2);
await dog.update(['owner', 'color']);
expect(dog.dirtyFields).toHaveLength(1);
obj = await DDB.db('get', params);
expect(obj.Item.owner).toBe('me');
// Still undefined because it was included in update fields
expect(obj.Item.birthDate).toBeUndefined();

await dog.update(['owner', 'birthDate']);
expect(dog.dirtyFields).toHaveLength(0);
obj = await DDB.db('get', params);
expect(obj.Item.owner).toBe('me');
expect(obj.Item.birthDate).toBe('yesterday');
Expand All @@ -75,5 +84,24 @@ describe('Update', () => {
obj = await DDB.db('get', params);
expect(obj.Item.address.address1).toBe('home');
expect(obj.Item.address.address2).toBeUndefined();

dog.color = '';
dog.birthDate = '';
await dog.update(['color', 'birthDate']);
obj = await DDB.db('get', params);
expect(obj.Item.color).toBeUndefined();
expect(obj.Item.birthDate).toBeUndefined();

await dog.update(['color', 'birthDate']);
obj = await DDB.db('get', params);
expect(obj.Item.color).toBeUndefined();
expect(obj.Item.birthDate).toBeUndefined();

dog.color = 'white';
dog.birthDate = 'now';
await dog.update(['color', 'birthDate']);
obj = await DDB.db('get', params);
expect(obj.Item.color).toBe('white');
expect(obj.Item.birthDate).toBe('now');
});
});

0 comments on commit 571f2a3

Please sign in to comment.