Skip to content

Commit

Permalink
Merge d2acf32 into 2fe1839
Browse files Browse the repository at this point in the history
  • Loading branch information
tywalch committed Nov 10, 2022
2 parents 2fe1839 + d2acf32 commit 9495a50
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,8 @@ All notable changes to this project will be documented in this file. Breaking ch

## [2.2.5] - 2022-11-09
### Fixed
- Addressed [issue#172](https://github.com/tywalch/electrodb/issues/172), where clause mishandling of nested attribute references
- Addressed [issue#172](https://github.com/tywalch/electrodb/issues/172), where clause mishandling of nested attribute references

## [2.2.6] - 2022-11-10
### Fixed
- Addressed issue where scans would incorrectly accumulate filters across usages
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.2.5",
"version": "2.2.6",
"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: 5 additions & 1 deletion src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Entity {
this._whereBuilder = new WhereFactory(this.model.schema.attributes, FilterOperations);
this._clausesWithFilters = this._filterBuilder.injectFilterClauses(clauses, this.model.filters);
this._clausesWithFilters = this._whereBuilder.injectWhereClauses(this._clausesWithFilters);
this.scan = this._makeChain(TableIndex, this._clausesWithFilters, clauses.index, {_isPagination: true}).scan();

this.query = {};
for (let accessPattern in this.model.indexes) {
let index = this.model.indexes[accessPattern].index;
Expand All @@ -75,6 +75,10 @@ class Entity {
this.schema = model;
}

get scan() {
return this._makeChain(TableIndex, this._clausesWithFilters, clauses.index, {_isPagination: true}).scan();
}

setIdentifier(type = "", identifier = "") {
if (!this.identifiers[type]) {
throw new e.ElectroError(e.ErrorCodes.InvalidIdentifier, `Invalid identifier type: "${type}". Valid identifiers include: ${u.commaSeparatedString(Object.keys(this.identifiers))}`);
Expand Down
118 changes: 117 additions & 1 deletion test/ts_connected.where.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,121 @@ describe("Where Clause Queries", () => {
},
"FilterExpression": "#example.#from <= :from0 AND (#example.#to >= :to0 OR attribute_not_exists(#example.#to))"
});
})
});

it('scan should start a new chain with each use', () => {
const table = "your_table_name";

const MyEntity = new Entity(
{
model: {
entity: "tasks",
version: "1",
service: "taskapp"
},
attributes: {
team: {
type: "string",
required: true
},
task: {
type: "string",
required: true
},
project: {
type: "string",
required: true
},
complete: {
type: 'boolean'
},
example: {
type: 'map',
properties: {
from: {
type: 'number',
},
to: {
type: 'number',
},
},
}
},
indexes: {
projects: {
pk: {
field: "pk",
composite: ["team"]
},
sk: {
field: "sk",
// create composite keys for partial sort key queries
composite: ["project", "task"]
}
}
}
},
{ table }
);

const where1 = MyEntity.scan.where(({ complete }, { eq }) => eq(complete, false))
expect(where1.params()).to.deep.equal({
TableName: 'your_table_name',
ExpressionAttributeNames: {
'#complete': 'complete',
'#pk': 'pk',
'#sk': 'sk',
'#__edb_e__': '__edb_e__',
'#__edb_v__': '__edb_v__'
},
ExpressionAttributeValues: {
':complete0': false,
':pk': '$taskapp#team_',
':sk': '$tasks_1#project_',
':__edb_e__': 'tasks',
':__edb_v__': '1'
},
FilterExpression: 'begins_with(#pk, :pk) AND #__edb_e__ = :__edb_e__ AND #__edb_v__ = :__edb_v__ AND begins_with(#sk, :sk) AND #complete = :complete0'
});

const where2 = MyEntity.scan.where(({ complete }, { eq }) => eq(complete, true))
expect(where2.params()).to.deep.equal({
TableName: 'your_table_name',
ExpressionAttributeNames: {
'#complete': 'complete',
'#pk': 'pk',
'#sk': 'sk',
'#__edb_e__': '__edb_e__',
'#__edb_v__': '__edb_v__'
},
ExpressionAttributeValues: {
':complete0': true,
':pk': '$taskapp#team_',
':sk': '$tasks_1#project_',
':__edb_e__': 'tasks',
':__edb_v__': '1'
},
FilterExpression: 'begins_with(#pk, :pk) AND #__edb_e__ = :__edb_e__ AND #__edb_v__ = :__edb_v__ AND begins_with(#sk, :sk) AND #complete = :complete0'
});

const where3 = MyEntity.scan.where(({ complete }, { eq }) => eq(complete, true))
expect(where3.params()).to.deep.equal({
TableName: 'your_table_name',
ExpressionAttributeNames: {
'#complete': 'complete',
'#pk': 'pk',
'#sk': 'sk',
'#__edb_e__': '__edb_e__',
'#__edb_v__': '__edb_v__'
},
ExpressionAttributeValues: {
':complete0': true,
':pk': '$taskapp#team_',
':sk': '$tasks_1#project_',
':__edb_e__': 'tasks',
':__edb_v__': '1'
},
FilterExpression: 'begins_with(#pk, :pk) AND #__edb_e__ = :__edb_e__ AND #__edb_v__ = :__edb_v__ AND begins_with(#sk, :sk) AND #complete = :complete0'
});
});
})

0 comments on commit 9495a50

Please sign in to comment.