{"action":"created","issue":{"url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite/issues/31","repository_url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite","labels_url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite/issues/31/labels{/name}","comments_url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite/issues/31/comments","events_url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite/issues/31/events","html_url":"https://github.com/fraserxu/react-native-couchbase-lite/issues/31","id":141874264,"number":31,"title":"Atomic Counter","user":{"login":"ssomnoremac","id":3521359,"avatar_url":"https://avatars.githubusercontent.com/u/3521359?v=3","gravatar_id":"","url":"https://api.github.com/users/ssomnoremac","html_url":"https://github.com/ssomnoremac","followers_url":"https://api.github.com/users/ssomnoremac/followers","following_url":"https://api.github.com/users/ssomnoremac/following{/other_user}","gists_url":"https://api.github.com/users/ssomnoremac/gists{/gist_id}","starred_url":"https://api.github.com/users/ssomnoremac/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ssomnoremac/subscriptions","organizations_url":"https://api.github.com/users/ssomnoremac/orgs","repos_url":"https://api.github.com/users/ssomnoremac/repos","events_url":"https://api.github.com/users/ssomnoremac/events{/privacy}","received_events_url":"https://api.github.com/users/ssomnoremac/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-03-18T13:58:47Z","updated_at":"2016-03-22T20:24:56Z","closed_at":null,"body":"I'm going to write an atomic counter api to increment a document key\r\n\r\n/user::234::post::12\r\n\r\nbut is that something that belongs in this module? And it begs the question: is using a bridge to the REST API over one to the platform specific APIs sustainable for larger projects?"},"comment":{"url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite/issues/comments/200005225","html_url":"https://github.com/fraserxu/react-native-couchbase-lite/issues/31#issuecomment-200005225","issue_url":"https://api.github.com/repos/fraserxu/react-native-couchbase-lite/issues/31","id":200005225,"user":{"login":"ssomnoremac","id":3521359,"avatar_url":"https://avatars.githubusercontent.com/u/3521359?v=3","gravatar_id":"","url":"https://api.github.com/users/ssomnoremac","html_url":"https://github.com/ssomnoremac","followers_url":"https://api.github.com/users/ssomnoremac/followers","following_url":"https://api.github.com/users/ssomnoremac/following{/other_user}","gists_url":"https://api.github.com/users/ssomnoremac/gists{/gist_id}","starred_url":"https://api.github.com/users/ssomnoremac/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ssomnoremac/subscriptions","organizations_url":"https://api.github.com/users/ssomnoremac/orgs","repos_url":"https://api.github.com/users/ssomnoremac/repos","events_url":"https://api.github.com/users/ssomnoremac/events{/privacy}","received_events_url":"https://api.github.com/users/ssomnoremac/received_events","type":"User","site_admin":false},"created_at":"2016-03-22T20:24:55Z","updated_at":"2016-03-22T20:24:55Z","body":"say I want to do ```UserID``` + ```counterID``` like so:\r\n\r\n    u::bob-1457227348::rs::18\r\n      baseID::prefix::counter\r\nThe idea is to store the counter in the '_counter' doc using the key of the UserID (baseID) to enforce atomic. This is what I have so far: \r\n```\r\n   incrementAndCreate: function (jsonDocument, baseID, prefix) {\r\n    let counter;\r\n    this.getDocument('_counter')\r\n      .then((res) => {\r\n        \r\n        // if no counter, create an empty counter\r\n        if (res.status == 404) {\r\n          this.createDocument({}, '_counter')\r\n            .then((response) => {\r\n              counter = response;\r\n            }\r\n        } else { counter = res; }  // existing counter         \r\n        \r\n        // create or increment the counter field with baseID key\r\n        if (counter[baseID] != undefined){\r\n          counter[baseID]++;\r\n        } else { counter[baseID] = 0; }\r\n        \r\n        let rev = counter['_rev'];\r\n        delete counter['_rev']\r\n        return this.updateDocument(counter, rev)\r\n      })\r\n      .then((res) => { return createDocument(jsonDocument, baseID + \"::\" + prefix + \"::\" counter[baseID]) })\r\n      .catch((err) => { throw err; });\r\n  },\r\n```"}}