Skip to content

Commit 2950ce1

Browse files
authored
fix: parse returns null for invalid data and allows ignoreOwnership override (#553, #561) (#562)
1 parent d73c644 commit 2950ce1

5 files changed

Lines changed: 104 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,9 @@ All notable changes to this project will be documented in this file. Breaking ch
638638

639639
## [3.7.1]
640640
### Added
641-
- [Issue #543](https://github.com/tywalch/electrodb/issues/543); New `client` option for `go()` method allowing dynamic DynamoDB client override at query execution time. This enables use cases like multi-region routing, per-request client selection, and easier testing. Supported for all operation types: entity operations, collection queries, batch operations, and transactions.
641+
- [Issue #543](https://github.com/tywalch/electrodb/issues/543); New `client` option for `go()` method allowing dynamic DynamoDB client override at query execution time. This enables use cases like multi-region routing, per-request client selection, and easier testing. Supported for all operation types: entity operations, collection queries, batch operations, and transactions.
642+
643+
## [3.7.2]
644+
### Fixed
645+
- [Issue #553](https://github.com/tywalch/electrodb/issues/553); Fixed `parse` method returning data instead of `null` for invalid items.
646+
- [Issue #561](https://github.com/tywalch/electrodb/issues/561); Fixed `parse` method not allowing the `ignoreOwnership` option to be overridden.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "electrodb",
3-
"version": "3.7.1",
3+
"version": "3.7.2",
44
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
55
"main": "index.js",
66
"scripts": {

src/entity.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ class Entity {
921921
is(item, config) {
922922
return (
923923
config.ignoreOwnership &&
924+
config.attributes &&
925+
config.attributes.length > 0 &&
924926
!this._itemIncludesKeys(item)
925927
) || (
926928
(config.ignoreOwnership || config.hydrate) &&
@@ -1019,8 +1021,8 @@ class Entity {
10191021
return null;
10201022
}
10211023
const config = {
1022-
...(options || {}),
10231024
ignoreOwnership: true,
1025+
...(options || {}),
10241026
};
10251027
return this.formatResponse(item, TableIndex, config);
10261028
}

test/offline.entity.spec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8920,4 +8920,96 @@ describe("Entity", () => {
89208920
});
89218921
});
89228922
});
8923+
8924+
describe("parse", () => {
8925+
const entity = new Entity({
8926+
model: {
8927+
entity: "Item",
8928+
service: "Playground",
8929+
version: "1",
8930+
},
8931+
attributes: {
8932+
id: {
8933+
type: "string",
8934+
required: true,
8935+
},
8936+
name: {
8937+
type: "string",
8938+
required: true,
8939+
},
8940+
description: {
8941+
type: "string",
8942+
required: true,
8943+
},
8944+
},
8945+
indexes: {
8946+
byId: {
8947+
pk: {
8948+
field: "pk",
8949+
composite: ["id"],
8950+
},
8951+
sk: {
8952+
field: "sk",
8953+
composite: ["id"],
8954+
},
8955+
},
8956+
},
8957+
});
8958+
8959+
it("should return null when item has no key fields", () => {
8960+
const result = entity.parse({ Item: { description: "desc" } });
8961+
expect(result.data).to.be.null;
8962+
});
8963+
8964+
it("should return null when item is empty", () => {
8965+
const result = entity.parse({ Item: {} });
8966+
expect(result.data).to.be.null;
8967+
});
8968+
8969+
it("should return null when item has unrelated attributes", () => {
8970+
const result = entity.parse({ Item: { other: "value" } });
8971+
expect(result.data).to.be.null;
8972+
});
8973+
8974+
it("should parse a valid item with correct keys", () => {
8975+
const result = entity.parse({
8976+
Item: {
8977+
pk: "$playground#id_123",
8978+
sk: "$item_1#id_123",
8979+
id: "123",
8980+
name: "test",
8981+
description: "desc",
8982+
},
8983+
});
8984+
expect(result.data).to.deep.equal({ id: "123", name: "test", description: "desc" });
8985+
});
8986+
8987+
it("should return null when ignoreOwnership is overridden to false and item lacks entity identifiers", () => {
8988+
const result = entity.parse({
8989+
Item: {
8990+
pk: "$playground#id_123",
8991+
sk: "$item_1#id_123",
8992+
id: "123",
8993+
name: "test",
8994+
description: "desc",
8995+
},
8996+
}, { ignoreOwnership: false });
8997+
expect(result.data).to.be.null;
8998+
});
8999+
9000+
it("should parse when ignoreOwnership is false and item has correct entity identifiers", () => {
9001+
const result = entity.parse({
9002+
Item: {
9003+
pk: "$playground#id_123",
9004+
sk: "$item_1#id_123",
9005+
id: "123",
9006+
name: "test",
9007+
description: "desc",
9008+
__edb_e__: "Item",
9009+
__edb_v__: "1",
9010+
},
9011+
}, { ignoreOwnership: false });
9012+
expect(result.data).to.deep.equal({ id: "123", name: "test", description: "desc" });
9013+
});
9014+
});
89239015
});

0 commit comments

Comments
 (0)