electrodb
Advanced tools
Comparing version 0.8.8 to 0.8.9
{ | ||
"name": "electrodb", | ||
"version": "0.8.8", | ||
"version": "0.8.9", | ||
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb", | ||
@@ -26,3 +26,6 @@ "main": "index.js", | ||
"uuid": "^7.0.1" | ||
}, | ||
"dependencies": { | ||
"jsonschema": "1.2.5" | ||
} | ||
} |
@@ -126,3 +126,3 @@ # ElectroDB | ||
field: "PK", | ||
facets: "value" | ||
facets: ["value"] | ||
}, | ||
@@ -196,3 +196,5 @@ sk: { | ||
"label": [string] | ||
"cast": ["number"|"string"|"boolean"] | ||
"cast": ["number"|"string"|"boolean"], | ||
get: (attribute, schema) => value, | ||
set: (attribute, schema) => value | ||
} | ||
@@ -212,2 +214,4 @@ } | ||
`cast` | `"number"`, `"string"`, `"boolean"` | no | Optionally cast attribute values when interacting with DynamoDB. Current options include: "number", "string", and "boolean". | ||
`set` | `(attribute, schema) => value` | no | A synchronous callback allowing you apply changes to a value before it is set in params or applied to the database. First value represents the value passed to ElectroDB, second value are the attributes passed on that update/put | ||
`get` | `(attribute, schema) => value` | no | A synchronous callback allowing you apply changes to a value after it is retrieved from the database. First value represents the value passed to ElectroDB, second value are the attributes retrieved from the database. | ||
@@ -214,0 +218,0 @@ ## Indexes |
@@ -5,2 +5,3 @@ "use strict"; | ||
const { FilterFactory, FilterTypes } = require("./filters"); | ||
const validations = require("./validations"); | ||
@@ -70,2 +71,3 @@ let clauses = { | ||
} | ||
state.query.put.data = Object.assign({}, record); | ||
@@ -323,3 +325,5 @@ return state; | ||
_validateModel(model = {}) {} | ||
_validateModel(model = {}) { | ||
return validations.model(model); | ||
} | ||
@@ -411,3 +415,3 @@ get(facets = {}) { | ||
raw: options.raw, | ||
params: options.params || {} | ||
params: options.params || {}, | ||
}; | ||
@@ -438,3 +442,13 @@ for (let [name, value] of Object.entries(config.params)) { | ||
} | ||
return data; | ||
let appliedGets; | ||
if (Array.isArray(data)) { | ||
appliedGets = data.map(item => | ||
this.model.schema.applyAttributeGetters(item), | ||
); | ||
} else { | ||
appliedGets = this.model.schema.applyAttributeGetters(data); | ||
} | ||
// let appliedGets = this.model.schema.applyAttributeGetters(data); | ||
return appliedGets; | ||
} catch (err) { | ||
@@ -504,6 +518,7 @@ if (config.originalErr) { | ||
_makeUpdateParams({ set } = {}, pk = {}, sk = {}) { | ||
let { indexKey, updatedKeys } = this._getUpdatedKeys(pk, sk, set); | ||
let translatedAttributes = this.model.schema.translateToFields(set); | ||
let setAttributes = this.model.schema.applyAttributeSetters(set); | ||
let { indexKey, updatedKeys } = this._getUpdatedKeys(pk, sk, setAttributes); | ||
let transatedFields = this.model.schema.translateToFields(setAttributes); | ||
let item = { | ||
...translatedAttributes, | ||
...transatedFields, | ||
...updatedKeys, | ||
@@ -528,7 +543,9 @@ }; | ||
_makePutParams({ data } = {}, pk, sk) { | ||
let { updatedKeys } = this._getUpdatedKeys(pk, sk, data); | ||
let translatedAttributes = this.model.schema.translateToFields(data); | ||
let setAttributes = this.model.schema.applyAttributeSetters(data); | ||
let { updatedKeys } = this._getUpdatedKeys(pk, sk, setAttributes); | ||
let transatedFields = this.model.schema.translateToFields(setAttributes); | ||
let params = { | ||
Item: { | ||
...translatedAttributes, | ||
...transatedFields, | ||
...updatedKeys, | ||
@@ -792,7 +809,9 @@ }, | ||
throw new Error( | ||
`Incomplete facets: Without the facets ${incomplete.join( | ||
", ", | ||
)} the following access patterns ${incompleteAccessPatterns.join( | ||
", ", | ||
)} cannot be updated`, | ||
`Incomplete facets: Without the facets ${incomplete | ||
.filter(val => val !== undefined) | ||
.join( | ||
", ", | ||
)} the following access patterns ${incompleteAccessPatterns | ||
.filter(val => val !== undefined) | ||
.join(", ")}cannot be updated.`, | ||
); | ||
@@ -799,0 +818,0 @@ } |
@@ -15,2 +15,4 @@ const { KeyTypes } = require("./types"); | ||
this.validate = this._makeValidate(definition.validate); | ||
this.get = this._makeGet(definition.name, definition.get); | ||
this.set = this._makeSet(definition.name, definition.set); | ||
this.indexes = [...(definition.indexes || [])]; | ||
@@ -22,2 +24,26 @@ let { type, enumArray } = this._makeType(this.name, definition.type); | ||
_makeGet(name, get) { | ||
if (typeof get === "function") { | ||
return get; | ||
} else if (get === undefined) { | ||
return attr => attr; | ||
} else { | ||
throw new Error( | ||
`Invalid "get" property for attribure ${name}. Please ensure value is a function or undefined.`, | ||
); | ||
} | ||
} | ||
_makeSet(name, set) { | ||
if (typeof set === "function") { | ||
return set; | ||
} else if (set === undefined) { | ||
return attr => attr; | ||
} else { | ||
throw new Error( | ||
`Invalid "set" property for attribure ${name}. Please ensure value is a function or undefined.`, | ||
); | ||
} | ||
} | ||
_makeCast(name, cast) { | ||
@@ -188,3 +214,3 @@ if (cast !== undefined && !CastTypes.includes(cast)) { | ||
} | ||
if (attribute.attr && facets.fields.includes(attribute.attr)) { | ||
if (attribute.field && facets.fields.includes(attribute.field)) { | ||
continue; | ||
@@ -196,3 +222,3 @@ } | ||
required: !!attribute.required, | ||
attr: attribute.attr || name, | ||
field: attribute.field || name, | ||
hide: !!attribute.hide, | ||
@@ -204,17 +230,19 @@ default: attribute.default, | ||
type: attribute.type, | ||
get: attribute.get, | ||
set: attribute.set, | ||
}; | ||
if (usedAttrs[definition.attr] || usedAttrs[name]) { | ||
if (usedAttrs[definition.field] || usedAttrs[name]) { | ||
invalidProperties.push({ | ||
name, | ||
property: "attr", | ||
value: definition.attr, | ||
expected: `Unique attr property, already used by attribute ${ | ||
usedAttrs[definition.attr] | ||
property: "field", | ||
value: definition.field, | ||
expected: `Unique field property, already used by attribute ${ | ||
usedAttrs[definition.field] | ||
}`, | ||
}); | ||
} else { | ||
usedAttrs[definition.attr] = definition.name; | ||
usedAttrs[definition.field] = definition.name; | ||
} | ||
translationForTable[definition.name] = definition.attr; | ||
translationForRetrieval[definition.attr] = definition.name; | ||
translationForTable[definition.name] = definition.field; | ||
translationForRetrieval[definition.field] = definition.name; | ||
normalized[name] = new Attribute(definition); | ||
@@ -246,2 +274,26 @@ } | ||
applyAttributeGetters(payload = {}) { | ||
let attributes = { ...payload }; | ||
for (let [name, value] of Object.entries(attributes)) { | ||
if (this.attributes[name] === undefined) { | ||
attributes[name] = value; | ||
} else { | ||
attributes[name] = this.attributes[name].get(value, { ...payload }); | ||
} | ||
} | ||
return attributes; | ||
} | ||
applyAttributeSetters(payload = {}) { | ||
let attributes = { ...payload }; | ||
for (let [name, value] of Object.entries(attributes)) { | ||
if (this.attributes[name] !== undefined) { | ||
attributes[name] = this.attributes[name].set(value, { ...payload }); | ||
} else { | ||
attributes[name] = value; | ||
} | ||
} | ||
return attributes; | ||
} | ||
translateToFields(payload = {}) { | ||
@@ -262,3 +314,3 @@ let record = {}; | ||
let value = payload[attribute.name]; | ||
record[attribute.field] = attribute.getValidate(value); | ||
record[attribute.name] = attribute.getValidate(value); | ||
} | ||
@@ -278,3 +330,3 @@ return record; | ||
} else { | ||
record[attribute.field] = attribute.getValidate(value); | ||
record[attribute.name] = attribute.getValidate(value); | ||
} | ||
@@ -281,0 +333,0 @@ } |
@@ -11,4 +11,4 @@ const { Entity } = require("../src/entity"); | ||
let model = { | ||
service: "MallStoreDirectory", | ||
entity: "MallStores", | ||
service: "BugBeater", | ||
entity: "test", | ||
table: "electro", | ||
@@ -322,2 +322,3 @@ version: "1", | ||
}); | ||
let belowMarketUnits = await MallStores.query | ||
@@ -327,2 +328,3 @@ .units({ mall, building }) | ||
.go(); | ||
expect(belowMarketUnits) | ||
@@ -332,5 +334,4 @@ .to.be.an("array") | ||
.and.deep.have.members(filteredStores); | ||
// console.log(belowMarketUnits); | ||
}).timeout(20000); | ||
}); | ||
}); |
@@ -24,3 +24,3 @@ const { Entity, clauses } = require("../src/entity"); | ||
required: true, | ||
field: "mallId", | ||
field: "mall", | ||
}, | ||
@@ -304,8 +304,8 @@ store: { | ||
UpdateExpression: | ||
"SET #mall = :mall, #store = :store, #building = :building, #unit = :unit, #category = :category, #leaseEnd = :leaseEnd, #rent = :rent", | ||
"SET #mall = :mall, #storeId = :storeId, #buildingId = :buildingId, #unitId = :unitId, #category = :category, #leaseEnd = :leaseEnd, #rent = :rent", | ||
ExpressionAttributeNames: { | ||
"#mall": "mall", | ||
"#store": "store", | ||
"#building": "building", | ||
"#unit": "unit", | ||
"#storeId": "storeId", | ||
"#buildingId": "buildingId", | ||
"#unitId": "unitId", | ||
"#category": "category", | ||
@@ -317,5 +317,5 @@ "#leaseEnd": "leaseEnd", | ||
":mall": mall, | ||
":store": store, | ||
":building": building, | ||
":unit": unit, | ||
":storeId": store, | ||
":buildingId": building, | ||
":unitId": unit, | ||
":category": category, | ||
@@ -340,13 +340,14 @@ ":leaseEnd": leaseEnd, | ||
}).params(); | ||
expect(put).to.deep.equal({ | ||
Item: { | ||
id: put.Item.id, | ||
storeLocationId: put.Item.storeLocationId, | ||
mall, | ||
store, | ||
building, | ||
unit, | ||
storeId: store, | ||
buildingId: building, | ||
unitId: unit, | ||
category, | ||
leaseEnd, | ||
rent, | ||
pk: `$mallstoredirectory_1#id_${put.Item.id}`, | ||
pk: `$mallstoredirectory_1#id_${put.Item.storeLocationId}`, | ||
gsi1pk: `$MallStoreDirectory_1#mall_${mall}`.toLowerCase(), | ||
@@ -353,0 +354,0 @@ gsi1sk: `$MallStores#building_${building}#unit_${unit}#store_${store}`.toLowerCase(), |
@@ -68,4 +68,4 @@ const moment = require("moment"); | ||
pk: { | ||
facets: "pk", | ||
compose: ["id"], | ||
field: "pk", | ||
facets: ["id"], | ||
}, | ||
@@ -76,8 +76,8 @@ }, | ||
pk: { | ||
facets: "gsi1pk", | ||
compose: ["mall"], | ||
field: "gsi1pk", | ||
facets: ["mall"], | ||
}, | ||
sk: { | ||
facets: "gsi1sk", | ||
compose: ["building", "unit", "store"], | ||
field: "gsi1sk", | ||
facets: ["building", "unit", "store"], | ||
}, | ||
@@ -88,8 +88,8 @@ }, | ||
pk: { | ||
facets: "gsi2pk", | ||
compose: ["mall"], | ||
field: "gsi2pk", | ||
facets: ["mall"], | ||
}, | ||
sk: { | ||
facets: "gsi2sk", | ||
compose: ["leaseEnd", "store", "building", "unit"], | ||
field: "gsi2sk", | ||
facets: ["leaseEnd", "store", "building", "unit"], | ||
}, | ||
@@ -100,8 +100,8 @@ }, | ||
pk: { | ||
facets: "gsi3pk", | ||
compose: ["mall"], | ||
field: "gsi3pk", | ||
facets: ["mall"], | ||
}, | ||
sk: { | ||
facets: "gsi3sk", | ||
compose: ["category", "building", "unit", "store"], | ||
field: "gsi3sk", | ||
facets: ["category", "building", "unit", "store"], | ||
}, | ||
@@ -112,8 +112,8 @@ }, | ||
pk: { | ||
facets: "gsi4pk", | ||
compose: ["store"], | ||
field: "gsi4pk", | ||
facets: ["store"], | ||
}, | ||
sk: { | ||
facets: "gsi4sk", | ||
compose: ["mall", "building", "unit"], | ||
field: "gsi4sk", | ||
facets: ["mall", "building", "unit"], | ||
}, | ||
@@ -120,0 +120,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
199142
3290
912
1
+ Addedjsonschema@1.2.5
+ Addedjsonschema@1.2.5(transitive)