NohmModel

NohmModel

(abstract) new NohmModel()

Source:

A nohm model class.

Members

client :redis.RedisClient

Source:

Redis client for this model

Type:
  • redis.RedisClient

errors :Object.<string, Array.<string>>

Source:

Validation errors that were set during the last call to NohmModel#validate. (so also during save())

The type is an object with property names as keys and then an array with validation names of the validations that failed

Type:
  • Object.<string, Array.<string>>

id

Source:

ID of the record. You can manually set it, but that doesn't automatically load it.

isDirty

Source:

True if there are any unsaved changes. This is triggered by changing the id manually, using .link()/.unlink() and changing properties from their stored state.

(readonly) isLoaded :boolean

Source:

Returns true if the model has been loaded from the database.

Type:
  • boolean

modelName :string

Source:

Name of the model, used for database keys and relation values

Type:
  • string

Methods

allProperties()

Source:

Get all properties with values either as an array or as json (param true).

(async) belongsTo(obj, relationNameopt) → {Promise.<boolean>}

Source:

Resolves with true if the given object has a relation (optionally with the given relation name) to this.

Parameters:
Name Type Attributes Default Description
obj NohmModel
relationName string <optional>
'default'
Returns:
Type
Promise.<boolean>

(async) exists(id) → {Promise.<boolean>}

Source:

Returns a Promise that resolves to true if the given id exists for this model.

Parameters:
Name Type Description
id *
Returns:
Type
Promise.<boolean>

(async) find(searches) → {Promise.<Array.<any>>}

Source:
See:

Finds ids of objects by search arguments

Parameters:
Name Type Description
searches ISearchOptions
Returns:
Type
Promise.<Array.<any>>

(async) getAll(otherModelName, relationNameopt) → {Promise.<Array.<any>>}

Source:

Returns an array of the ids of all objects that are linked with the given relation.

Parameters:
Name Type Attributes Default Description
otherModelName string
relationName string <optional>
'default'
Returns:
Type
Promise.<Array.<any>>

getDefinitions() → {Object}

Source:

Returns the property definitions of this model.

Returns:
Type
Object
Source:

Links one object to another. Does not save the link directly but marks it for the next .save() call. When linking an instance that has not been saved that instance will then be saved during the .save() call on this instance.

Note: link names should not end with 'Foreign' as that is an internally used identifier.

Examples
const user = new UserModel();
 const comment = new CommentModel();
 await user.load(123);
 user.linK(comment, 'author');
 await user.save(); // this will save the link to the database and also call .save() on comment
// options object typing:
 {
   error?: (err: Error | string, otherName: string, otherObject: NohmModel) => any;
   name: string;
   silent?: boolean;
 }
Parameters:
Name Type Attributes Description
other NohmModel

The other instance that is being linked

optionsOrNameOrCallback string | ILinkOptions | function <optional>

Either a string for the relation name (default: 'default') or an options object (see example above) or the callback

callback function <optional>

Function that is called when the link is saved.

(async) load(id) → {Object}

Source:

Loads the record from the database.

Parameters:
Name Type Description
id *
Throws:

If no record exists of the given id, an error is thrown with the message 'not found'

Type
Error('not found')
Returns:

Resolves with the return of NohmModel.allProperties of NohmModel.allProperties after loading

Type
Object
Source:

Returns the number of links of a specified relation (or the default) an instance has to models of a given modelName.

Parameters:
Name Type Attributes Default Description
otherModelName string

Name of the model on the other end of the relation.

relationName string <optional>
'default'

Name of the relation

Returns:
Type
Promise.<number>

property(keyOrValues, valueopt) → {any}

Source:

Read and write properties to the instance.

Parameters:
Name Type Attributes Description
keyOrValues string | PropertyObject

Name of the property as string or an object where the keys are the names and the values the new values

value * <optional>

If changing a property and using the .property('string', value) call signature this is the value

Returns:

Returns the property as set after type casting

Type
any

propertyDiff()

Source:

Returns an array of all the properties that have been changed since init/load/save.

Example
user.propertyDiff('country') ===
   [{
     key: 'country',
     before: 'GB',
     after: 'AB'
   }]

propertyReset(keyopt)

Source:

Resets a property to its state as it was at last init/load/save.

Parameters:
Name Type Attributes Description
key string <optional>

If given only this key is reset

(async) remove(silentopt) → {Promise.<void>}

Source:

Remove an object from the database. Note: Does not destroy the js object or its properties itself!

Parameters:
Name Type Attributes Default Description
silent boolean <optional>
false

Fire PubSub events or not

Returns:
Type
Promise.<void>

(async) save([options={ silent: false, skip_validation_and_unique_indexes: false, }]) → {Promise.<void>}

Source:

Save an instance to the database. Updating or Creating as needed depending on if the instance already has an id.

Parameters:
Name Type Description
[options={ silent: false, skip_validation_and_unique_indexes: false, }] ISaveOptions
Returns:
Type
Promise.<void>

(async) setUniqueIds()

Source:

Sets the unique ids of all unique property values in this instance to the given id. Warning: Only use this during create() when overwriting temporary ids!

(async) sort(optionsopt, idsopt) → {Promise.<Array.<string>>}

Source:
See:

Sort records by some criteria and return the sorted ids.

Parameters:
Name Type Attributes Default Description
options Object <optional>
{}
ids Array.<string> | false <optional>
false
Returns:
Type
Promise.<Array.<string>>

(async) subscribe(eventName, callback) → {Promise.<void>}

Source:

Subscribe to nohm events for this model.

Parameters:
Name Type Description
eventName string

One of 'create', 'update', 'save', 'remove', 'unlink', 'link'

callback function
Returns:

Resolves after the subscription has been set up.

Type
Promise.<void>

(async) subscribeOnce(eventName, callback) → {Promise.<void>}

Source:

Subscribe to only the next occurrence of an event for this model.

Parameters:
Name Type Description
eventName string

One of 'create', 'update', 'save', 'remove', 'unlink', 'link'

callback function
Returns:

Resolves after the subscription has been set up.

Type
Promise.<void>
Source:

Unlinks one object to another. Does not remove the link directly but marks it for the next .save() call.

Example
// options object typing:
 {
   error?: (err: Error | string, otherName: string, otherObject: NohmModel) => any;
   name: string;
   silent?: boolean;
 }
Parameters:
Name Type Attributes Description
other NohmModel

The other instance that is being unlinked (needs to have an id)

optionsOrNameOrCallback string | ILinkOptions | function <optional>

Either a string for the relation name (default: 'default') or an options object (see example above) or the callback

callback function <optional>

(async) unlinkAll(givenClientopt) → {Promise.<void>}

Source:

Unlinks all relations a record has to all other models.

Parameters:
Name Type Attributes Description
givenClient redis.RedisClient | redis.Multi <optional>
Returns:
Type
Promise.<void>

unsubscribeEvent(eventName, fnopt)

Source:

Unsubscribe from an event.

Parameters:
Name Type Attributes Description
eventName string

One of 'create', 'update', 'save', 'remove', 'unlink', 'link'

fn function <optional>

If a function is given, only that function is removed as a listener.

(async) validate(propertyopt, setDirectlyopt) → {Promise.<boolean>}

Source:

Check if one or all properties are valid and optionally set the unique indices immediately. If a property is invalid the NohmModel#errors object will be populated with error messages.

Parameters:
Name Type Attributes Default Description
property string <optional>

Property name if you only want to check one property for validity or null for all properties

setDirectly boolean <optional>
false

Set to true to immediately set the unique indices while checking. This prevents race conditions but should probably only be used internally

Returns:

Promise resolves to true if checked properties are valid.

Type
Promise.<boolean>