Search code examples
ibm-mobilefirstauto-incrementjsonstore

IBM Worklight JSONStore Auto-increment field


I use Worklight JSONStore and I need a numeric field autoincrement. I tried this way but it did not work.

var COLLECTION_SPESE = {
    Spese : {
        searchFields:
            {id: "INTEGER primary key autoincrement", importo: "string", valuta: "string", metodoPagamento: "string", 
            acconto: "string", data: "string", motivazione: "string", categoria: "string", 
            icona: "string", checked: "boolean"}
    }
};

How can I do?


Solution

  • You would have to provide code to do the auto-incrementing yourself. For example WL.JSONStore.get('collection').add({id: getLastId() + 1, ...}). The getLastId() function would return the last id value used in the collection. You would have to write the implementation for the getLastId function. The search field type for id would be integer.

    Alternatively, you could depend of the value of _id which is generated by JSONStore. It is an auto-incremented integer starting from 1. The value assigned to _id is never re-used, for example, if you remove the document with _id == 1 and then add a new one, 1 is not used again for the new document.

    WL.JSONStore.get('collection').add({name: 'carlos})
    .then(function () {
      return WL.JSONStore.get('collection').findAll();
    })
    .then(function (res) {
      //res => [{_id: 1, json: {name: 'carlos'}}]
    })
    

    FYI - Feature requests here.