datumo
Lightweight data modelling for Node.js
Basic Usage
Datumo requires Node.js 4.0 or later.
$ npm install --save datumo
let Datumo = Model static { return givenName: type: 'string' required: true middleName: type: 'string' familyName: type: 'string' required: true email: type: 'string' format: 'email' } let amanda = givenName: 'Amanda' familyName: 'Bryson' email: 'amanda@example.com' amandagivenName === 'Amanda'amandafamilyName === 'Bryson'amandaemail === 'amanda@example.com' Personvalid === true
Why
ES6 provides a lot of new conveniences, with classes arguably as the most notable addition. Datumo makes data modelling using ES6 classes incredibly easy and syntactically compact.
Details
You can do a number of nifty things with Datumo:
- Validate data - harness the power of lx-valid for quick-and-easy validation
- Subclass models - easily create models for subsets of your data
- Extend models - take advantage of ES6 classes to easily extend models
- Map data properties - easily handle data from a 3rd-party that has the data you need, but with different property names
- Avoid extraneous data - Datumo model instances only allow setting properties defined on the schema
Validation
Datumo uses lx-valid for validation. This makes it incredibly easy for you to define your model's schema and solve the problem of validating your data in one shot.
Because Datumo delegates validation to lx-valid, Datumo supports all of the options lx-valid supports on schemas.
let Datumo = Model static { return givenName: type: 'string' required: true middleName: type: 'string' familyName: type: 'string' required: true email: type: 'string' format: 'email' } let jeff = givenName: 'Jeff' email: 'Don\'t send me any e-mails!'let validationResults = Person validationResultsvalid === falsevalidationResultserrorslength === 2// familyName is required// email is not a valid e-mail address
Validating defined properties only
If you're working with a database, occassionally you may want only to validate
properties that are defined as a part of an update or a patch request. With the
definedOnly
option, the validate
function treats all properties on the
schema as though they are not required, effectively ignoring any undefined
properties.
let vanessa = givenName: 'Vanessa' email: 'thisisnotavalidemail'let results = Personlet resultsDefinedOnly = Person resultsvalid === falseresultserrorslength === 2// familyName is required// email is not a valid e-mail address resultsDefinedOnlyvalid === falseresultsDefinedOnlyerrorslength === 1// email is not a valid e-mail address
Extension
Because Datumo models are ES6 classes, you can easily extend them:
let Datumo = Model static { return givenName: type: 'string' required: true middleName: type: 'string' familyName: type: 'string' required: true email: type: 'string' format: 'email' } static { let schema = superschema Object return schema } let alex = givenName: 'Alex' familyName: 'Ueltzhöfer' position: 'Lead Architectural Engineer' company: 'ACME Company'
Subclassing
Say you have a model that may contain sensitive information. Or, perhaps, you have a model that has all the data you need, but has a bit too much. With Datumo, it's easy to create a new model that has only the data you need.
Let's assume you have a Person
model defined as such:
let Datumo = Model static { return givenName: type: 'string' required: true middleName: type: 'string' familyName: type: 'string' required: true email: type: 'string' format: 'email' }
Now, let's create an instance of Person
.
let amanda = amandagivenName = 'Amanda'amandamiddleName = 'Barrett'amandafamilyName = 'Bryson'amandaemail = 'amanda@example.com'
Let's assume you don't want certain code to ever see a person's e-mail address.
Simple! We can exclude the email
property on a new, subclassed model.
{} let offlineAmanda = amanda offlineAmandagivenName === 'Amanda'offlineAmandamiddleName === 'Barrett'offlineAmandafamilyName === 'Bryson'offlineAmandaemail === undefined
You can also subclass a subset of the data inclusively instead of exclusively. For example:
{} let friendAmanda = amanda friendAmandagivenName === 'Amanda'friendAmandamiddleName === undefinedfriendAmandafamilyName === undefinedfriendAmandaemail === 'amanda@example.com'
Mapping
Web services are ubiquitous, and with something widespread, comes the issue of consistency. Many services have certain sets of data that they may share in common. For example, the concept of a user is universal. However, the format in which services serve user data isn't.
Imagine that the property names used for data, both by your app and the services, looks like this:
Your app | Service A | Service B | Service C |
---|---|---|---|
givenName |
given_name |
firstName |
name |
middleName |
middle_name |
middleName |
middle_initial |
familyName |
family_name |
lastName |
surname |
email |
email |
emailAddress |
mail |
Datumo helps you navigate the SNAFU of the modern world of data with property mapping. Simply define the mappings on the model:
let Datumo = Model static { return givenName: type: 'string' required: true middleName: type: 'string' familyName: type: 'string' required: true email: type: 'string' format: 'email' } static { return serviceA: givenName: 'given_name' middleName: 'middle_name' familyName: 'family_name' serviceB: givenName: 'firstName' familyName: 'lastName' email: 'emailAddress' serviceC: givenName: 'name' middleName: 'middle_initial' familyName: 'surname' email: 'mail' }
Now, when you receive data from any of these services, accepting that data is as easy as:
apiClient // ...
You can also map data out of a Datumo model to use it in a service. Only
properties defined in the mapping will be in the output object. For example,
using the sample User
class above:
let amanda = givenName: 'Amanda' middleName: 'Barrett' familyName: 'Bryson' email: 'amanda@example.com' let apiFriendlyAmanda = User apiFriendlyAmandaname === 'Amanda'apiFriendlyAmandamiddle_initial === 'Barrett'apiFriendlyAmandasurname === 'Bryson'apiFriendlyAmandamail === 'amanda@example.com' apiClient// ...
Expressions
NOTE: Expressions work great with mapping data into a Datumo model. However, they're not designed to work with mapping data out of a Datumo model. If you're using the
map
function, make sure that you are using a mapping that uses simple property names instead of expressions.
Property mappings support a limited subset of Javascript that you can use when facing data that requires more complex logic to be mapped properly.
For example:
let Datumo = Model static { return givenName: type: 'string' required: true // ... } static { return serviceA: givenName: 'cn || given_name' // ... // ... }
License
MIT