# x-localstore

> Custom store, Use IndexedDB first, if the browser do not support it, change to Localstorage

Latest version **1.0.2** (published 2023-07-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install x-localstore
pnpm add x-localstore
yarn add x-localstore
bun add x-localstore
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-07-04 |
| First published | 2023-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mikexia930 |
| Keywords | IndexedDB, Localstorage, 浏览器存储 |

## Links

- npm: https://www.npmjs.com/package/x-localstore
- npm.io page: https://npm.io/package/x-localstore

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-07-04
- 1.0.1 — 2023-06-06
- 1.0.0 — 2023-05-05

## README

# xLocalStore
>
>客户端数据存储封装，默认如果不支持 IndexedDB 会使用 LocalStorage，也可以自动移设置存储引擎。
>
>
[Demo](https://mikexia930.github.io/xLocalStore/)
## 版本
- v1.0.0

## 基于
- IndexedDB
- LocalStorage

## 安装
```
npm install x-localstore
```
或者
```
github下载源码
```

## 使用
**直接用script引入**
```
<script src="xLocalstore/dist/xlocalstore.umd.js"></script>
```
#### Vue示例

```
import xLocalStore from 'x-localstore';
```

**初始化**
```
const storeConfig = {
  dbName: '', // 可选，库名称
  tableName: '', // 可选，表名称
  useStores: [ 'indexedDB', 'localstorage' ], // 可选，选择存储引擎的顺序。不设置默认为 indexedDB/localstorage
}
const storeIns = new xLocalStore(storeConfig);
```

**创建数据表**
```
storeIns.createTable(keyPath, [
  {
    name: 'id',  // 字段名称
    keyPath: 'id' // 用于查询时候使用的字段名
  },
  {
    name: 'title',  // 字段名称
    keyPath: 'title' // 用于查询时候使用的字段名
  },
  {
    name: 'time',  // 字段名称
    keyPath: 'time' // 用于查询时候使用的字段名
  }
]);

说明：
keyPath: 当前表不重复值的字段名称，用于查询/更新/删除
```

**获取全量数据**
```
storeIns.getAll().then((result) => {
   console.log(result);
})
```

**获取指定数据**
```
storeIns.get(value, keyPath).then((result) => {
    console.log(result);
});

说明： 
value: 必填。查询的值
keyPath: 可选，查询的字段名，不填使用默认的字段
```

**添加数据**
```
如果和 createTable keyPath 重复的数据将不会被加入

storeIns.add([
    {
      id: '',
      title: '',
      time: ''
    }
]).then((result) => {
    console.log(result);
})

说明： 
result： 返回值，格式为 {
    success: [], // 成功的条目
    failed: [], // 失败的条目
}
```

**添加并更新数据**
```
storeIns.set([
    {
      id: '',
      title: '',
      time: ''
    }
]).then((result) => {
    console.log(result);
})

说明： 
result： 返回值，格式为 {
    success: [], // 成功的条目
    failed: [], // 失败的条目
}
```

**删除条目**
```
storeIns.delete([ value1, value2]).then((result) => {
    console.log(result);
})

说明： 
value: createTable keyPath 对应的条目值
```

**清空数据表**
```
storeIns.clear().then((result) => {
    console.log(result);
})
```

---
_Source: https://npm.io/package/x-localstore · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
