@aadityataparia/browser-storage 中文文档教程

发布于 6年前 浏览 22 项目主页 更新于 3年前

BrowserStorage

具有牛权的浏览器键值(JSON)存储库。

构建: CircleCI

Size

TypeSize
Normal (dist/browserstorage.js)Normal
Minified (dist/browserstorage.min.js)Minified
Minified + Gzipped (dist/browserstorage.min.js)Minified + Gzipped

Types of storages available (in default priority order)

  • IndexedDB
  • WebSQL
  • LocalStorage
  • Cookies

How to use

Directly in Browser using standalone distribution

在您的网站中添加任何 /dist/browserstorage.js/dist/browserstorage.min.js 作为脚本标签。

Compatibility table for standalone distribution

  • chrome >= 55
  • safari >= 10.1
  • opera >= 42
  • firefox >= 53

Using npm

执行 npm install browserstorage 或将包添加到您的 package.json

API

BrowserStorage 使用 Promises API。

Initialization

  • Initialize a storage with a type
let storage = new BrowserStorage(type)

其中类型是 indexeddbwebsqllocalstoragecookies 之一。

注意:如果浏览器不支持该类型,则将根据优先顺序选择第一个支持的存储。

  • Initialize with options
// Options with default values
let options = {
  priority: ['indexeddb', 'websql', 'localstorage', 'cookies'], // Priority Array of type of storages to use
  name: 'BroswerStorage', // name of table (treat this as a variable name, i.e. no Spaces or special characters allowed)
  version: 1, // version number (integer / float / string), 1 is treated same as '1'
  desciption: 'Browser Storage', // description (text)
  size: 5 * 1024 * 1024 // Max db size in bytes only for websql (integer)
}
storage = new BrowserStorage(options)

Inserting key-value

// insert single key-value
let key = 'key';
let value = { value: 'value' };
storage.insert(key, value).then(() => {/* Do something here */});

// insert multiple key-values
let data = { a: 'b', c: { d: 'e' } }
storage.insert(data).then(() => {/* Do something here */});

Getting value

// select single key-value
storage.select('key').then((value) => console.log(value)); // > { key: { value: 'value' } }

// select multiple key-values
storage.select(['a', 'c']).then((value) => console.log(value)); // > { a: 'b', c: { d: 'e' } }

Deleting a key

// delete single key-value
storage.delete('key').then(() => {/* Do something here */});

// delete multiple key-values
storage.delete(['a', 'c']).then(() => {/* Do something here */});

Updating a key

// update single key-value
let newValue = { newValue: 'new' };
storage.update('key', newValue).then(() => {
  // checking if value is updated
  storage.select('key').then((v) => console.log(v)); // > { key: { newValue: 'new' } }
});

// update multiple key-values
storage.update({ a: 'bnew', c: { dnew: 'enew' } }).then(() => {
  // checking if value is updated
  storage.select(['a', 'c']).then((v) => console.log(v)); // > { a: 'bnew', c: { dnew: 'enew' } }
});

Upserting a key-value (Insert else update)

// upsert single key-value
let upValue = { upValue: 'up' };
storage.upsert('key', upValue).then(() => {
  // checking if value is updated
  storage.select('key').then((v) => console.log(v)); // > { key: { upValue: 'up' } }
});

// upsert multiple key-values
storage.upsert({ a: 'bup', c: { dup: 'eup' } }).then(() => {
  // checking if value is updated
  storage.select(['a', 'c']).then((v) => console.log(v)); // > { a: 'bup', c: { dup: 'eup' } }
});

Clear table

storage.clear().then(() => {
  // checking if value is deleted
  storage.select('key').then((v) => console.log(v)); // > {}
});

Get all data in table

storage.data().then((data) => console.log(data)); // > { key: { upValue: 'up' }, a: 'bup', c: { dup: 'eup' } }

Get all created storage instances

BrowserStorage.all;

BrowserStorage

Browser key-value(JSON) storage library with cow powers.

Build: CircleCI

Size

TypeSize
Normal (dist/browserstorage.js)Normal
Minified (dist/browserstorage.min.js)Minified
Minified + Gzipped (dist/browserstorage.min.js)Minified + Gzipped

Types of storages available (in default priority order)

  • IndexedDB
  • WebSQL
  • LocalStorage
  • Cookies

How to use

Directly in Browser using standalone distribution

Add any of /dist/browserstorage.js or /dist/browserstorage.min.js as script tag in your website.

Compatibility table for standalone distribution

  • chrome >= 55
  • safari >= 10.1
  • opera >= 42
  • firefox >= 53

Using npm

Do npm install browserstorage or add the package to your package.json.

API

BrowserStorage uses Promises API.

Initialization

  • Initialize a storage with a type
let storage = new BrowserStorage(type)

where type is one of indexeddb, websql, localstorage, cookies.

Note: If that type is not supported in the browser, then first supported storage will be selected based on priority order.

  • Initialize with options
// Options with default values
let options = {
  priority: ['indexeddb', 'websql', 'localstorage', 'cookies'], // Priority Array of type of storages to use
  name: 'BroswerStorage', // name of table (treat this as a variable name, i.e. no Spaces or special characters allowed)
  version: 1, // version number (integer / float / string), 1 is treated same as '1'
  desciption: 'Browser Storage', // description (text)
  size: 5 * 1024 * 1024 // Max db size in bytes only for websql (integer)
}
storage = new BrowserStorage(options)

Inserting key-value

// insert single key-value
let key = 'key';
let value = { value: 'value' };
storage.insert(key, value).then(() => {/* Do something here */});

// insert multiple key-values
let data = { a: 'b', c: { d: 'e' } }
storage.insert(data).then(() => {/* Do something here */});

Getting value

// select single key-value
storage.select('key').then((value) => console.log(value)); // > { key: { value: 'value' } }

// select multiple key-values
storage.select(['a', 'c']).then((value) => console.log(value)); // > { a: 'b', c: { d: 'e' } }

Deleting a key

// delete single key-value
storage.delete('key').then(() => {/* Do something here */});

// delete multiple key-values
storage.delete(['a', 'c']).then(() => {/* Do something here */});

Updating a key

// update single key-value
let newValue = { newValue: 'new' };
storage.update('key', newValue).then(() => {
  // checking if value is updated
  storage.select('key').then((v) => console.log(v)); // > { key: { newValue: 'new' } }
});

// update multiple key-values
storage.update({ a: 'bnew', c: { dnew: 'enew' } }).then(() => {
  // checking if value is updated
  storage.select(['a', 'c']).then((v) => console.log(v)); // > { a: 'bnew', c: { dnew: 'enew' } }
});

Upserting a key-value (Insert else update)

// upsert single key-value
let upValue = { upValue: 'up' };
storage.upsert('key', upValue).then(() => {
  // checking if value is updated
  storage.select('key').then((v) => console.log(v)); // > { key: { upValue: 'up' } }
});

// upsert multiple key-values
storage.upsert({ a: 'bup', c: { dup: 'eup' } }).then(() => {
  // checking if value is updated
  storage.select(['a', 'c']).then((v) => console.log(v)); // > { a: 'bup', c: { dup: 'eup' } }
});

Clear table

storage.clear().then(() => {
  // checking if value is deleted
  storage.select('key').then((v) => console.log(v)); // > {}
});

Get all data in table

storage.data().then((data) => console.log(data)); // > { key: { upValue: 'up' }, a: 'bup', c: { dup: 'eup' } }

Get all created storage instances

BrowserStorage.all;
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文