Skip to content

Commit

Permalink
adds tests, doc updates (tywalch#168)
Browse files Browse the repository at this point in the history
* adds tests, doc updates

* adds test

* fixes wording

* version bump
  • Loading branch information
tywalch authored Nov 3, 2022
1 parent 96ee020 commit 1b1a269
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 24 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,8 @@ All notable changes to this project will be documented in this file. Breaking ch
## [2.2.0] - 2022-10-31
### Added
- A BIG addition to the library: Clustered Indexes. Clustered indexes allow for Collections to be composed of more similar, homogenous data.
- The addition of new Entity and Service methods: `setTableName`, `getTableName`, `setClient`, `getClient`.
- The addition of new Entity and Service methods: `setTableName`, `getTableName`, `setClient`, `getClient`.

## [2.2.1] - 2022-11-02
### Fixed
- Addressed github issue #144, root map attributes would set an empty object regardless if the user supplied it.
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.0",
"version": "2.2.1",
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3037,7 +3037,7 @@ class Entity {
indexAccessPattern,
indexHasSubCollections,
} = this._normalizeIndexes(model.indexes);
let schema = new Schema(model.attributes, facets, {client});
let schema = new Schema(model.attributes, facets, {client, isRoot: true});
let filters = this._normalizeFilters(model.filters);
// todo: consider a rename
let prefixes = this._normalizeKeyFixings({service, entity, version, indexes, modelVersion, clusteredIndexes});
Expand Down
62 changes: 45 additions & 17 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,22 @@ class MapAttribute extends Attribute {
traverser: this.traverser
});
this.properties = properties;
this.isRoot = !!definition.isRoot;
this.get = this._makeGet(definition.get, properties);
this.set = this._makeSet(definition.set, properties);
}

_makeGet(get, properties) {
this._checkGetSet(get, "get");

const getter = get || ((attr) => attr);

const getter = get || ((val) => {
const isEmpty = !val || Object.keys(val).length === 0;
const isNotRequired = !this.required;
const isRoot = this.isRoot;
if (isEmpty && isRoot && !isNotRequired) {
return undefined;
}
return val;
});
return (values, siblings) => {
const data = {};

Expand All @@ -541,6 +548,9 @@ class MapAttribute extends Attribute {
}

if (values === undefined) {
if (!get) {
return undefined;
}
return getter(data, siblings);
}

Expand All @@ -561,11 +571,23 @@ class MapAttribute extends Attribute {

_makeSet(set, properties) {
this._checkGetSet(set, "set");
const setter = set || ((attr) => attr);
const setter = set || ((val) => {
const isEmpty = !val || Object.keys(val).length === 0;
const isNotRequired = !this.required;
const isRoot = this.isRoot;
if (isEmpty && isRoot && !isNotRequired) {
return undefined;
}
return val;
});

return (values, siblings) => {
const data = {};
if (values === undefined) {
return setter(data, siblings);
if (!set) {
return undefined;
}
return setter(values, siblings);
}
for (const name of Object.keys(properties.attributes)) {
const attribute = properties.attributes[name];
Expand Down Expand Up @@ -624,18 +646,18 @@ class MapAttribute extends Attribute {
}

val(value) {
const getValue = (v) => {
v = this.cast(v);
if (v === undefined) {
v = this.default();
const incomingIsEmpty = value === undefined;
let fromDefault = false;
let data;
if (value === undefined) {
data = this.default();
if (data !== undefined) {
fromDefault = true;
}
return v;
} else {
data = value;
}

let data = value === undefined
? getValue(value)
: value;

const valueType = getValueType(data);

if (data === undefined) {
Expand All @@ -654,6 +676,10 @@ class MapAttribute extends Attribute {
}
}

if (Object.keys(response).length === 0 && !fromDefault && this.isRoot && !this.required && incomingIsEmpty) {
return undefined;
}

return response;
}
}
Expand Down Expand Up @@ -959,9 +985,9 @@ class SetAttribute extends Attribute {
}

class Schema {
constructor(properties = {}, facets = {}, {traverser = new AttributeTraverser(), client, parent} = {}) {
constructor(properties = {}, facets = {}, {traverser = new AttributeTraverser(), client, parent, isRoot} = {}) {
this._validateProperties(properties, parent);
let schema = Schema.normalizeAttributes(properties, facets, {traverser, client, parent});
let schema = Schema.normalizeAttributes(properties, facets, {traverser, client, parent, isRoot});
this.client = client;
this.attributes = schema.attributes;
this.enums = schema.enums;
Expand All @@ -972,9 +998,10 @@ class Schema {
this.requiredAttributes = schema.requiredAttributes;
this.translationForWatching = this._formatWatchTranslations(this.attributes);
this.traverser = traverser;
this.isRoot = !!isRoot;
}

static normalizeAttributes(attributes = {}, facets = {}, {traverser, client, parent} = {}) {
static normalizeAttributes(attributes = {}, facets = {}, {traverser, client, parent, isRoot} = {}) {
const attributeHasParent = !!parent;
let invalidProperties = [];
let normalized = {};
Expand Down Expand Up @@ -1073,6 +1100,7 @@ class Schema {
postfix,
traverser,
isKeyField,
isRoot: !!isRoot,
label: attribute.label,
required: !!attribute.required,
default: attribute.default,
Expand Down
Loading

0 comments on commit 1b1a269

Please sign in to comment.