electrodb
Advanced tools
Comparing version 0.9.4 to 0.9.5
{ | ||
"name": "electrodb", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,3 @@ const { QueryTypes, MethodTypes } = require("./types"); | ||
index: { | ||
name: "index", | ||
// action(entity, state, facets = {}) { | ||
@@ -12,4 +13,6 @@ // // todo: maybe all key info is passed on the subsequent query identifiers? | ||
children: ["get", "delete", "update", "query", "put", "scan", "collection"], | ||
}, | ||
collection: { | ||
name: "collection", | ||
action(entity, state, collection /* istanbul ignore next */ = "", facets /* istanbul ignore next */ = {}) { | ||
@@ -23,5 +26,6 @@ state.query.keys.pk = entity._expectFacets(facets, state.query.facets.pk); | ||
}, | ||
children: ["params", "go"], | ||
children: ["params", "go", "page"], | ||
}, | ||
scan: { | ||
name: "scan", | ||
action(entity, state) { | ||
@@ -31,5 +35,6 @@ state.query.method = MethodTypes.scan; | ||
}, | ||
children: ["params", "go"], | ||
children: ["params", "go", "page"], | ||
}, | ||
get: { | ||
name: "get", | ||
action(entity, state, facets = {}) { | ||
@@ -54,2 +59,3 @@ state.query.keys.pk = entity._expectFacets(facets, state.query.facets.pk); | ||
delete: { | ||
name: "delete", | ||
action(entity, state, facets = {}) { | ||
@@ -74,2 +80,3 @@ state.query.keys.pk = entity._expectFacets(facets, state.query.facets.pk); | ||
put: { | ||
name: "put", | ||
action(entity, state, payload = {}) { | ||
@@ -97,2 +104,3 @@ let record = entity.model.schema.checkCreate({ ...payload }); | ||
update: { | ||
name: "update", | ||
action(entity, state, facets = {}) { | ||
@@ -117,2 +125,3 @@ state.query.keys.pk = entity._expectFacets(facets, state.query.facets.pk); | ||
set: { | ||
name: "set", | ||
action(entity, state, data) { | ||
@@ -130,2 +139,3 @@ let record = entity.model.schema.checkUpdate({ ...data }); | ||
query: { | ||
name: "query", | ||
action(entity, state, facets = {}, options = {}) { | ||
@@ -143,5 +153,6 @@ state.query.keys.pk = entity._expectFacets(facets, state.query.facets.pk); | ||
}, | ||
children: ["between", "gte", "gt", "lte", "lt", "params", "go"], | ||
children: ["between", "gte", "gt", "lte", "lt", "params", "go", "page"], | ||
}, | ||
between: { | ||
name: "between", | ||
action(entity, state, startingFacets = {}, endingFacets = {}) { | ||
@@ -177,5 +188,6 @@ entity._expectFacets( | ||
}, | ||
children: ["go", "params"], | ||
children: ["go", "params", "page"], | ||
}, | ||
gt: { | ||
name: "gt", | ||
action(entity, state, facets = {}) { | ||
@@ -191,5 +203,6 @@ entity._expectFacets(facets, Object.keys(facets), `"gt" facets`); | ||
}, | ||
children: ["go", "params"], | ||
children: ["go", "params", "page"], | ||
}, | ||
gte: { | ||
name: "gte", | ||
action(entity, state, facets = {}) { | ||
@@ -205,5 +218,6 @@ entity._expectFacets(facets, Object.keys(facets), `"gte" facets`); | ||
}, | ||
children: ["go", "params"], | ||
children: ["go", "params", "page"], | ||
}, | ||
lt: { | ||
name: "lt", | ||
action(entity, state, facets = {}) { | ||
@@ -219,5 +233,6 @@ entity._expectFacets(facets, Object.keys(facets), `"lt" facets`); | ||
}, | ||
children: ["go", "params"], | ||
children: ["go", "params", "page"], | ||
}, | ||
lte: { | ||
name: "lte", | ||
action(entity, state, facets = {}) { | ||
@@ -233,6 +248,7 @@ entity._expectFacets(facets, Object.keys(facets), `"lte" facets`); | ||
}, | ||
children: ["go", "params"], | ||
children: ["go", "params", "page"], | ||
}, | ||
params: { | ||
action(entity, state, options) { | ||
name: "params", | ||
action(entity, state, options = {}) { | ||
if (state.query.method === MethodTypes.query) { | ||
@@ -247,3 +263,4 @@ return entity._queryParams(state.query, options); | ||
go: { | ||
action(entity, state, options) { | ||
name: "go", | ||
action(entity, state, options = {}) { | ||
if (entity.client === undefined) { | ||
@@ -262,2 +279,20 @@ throw new Error("No client defined on model"); | ||
}, | ||
page: { | ||
name: "page", | ||
action(entity, state, page = null, options = {}) { | ||
options.page = page; | ||
options.pager = true; | ||
if (entity.client === undefined) { | ||
throw new Error("No client defined on model"); | ||
} | ||
let params = {}; | ||
if (state.query.method === MethodTypes.query) { | ||
params = entity._queryParams(state.query, options); | ||
} else { | ||
params = entity._params(state.query, options); | ||
} | ||
return entity.go(state.query.method, params, options); | ||
}, | ||
children: [] | ||
}, | ||
}; | ||
@@ -264,0 +299,0 @@ |
@@ -11,3 +11,3 @@ "use strict"; | ||
structure, | ||
{ index, type, name } /* istanbul ignore next */ = {}, | ||
{ index, type, name } = {}, | ||
i, | ||
@@ -29,6 +29,18 @@ attributes, | ||
}, | ||
safeParse(str = "") { | ||
try { | ||
if (typeof str === "string") { | ||
} | ||
} catch(err) { | ||
} | ||
}, | ||
safeStringify() { | ||
} | ||
}; | ||
class Entity { | ||
constructor(model /* istanbul ignore next */ = {}, config /* istanbul ignore next */ = {}) { | ||
constructor(model = {}, config = {}) { | ||
this._validateModel(model); | ||
@@ -64,3 +76,3 @@ this.client = config.client; | ||
collection(collection /* istanbul ignore next */ = "", clauses /* istanbul ignore next */ = {}, facets /* istanbul ignore next */ = {}) { | ||
collection(collection = "", clauses = {}, facets = {}) { | ||
let index = this.model.translations.collections.fromCollectionToIndex[ | ||
@@ -160,29 +172,38 @@ collection | ||
try { | ||
let results; | ||
if (config.raw) { | ||
if (response.TableName) { | ||
// a VERY hacky way to deal with PUTs | ||
return {}; | ||
results = {}; | ||
} else { | ||
return response; | ||
results = response; | ||
} | ||
} else { | ||
let data = {}; | ||
if (response.Item) { | ||
data = this.cleanseRetrievedData(response.Item, config); | ||
} else if (response.Items) { | ||
data = response.Items.map((item) => | ||
this.cleanseRetrievedData(item, config), | ||
); | ||
} | ||
if (Array.isArray(data)) { | ||
results = data.map((item) => | ||
this.model.schema.applyAttributeGetters(item), | ||
); | ||
} else { | ||
results = this.model.schema.applyAttributeGetters(data); | ||
} | ||
} | ||
let data = {}; | ||
if (response.Item) { | ||
data = this.cleanseRetrievedData(response.Item, config); | ||
} else if (response.Items) { | ||
data = response.Items.map((item) => | ||
this.cleanseRetrievedData(item, config), | ||
); | ||
if (config.pager) { | ||
let nextPage = response.LastEvaluatedKey || null; | ||
results = [nextPage, results]; | ||
} | ||
let appliedGets; | ||
if (Array.isArray(data)) { | ||
appliedGets = data.map((item) => | ||
this.model.schema.applyAttributeGetters(item), | ||
); | ||
} else { | ||
appliedGets = this.model.schema.applyAttributeGetters(data); | ||
} | ||
return appliedGets; | ||
return results; | ||
} catch (err) { | ||
@@ -198,2 +219,4 @@ if (config.originalErr) { | ||
async go(method, params = {}, options = {}) { | ||
@@ -205,15 +228,19 @@ let config = { | ||
params: options.params || {}, | ||
page: options.page, | ||
pager: !!options.pager | ||
}; | ||
let parameters = Object.assign({}, params); | ||
for (let [name, value] of Object.entries(config.params)) { | ||
if (value !== undefined) { | ||
params[name] = value; | ||
parameters[name] = value; | ||
} | ||
} | ||
let stackTrace = new Error(); | ||
try { | ||
let response = await this.client[method](params).promise(); | ||
let response = await this.client[method](parameters).promise(); | ||
if (method === "put") { | ||
// a VERY hacky way to deal with PUTs | ||
return this.formatResponse(params, config); | ||
return this.formatResponse(parameters, config); | ||
} else { | ||
@@ -460,5 +487,6 @@ return this.formatResponse(response, config); | ||
); | ||
let parameters = {}; | ||
switch (chainState.type) { | ||
case QueryTypes.begins: | ||
return this._makeBeginsWithQueryParams( | ||
parameters = this._makeBeginsWithQueryParams( | ||
chainState.index, | ||
@@ -469,4 +497,5 @@ chainState.filter, | ||
); | ||
break; | ||
case QueryTypes.collection: | ||
return this._makeBeginsWithQueryParams( | ||
parameters = this._makeBeginsWithQueryParams( | ||
chainState.index, | ||
@@ -477,4 +506,5 @@ chainState.filter, | ||
); | ||
break; | ||
case QueryTypes.between: | ||
return this._makeBetweenQueryParams( | ||
parameters = this._makeBetweenQueryParams( | ||
chainState.index, | ||
@@ -485,2 +515,3 @@ chainState.filter, | ||
); | ||
break; | ||
case QueryTypes.gte: | ||
@@ -490,3 +521,3 @@ case QueryTypes.gt: | ||
case QueryTypes.lt: | ||
return this._makeComparisonQueryParams( | ||
parameters = this._makeComparisonQueryParams( | ||
chainState.index, | ||
@@ -498,5 +529,11 @@ chainState.type, | ||
); | ||
break; | ||
default: | ||
throw new Error(`Invalid method: ${method}`); | ||
} | ||
// if (typeof options.page === "string" && options.page.length) { | ||
if (Object.keys(options.page || {}).length) { | ||
parameters.ExclusiveStartKey = options.page; | ||
} | ||
return parameters; | ||
} | ||
@@ -503,0 +540,0 @@ |
@@ -10,2 +10,4 @@ process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1; | ||
}); | ||
const SERVICE = "BugBeater"; | ||
const ENTITY = "TEST_ENTITY" | ||
function sleep(ms) { | ||
@@ -17,4 +19,4 @@ return new Promise((resolve) => { | ||
let model = { | ||
service: "BugBeater", | ||
entity: "test", | ||
service: SERVICE, | ||
entity: ENTITY, | ||
table: "electro", | ||
@@ -284,4 +286,4 @@ version: "1", | ||
{ | ||
service: "testservice", | ||
entity: "testentity", | ||
service: SERVICE, | ||
entity: ENTITY, | ||
table: "electro", | ||
@@ -372,3 +374,3 @@ version: "1", | ||
{ | ||
service: "testing", | ||
service: SERVICE, | ||
entity: uuidv4(), | ||
@@ -458,3 +460,3 @@ table: "electro", | ||
{ | ||
service: "testing", | ||
service: SERVICE, | ||
entity: entity, | ||
@@ -506,3 +508,3 @@ table: "electro", | ||
sk: `$${entity}#id_${id}`.toLowerCase(), | ||
pk: `$testing_1#date_${date}`.toLowerCase(), | ||
pk: `$${SERVICE}_1#date_${date}`.toLowerCase(), | ||
}, | ||
@@ -524,3 +526,3 @@ }); | ||
sk: `$${entity}#id_${id}`.toLowerCase(), | ||
pk: `$testing_1#date_${date}`.toLowerCase(), | ||
pk: `$${SERVICE}_1#date_${date}`.toLowerCase(), | ||
}, | ||
@@ -538,3 +540,3 @@ ], | ||
sk: `$${entity}#id_${id}`.toLowerCase(), | ||
pk: `$testing_1#date_${date}`.toLowerCase(), | ||
pk: `$${SERVICE}_1#date_${date}`.toLowerCase(), | ||
}) | ||
@@ -602,3 +604,3 @@ }).timeout(10000); | ||
{ | ||
service: "testing", | ||
service: SERVICE, | ||
entity: uuidv4(), | ||
@@ -662,3 +664,3 @@ table: "electro", | ||
{ | ||
service: "testing", | ||
service: SERVICE, | ||
entity: entity, | ||
@@ -724,2 +726,24 @@ table: "electro", | ||
}); | ||
describe("Pagination", async () => { | ||
it("Should return a pk and sk that match the last record in the result set, and should be able to be passed in for more results", async () => { | ||
// THIS IS A FOOLISH TEST: IT ONLY FULLY WORKS WHEN THE TABLE USED FOR TESTING IS FULL OF RECORDS. THIS WILL HAVE TO DO UNTIL I HAVE TIME TO FIGURE OUT A PROPER WAY MOCK DDB. | ||
let MallStores = new Entity(model, { client }); | ||
let results = await MallStores.scan.page(null, {raw: true}); | ||
expect(results).to.be.an("array").and.have.length(2); | ||
// Scan may not return records, dont force a bad test then | ||
let [index, stores] = results; | ||
if (stores.length) { | ||
expect(index).to.have.a.property('pk').and.to.have.a.property('sk'); | ||
expect(stores.Items).to.be.an("array") | ||
expect(stores.Items[0]).to.have.a.property('pk').and.to.have.a.property('sk'); | ||
let [nextIndex, nextStores] = await MallStores.scan.page(index); | ||
expect(stores).to.be.an("array").and.have.length(2); | ||
expect(nextIndex).to.not.deep.equal(index); | ||
expect(nextStores).to.be.an("array"); | ||
if (nextStores.length) { | ||
expect(nextStores[0]).to.not.have.a.property('pk').and.to.not.have.a.property('sk'); | ||
} | ||
} | ||
}).timeout(10000); | ||
}) | ||
}); |
@@ -237,3 +237,2 @@ process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1; | ||
.go(); | ||
console.log("animals", animals); | ||
expect(animals) | ||
@@ -240,0 +239,0 @@ .to.be.an("array") |
@@ -405,2 +405,3 @@ const { Entity, clauses } = require("../src/entity"); | ||
"params", | ||
"page", | ||
"rentsLeaseEndFilter", | ||
@@ -413,2 +414,3 @@ ); | ||
"params", | ||
"page", | ||
"rentsLeaseEndFilter", | ||
@@ -421,2 +423,3 @@ ); | ||
"params", | ||
"page", | ||
"rentsLeaseEndFilter", | ||
@@ -429,2 +432,3 @@ ); | ||
"params", | ||
"page", | ||
"rentsLeaseEndFilter", | ||
@@ -437,2 +441,3 @@ ); | ||
"params", | ||
"page", | ||
"rentsLeaseEndFilter", | ||
@@ -439,0 +444,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
293278
6148