Model schema definition

Model schema is defined in JSON SCHEMA format.
json-schema implementation is provided by Ajv library and thus you can utilize the full power of Ajv!

Primitive data type definition

    {
        type: 'object',
        properties: {
            required: ['username', 'sex'],//by default all properties are optional
            username: { // defines `username` property of type `string` with default value being `happiecat`
                type: 'string',
                default: "happiecat"
            },
            age: {type: 'integer'},
            sex: {
                type: 'string',
                enum: ['male', 'female']
            }
        }
    }

Compound data types

    {
        type: 'object',
        properties: {
            apps: {
                type: 'array',
                default: [], // => defines default array value
                items: { // => array item schema definition is optional. Defines schema of an item of the array
                    type: 'object',
                    default: {name: 'Snapchat'}, // => defines default array ITEM value
                    properties: {/* ... */}
                }
            },
            address: {
                type: 'object',
                additionalProperties: false, //filters out everything except described properties
                properties: {
                    street: {type: 'string'},
                    city: {type: 'string'}
                }
            },
            friend: {
                type: 'object',
                $relation: {type: 'User'} // defines relation to User Model
            }
        }
    }

More complex example

//File image model
const Image = couchbase.define('Image', {
    type: 'string'
});

//User model
const User = couchbase.define('User', {
    type: 'object',
    required: ['username', 'born_at'],
    properties: {
        username: {type: 'string'},
        friends: {
            type: 'array',
            items: {
                type: 'object',
                $relation: {type: 'User'}
            }
        },
        address: { type: 'object' },
        profilePicture: {
            type: 'object',
            $relation: {type: "Image"}
        },
        isVerified: {
            type: 'boolean',
            default: false
        }
        born_at: {
            type: 'string',
            format: 'date-time'
        }
    }
});

Root schema data type does not have to be an object, it can also be an array or one of the primitive data types like string or integer.

comments powered by Disqus