alternatex/store

轻量级存储包装器

安装: 38

依赖项: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 0

开放性问题: 0

语言:JavaScript

1.0.11 2013-11-15 02:47 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:52:41 UTC


README

Build Status

提供CRUD操作的轻量级数据存储包装器。

索引

快速入门

嵌入

Require.js

require(["store"], function(Store) {
  // ...
});

HTML

<script src="src/client/store.js"></script>

客户端API

存储

配置

默认配置如下

Store.configure({ 
  url: "https:///datastore.php", 
  ttl: 3600
});

注意

configure() 修改了原型对象,因此如果相应的实例属性之前未被修改,则修改会传播到实例属性级别。

创建

var objectStore = new Store({ namespace: 'object' });

对象

创建

预设
var object = objectStore.create({ 
  country: 'USA', 
  firstname: 'Stephen', 
  lastname: 'Colbert' 
}); 
Barebone
var object = objectStore.create(); 
object.set('country', 'USA');
object.set('firstname', 'Jon'); 
object.set('lastname', 'Stewart'); 

读取

列表
objectStore.list();
单个
objectStore.get('8c0c1ff0-d0fe-38b7-376a-b0b1d53bd557');

更新

实例
object.update();
数据存储
objectStore.update(object);

删除

实例
object.remove();
数据存储
objectStore.remove(object);

摘要

<script src="src/client/store.js"></script>
<script>
(function(){

  // local variables
  var object, objectId, objectStore, objects;

  // configure store defaults 
  Store.configure({ 
    url: "https:///store/examples/server.php", 
    ttl: 3600
  });

  // create new store
  objectStore = new Store({ namespace: 'object' });

  // create new store bound object
  object = objectStore.create({ 
    country: 'US', 
    firstname: 'Stephen', 
    lastname: 'Colbert' 
  });
  
  // update on insert 
  objectStore.update(object).done(function(object){
    
    // wrap json object
    object = objectStore.create(object);

    // update properties
    object.set('firstname', 'Jon');
    object.set('lastname', 'Stewart');

    // extract
    objectId = object.get('id');

    // update object
    objectStore.update(object).done(function(object){        

        // fetch object w/previously retrieved objectId
        objectStore.get(objectId).done(function(object){

           // wrap json object
           object = objectStore.create(object);

           // say hi
           console.log(object.get('firstname') + " " + object.get('lastname'));
        });
    });
  });

  // collect update promises
  objects = [];

  // create some objects
  Store.times(10, function(count){

    // store update promises
    objects.push(objectStore.create({ 
      title: 'No.' + count,       
      abstract: 'Lorem Ipsum [...]',       
      text: 'Lorem Ipsum Si Amet They Say',             
      author: 'me', 
      lastmod: new Date().getTime()
    }).update());
  });

  // when all objects have been updated
  Store.when(objects).done(function(objects){
  
    // fetch all objects 
    objects = objectStore.list();
    
    // print when object have been retrieved
    Store.when(objects).done(function(objects){

      // log each
      objects.forEach(function(object, index){
        console.log(index, object);
      });
    });    
  });

  // fetch object 
  object = objectStore.get(objectId);

  // process fetched object
  Store.when(object).done(function(object){  

    // execute on success  
    console.info("done", object);  

  }).fail(function(objects){    

    // execute on fail
    console.error("fail", objects);    

  }).always(function(objects){    

    // always execute this block
    console.log("always", objects);

  });

})();
</script>

<!-- example html input -->
<form method="POST" action="///store/examples/server.php/resources/update" enctype="multipart/form-data">      
  <input type="hidden" name="namespace" value="resources"/>
  <input type="hidden" name="action" value="update"/>
  <input type="hidden" name="instance[type]" value="type"/>
  <input type="hidden" name="instance[name]" value="name"/>
  <input type="hidden" name="instance[option][key]" value="key"/>
  <input type="hidden" name="instance[option][value]" value="value"/>
  <input type="hidden" name="instance[option][lists][whitelist][]" value="good.host.com"/>
  <input type="hidden" name="instance[option][lists][blacklist][]" value="evil.host.com"/>
  <input type="file" name="instance[file]"/>
  <button>Update</button>
</form>

路由

...

服务器API

Latest Stable Version Dependency Status

存储库

文件存储

基于文件的集合存储。

限制

考虑到原型设计。无法扩展。

CSV

...

JSON

支持嵌入基于Base64编码的二进制数据。

序列化

使用PHP的serialize()函数存储数据。支持嵌入基于Base64编码的二进制数据。

文档

路线图

  • 访问控制
  • 同步
  • JSON Schema
    • 生成器

许可证

根据两个许可证发布:新BSD和MIT。您可以选择最适合您开发需求的许可证。

https://raw.github.com/alternatex/store/master/LICENSE