Socket
Book a DemoInstallSign in
Socket

@goldnet/localdb

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@goldnet/localdb

不限制大小的本地存储库

latest
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

localdb

不限制大小的本地存储库

支持类型

number, string, json, blob, arraybuffer

安装

 npm i @goldnet/localdb

导入

import { LocalDB } from ' @goldnet/localdb';

示例

// 建立全局的存储库
const localDB = getLocalDB();

await localDB.open(1);

async function load() {
    // 相关模块使用
    const key = 'key1';
    let data = await localDB.get(key);

    if (!data) {
        console.log('存储');
        const res = await fetch('/colors.png', {
            "headers": {
                responseType: 'arraybuffer'
            }
        });
        const arraybuffer = await res.arrayBuffer();
        await localDB.put({ md5: '1', data: arraybuffer }, key);
    } else {
        console.log('读取');
        data = data.data;
    }

    console.log(data);
}

load();

API

快速创建

const localDB = getLocalDB();

实例创建

const localDB = new LocalDB(dbname);

需要添加数据库名,自定义 需要添加表明,自定义 版本号,默认1, 如果修改表或数据结构,版本号必须递增 在调用open方法之前,需要配置结构

    localDB.configStoreObject([
        {
            name: tableName,
            options: {
                autoIncrement: true
            }
        }
    ]);

打开数据库

await localDB.open(version);

version 如果不传,默认版本号为1.

更新/添加数据

const result = await localDB.put(value,id);

value 要存储的数据,可以是 number, string, json, blob, arraybuffer id,用于查找的键值,如果没传入,会返回递增键值, 如果传入一个已经存在的,会更新数据 result 如果为 undefined, 数据库可能关闭、写入失败

获取

const result = await localDB.get(id);

获取版本

const result = await localDB.version();

只有版本变更才会更新 localDB.configStoreObject 中的数据结构,在每次版本变更需要考虑之前的结构,或调用删除方法

删除

const result = await localDB.delete(id);

如果没有返回值,删除成功

清除所有

await localDB.clear();

Keywords

本地存储

FAQs

Package last updated on 08 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts