commit 8e020a5d4ea6972ab94d2c71dd6ac720a1666c35 Author: Anatoliy Chakkaev Date: Tue Mar 27 18:46:55 2012 +0400 docs diff --git a/abstract-class.html b/abstract-class.html new file mode 100644 index 00000000..65cd5d81 --- /dev/null +++ b/abstract-class.html @@ -0,0 +1,702 @@ + + + + AbstractClass | JugglingDB API docs + + + + + + +
+
+ +
+

AbstractClass

Abstract class - base class for all persist objects +provides common API to access any database adapter. +This class describes only abstract behavior layer, refer to lib/adapters/*.js +to learn more about specific adapter implementations

+ +

AbstractClass mixes Validatable and Hookable classes methods

+ +


constructor +
param Object data - initial object data

Source code
Class methods Instance methods Helper methods

AbstractClass.defineProperty

Declared as function (prop, params)


param String prop - property name +
param Object params - various property configuration

Source code

AbstractClass.create

Declared as function (data, callback)

Create new instance of Model class, saved in database

+ +


param data [optional] +
param callback(err, obj) +callback called with arguments:

+ +
    +
  • err (null or Error)
  • +
  • instance (null or Model)
  • +
Source code

AbstractClass.upsert

Declared as AbstractClass.updateOrCreate

Update or insert

Source code

AbstractClass.exists

Declared as function exists(id, cb)

Check whether object exitst in database

+ +


param id id - identifier of object (primary key value) +
param Function cb - callbacl called with (err, exists: Bool)

Source code

AbstractClass.find

Declared as function find(id, cb)

Find object by id

+ +


param id id - primary key value +
param Function cb - callback called with (err, instance)

Source code

AbstractClass.all

Declared as function all(params, cb)

Find all instances of Model, matched by query +make sure you have marked as index: true fields for filter or sort

+ +


param Object params (optional)

+ +
    +
  • where: Object { key: val, key2: {gt: 'val2'}}
  • +
  • order: String
  • +
  • limit: Number
  • +
  • skip: Number
  • +
+ +


param Function callback (required) called with arguments:

+ +
    +
  • err (null or Error)
  • +
  • Array of instances
  • +
Source code

AbstractClass.findOne

Declared as function findOne(params, cb)

Find one record, same as all, limited by 1 and return object, not collection

+ +


param Object params - search conditions +
param Function cb - callback called with (err, instance)

Source code

AbstractClass.destroyAll

Declared as function destroyAll(cb)

Destroy all records +
param Function cb - callback called with (err)

Source code

AbstractClass.count

Declared as function (where, cb)

Return count of matched records

+ +


param Object where - search conditions (optional) +
param Function cb - callback, called with (err, count)

Source code

AbstractClass.toString

Declared as function ()

Return string representation of class

+ +


override default toString method

Source code

AbstractClass.hasMany

Declared as function hasMany(anotherClass, params)

Declare hasMany relation

+ +


param Class anotherClass - class to has many +
param Object params - configuration {as:, foreignKey:} +
example User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});

Source code

AbstractClass.belongsTo

Declared as function (anotherClass, params)

Declare belongsTo relation

+ +


param Class anotherClass - class to belong +
param Object params - configuration {as: 'propertyName', foreignKey: 'keyName'}

Source code

AbstractClass.scope

Declared as function (name, params)

Define scope +TODO: describe behavior and usage examples

Source code

AbstractClass.prototype.save

Declared as function (options, callback)

Save instance. When instance haven't id, create method called instead. +Triggers: validate, save, update | create +
param options {validate: true, throws: false} [optional] +
param callback(err, obj)

Source code

AbstractClass.prototype._adapter

Declared as function ()

Return adapter of current record +
private

Source code

AbstractClass.prototype.toObject

Declared as function (onlySchema)

Convert instance to Object

+ +


param Boolean onlySchema - restrict properties to schema only, default false +when onlySchema == true, only properties defined in schema returned, +otherwise all enumerable properties returned +
returns Object - canonical object representation (no getters and setters)

Source code

AbstractClass.prototype.destroy

Declared as function (cb)

Delete object from persistence

+ +


triggers destroy hook (async) before and after destroying object

Source code

AbstractClass.prototype.updateAttribute

Declared as function updateAttribute(name, value, callback)

Update single attribute

+ +

equals to `updateAttributes({name: value}, cb)

+ +


param String name - name of property +
param Mixed value - value of property +
param Function callback - callback called with (err, instance)

Source code

AbstractClass.prototype.updateAttributes

Declared as function updateAttributes(data, cb)

Update set of attributes

+ +

this method performs validation before updating

+ +


trigger validation, save and update hooks +
param Object data - data to update +
param Function callback - callback called with (err, instance)

Source code

AbstractClass.prototype.propertyChanged

Declared as function propertyChanged(attr)

Checks is property changed based on current property and initial value

+ +


param String attr - property name +
return Boolean

Source code

AbstractClass.prototype.reload

Declared as function reload(callback)

Reload object from persistence

+ +


requires id member of object to be able to call find +
param Function callback - called with (err, instance) arguments

Source code

AbstractClass.prototype.reset

Declared as function ()

Reset dirty attributes

+ +

this method does not perform any database operation it just reset object to it's +initial state

Source code

isdef

Declared as function isdef(s)

Check whether s is not undefined +
param Mixed s +
return Boolean s is undefined

Source code

merge

Declared as function merge(base, update)

Merge base and update params +
param Object base - base object (updating this object) +
param Object update - object with new data to update base +
returns Object base

Source code

defineReadonlyProp

Declared as function defineReadonlyProp(obj, key, value)

Define readonly property on object

+ +


param Object obj +
param String key +
param Mixed value

Source code

addToCache

Declared as function addToCache(constr, obj)

Add object to cache

Source code

touchCache

Declared as function touchCache(constr, id)

Renew object position in LRU cache index

Source code

getCached

Declared as function getCached(constr, id)

Retrieve cached object

Source code

clearCache

Declared as function clearCache(constr)

Clear cache (fully)

+ +

removes both cache and LRU index

+ +


param Class constr - class constructor

Source code

removeFromCache

Declared as function removeFromCache(constr, id)

Remove object from cache

+ +


param Class constr +
param id id

Source code
+
+ +
+
+

© 1602 Software

+
+
+ + + + + diff --git a/schema.html b/schema.html new file mode 100644 index 00000000..61b0794b --- /dev/null +++ b/schema.html @@ -0,0 +1,287 @@ + + + + Schema | JugglingDB API docs + + + + + + +
+
+ +
+

Schema

Schema - adapter-specific classes factory.

+ +

All classes in single schema shares same adapter type and +one database connection

+ +


param name - type of schema adapter (mysql, mongoose, sequelize, redis) +
param settings - any database-specific settings which we need to +establish connection (of course it depends on specific adapter)

+ +
    +
  • host
  • +
  • port
  • +
  • username
  • +
  • password
  • +
  • database
  • +
  • debug Boolean = false
  • +
+ +


example Schema creation, waiting for connection callback

+ +
var schema = new Schema('mysql', { database: 'myapp_test' });
+schema.define(...);
+schema.on('connected', function () {
+    // work with database
+});
+
Source code
Instance methods Helper methods

Schema.prototype.define

Declared as function defineClass(className, properties, settings)

Define class

+ +


param String className +
param Object properties - hash of class properties in format + {property: Type, property2: Type2, ...} + or + {property: {type: Type}, property2: {type: Type2}, ...} +
param Object settings - other configuration of class +
return newly created class

+ +


example simple case

+ +
var User = schema.defind('User', {
+    email: String,
+    password: String,
+    birthDate: Date,
+    activated: Boolean
+});
+
+ +


example more advanced case

+ +
var User = schema.defind('User', {
+    email: { type: String, limit: 150, index: true },
+    password: { type: String, limit: 50 },
+    birthDate: Date,
+    registrationDate: {type: Date, default: function () { return new Date }},
+    activated: { type: Boolean, default: false }
+});
+
Source code

Schema.prototype.defineProperty

Declared as function (model, prop, params)

Define single property named prop on model

+ +


param String model - name of model +
param String prop - name of propery +
param Object params - property settings

Source code

Schema.prototype.automigrate

Declared as function (cb)

Drop each model table and re-create. +This method make sense only for sql adapters.

+ +
Warning! All data will be lost! Use autoupdate if you need your data.
Source code

Schema.prototype.autoupdate

Declared as function (cb)

Update existing database tables. +This method make sense only for sql adapters.

Source code

Schema.prototype.isActual

Declared as function (cb)

Check whether migrations needed +This method make sense only for sql adapters.

Source code

Schema.prototype.log

Declared as function (sql, t)

Log benchmarked message. Do not redefine this method, if you need to grab +chema logs, use schema.on('log', ...) emitter event

+ +


private used by adapters

Source code

Schema.prototype.freeze

Declared as function freeze()

Freeze schema. Behavior depends on adapter

Source code

Schema.prototype.tableName

Declared as function (modelName)

Return table name for specified modelName +
param String modelName

Source code

Schema.prototype.defineForeignKey

Declared as function defineForeignKey(className, key)

Define foreign key +
param String className +
param String key - name of key field

Source code

Schema.prototype.disconnect

Declared as function disconnect()

Close database connection

Source code

hiddenProperty

Declared as function hiddenProperty(where, property, value)

Define hidden property

Source code
+
+ +
+
+

© 1602 Software

+
+
+ + + + + diff --git a/validatable.html b/validatable.html new file mode 100644 index 00000000..030fd420 --- /dev/null +++ b/validatable.html @@ -0,0 +1,286 @@ + + + + Validatable | JugglingDB API docs + + + + + + +
+
+ +
+

Validatable

Validation encapsulated in this abstract class.

+ +

Basically validation configurators is just class methods, which adds validations +configs to AbstractClass._validations. Each of this validations run when +obj.isValid() method called.

+ +

Each configurator can accept n params (n-1 field names and one config). Config +is Object depends on specific validation, but all of them has one common part: +message member. It can be just string, when only one situation possible, +e.g. Post.validatesPresenceOf('title', { message: 'can not be blank' });

+ +

In more complicated cases it can be Hash of messages (for each case): +User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});

Source code
Class methods Instance methods Helper methods

Validatable.validatesPresenceOf

Declared as getConfigurator('presence');

Validate presence. This validation fails when validated field is blank.

+ +

Default error message "can't be blank"

+ +


example Post.validatesPresenceOf('title') +
example Post.validatesPresenceOf('title', {message: 'Can not be blank'}) +
sync

+ +


see helper/validatePresence

Source code

Validatable.validatesLengthOf

Declared as getConfigurator('length');

Validate length. Three kinds of validations: min, max, is.

+ +

Default error messages:

+ +
    +
  • min: too short
  • +
  • max: too long
  • +
  • is: length is wrong
  • +
+ +


example User.validatesLengthOf('password', {min: 7}); +
example User.validatesLengthOf('email', {max: 100}); +
example User.validatesLengthOf('state', {is: 2}); +
example `User.validatesLengthOf('nick', {min: 3, max: 15}); +
sync

+ +


see helper/validateLength

Source code

Validatable.validatesNumericalityOf

Declared as getConfigurator('numericality');

Validate numericality.

+ +


example User.validatesNumericalityOf('age', { message: { number: '...' }}); +
example User.validatesNumericalityOf('age', {int: true, message: { int: '...' }});

+ +

Default error messages:

+ +
    +
  • number: is not a number
  • +
  • int: is not an integer
  • +
+ +


sync

+ +


see helper/validateNumericality

Source code

Validatable.validatesInclusionOf

Declared as getConfigurator('inclusion');

Validate inclusion in set

+ +


example User.validatesInclusionOf('gender', {in: ['male', 'female']});

+ +

Default error message: is not included in the list

+ +


see helper/validateInclusion

Source code

Validatable.validatesExclusionOf

Declared as getConfigurator('exclusion');

Validate exclusion

+ +


example Company.validatesExclusionOf('domain', {in: ['www', 'admin']});

+ +

Default error message: is reserved

+ +


see helper/validateExclusion

Source code

Validatable.validatesFormatOf

Declared as getConfigurator('format');

Validate format

+ +

Default error message: is invalid

+ +


see helper/validateFormat

Source code

Validatable.validate

Declared as getConfigurator('custom');

Validate using custom validator

+ +

Default error message: is invalid

+ +


see helper/validateCustom

Source code

Validatable.validateAsync

Declared as getConfigurator('custom',

Validate using custom async validator

+ +

Default error message: is invalid

+ +


async +
see helper/validateCustom

Source code

Validatable.validatesUniquenessOf

Declared as getConfigurator('uniqueness',

Validate uniqueness

+ +

Default error message: is not unique

+ +


async +
see helper/validateUniqueness

Source code

Validatable.prototype.isValid

Declared as function (callback)

This method performs validation, triggers validation hooks. +Before validation obj.errors collection cleaned. +Each validation can add errors to obj.errors collection. +If collection is not blank, validation failed.

+ +
Warning! This method can be called as sync only when no async validation configured. It's strongly recommended to run all validations as asyncronous.
+ +


param Function callback called with (valid) +
return Boolean true if no async validation configured and all passed

+ +


example ExpressJS controller: render user if valid, show flash otherwise

+ +
user.isValid(function (valid) {
+    if (valid) res.render({user: user});
+    else res.flash('error', 'User is not valid'), console.log(user.errors), res.redirect('/users');
+});
+
Source code

validatePresence

Declared as function validatePresence(attr, conf, err)

Presence validator

Source code

validateLength

Declared as function validateLength(attr, conf, err)

Length validator

Source code

validateNumericality

Declared as function validateNumericality(attr, conf, err)

Numericality validator

Source code

validateInclusion

Declared as function validateInclusion(attr, conf, err)

Inclusion validator

Source code

validateExclusion

Declared as function validateExclusion(attr, conf, err)

Exclusion validator

Source code

validateFormat

Declared as function validateFormat(attr, conf, err)

Format validator

Source code

validateCustom

Declared as function validateCustom(attr, conf, err, done)

Custom validator

Source code

validateUniqueness

Declared as function validateUniqueness(attr, conf, err, done)

Uniqueness validator

Source code

blank

Declared as function blank(v)

Return true when v is undefined, blank array, null or empty string +otherwise returns false

+ +


param Mix v +
returns Boolean whether v blank or not

Source code
+
+ +
+
+

© 1602 Software

+
+
+ + + + +