Skip to content

Defining Content Types

Gerardo Bort edited this page Apr 4, 2014 · 5 revisions

Each Content-Type must be defined by code, more specifically in the config document. A Content Type in Mongorilla refers to a Model which is stored in a MongoDB collection. All these content definitions are placed in the collections array.

Schema

Data Types

Data Types have to be seen from two perspectives, the way they are persisted in MongoDB, and the way they're shown as a control to the CMS user. In order to simplify your work, we´ve already mapped each control component to a MongoDB data type, as follows:

Backbone-Forms Default Types

  • Backbone.Form.editors.Text results in String
  • Backbone.Form.editors.TextArea results in String
  • Backbone.Form.editors.Checkboxes results in String
  • Backbone.Form.editors.Select results in String
  • Backbone.Form.editors.Radio results in String
  • Backbone.Form.editors.Date results in Date
  • Backbone.Form.editors.DateTime results in Date
  • Backbone.Form.editors.List results in Array
  • Backbone.Form.editors.Object results in Object
  • Backbone.Form.editors.NestedModel (not supported)

Mongorilla Custom Types

  • Backbone.Form.editors.CKEditor results in String
  • Backbone.Form.editors.Datepicker results in Date
  • Backbone.Form.editors.File results in ObjectId that refers the collection fs.files
  • Backbone.Form.editors.Image results in ObjectId that refers the collection fs.files
  • Backbone.Form.editors.ObjectId results in ObjectId that refers the a custom collection

Fieldsets

Mongorilla renders its forms using Backbone-Forms, this allows you to use the Fieldsets functionality, which should be specified in collections.collection.backboneForms.fieldsets.

Relationships

Type of Relations

You can also specify relationships between Models, similar as in Backbone-Relationships. These relationships will give the forms the ability to show autocompletion options and also to make the backend API populate the model subdocuments based on the relations defined. Each relation must contain a type HasOne|HasMany and a relatedCollection referring the name of the collection related. The relations definitions should be placed in this path collections.collection.relations.

Example

{
    "collections": [
        {
            "name": "developer",
            "humanName": "Developer",
            "previewUrl": "http://example.com/${uri}.html",
            "backboneForms": {
                "schema": {
                    "firstname": { "type": "Text", "title": "First Name", "validators": ["required"] },
                    "lastname": { "type": "Text", "title": "Last Name", "validators": ["required"] },
                    "twitter.username": { "type": "Text", "title": "Twitter Username", "editorAttrs": { "placeholder": "@YourTwitterAccount" } },
                    "github.username": { "type": "Text", "title": "GitHub Username", "help": "e.g. pepe" },
                    "dob": { "type": "Datepicker", "title": "Day of Birth", "editorAttrs": { "placeholder": "dd/mm/yyyy", "format": "dd/mm/yyyy" } },
                    "death_year": { "type": "Number", "title": "Death Year" },
                    "bio": { "type": "CKEditor", "title": "Bio" },
                    "website_url": { "type": "Text", "validators": ["url"], "title": "Website URL", "help": "please, include http:// prefix", "editorAttrs": { "placeholder": "http://" } },
                    "photo": { "type": "Image", "title": "Photo", "pushToS3": true },
                    "other_photos": { "type": "List", "itemType": "Image", "title": "Other Photos", "pushToS3": true },
                    "cv": { "type": "File", "title": "Curriculum Vitae", "pushToS3": true },
                    "position": { "type": "Text", "title": "Current Position", "validators": ["required"] },
                    "company": { "type": "ObjectId", "title": "Actual Company", "validators": [] },
                    "previous_companies": { "type": "List", "itemType": "ObjectId", "title": "Previous Companies" },
                    "uri": {
                        "validators": [
                            "required",
                            {
                                "type": "regexp",
                                "regexp": { "__constructor": "RegExp", "__arguments": ["^[0-9a-z-]{2,}[0-9a-z]$"] }
                            }
                        ],
                        "title": "URI",
                        "editorAttrs": { "placeholder": "developer-name" }, 
                        "help": "URI is a slug token that helps to construct URLs:  example: /developers/developer-name.html" 
                    }
                },
                "fieldsets": [
                    {
                        "legend": "Basic Information",
                        "fields": ["firstname", "lastname", "dob", "photo", "other_photos"]
                    },
                    {
                        "legend": "About",
                        "fields": ["github.username", "twitter.username", "bio", "website_url"]
                    },
                    {
                        "legend": "Work",
                        "fields": ["cv", "position", "company", "previous_companies"]
                    },
                    {
                        "legend": "System Data",
                        "fields": ["uri"]
                    }
                ],
                "defaults": {
                }
            },
            "toStringField": "lastname",
            "updatedField": { "key": "updated", "type": "Date" },
            "createdField": { "key": "created", "type": "Date" },
            "fastSearch": {
                "find": {
                    "firstname": { "__constructor": "RegExp", "__arguments": ["(^|\\W*)${q}", "ig"] },
                    "lastname": { "__constructor": "RegExp", "__arguments": ["(^|\\W*)${q}", "ig"] }
                },
                "sort": { "updated": -1 },
                "limit": 10,
                "columns": ["lastname", "firstname", "uri", "created", "updated"]
            },
            "relations": { 
                "photo": {
                    "type": "HasOne",
                    "relatedCollection": "fs.files"
                },
                "other_photos": {
                    "type": "HasMany",
                    "relatedCollection": "fs.files"
                },
                "cv": {
                    "type": "HasOne",
                    "relatedCollection": "fs.files"
                },
                "company": {
                    "type": "HasOne",
                    "relatedCollection": "company"
                },
                "previous_companies": {
                    "type": "HasMany",
                    "relatedCollection": "company"
                }
            },
            "mongoose": {
                "schema": null
            },
            "readonly": false,
            "revisionable": true
        },
        {
            "name": "company",
            "humanName": "Company",
            "backboneForms": {
                "schema": {
                    "name": { "type": "Text", "title": "Name", "validators": ["required"] },
                    "description": { "type": "CKEditor", "title": "Description" },
                    "uri": {
                        "validators": [
                            "required",
                            {
                                "type": "regexp",
                                "regexp": { "__constructor": "RegExp", "__arguments": ["^[0-9a-z-]{2,}[0-9a-z]$"] }
                            }
                        ] 
                    }
                }
            },
            "relations": { 
            },
            "toStringField": "name",
            "updatedField": { "key": "updated", "type": "Date" },
            "createdField": { "key": "created", "type": "Date" },
            "fastSearch": {
                "find": { "name": { "__constructor": "RegExp", "__arguments": ["(^|\\W*)${q}", "ig"] } },
                "sort": { "updated": -1 },
                "limit": 10,
                "columns": ["name", "uri"]
            },
            "mongoose": {
                "schema": null
            },
            "readonly": false,
            "revisionable": true
        }
    ]
}