Tilla
This is Version 2 Docs
If you're looking for version 1 docs. Check out the v1 branch
Tilla transforms objects, based on the rules you specify. It has a fluid, composable API, and non-blocking transformations. It also comes packaged with sensible default permission levels, and a registry to keep track of all of your Transformers that can easily be tied into other parts of your app.
Table of Contents
Installation
$ npm install --save tilla
Goals
I wanted to have a fluid, easy to read, chainable API to build Transformers with sensible defaults. I wanted permissions to be incorporated in the Transformer API itself and allow the user to provide their own domain specific permissions and permission ranking that would cascade down to sub-transformations. All transformations should be completely asynchronous, down to the field level. Looking at the Transformer code should give an idea as to the shape of the resultant object produced by that Transformer. It should also be easy to transform fields on an object using other Transformers, in other words Sub-transformations, and these would be lazy loaded at runtime.
Features
- Chainable, fluid Transformer API
- Asynchronous transformations at the field level
- Built-in Transformer registry
- Field Permission-Masking
- Cascading permissions (for sub-transformations)
Usage
const fd = // use the built permission levels const addressTransformer = street: city: state: otherThing: const personTransformer = // always directly map src.firstName -> dest.firstName firstName: // computed field using a custom builder name:
Tilla is used to transform objects. It's great for building DTOs and controlling access to certain fields on those DTOs. The core of Tilla is Transformers
, Rules
, and FieldMappers
. Transformers
describe the shape of the result object, Rules
tell the Transformer
when to map each field, and FieldMappers
tell the Rule
how to map each field.
FieldMappers
are grouped into Rules
which are grouped together in a FieldMapperDelegate
which are further grouped together in a Transformer
.
Transformer
Call transform()
on a Transformer
and provide the permission lvl and object to transform. This will return a Promise
that will resolve with the transformed object.
Field Masking & Permissions
By default Tilla
ships with 4 permission levels: PUBLIC
, PRIVILEGED
, PRIVATE
, and ADMIN
and the ranking of these fields, from least sensitive to most sensitive is [PUBLIC
, PRIVILEGED
, PRIVATE
, ADMIN
]
FieldMapperDelegate
s can set multiple masking levels for each field, based on permissions, and their chainable API makes it easy to set up complex mappings for each field on a Transformer
. In the example above, always()
was used for each field, which indicates a single builder for all permission levels -- "'Always' use this method to transform the value provided". We can specify multiple methods like so:
const fd = // use the built permission levels let oldPersonTransformer = // different transformations for PUBLIC and PRIVATE permission levels. age: name: // only transformations at PRIVATE and above permission lvls will have this field ssn:
You can specify your own permission ranking, when instantiating the fieldDelegate
. and Tilla
will ensure that ranking is enforced throughout the entire FieldDelegate
chain.
let ranking = 'USER' 'EMPLOYEE' 'MANAGER' const fd = // pass your ranking to the util wrapper let oldPersonTransformer = // different transformations for PUBLIC and PRIVATE permission levels. age: name: // only transformations at PRIVATE and above permission lvls will have this field ssn: // Will throw an ERROR because this permission lvl does not exist in the provided ranking broken:
Can I Use a Transformer to Map a field?
Yes! This is called a 'subTransform'. You may want to do this for an attached association. For example, a Person
may have an eagerly loaded Address
. With Tilla
you can specify each of these Transformer
s and then specify a SubTransformation
in the Person
Transformer
for the key, address
. You can specify a string which will
search the built in Transformer registry, a Transformer
, or a function that returns a Promise that resolves to a Transformer
.
const fd = // use the built permission levels let addressTransformer = street: city: state: otherThing: let personTransformer = age: /*...*/ // Subtransform from the registry address: // OR directly provide the transformer address: // OR a Thunk that returns a Transformer address: let person = age: 22 firstName: 'John' lastName: 'Doe' ssn: '123-45-6789' address: street: '123 Street' city: 'Chicago' state: 'IL' otherThing: 'other' personTransformer personTransformer
Transformer
has another constructor that accepts a string, the registry string, and an object, the field mapping. This will automatically add that Transformer
instance to the internal registry at the key. However, you can also use your own registry system separate from tilla
.
All the permission APIs work the same with SubTransform
. The permission provided to the parent propogates down to the subTransform
. This is the default behavior. To override this, you can specify a permission lvl to use for the SubTransformation when defining the transformer.
const fd = // use the built permission levels let addressTransformer = street: city: state: otherThing: let personTransformer = age: /*...*/ // transform with PUBLIC permission lvl, regardless of the parents permission lvl address: let person = age: 22 firstName: 'John' lastName: 'Doe' ssn: '123-45-6789' address: street: '123 Street' city: 'Chicago' state: 'IL' otherThing: 'other' // public permission lvlpersonTransformer // private permission lvlpersonTransformer
Can a Transformer be used to transform a list of object?
Yes! It is common to have a list of objects to transform using a specified Transformer. For example, a Person
could have multiple Car
s that are eagerly loaded. To specify a list of objects to transform with a common Transformer
, simply add asList()
Rule on the chain provided to the FieldMapperDelegate
.
const fd = // use the built permission levelslet personTransformer =age:/*...*/// will transform each object in the list with the Transformer registerd at 'car' in the registrycars:
Can I specify a default for a set of fields?
Yes! Transformers
have a method byDefault()
that will accept an Array of string attributes. You can then specify how all of those attributes will be transformed. A common case is just mark all those fields as passthrough
.
const fd = // use the built permission levels let personTransformer = // Special transformation cases here name: city: state: // .BUILD_WITH() can also be used and follows the same builder API as customer field builders
Can I build a Transformer based off of another?
Yes! You can extend an exisiting Transformer
by calling extend()
and passing a map just like you would a normal Transformer
. This will merge the two mappings and return a new Transformer
instance.
const fd = // use the built permission levelslet personTransformer =// Special transformation cases herename:city:// childTransformer will have all attributes of personTransformer, add a favoriteToy fieldDelegate, and override the name transformerlet childTransformer = personTransformer
Can I write my own custom rules and mappers?
Yes you can! Most use cases are covered by the Rules and FieldMappers provided by tilla
, but you might want to write your own. A Rule
and a FieldMapper
are nothing more than functions. Here are their APIs:
Notice that both Rule
and FieldMapper
eventually receive the FieldMapperDelegate
instance. This enables both Rule
or FieldMapper
to access and/or mutate the state maintained by the delegate that is used later on when tranform
is called.
Any function that implements either of those APIs can be used as a Rule
or a FieldMapper
, respectively! Let's show an example.
EitherOr Custom Rule Example
Say I want a rule that will only transform a field only if the permission lvl is PUBLIC
or ADMIN
. You could of course implement this as a list of rules that tilla
already provides:
const fd = // use the built permission levels
But, let's write a single Rule that accomplishes this:
const fd = // use the built permission levels // Our custom Ruleconst eitherOr = { const delegateMap = fieldMapperDelegate delegateMapeitherPermission = delegateMaporPermission = return fieldDelegateMapper} // using our custom Rule
Now whenever we call transform
, the fieldDelegate will only map the field, as a passthrough()
, only if the provided permission is either PUBLIC
or ADMIN
addMapper Custom FieldMapper Example
Let's extend out last example. Say we wanted a FieldMapper
that simply adds a provided number to the value that it was mapping. Again, you could implement this using the buildWith
mapper tilla
already provides:
const fd = // use the built permission levels const addMapper =
But let's write our own mapper that accomplishes this:
const fd = // use the built permission levels // Our custom Ruleconst eitherOr = { const delegateMap = fieldMapperDelegate delegateMapeitherPermission = delegateMaporPermission = return fieldDelegateMapper} const addMapper = async instancekey + n // using our custom Rule AND custom FieldMapper
Now whenever we call transform
, the fieldDelegate will only map the field, adding 1 to it's value, only if the provided permission is either PUBLIC
or ADMIN
Transformer Registry
Tilla
exposes an instantiated instance of the TransformRegistry
. The registry
is a great way to manage all of Transformers
and then pass them around your app as needed. For example, you can easily incorporate in Express
middleware.
// add some transformers to the registry somewhereregistryregistryconst attachTransformer = {return {let transformer = registryreqtransformer = transformer // then use the transformer later on}}
The registry also provides a subscribe(observer)
api that allows you to listen for changes to the registry. The registry emits events on register
and clear
const unsubscribe = registry registry // produces 'Registered transform at key: person' in the logs // later on // unsubscribes the observer from the registry
the observer can be a function with single arity or an object that conforms to the or an object that satisfies this interface:
TODO
- Better document API
Contribute
Submit an issue or a PR
License
MIT
Name
I couldn't find any open npm module names that I liked that weren't already taken. As a result, I used a shotened version of my name :p. If you have a better idea, please make a suggestion!