npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tc-utils/indexed-db

v1.0.2-rc.0

Published

## 安装

Downloads

8

Readme

@tc-utils/indexed-db

安装

使用 npm:

$ npm i -g npm
$ npm i @tc-utils/indexed-db

基本用法

直接引用 IndexedDBUtil 构造新实例

import IndexedDBUtil from '@tc-utils/indexed-db'
import moment from 'moment'
import React, { useEffect, useState } from 'react'
import ReactDOM from 'react-dom'

type ItemType = {
  pkid: string
  value: any
  birthday: Date
  create_time: Date
  update_time: Date
}

const dbInstanceProps = {
  dbConfig: {
    name: 'tc-utils-indexed-db', // 数据库名称
    version: 1,
  },
  tables: [
    {
      tableName: 'module_a',
      pkidField: 'pkid',
      tableFields: [
        {
          indexName: 'pkid',
          unique: true,
        },
        {
          indexName: 'value',
        },
        {
          indexName: 'birthday',
        },
        {
          indexName: 'create_time',
        },
        {
          indexName: 'update_time',
        },
      ],
    },
  ],
  onSuccess: (util) => {
    // 数据库打开成功好了
    console.log('数据库打开成功', util)
  },
}

const dbInstance = new IndexedDBUtil(dbInstanceProps)

const App = () => {
  const [records, setRecords] = useState<ItemType[]>([])
  const [total, setTotal] = useState<number>(0)

  const handlerQuery = () => {
    dbInstance
      .query<ItemType>({
        tableName: 'module_a',
      })
      .then((dataSource) => {
        setRecords(dataSource)
      })
  }

  const handlerCount = () => {
    dbInstance
      .count({
        tableName: 'module_a',
      })
      .then((total) => {
        setTotal(total)
      })
  }

  const handlerInsert = () => {
    const key = new Date().getTime()
    dbInstance
      .insert<ItemType>({
        tableName: 'module_a',
        record: {
          pkid: `pkid-${key}`,
          value: key,
          birthday: new Date(),
          create_time: new Date(),
          update_time: new Date(),
        },
      })
      .then((pkid) => {
        console.log('~ handlerInsert ~ pkid', pkid)
        handlerQuery()
        handlerCount()
      })
  }

  const handlerUpdate = () => {
    const lastRecord = records[records.length - 1]
    if (lastRecord) {
      dbInstance
        .update<ItemType>({
          tableName: 'module_a',
          pkidValue: lastRecord.pkid,
          record: {
            value: `新数据-${lastRecord.value}`,
            update_time: new Date(),
          },
        })
        .then((pkid) => {
          console.log('~ handlerUpdate ~ pkid', pkid)
          handlerQuery()
          handlerCount()
        })
    }
  }

  const handlerDelete = () => {
    const lastRecord = records[records.length - 1]
    if (lastRecord) {
      dbInstance
        .delete({
          tableName: 'module_a',
          pkidValue: lastRecord.pkid,
        })
        .then(() => {
          handlerQuery()
          handlerCount()
        })
    }
  }

  const handlerClear = () => {
    const lastRecord = records[records.length - 1]
    if (lastRecord) {
      dbInstance
        .clear({
          tableName: 'module_a',
        })
        .then(() => {
          handlerQuery()
          handlerCount()
        })
    }
  }

  useEffect(() => {
    handlerQuery()
    handlerCount()
  }, [])

  return (
    <div className="container">
      <div className="toolbar">
        <button onClick={() => handlerInsert()}>插入数据</button>
        <button onClick={() => handlerUpdate()}>修改最后一条数据</button>
        <button onClick={() => handlerDelete()}>删除最后一条数据</button>
        <button onClick={() => handlerClear()}>清空全部数据</button>
        <button onClick={() => handlerQuery()}>查询数据</button>
        <button onClick={() => handlerCount()}>统计记录</button>
      </div>
      <div className="main">
        <table>
          <thead>
            <tr>
              <th>主键</th>
              <th>记录值</th>
              <th>生日</th>
              <th>创建时间</th>
              <th>修改时间</th>
            </tr>
          </thead>
          <tbody>
            {records.map((record) => {
              return (
                <tr key={record.pkid}>
                  <td>{record.pkid}</td>
                  <td>{record.value}</td>
                  <td>
                    {moment(record.birthday).format('YYYY-MM-DD HH:mm:ss')}
                  </td>
                  <td>
                    {moment(record.create_time).format('YYYY-MM-DD HH:mm:ss')}
                  </td>
                  <td>
                    {moment(record.update_time).format('YYYY-MM-DD HH:mm:ss')}
                  </td>
                </tr>
              )
            })}
          </tbody>
          <tfoot>
            <tr>
              <td colSpan={5}>总共 {total} 条记录</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))

Hooks 用法

React 中使用 useIndexDB Hook,返回对应的实例

import { useIndexDB } from '@tc-utils/indexed-db'
import moment from 'moment'
import React, { useEffect, useState } from 'react'
import ReactDOM from 'react-dom'

type ItemType = {
  pkid: string
  value: any
  birthday: Date
  create_time: Date
  update_time: Date
}

const dbInstanceProps = {
  dbConfig: {
    name: 'tc-utils-indexed-db', // 数据库名称
    version: 1,
  },
  tables: [
    {
      tableName: 'module_a',
      pkidField: 'pkid',
      tableFields: [
        {
          indexName: 'pkid',
          unique: true,
        },
        {
          indexName: 'value',
        },
        {
          indexName: 'birthday',
        },
        {
          indexName: 'create_time',
        },
        {
          indexName: 'update_time',
        },
      ],
    },
  ],
  onSuccess: (util) => {
    // 数据库打开成功好了
    console.log('数据库打开成功', util)
  },
}

const App = () => {
  const [records, setRecords] = useState<ItemType[]>([])
  const [total, setTotal] = useState<number>(0)
  const { instance: dbInstance, update, reset } = useIndexDB(dbInstanceProps)

  const handlerQuery = () => {
    dbInstance
      .query<ItemType>({
        tableName: 'module_a',
      })
      .then((dataSource) => {
        setRecords(dataSource)
      })
  }

  const handlerCount = () => {
    dbInstance
      .count({
        tableName: 'module_a',
      })
      .then((total) => {
        setTotal(total)
      })
  }

  const handlerInsert = () => {
    const key = new Date().getTime()
    dbInstance
      .insert<ItemType>({
        tableName: 'module_a',
        record: {
          pkid: `pkid-${key}`,
          value: key,
          birthday: new Date(),
          create_time: new Date(),
          update_time: new Date(),
        },
      })
      .then((pkid) => {
        console.log('~ handlerInsert ~ pkid', pkid)
        handlerQuery()
        handlerCount()
      })
  }

  const handlerUpdate = () => {
    const lastRecord = records[records.length - 1]
    if (lastRecord) {
      dbInstance
        .update<ItemType>({
          tableName: 'module_a',
          pkidValue: lastRecord.pkid,
          record: {
            value: `新数据-${lastRecord.value}`,
            update_time: new Date(),
          },
        })
        .then((pkid) => {
          console.log('~ handlerUpdate ~ pkid', pkid)
          handlerQuery()
          handlerCount()
        })
    }
  }

  const handlerDelete = () => {
    const lastRecord = records[records.length - 1]
    if (lastRecord) {
      dbInstance
        .delete({
          tableName: 'module_a',
          pkidValue: lastRecord.pkid,
        })
        .then(() => {
          handlerQuery()
          handlerCount()
        })
    }
  }

  const handlerClear = () => {
    const lastRecord = records[records.length - 1]
    if (lastRecord) {
      dbInstance
        .clear({
          tableName: 'module_a',
        })
        .then(() => {
          handlerQuery()
          handlerCount()
        })
    }
  }

  useEffect(() => {
    handlerQuery()
    handlerCount()
  }, [])

  return (
    <div className="container">
      <div className="toolbar">
        <button onClick={() => handlerInsert()}>插入数据</button>
        <button onClick={() => handlerUpdate()}>修改最后一条数据</button>
        <button onClick={() => handlerDelete()}>删除最后一条数据</button>
        <button onClick={() => handlerClear()}>清空全部数据</button>
        <button onClick={() => handlerQuery()}>查询数据</button>
        <button onClick={() => handlerCount()}>统计记录</button>
      </div>
      <div className="main">
        <table>
          <thead>
            <tr>
              <th>主键</th>
              <th>记录值</th>
              <th>生日</th>
              <th>创建时间</th>
              <th>修改时间</th>
            </tr>
          </thead>
          <tbody>
            {records.map((record) => {
              return (
                <tr key={record.pkid}>
                  <td>{record.pkid}</td>
                  <td>{record.value}</td>
                  <td>
                    {moment(record.birthday).format('YYYY-MM-DD HH:mm:ss')}
                  </td>
                  <td>
                    {moment(record.create_time).format('YYYY-MM-DD HH:mm:ss')}
                  </td>
                  <td>
                    {moment(record.update_time).format('YYYY-MM-DD HH:mm:ss')}
                  </td>
                </tr>
              )
            })}
          </tbody>
          <tfoot>
            <tr>
              <td colSpan={5}>总共 {total} 条记录</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))