Allows for creating and manipulating changesets. As an entry point to create changeset use cast function.
- Source:
Methods
(static) applyAction(changeset, action) → {Array}
Applies action to the changeset only if the changes are valid.
If the changes are valid all changes will be merged with the data model.
Parameters:
Name | Type | Description |
---|---|---|
changeset |
Object | target changeset |
action |
String | action name |
- Source:
Returns:
Success/Error Array tuple
- Type
- Array
Example
// with valid changes
applyChanges(oldChangeset, 'INSERT') === [true, oldChangeset]
oldChangeset.action === null
// with invalid changes
applyChanges(oldChangeset, 'INSERT') === [false, newChangeset]
newChangeset.action === 'INSERT'
(static) applyChanges(changeset) → {Object}
Applies changes to the data model regardless if the changes are valid or not.
Parameters:
Name | Type | Description |
---|---|---|
changeset |
Object | target changeset |
- Source:
Returns:
data
- Type
- Object
(static) cast(data, attrs, params) → {Object}
Casts association with the changeset parameters.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | entity model data |
attrs |
Object | changes |
params |
Array | changes to pick |
- Source:
Returns:
changeset
- Type
- Object
Example
cast({id: 1, title: 'title'}, {title: 'new title', body: 'body'}, ['title', 'body'])
(static) cast(data, attrs, params) → {Object}
Applies properties as changes for the data model according to the set of keys.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | entity model data |
attrs |
Object | changes |
params |
Array | changes to pick |
- Source:
Returns:
changeset
- Type
- Object
Example
cast({id: 1, title: 'title'}, {title: 'new title', body: 'body'}, ['title', 'body'])
(static) change(change, changeset, attrs)
Applies changes to the given changeset.
Parameters:
Name | Type | Description |
---|---|---|
change |
function | cast function with signature (data:{Object}, attrs:{Object}) -> changeset |
changeset |
Object | target changeset |
attrs |
Object | changes to apply |
- Source:
(static) deleteChange(field) → {Object}
Deletes change from the changeset
Parameters:
Name | Type | Description |
---|---|---|
field |
String | name of the field |
- Source:
Returns:
changeset without given change
- Type
- Object
(static) getChange(field, changeset) → {Array}
Gets change from the changeset.
Parameters:
Name | Type | Description |
---|---|---|
field |
String | name of the change field |
changeset |
Object | changeset with expected change |
- Source:
Returns:
Success/Error Array tuple with the change value
- Type
- Array
Example
// when change exists
getChange('title', changeset) === [true, 'some title']
// when change does not exist
getChange('title', changeset) === [false, null]
(static) getErrors(field, changeset) → {Array}
Gets errors from the changeset for the given field.
Parameters:
Name | Type | Description |
---|---|---|
field |
String | name of the field |
changeset |
Object | changeset with expected errors |
- Source:
Returns:
field errors
- Type
- Array
(static) getField(field, changeset) → {Array}
Gets field from the changeset.
Parameters:
Name | Type | Description |
---|---|---|
field |
String | name of the field |
changeset |
Object | changeset with expected field |
- Source:
Returns:
Success/Error Array tuple with the field value
- Type
- Array
Example
// when field exists
getField('title', changeset) === [true, 'some title']
// when field does not exist
getField('title', changeset) === [false, null]
(static) merge(from, from) → {Object}
Merges two changesets with the same data model.
If data models are not equal, function will throw error.
Changes, errors and action are merged into the second changeset.
Parameters:
Name | Type | Description |
---|---|---|
from |
Object | from - source changeset |
from |
Object | to - target changeset |
- Source:
Returns:
changeset
- Type
- Object
(static) putChange(field, change, changeset) → {Object}
Puts change into changeset for the given field
Parameters:
Name | Type | Description |
---|---|---|
field |
String | name of the change field |
change |
Any | value of the change |
changeset |
Object | target changeset |
- Source:
Returns:
changeset with the given change
- Type
- Object
(static) putError(field, error, changeset) → {Object}
Puts error to the changeset for given field
Parameters:
Name | Type | Description |
---|---|---|
field |
String | name of the field |
error |
Any | works with any data, but preferably object with message:String property |
changeset |
Object | target changeset |
- Source:
Returns:
changeset with given errors
- Type
- Object
(static) traverseErrors(changeset, fn) → {Object}
Traverses through changeset errors and associations. With the given transform function will apply it to the error messages.
Parameters:
Name | Type | Description |
---|---|---|
changeset |
Object | target changeset |
fn |
function | transform function |
- Source:
Returns:
object with changeset errors corresponding to the changeset graph
- Type
- Object
Example
traverseErrors(changeset, function transform(field, opts){
return `${field}: ${opts.message}`
})