Skip to content

Commit

Permalink
Merge pull request #220 from tysonrm/workflow
Browse files Browse the repository at this point in the history
use idempontency key for request id if present
  • Loading branch information
tysonrm authored Oct 16, 2022
2 parents 8b43ee1 + 5123581 commit dc2085e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
7 changes: 3 additions & 4 deletions src/aegis.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,10 @@ async function handle(path, method, req, res) {
// track this request
requestContext.enterWith(
new Map([
['id', nanoid()],
['id', req.headers['idempotency-key'] || nanoid()],
['begin', Date.now()],
['req', req],
['res', res],
['headers', req.headers]
['user', req.user],
['res', res]
])
)
console.debug(`enter context ${requestContext.getStore().get('id')}`)
Expand Down
21 changes: 2 additions & 19 deletions src/domain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,29 +231,12 @@ const onloadEvent = model => ({
})

function getUniqueId() {
return getIdempotencyKey() || getContextId() || uuid()
return getContextId() || uuid()
}

function getContextId() {
const store = requestContext.getStore()
if (store) {
return store.get('id')
}
}

function getIdempotencyKey() {
const store = requestContext.getStore()
if (store) {
const headers = store.get('headers')
console.log(headers)
if (headers) {
const idemKey = Object
.values(headers)
.find(k => k === headers['idempotency-key'])
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>idempotency-key', idemKey)
return idemKey
}
}
if (store) return store.get('id')
}

/**
Expand Down
22 changes: 17 additions & 5 deletions src/domain/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ const Model = (() => {
*/
async update(changes, validate = true) {
// get the last saved version
const saved = await datasource.find(this[ID])
const saved = await datasource.find(this[ID]) || {}
// merge changes with last saved and optionally validate
const valid = validateUpdates(saved || this, changes, validate)
const valid = validateUpdates({ ...this, ...saved }, changes, validate)

// update timestamp
const merge = await datasource.save(this[ID], {
Expand Down Expand Up @@ -315,10 +315,8 @@ const Model = (() => {
* @returns {Model}
*/
updateSync(changes, validate = true) {
// get the last saved version
const saved = datasource.findSync(this[ID])
// merge changes with lastest copy and optionally validate
const valid = validateUpdates(saved || this, changes, validate)
const valid = validateUpdates(this, changes, validate)

// update timestamp
const merge = datasource.saveSync(this[ID], {
Expand Down Expand Up @@ -465,13 +463,27 @@ const Model = (() => {
return asyncContext[name]
},

/**
* Returns service of related domain provided
* this domain is related to it via modelspec.
* If not, we won't be able to access the
* domain's memory. Every domain model employs
* this capability-based security measure.
* @returns
*/
fetchRelatedModel(modelName) {
const rel = Object.values(relations).find(
v => v.modelName.toUpperCase() === modelName.toUpperCase()
)

if (!rel) throw new Error('no relation found')

if (!datasource
.getFactory()
.listDataSources()
.includes(modelName.toUpperCase()))
throw new Error('no datasource found')

const ds = datasource
.getFactory()
.getDataSource(modelName.toUpperCase())
Expand Down

0 comments on commit dc2085e

Please sign in to comment.