Skip to content

Commit

Permalink
Condition expressions could not be added for upsert. (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
tywalch committed Nov 23, 2022
1 parent e43a7ed commit 4bbb3c6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,8 @@ All notable changes to this project will be documented in this file. Breaking ch

## [2.3.1] - 2022-11-23
### Fixed
- Address issue#179, the query option `table` was not correctly propagated, resulting a failure for that declared the table name was "missing"
- Address issue#179, the query option `table` was not correctly propagated, resulting a failure for that declared the table name was "missing"

## [2.3.2] - 2022-11-23
### Fixed
- Upsert method would silently disregard `where` clause usage, and would not add condition expression to parameters.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electrodb",
"version": "2.3.1",
"version": "2.3.2",
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ const ErrorCodes = {
name: "InvalidListenerProvided",
sym: ErrorCode,
},
InvalidLoggerProvided: {
code: 1020,
section: "invalid-listener-provided",
name: "InvalidListenerProvided",
sym: ErrorCode,
},
InvalidClientProvided: {
code: 1021,
section: "invalid-client-provided",
Expand Down
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class FilterFactory {
case MethodTypes.patch:
case MethodTypes.delete:
case MethodTypes.get:
case MethodTypes.upsert:
return ExpressionTypes.ConditionExpression
default:
return ExpressionTypes.FilterExpression
Expand Down
1 change: 1 addition & 0 deletions src/where.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class WhereFactory {
case MethodTypes.patch:
case MethodTypes.delete:
case MethodTypes.remove:
case MethodTypes.upsert:
case MethodTypes.get:
return ExpressionTypes.ConditionExpression
default:
Expand Down
98 changes: 96 additions & 2 deletions test/ts_connected.where.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const client = new DynamoDB.DocumentClient({
region: "us-east-1",
endpoint: process.env.LOCAL_DYNAMO_ENDPOINT
});
const table = 'electro';

describe("Where Clause Queries", () => {
before(async () => sleep(1000));
});


describe("Where Clause Queries", () => {
before(async () => sleep(1000));
let WhereTests = new Entity({
Expand Down Expand Up @@ -542,4 +542,98 @@ describe("Where Clause Queries", () => {
FilterExpression: 'begins_with(#pk, :pk) AND #__edb_e__ = :__edb_e__ AND #__edb_v__ = :__edb_v__ AND begins_with(#sk, :sk) AND #complete = :complete0'
});
});
})

it('should upsert with a condition', () => {
const actors = new Entity(
{
model: {
entity: "actors",
version: "1",
service: "taskapp"
},
attributes: {
studio: {
type: "string",
required: true
},
productionHouse: {
type: "string",
required: true
},
project: {
type: "string",
required: true,
},
genre: {
type: 'string'
},
movie: {
type: 'string'
},
actor: {
type: 'string'
}
},
indexes: {
byStudio: {
pk: {
field: "pk",
composite: ["studio"]
},
sk: {
field: "sk",
// create composite keys for partial sort key queries
composite: ["productionHouse", "actor"]
}
},
byActor: {
index: 'gsi2pk-gsi2sk-index',
pk: {
field: 'gsi2pk',
composite: ['actor']
},
sk: {
field: 'gsi2sk',
composite: ['genre', 'movie']
}
}
}
},
{ table, client }
);
const params = actors.upsert({productionHouse: 'productionHouse1', studio: 'studio1', project: 'project2', actor: 'actor1'})
.where((attr, op) => op.eq(attr.project, 'project1'))
.params();

expect(params).to.deep.equal({
TableName: 'electro',
UpdateExpression: 'SET #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0, #studio = :studio_u0, #productionHouse = :productionHouse_u0, #project = :project_u0, #actor = :actor_u0, #gsi2pk = :gsi2pk_u0, #gsi2sk = :gsi2sk_u0',
ExpressionAttributeNames: {
'#project': 'project',
'#__edb_e__': '__edb_e__',
'#__edb_v__': '__edb_v__',
'#studio': 'studio',
'#productionHouse': 'productionHouse',
'#actor': 'actor',
'#gsi2pk': 'gsi2pk',
'#gsi2sk': 'gsi2sk'
},
ExpressionAttributeValues: {
':project0': 'project1',
':__edb_e___u0': 'actors',
':__edb_v___u0': '1',
':studio_u0': 'studio1',
':productionHouse_u0': 'productionHouse1',
':project_u0': 'project2',
':actor_u0': 'actor1',
':gsi2pk_u0': '$taskapp#actor_actor1',
':gsi2sk_u0': '$actors_1#genre_'
},
Key: {
pk: '$taskapp#studio_studio1',
sk: '$actors_1#productionhouse_productionhouse1#actor_actor1'
},
ConditionExpression: '#project = :project0'
});
});
});

0 comments on commit 4bbb3c6

Please sign in to comment.