├── .gitignore ├── bower.json ├── LICENSE ├── storedb.js ├── README.md └── README_EN.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "storedb", 3 | "main": "storedb.js", 4 | "version": "0.1.2", 5 | "homepage": "https://github.com/djyde/StoreDB", 6 | "authors": [ 7 | "randy " 8 | ], 9 | "description": "MongoDB-like localStorage library", 10 | "keywords": [ 11 | "mongodb", 12 | "localStorage", 13 | "SPA", 14 | "html5", 15 | "angularjs" 16 | ], 17 | "license": "MIT", 18 | "private": true 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Randy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /storedb.js: -------------------------------------------------------------------------------- 1 | (function (global, factory) { 2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : 3 | typeof define === 'function' && define.amd ? define(factory) : 4 | global.storedb = factory() 5 | }(this, function () { 6 | 'use strict'; 7 | return function (collectionName) { 8 | collectionName = collectionName ? collectionName : 'default'; 9 | 10 | var err; 11 | var cache = localStorage[collectionName] ? JSON.parse(localStorage[collectionName]) : []; 12 | 13 | return { 14 | 15 | insert: function (obj, callback) { 16 | obj["_id"] = new Date().valueOf(); 17 | cache.push(obj); 18 | localStorage.setItem(collectionName, JSON.stringify(cache)); 19 | if (callback) 20 | callback(err, obj); 21 | }, 22 | 23 | find: function (obj, callback) { 24 | if (arguments.length == 0) { 25 | return cache; 26 | } else { 27 | var result = []; 28 | 29 | for (var key in obj) { 30 | for (var i = 0; i < cache.length; i++) { 31 | if (cache[i][key] == obj[key]) { 32 | result.push(cache[i]); 33 | } 34 | } 35 | } 36 | if (callback) 37 | callback(err, result); 38 | else 39 | return result; 40 | } 41 | }, 42 | 43 | update: function (obj, upsert, callback) { 44 | 45 | for (var key in obj) { 46 | for (var i = 0; i < cache.length; i++) { 47 | if (cache[i][key] == obj[key]) { 48 | 49 | end_loops: 50 | for (var upsrt in upsert) { 51 | switch (upsrt) { 52 | case "$inc": 53 | for (var newkey in upsert[upsrt]) { 54 | cache[i][newkey] = parseInt(cache[i][newkey]) + parseInt(upsert[upsrt][newkey]); 55 | } 56 | break; 57 | 58 | case "$set": 59 | for (var newkey in upsert[upsrt]) { 60 | cache[i][newkey] = upsert[upsrt][newkey]; 61 | } 62 | break; 63 | 64 | case "$push": 65 | for (var newkey in upsert[upsrt]) { 66 | cache[i][newkey].push(upsert[upsrt][newkey]); 67 | } 68 | break; 69 | 70 | default: 71 | upsert['_id'] = cache[i]['_id']; 72 | cache[i] = upsert; 73 | break end_loops; 74 | } 75 | } 76 | } 77 | } 78 | } 79 | localStorage.setItem(collectionName, JSON.stringify(cache)); 80 | if (callback) 81 | callback(err); 82 | 83 | }, 84 | 85 | remove: function (obj, callback) { 86 | if (arguments.length == 0) { 87 | localStorage.removeItem(collectionName); 88 | } else { 89 | 90 | for (var key in obj) { 91 | for (var i = cache.length - 1; i >= 0; i--) { 92 | if (cache[i][key] == obj[key]) { 93 | cache.splice(i, 1); 94 | } 95 | } 96 | } 97 | localStorage.setItem(collectionName, JSON.stringify(cache)); 98 | } 99 | 100 | if (callback) 101 | callback(err); 102 | 103 | } 104 | 105 | }; 106 | }; 107 | })); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | StoreDB 2 | ======= 3 | 4 | [DEPRECATED] Use [localForage](https://localforage.github.io/localForage) instead. 5 | 6 | [English version](https://github.com/djyde/StoreDB/blob/master/README_EN.md) 7 | 8 | 9 | StoreDB是一个基于localStorage的本地储存库,通过模拟MongoDB的一些API和概念(如“集(`collection`)”和“文档(`document`)”),使你能使用 localStorage 储存复杂数据。 10 | 11 | Why StoreDB? 12 | ------ 13 | * StoreDB使你在无须配置数据库的情况下,在静态页面中也能实现大量数据储存和交互。这意味着你能用StoreDB非常简便地建立一个功能强大的SPA(单页面应用,Single Page Application)。 14 | 15 | * StoreDB也适用于demo产品的开发。比如,假定你正在参加编程马拉松,你的团队只不过是想做出一个用以展示的demo,却不得不花费时间在远程或本地架设server,再配置数据库,白白浪费了宝贵的时间。使用StoreDB,你只需嵌入一段javascript代码就能实现丰富的数据交互。 16 | 17 | * 使用AngularJS配合StoreDB更是如虎添翼。 18 | 19 | Demo 20 | ------ 21 | Everfeed - RSS Reader 22 | http://everfeed.ml/ 23 | 24 | MoniCoin - 比特币虚拟交易 25 | http://djyde.github.io/MoniCoin 26 | 27 | 28 | Tutorial 29 | ------ 30 | [入门指南](http://www.cnblogs.com/Randylu/p/3523680.html) 31 | 32 | Install 33 | ------ 34 | 35 | ### bower: 36 | ``` 37 | $ bower install storedb 38 | ``` 39 | 40 | ### HTML: 41 | ```html 42 | 43 | ``` 44 | 45 | Quick Start 46 | ------ 47 | 48 | ### 插入(Insert) 49 | 向名为`players`的集合中插入一条文档: 50 | ```javascript 51 | storedb('players').insert({"name":"Randy","sex":"male","score":20},function(err,result){ 52 | if(!err){ 53 | //do sth... 54 | } else //do sth... 55 | }) 56 | ``` 57 | 58 | ### 查询(Find) 59 | 查询`players`集合中`name`为`Randy`的文档: 60 | ```javascript 61 | storedb('players').find({"name":"Randy"},function(err,result){ 62 | if(!err){ 63 | //use result to do sth... 64 | } else //do sth... 65 | }) 66 | ``` 67 | 如果需要查询集合中所有文档,将参数设置为空即可: 68 | ```javascript 69 | storedb('players').find() 70 | ``` 71 | 函数将返回一个数组类型。 72 | 73 | ### 更新(Update) 74 | 为`players`集合中`name`为`Randy`的`score`增加`10`: 75 | ```javascript 76 | storedb('players').update({"name":"Randy"},{"$inc":{"score":"10"}},function(err){ 77 | if(!err){ 78 | //do sth... 79 | } else //do sth... 80 | }) 81 | ``` 82 | 你可能已经注意到,StoreDB拥有和MongoDB一样的修改器!关于修改器类型请查看[API](#apis)。 83 | 84 | 如果修改器为空,则默认为`$set`修改器: 85 | ```javascript 86 | storedb('players').update({"name":"Randy"}, {"sex":"male","name":"kriss"}) 87 | ``` 88 | 89 | ### 删除(Remove) 90 | 删除在`players`集合中`name`为`Randy`的一条文档: 91 | ```javascript 92 | storedb('players').remove({"name":"Randy"},function(err){ 93 | if(!err){ 94 | //do sth... 95 | } else //do sth... 96 | }) 97 | ``` 98 | 如果要把整个集合删除,把参数设置为空: 99 | ```javascript 100 | storedb('players').remove() 101 | ``` 102 | 103 | APIs 104 | ------ 105 | 106 | #### storedb(collectionName) 107 | * `collectionName`:`string`,需要操作的集合名。如果集合不存在,则自动创建。 108 | 109 | ##### .insert(newObj,callback) 110 | * `newObj`:`JSON object`,插入的文档。 111 | * `callback`:`function`,包含参数`err`和`result`:无错误时`err`返回`undefined`。`result`返回此次创建的文档对象。 112 | * **系统会自动为每一条文档创建unix时间戳id——`_id`**,可通过callback中的result._id查看插入文档时所创建的id。 113 | 114 | ##### .find() 115 | * 返回`Array`,该集合所有文档。 116 | 117 | ##### .find(matchObj,callback) 118 | * `matchObj`:`JSON object`,匹配的文档 119 | * `callback`:`function`,包含参数`err`和`result`:无错误时`err`返回`undefined`。`result`返回查询结果数组。 120 | 121 | ##### .update(matchObj,upsert,callback) 122 | * `matchObj`:`JSON object`,匹配的文档 123 | * `upsert`:`JSON object`,对象中key应为修改器类型,value为修改对象。例如: 124 | ``` 125 | storedb('collectionA').update({"foo":"hi"},{"$set":{"bar":"hello"}},function(err){}) 126 | ``` 127 | * `callback`:`function`,包含参数`err`:无错误时`err`返回`undefined`。 128 | 129 | 修改器类型: 130 | - `$inc`:为目标增加(或减小)对应数值 131 | - `$set`: 修改目标内容 132 | - `$push`:为目标数组插入对应元素 133 | 134 | ##### .remove() 135 | * 移除该集合所有文档 136 | 137 | ##### .remove(matchObj,callback) 138 | * `matchObj`:`JSON object`,匹配的对应要删除的文档。 139 | * `callback`:`function`,包含参数`err`:无错误时`err`返回`undefined`。 140 | 141 | Donate 142 | ------ 143 | 由于支付宝取消支付页面,如果你支持 StoreDB,可以通过转账到我的支付宝账户 djyde520@gmail.com 。 144 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | StoreDB 2 | ======= 3 | 4 | StoreDB is a web local database based on localStorage. You can store more complex data in localStorage easily and APIs-friendly(MongoDB like). 5 | 6 | Why StoreDB? 7 | ------ 8 | * StoreDB supports large data store and interaction in static webpage without database configuration. Anyone could easily build a powerful SPA(Single Page Application) with StoreDB. 9 | 10 | * StoreDB is suitable for the development of demo production. Imagine that you are participating in a marathon programming and time is so running out that you do not have time to set up a local/remote server and database configuration, all your team need is a show demo. Using StoreDB to implement data interaction could be a much better solution. 11 | 12 | * combination of AngularJS and StoreDB would be a much more powerful solution. 13 | 14 | Example 15 | ------ 16 | [EverFeed](http://djyde.github.io/everfeed):Simple RSS reader 17 | 18 | Tutorial 19 | ------ 20 | [入门指南](http://www.cnblogs.com/Randylu/p/3523680.html) 21 | 22 | Install 23 | ------ 24 | 25 | ### bower: 26 | ``` 27 | $ bower install storedb 28 | ``` 29 | 30 | ### HTML: 31 | ```html 32 | 33 | ``` 34 | 35 | Quick Start 36 | ------ 37 | 38 | ### Insert 39 | Insert data into `players` set: 40 | ```javascript 41 | storedb('players').insert({"name":"Randy","sex":"male","score":20},function(err,result){ 42 | if(!err){ 43 | //do sth... 44 | } else //do sth... 45 | }) 46 | ``` 47 | 48 | ### Find 49 | query data with the `name` as `Randy` in `players` collection 50 | ```javascript 51 | storedb('players').find({"name":"Randy"},function(err,result){ 52 | if(!err){ 53 | //use result to do sth... 54 | } else //do sth... 55 | }) 56 | ``` 57 | find all data by passing no parameter to the function 58 | ```javascript 59 | storedb('players').find() 60 | ``` 61 | List-type data would be returned from function 62 | 63 | ### Update 64 | update `score` value by increasing 10 with the `name` as `Randy` in `player` collection 65 | ```javascript 66 | storedb('players').update({"name":"Randy"},{"$inc":{"score":"10"}},function(err){ 67 | if(!err){ 68 | //do sth... 69 | } else //do sth... 70 | }) 71 | ``` 72 | You may have noticed that StoreDB has the MongoDB-style modifier! Check the modifier type out in [API](#apis). 73 | 74 | 75 | ### Remove 76 | remove data with the `name` as `Randy` in `players` collection 77 | ```javascript 78 | storedb('players').remove({"name":"Randy"},function(err){ 79 | if(!err){ 80 | //do sth... 81 | } else //do sth... 82 | }) 83 | ``` 84 | remove all data in collection by passing no parameter to the function 85 | ```javascript 86 | storedb('players').remove() 87 | ``` 88 | 89 | APIs 90 | ------ 91 | 92 | #### storedb(collectionName) 93 | * `collectionName`:`string`,automatically create if not exist。 94 | 95 | ##### .insert(newObj,callback) 96 | * `newObj`:`JSON object`,插入的文档。 97 | * `callback`:`function`,包含参数`err`和`result`:无错误时`err`返回`undefined`。`result`返回此次创建的文档对象。 98 | * **系统会自动为每一条文档创建unix时间戳id——`_id`**,可通过callback中的result._id查看插入文档时所创建的id。 99 | 100 | ##### .find() 101 | * return `Array`,该集合所有文档。 102 | 103 | ##### .find(matchObj,callback) 104 | * `matchObj`:`JSON object`,匹配的文档 105 | * `callback`:`function`,包含参数`err`和`result`:无错误时`err`返回`undefined`。`result`返回查询结果数组。 106 | 107 | ##### .update(matchObj,upsert,callback) 108 | * `matchObj`:`JSON object`,匹配的文档 109 | * `upsert`:`JSON object`,对象中key应为修改器类型,value为修改对象。例如: 110 | ``` 111 | storedb('collectionA').update({"foo":"hi"},{"$set":{"bar":"hello"}},function(err){}) 112 | ``` 113 | * `callback`:`function`,包含参数`err`:无错误时`err`返回`undefined`。 114 | 115 | 修改器类型: 116 | - `$inc`:为目标增加(或减小)对应数值 117 | - `$set`: 修改目标内容 118 | - `$push`:为目标数组插入对应元素 119 | 120 | ##### .remove() 121 | * 移除该集合所有文档 122 | 123 | ##### .remove(matchObj,callback) 124 | * `matchObj`:`JSON object`,匹配的对应要删除的文档。 125 | * `callback`:`function`,包含参数`err`:无错误时`err`返回`undefined`。 126 | 127 | Donate 128 | ------ 129 | Alipay: djyde520@gmail.com 130 | --------------------------------------------------------------------------------