NohmClass

NohmClass

new NohmClass()

Source:

Main Nohm class. Holds models, generic configuration and can generate the middleware for client validations.

Can be instantiated multiple times if you want different configurations, but usually you only need the default that is exported as require('nohm').nohm.

Example
// To instantiate another you can do this:
const NohmClass = require('nohm').NohmClass;
const myNohm = new NohmClass({ prefix: 'SomePrefix' });

Methods

(async) factory(name, idopt) → {Promise.<NohmModel>}

Source:

Creates a new instance of the model with the given modelName. When given an id as second parameter it also loads it.

Parameters:
Name Type Attributes Description
name string

Name of the model, must match the modelName of one of your defined models.

id * <optional>

ID of a record you want to load.

Throws:
  • Rejects when there is no registered model with the given modelName.

    Type
    Error('Model %name not found.')
  • If no record exists of the given id, an error is thrown with the message 'not found'

    Type
    Error('not found')
Returns:
Type
Promise.<NohmModel>

getModels() → {Array.<NohmModelStatic>}

Source:

Get all model classes that are registered via .register() or .model()

Returns:
Type
Array.<NohmModelStatic>

middleware(options) → {Middleware~callback}

Source:
See:

Returns a middleware that can deliver the validations as a javascript file and the modelspecific validations as a JSON object to the browser. This is useful if you want to save some bandwith by doing the validations in the browser before saving to the server.

Example:

   server.use(nohm.middleware(
     // options object
     {
       url: '/nohm.js',
       namespace: 'nohm',
       exclusions: {

         User: { // modelName

           // this will ignore the second validation in the validation definition array for
           // the property 'name' in the model definition
           name: [false, true],

           // this will completely ignore all validations for the salt property
           salt: true
         },

         Privileges: true // this will completely ignore the Priviledges model
       }
     }
   ));
Parameters:
Name Type Description
options Object

Options for the middleware

Properties
Name Type Attributes Default Description
url string <optional>
'/nomValidations.js'

Url under which the js file will be available.

exclusions object.<string, (object|boolean)> <optional>
{}

Object containing exclusions for the validations export - see example for details

namespace string <optional>
'nomValidations'

Namespace to be used by the js file in the browser.

extraFiles string <optional>
[]

Extra files containing validations. You should only use this if they are not already set via Nohm.setExtraValidations as this automatically includes those.

maxAge number <optional>
3600

Cache control in seconds. (Default is one hour)

uglify boolean <optional>
false

True to enable minification. Requires uglify-js to be installed in your project!

Returns:
Type
Middleware~callback

model(modelName, options, temp) → {NohmStaticModel}

Source:

Creates and returns a new model class with the given name and options. If you're using Typescript it is strongly advised to use Nohm.register() instead.

Parameters:
Name Type Default Description
modelName string

Name of the model. This needs to be unique and is used in data storage. Thus changing this will invalidate existing data!

options IModelDefinitions

This is an object containing the actual model definitions. These are: properties, methods (optional) and the client (optional) to be used.

temp boolean false

When true, this model is not added to the internal model cache, meaning methods like factory() and getModels() cannot access them. This is mostly useful for meta things like migrations.

Returns:
Type
NohmStaticModel

(async) purgeDb(clientopt)

Source:

DO NOT USE THIS UNLESS YOU ARE ABSOLUTELY SURE ABOUT IT!

Deletes any keys from the db that start with the set nohm prefixes.

DO NOT USE THIS UNLESS YOU ARE ABSOLUTELY SURE ABOUT IT!

Parameters:
Name Type Attributes Description
client Object <optional>

You can specify the redis client to use. Default: Nohm.client

register(subClass, temp) → {NohmStaticModel}

Source:

Creates, registers and returns a new model class from a given class. When using Typescript this is the preferred method of creating new models over using Nohm.model().

Example
// Typescript
  import { Nohm, NohmModel, TTypedDefinitions } from 'nohm';

  // this interface is useful for having typings in .property() and .allProperties() etc. but is optional
  interface IUserModelProps {
   name: string;
  }

  class UserModelClass extends NohmModel<IUserModelProps> {
    protected static modelName = 'user'; // used in redis to store the keys

    // the TTypedDefinitions generic makes sure that our definitions have the same keys as
    // defined in our property interface.
    // If you don't want to use the generic, you have to use the exported {type}Property types
    // to get around the tsc throwing an error.
    // TODO: look into the error thrown by tsc when leaving out TTypedDefinitions and using 'sometype' as type
    protected static definitions: TTypedDefinitions<IUserModelProps> = {
      name: {
        defaultValue: 'testName',
        type: 'string', // you have to manually make sure this matches the IUserModelProps type!
        validations: [
          'notEmpty',
        ],
      },
    };
    public async foo() {
      const test = bar.property('name'); // no error and test typed to string

      await bar.validate();
      bar.errors.name; // no error and typed

      // accessing unknown props does not work,
      // because we specified that UserModel only has properties of IUserModelProps
      bar.property('foo'); // typescript errors
      bar.errors.foo; // typescript error
    };
  }
  const userModel = Nohm.register(UserModelClass);
  // typescript now knows about bar.foo() and all the standard nohm methods like bar.property();
  const bar = new userModel();
  bar.foo(); // no error
  bar.allProperties().name === 'testName'; // no error
Parameters:
Name Type Default Description
subClass NohmModel

Complete model class, needs to extend NohmModel.

temp boolean false

When true, this model is not added to the internal model cache, meaning methods like factory() and getModels() cannot access them. This is mostly useful for meta things like migrations.

Returns:
Type
NohmStaticModel

setClient()

Source:

Set the Nohm global redis client. Note: this will not affect models that have a client set on their own.

setPrefix()

Source:

Set the Nohm global redis client. Note: this will not affect models that have a client set on their own.