@actualwave/deferred-data-access
v2.0.1
Published
Deferred Data Access(DDA) is a framework for building async CRUD APIs. It transforms object calls into commands that can be interpreted by custom function.
Readme
Deferred Data Access
Deferred Data Access(DDA) is a framework for building async CRUD APIs. It transforms object calls into commands that can be interpreted by custom function.
How it works
- Framework accepts an async handler function and returns an object.
const obj = handle((command, context) => {
let result;
// Some logic that gets somewhere value and saves in into "result" variable.
return result;
})();- Developer accesses object property and gets Promise
const value = await obj.prop;- Framework records property access into a command object, handler function is called with command object as an argument.
{ type: ProxyCommand.GET, name: 'prop' }- Whatever function returns is returned as promise value.
DDA can operate in two modes, mode is defined by passing second argument to handle() function.
- lazy - Default mode, custom handler function is called only when
then()orcatch()methods are accessed. Accessingobj.child.grand.prop.then(func)calls function once. - reactive - Non-lazy, custom handler function called on every operation. Accessing
obj.child.grand.prop.then(func)calls function 3 times.
There are multiple predefined commands that can be passed to handler function.
- ProxyCommand.GET - when property accessed
- ProxyCommand.SET - when property is set, new value is recorded as
valuein command object - ProxyCommand.DELETE_PROPERTY - when property is being deleted
- ProxyCommand.APPLY - when function is called, call arguments recorded as
valueof command object - ProxyCommand.METHOD_CALL - only generated in lazy mode, it combines GET and APPLY commands.
