## CoCreateJS

Learn how to convert your html into a dynamic realtime application.

# CRUD Objects

Simple objects you can use to create custom components.

### Create Document Parameters

- #### namespace string optional

  create namespace

- #### rooms string optional

  creates room

- #### broadcast boolean optional

  broadcast or only save to db

- #### collection string required

  collection to save to

- #### document array object optional

  data to save

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crud.createDocument({
        collection: "string",
        document: {
          name1: "hello"
          name2: "hello1"
        }
      })

### Read Document Parameters

- #### collection string required

  collection to save to

- #### document_id _id required

  document_id to read from

- #### name array optional

  names of values to from CMS. If not used whole document will be fetched

- #### metaData string array object optional

  Additonal data in the form of a string ,array or object

      crud.readDocument({
        collection: "module",
        document : {
    		_id: "",
    		name: ''
    		...
    	}
      })

### Update Document Parameters

- #### namespace string optional

  create namespace

- #### rooms string optional

  creates room

- #### broadcast boolean optional

  broadcast or only save to db

- #### collection string required

  collection to save to

- #### document_id string required

  Document _id to save to

- #### data object optional

  data to save

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crud.updateDocument({
        collection: "test123",
        document: {
    	  _id: "document_id",
          name1: "hello",
          name2:  "hello1"
        },
        updateName: ["name3", "name4"],
        deleteName: ["name3", "name4"],
    	upsert: true|false //default to true
      })

### Delete Document Parameters

- #### namespace string optional

  create namespace

- #### rooms string optional

  creates room

- #### broadcast boolean optional

  broadcast or only save to db

- #### collection string required

  collection to save to

- #### document_id string required

  collection to save to

- #### data object optional

  data to save

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crud.deleteDocument({
        collection: "module",
        document: {
    		_id: ""
    	}
      })

### Read Document Filter Parameters

- #### collection string required

  collection to save to

- #### data object optional

  data to save

- #### element boolean optional

  Save data onChange/submitBtn. If attribute is empty or not used Default's to true.

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

- #### operator object required

  The operator object for read document list

  - #### fetch object optional

    {name: the field name for fetching, value: the value for fetching}

    If the "fetch" parameter define, result will fetch the items that is [name] = value

  - #### filters array[object] required

    The data structure for each item: {name: string, operator: string, value: array}

    name: The field name

    operator: Query operator (includes | $range | $eq | $ne | $lt | $lte | $gt | $gte | $in | $nin | $geoWithin)

    value: The operator values

  - #### orders array[object] optional

    The data structure for each item: {name: string, type: integer}

    name: The field name

    type: The order type(-1: desc, 1: asc)

  - #### search object optional

    All fields values search

    type string(or|and): The search type (or | and)

    value array: The search values

  - #### startIndex integer required

    Set the start index that fetch items. If don't need it, startIndex can define 0

  - #### count integer optional

    Set the count that fetch items. If count didn't defined, it will fetch whole result items

- #### export boolean optional

  If export is true, it will downlod the file about response

      crud.readDcoumentList {
        collection: "modules",
        filter: {
    	    query: [{
    				name: 'field1',
    				value: [v1, v2, ...],
    				operator: "includes | $range | $eq | $ne | $lt | $lte | $gt | $gte | $in | $nin | $geoWithin"
    		    }, {
    		    	name: "_id",
    				opreator: "in",
    				value: ["id1"]
    	    	}
    	    ],
    	    sort: [{
    			name: 'field-x',
    			type: 1 | -1
    	    }],
    	    search: {
    			type: 'or | and',
    			value: [value1, value2]
    	    },

    	    startIndex: 0 (integer),
    	    count: 0 (integer)
        }

        -------- response data -----------
        document: [] // array

        Example:

    crud.readDocument({
      "key": "2061acef-0451-4545-f754-60cf8160",
      "organization_id": "5ff747727005da1c272740ab",
      "collection": "module_activity",
      "metadata": "",
      // TODO: update to new filters
      "filter": {
        "query": [
          {
            "name": "color",
            "value": [ "red", "blue" ],
            "operator": "in"
          }
        ],
        "sort": [
          {
            "name": "name",
            "type": 'asc'
          }
        ],
        "search": {
          "operator": "or",
          "value": [ "test" ]
        },
        "startIndex": 0,
        "limit": 20
      }
    })

# CRDT Objects

Conflict resolving objects to create collaborative apps.

### Init CRDT Parameters

- #### namespace string optional

  create namespace

- #### rooms string optional

  creates room

- #### broadcast boolean optional

  broadcast or only save to db

- #### collection string required

  collection to save to

- #### data object optional

  data to save

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crdt.init({
        collection: "module",
        document_id: "",
        name: ""
      })

### Get Data CRDT Parameters

- #### collection string required

  collection to save to

- #### data object optional

  data to save

- #### element boolean optional

  Save data onChange/submitBtn. If attribute is empty or not used Default's to true.

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crdt.getText({
      	collection: 'module_activities',
      	document_id: '5e4802ce3ed96d38e71fc7e5',
      	name: 'name'
      })

### Update Data CRDT Parameters

- #### collection string required

  collection to save to

- #### document_id _id required

  document_id to insert-crdt from

- #### name object optional

  data to save

- #### value object optional

  data to save

- #### start object optional

  data to save

- #### crud object optional

  data to save

- #### element boolean optional

  Save data onChange/submitBtn. If attribute is empty or not used Default's to true.

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crdt.updateText({
    	collection: 'module_activities',
    	document_id: '5e4802ce3ed96d38e71fc7e5',
    	name: 'name',
    	value: 'T',
    	start: 8,
      })

### Replace Data CRDT Parameters

- #### broadcast boolean optional

  broadcast or only save to db

- #### collection string required

  collection to save to

- #### document_id string required

  collection to save to

- #### name object optional

  name to save

- #### value object optional

  characters to insert

- #### element boolean optional

  Save data onChange/submitBtn. If attribute is empty or not used Default's to true.

- #### metaData string array object optional

  Additonal data in the form of a string , array or object

      crdt.replaceText({
        collection: "module",
        document_id: "",
        name: "",
        value: "",
      })

# WebSocket Objects

Send and listen to messages

### Create Socket Parameters

- #### collection string required

  collection to save to

- #### document_id string required

  Document _id to save to

- #### name string required

  Save data onChange. If attribute is empty or not used Default's to true.

- #### start string optional

  Save data onChange/submitBtn. If attribute is empty or not used Default's to true.

- #### end string array object optional

  Additonal data in the form of a string , array or object

      socket.create({
        namespace: '',
        rooms: [r1, r2],
      })

### Send Message Parameters

- #### collection string required

  collection to save to

- #### document_id string required

  Document _id to save to

- #### name string required

  Save data onChange. If attribute is empty or not used Default's to true.

- #### start string optional

  Save data onChange/submitBtn. If attribute is empty or not used Default's to true.

- #### end string array object optional

  Additonal data in the form of a string , array or object

      socket.send('action', {
        namespace: '',
        room: '',
        broadcast: true/false,
        broadcast-sender: true/false
        broadcast-browser: true/false
    	data': {
    		...
    	}
      })

### Listen Message Parameters

- #### data string required

  collection to save to

- #### document_id _id required

  document_id to get-cursor from

- #### data object optional

  data to save

      socket.listen('action', function(data) {
    	...
      })