Datastore API 是 dropbox 已經廢棄的功能,不過 dropbox 將 jssdk 開源,這樣對於研究 A-CS 實現有很好的參考價值, 這裏將它翻譯出來,既當備份也當對 datastore 的設計有個基本的瞭解javascript
官方的介紹是,應用不單單是存儲和同步文件,經過 Datastore API , 結構化的數據如聯繫人列表,任務列表,遊戲狀態均可以像文件同步同樣簡單。 而且 Datastore 支持多平臺,離線訪問,自動衝突解決。而且能夠經過瀏覽器查看全部應用的數據 https://www.dropbox.com/developers-v1/br... 。 java
應用實例git
listsgithub
總結而言, Datastore 就是結構化數據的 A-CS 的實現api
用戶在客戶端經過 app_key 啓動 dropbox 認證,認證經過後,就能夠在客戶端建立 datastore manager 實例, 經過 manager 能夠獲取 datastores 列表,監聽 datastore 的變化, 同步 datastore, 更新 datastore數組
datastores 是應用結構化數據的容器, 每個 datastore 是一個 tables 的集合,每一個 table 是一個 records 的集合。 同數據庫同樣瀏覽器
datastore <-> 數據庫緩存
table <-> 數據庫表app
record <-> 數據記錄
當一個 datastore 被打開事後就會被緩存到本地,這樣就能夠實現應用數據的離線訪問,每一個 datastore 是相互獨立的, 當一個 datastore 改變的時候,會自動同步數據到 dropbox, 也可能會將 dropbox 的新數據(其餘客戶端的改變)同步到本地。
用戶之間能夠分享數據, 分享的基本單元是 datastore ,
records 是應用存儲數據的基本單元,每一個 record 有一個或任意多個 fields 組成,有一個 ID , 每一個 field 是 {key: value} 的組合, value 能夠是基本數據,也能夠是基本數據的數組, 基本數據
String (String)
Boolean (Boolean)
Integer (Dropbox.Datastore.int64)
Floating point (Number) – IEEE double. All native JavaScript numbers will be interpreted as floating-point numbers. To store integers, use the Dropbox.Datastore.int64 type.
Date (Date) – POSIX-like timestamp stored with millisecond precision.
Bytes (Uint8Array) – Arbitrary data, which is treated as binary, such as thumbnail images or compressed data. Individual records can be up to 100KB, which limits the size of the data.
List (Dropbox.Datastore.List) – A special value that can contain other values, though not other lists
和 SQL 不一樣的是, datastore 中的 table 不包含 schema , 因此 每一個 record 能夠有任意數目的 field
查詢 records
var results = taskTable.query({completed: false}); var firstResult = results[0];
監聽 record 變化
datastore.recordsChanged.addListener(function (event) { // affectedRecordsForTable 判斷哪些記錄變化了 console.log('records changed:', event.affectedRecordsForTable('tasks')); });
datastore 會自動的以 field 爲基本單位合併改變 , 舉個例子, 若是用戶在一個客戶端上修改了 taskname 和 complete 狀態,在另外的一個設備上 datastore api 會沒有衝突的自動合併這個改變。
若是同時的改變了相同 record 的相同 field, 這就須要解決衝突了, 能夠自定義衝突解決規則
taskTable.setResolutionRule('completed', 'local');
總共有 5個規則
remote – The remote value will be chosen. This is the default behavior for all fields.
local – The local value of the field will be chosen.
max – The greater of the two changes will be chosen.
min – The lesser of the two changes will be chosen.
sum – Additions and subtractions to the value will be preserved and combined.
// Shareable datastore datastoreManager.createDatastore(function (error, datastore) { /** * 給一組用戶設置角色 (principal) * 第一個參數是表示分享給誰 Dropbox.Datastore.PUBLIC – The role will apply to all Dropbox users. Dropbox.Datastore.TEAM – The role will apply to everyone on the user's team (only applicable for Dropbox for Business * 第二個參數表示分享模式 Dropbox.Datastore.NONE – The principal has no access to this datastore. Dropbox.Datastore.VIEWER – The principal is able to view this datastore. Dropbox.Datastore.EDITOR – The principal is able to edit this datastore. Dropbox.Datastore.OWNER – The principal is the owner of this datastore. This role cannot be assigned directly. The user who created a datastore is always that datastore's owner. */ datastore.setRole(Dropbox.Datastore.PUBLIC, Dropbox.Datastore.EDITOR); }); // 接受分享的用戶 datastoreManager.openDatastore(datastoreId, function (error, datastore) { // The datastore is now shared with this user. }); // 列舉 datastore的訪問人列表 datastore.listRoles() // 獲取當前用戶的 role datastore.getEffectiveRole()