JavaScript數據庫:Taffy DB

openkk 12年前發布 | 25K 次閱讀 JavaScript開發 JavaScript

TaffyDB 是一個免費開源的 JavaScript 庫,用于在 Web 上實現一個輕量級的數據訪問層,也就是一個簡單的數據庫。

主要特點:

  • 很小,只有10K左右
  • 簡單,JavaScript的語法
  • 快速
  • 易于集成到任何Web應用
  • 兼容主流的Ajax庫,例如:YUI, JQuery, Dojo, Prototype, EXT, etc
  • CRUD 接口 (Create, Read, Update, Delete)
  • 排序
  • 循環
  • 高級查詢

這就是瀏覽器上的SQL數據庫:)

創建一個數據庫
// Create DB and fill it with records
var friends = TAFFY([
    {"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
    {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
    {"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
    {"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"}   
]);

過濾器使用的數據庫的名稱和對象的比較
// Find all the friends in Seattle
friends({city:"Seattle, WA"});

// Find John Smith, by ID
friends({id:1});

// Find John Smith, by Name
friends({first:"John",last:"Smith"});

輕松讀取數據
// Kelly's record
var kelly = friends({id:2}).first();

// Kelly's last name
var kellyslastname = kelly.last;

// Get an array of record ids
var cities = friends().select("id");

// Get an array of distinct cities
var cities = friends().distinct("city");

// Apply a function to all the male friends
friends({gender:"M"}).each(function (r) {
   alert(r.name + "!");
});

快速修改數據
// Move John Smith to Las Vegas
friends({first:"John",last:"Smith"}).update({city:"Las Vegas, NV:"});

// Remove Jennifer Gill as a friend
friends({id:4}).remove();

// insert a new friend
friends.insert({"id":5,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"});

項目主頁:http://www.baiduhome.net/lib/view/home/1345943714536

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!