Class: Record

Record


new Record(props, opts)

js-data's Record class.

import {Record} from 'js-data'
Method parameters:
Name Type Argument Description
props Object <optional>

The initial properties of the new Record instance.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
noValidate Boolean <optional>
false

Whether to skip validation on the initial properties.

Details
Since Source
3.0.0 Record.js, line 16

Extends

This class extends the Component class.

Members


_listeners

Event listeners attached to this Component. Do not modify. Use Component#on and Component#off instead.

Details
Type Since Source
Object 3.0.0 Component.js, line 7
Inherited From:

Methods


<static> extend(props, classProps)

Create a subclass of this Record.

Method parameters:
Name Type Argument Default Description
props Object <optional>
{}

Properties to add to the prototype of the subclass.

classProps Object <optional>
{}

Static properties to add to the subclass.

Return value:
Type Description
Constructor

Subclass of this Record class.

Details
Since Source
3.0.0 Record.js, line 441
Examples

Extend the class in a cross-browser manner.

import {Record} from 'js-data'
const CustomRecordClass = Record.extend({
  foo () { return 'bar' }
})
const customRecord = new CustomRecordClass()
console.log(customRecord.foo()) // "bar"

Extend the class using ES2015 class syntax.

class CustomRecordClass extends Record {
  foo () { return 'bar' }
}
const customRecord = new CustomRecordClass()
console.log(customRecord.foo()) // "bar"

afterLoadRelations(relations, opts)

Lifecycle hook.

Method parameters:
Name Type Description
relations Array.<String>

The relations argument passed to Record#loadRelations.

opts Object

The opts argument passed to Record#loadRelations.

Details
Since Source
3.0.0 Record.js, line 72

beforeLoadRelations(relations, opts)

Lifecycle hook.

Method parameters:
Name Type Description
relations Array.<String>

The relations argument passed to Record#loadRelations.

opts Object

The opts argument passed to Record#loadRelations.

Details
Since Source
3.0.0 Record.js, line 82

changes(opts)

Return changes to this record since it was instantiated or Record#commit was called.

Method parameters:
Name Type Argument Description
opts <optional>

Configuration options.

Properties
Name Type Argument Default Description
equalsFn Function <optional>
utils.deepEqual

Equality function.

ignore Array <optional>
[]

Array of strings or RegExp of fields to ignore.

Return value:
Type Description
Object

Object describing the changes to this record since it was instantiated or its Record#commit method was last called.

Details
Since Source
3.0.0 Record.js, line 92

commit()

Make the record's current in-memory state it's only state, with any previous property values being set to current values.

Details
Since Source
3.0.0 Record.js, line 109

dbg(args)

Log the provided values at the "debug" level.

Method parameters:
Name Type Argument Description
args * <optional>
<repeatable>

Values to log.

Details
Since Source
3.0.0 Component.js, line 32
Inherited From:

destroy(opts)

Call Mapper#destroy using this record's primary key.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options passed to Mapper#destroy.

Return value:
Type Description
Promise

The result of calling Mapper#destroy with the primary key of this record.

Details
Since Source
3.0.0 Record.js, line 121

emit(event, args)

Trigger an event on this Component.

Method parameters:
Name Type Argument Description
event String

Name of event to emit.

args * <optional>
<repeatable>

Arguments to pass to any listeners.

Details
Since Source
3.0.0 Component.js, line 98
Inherited From:
Examples
collection.on('foo', (msg) => {
  console.log(msg) // "bar"
})
collection.emit('foo', 'bar')
store.on('foo', (msg, val1, val2) => {
  console.log(msg, val1, val2) // "bar" "beep" "boop"
})
store.emit('foo', 'bar', 'beep', 'boop')

get(key)

Return the value at the given path for this instance.

Method parameters:
Name Type Description
key String

Path of value to retrieve.

Return value:
Type Description
*

Value at path.

Details
Since Source
3.0.0 Record.js, line 136

hasChanges(opts)

Return whether this record has changed since it was instantiated or Record#commit was called.

Method parameters:
Name Type Argument Description
opts <optional>

Configuration options.

Properties
Name Type Argument Default Description
equalsFn Function <optional>
utils.deepEqual

Equality function.

ignore Array <optional>
[]

Array of strings or RegExp of fields to ignore.

Return value:
Type Description
Boolean

Return whether the record has changed since it was instantiated or since its Record#commit method was called.

Details
Since Source
3.0.0 Record.js, line 148

isValid(opts)

Return whether the record in its current state passes validation.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options. Passed to Mapper#validate.

Return value:
Type Description
Boolean

Whether the record in its current state passes validation.

Details
Since Source
3.0.0 Record.js, line 165

loadRelations(relations, opts)

Lazy load relations of this record, to be attached to the record once their loaded.

Method parameters:
Name Type Argument Description
relations Array.<String> <optional>

List of relations to load.

opts Object <optional>

Configuration options.

Return value:
Type Description
Promise

Resolves with the record, with the loaded relations now attached.

Details
Since Source
3.0.0 Record.js, line 178

log(level, args)

Log the provided values. By default sends values to console[level].

Method parameters:
Name Type Argument Description
level String

Log level

args * <optional>
<repeatable>

Values to log.

Details
Since Source
3.0.0 Component.js, line 39
Inherited From:

off(event, listener)

Remove an event listener from this Component. If no listener is provided, then all listeners for the specified event will be removed. If no event is specified then all listeners for all events will be removed.

Method parameters:
Name Type Argument Description
event String <optional>

Name of event to unsubsribe to.

listener Function <optional>

Listener to remove.

Details
Since Source
3.0.0 Component.js, line 79
Inherited From:
Examples

Remove a listener to a single event

collection.off('add', handler)

Remove all listeners to a single event

record.off('change')

Remove all listeners to all events

store.off()

on(event, listener, ctx)

Register a new event listener on this Component.

Method parameters:
Name Type Argument Description
event String

Name of event to subsribe to.

listener Function

Listener function to handle the event.

ctx * <optional>

Optional content in which to invoke the listener.

Details
Since Source
3.0.0 Component.js, line 49
Inherited From:
Examples

Listen for all "afterCreate" events in a DataStore

store.on('afterCreate', (mapperName, props, opts, result) => {
  console.log(mapperName) // "post"
  console.log(props.id) // undefined
  console.log(result.id) // 1234
})
store.create('post', { title: 'Modeling your data' }).then((post) => {
  console.log(post.id) // 1234
})

Listen for the "add" event on a collection

collection.on('add', (records) => {
  console.log(records) // [...]
})

Listen for "change" events on a record

post.on('change', (record, changes) => {
  console.log(changes) // { changed: { title: 'Modeling your data' } }
})
post.title = 'Modeling your data'

previous(key)

Return the properties with which this record was instantiated.

Method parameters:
Name Type Argument Description
key String <optional>

If specified, return just the initial value of the given key.

Return value:
Type Description
Object

The initial properties of this record.

Details
Since Source
3.0.0 Record.js, line 266

revert(opts)

Revert changes to this record back to the properties it had when it was instantiated.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options.

Properties
Name Type Argument Description
preserve Array.<String> <optional>

Array of strings or Regular Expressions denoting properties that should not be reverted.

Details
Since Source
3.0.0 Record.js, line 282

save(opts)

Delegates to Mapper#create or Mapper#update.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options. See Mapper#create and Mapper#update.

Properties
Name Type Argument Description
changesOnly Boolean <optional>

Equality function. Default uses ===.

equalsFn Function <optional>

Passed to Record#changes when opts.changesOnly is true.

ignore Array <optional>

Passed to Record#changes when opts.changesOnly is true.

Return value:
Type Description
Promise

The result of calling Mapper#create or Mapper#update.

Details
Since Source
3.0.0 Record.js, line 309

set(key, value, opts)

Set the value for a given key, or the values for the given keys if "key" is an object.

Method parameters:
Name Type Argument Description
key String | Object

Key to set or hash of key-value pairs to set.

value * <optional>

Value to set for the given key.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
silent Boolean <optional>
false

Whether to trigger change events.

Details
Since Source
3.0.0 Record.js, line 348

toJSON(opts)

Return a plain object representation of this record. If the class from which this record was created has a Mapper, then Mapper#toJSON will be called with this record instead.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options.

Properties
Name Type Argument Description
with Array.<String> <optional>

Array of relation names or relation fields to include in the representation. Only available as an option if the class from which this record was created has a Mapper and this record resides in an instance of DataStore.

Return value:
Type Description
Object

Plain object representation of this record.

Details
Since Source
3.0.0 Record.js, line 373

unset(key, opts)

Unset the value for a given key.

Method parameters:
Name Type Argument Description
key String

Key to unset.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
silent Boolean <optional>
false

Whether to trigger change events.

Details
Since Source
3.0.0 Record.js, line 400

validate(opts)

Validate this record based on its current properties.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options. Passed to Mapper#validate.

Return value:
Type Description
*

Array of errors or undefined if no errors.

Details
Since Source
3.0.0 Record.js, line 413