Class Model

Meta Model Class. Used to define database models in an object-oriented way.

class Model(modelDef)
Arguments:
  • modelDef (Object) – The model definition, that is, field definitions and meta data
Returns:

(Function) Model instance constructor

Examples

// define a model
var TYPE_CHOICES = [
     [1, "Book"],
     [2, "Brochure"],
     [3, "Flyer"]
];

var Literature = new Model({
    Meta: {
        dbTable: "literature_types"
    },

    title: new CharField(),
    author: new CharField({ maxLength: 50 }),
    orderId: new CharField({ maxLength: 10, primaryKey: true }),
    type: new IntegerField({ choices: TYPE_CHOICES })
});

// use the model to create a new instance
var literature = new Literature({
    title: "Alice's Adventures in Wonderland",
    author: "Lewis Carroll",
    orderId: 'AA',
    type: 1
});

See also

Field

Methods

getFields (static)

Returns a dictionary of field names and types

Model.getFields()
Returns:(Object) { fieldName1: FieldType1, fieldName2: FieldType2, ... }

See also

Field

save

Save method that every model instance has.

Model.prototype.save(onComplete)
Arguments:
  • onComplete (Function) – Callback when saving is finished. It is passed the saved model instance.
var Literature = new Model({ ... });

var literature = new Literature({ ... });

// save to database
literature.save();

validate

Validation method that every model instance has. Validates every field of the model.

Model.prototype.validate()
Returns:(Boolean|String) true if every field is valid. If that is not the case the validation error message is returned.

Attributes

Model.objects

(static)

The default model manager to be used for querys