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

@szgc/h5sql

v1.0.4

Published

根据 plus.sqlite Api 进行包装的数据库操作库

Downloads

1

Readme

H5 数据库操作库

根据 plus.sqlite Api 进行包装的数据库操作库

导出项

sql

将 plus.sqlite 包含的方法全部改写为 Promise 返回

  • 引入方式

    import { sql } from "@szgc/h5sql";
  • 成员方法

    /**
     * 打开数据库连接
     * @description
     *  1、判断数据库是否已经打开,已经打开直接返回结果
     *  2、如果没有打开,尝试调用打开数据库api,如果没有数据库文件会自动创建
     * @returns {Promise<boolean>}
     */
    open(): Promise<SqlOpResult>;
    
    /**
     * 关闭数据库连接
     * @description
     *  1、判断数据库是否关闭,如果已经关闭直接返回结果
     *  2、尝试调用关闭api
     * @returns
     */
    close(): Promise<SqlOpResult>;
    
    /**
     * 执行数据库事务操作
     * @param operation 只能是:"begin" | "commit" | "rollback"
     * @returns
     */
    transaction(operation: "begin" | "commit" | "rollback"): Promise<SqlOpResult>;
    
    /**
     * 执行sql语句
     * @param sql 字符串或字符串数组
     * @returns
     */
    executeSql(sql: string | Array<string>): Promise<SqlOpResult>;
    
    /**
     * 执行sql查询语句
     * @param sql
     * @returns
     */
    selectSql(sql: string): Promise<SqlOpResult>;

Dao

数据库操作基类,其实例自动具有增删改查能力

  • 成员方法

    /**
     * 构造函数
     * @param options
     */
    constructor(
      tableName: string,
      tableColumns: {
        [key in keyof K]: { columnName?: string; type: "TEXT" | "INTEGER" };
      }
    );
    
    /**
     * 创建表
     * @param drop 是否先执行删除表操作
     * @returns
     */
    create(drop?: boolean): Promise<SqlOpResult>;
    
    /**
     * 插入数据
     * @param data
     * @returns
     */
    insert(data: Partial<K>): Promise<SqlOpResult>;
    
    /**
     * 修改数据
     * @param data 要修改的数据项
     * @param where 符合修改数据项的条件
     */
    update(options: {
      data: Partial<K>;
      where?: WhereOf<K>;
    }): Promise<SqlOpResult>;
    
    /**
     * 删除数据
     * @param where
     * @returns
     */
    remove(where?: WhereOf<K>): Promise<SqlOpResult>;
    
    /**
     * 分页查询
     */
    page(options: {
      where?: WhereOf<K>;
      order?: OrderOf<K>;
      pageSize?: number;
      current?: number;
    }): Promise<PageQueryResult<K>>;
    
    /**
     * 列表查询
     */
    list(options: {
      where?: WhereOf<K>;
      order?: OrderOf<K>;
    }): Promise<ListQueryResult<K>>;
    
    /**
     * 单条查询
     */
    single(where?: WhereOf<K>): Promise<SingleQueryResult<K>>;
  • 示例

    import { Dao } from "@szgc/h5sql";
    
    /**
     * 用户对象模型
     */
    interface UserModel {
      userName: string;
      id: string;
      account: string;
      age: number;
    }
    
    /**
     * 创建userDao
     */
    export const userDao = new Dao<UserModel>("USER1", {
      userName: {
        type: "TEXT"
      },
      id: {
        columnName: "USER_ID",
        type: "TEXT"
      },
      account: {
        type: "TEXT"
      },
      age: {
        type: "INTEGER"
      }
    });
    
    // 建表,如果存在则不创建
    await userDao.create();
    
    // 建表,如果存在则先删除原有表,再创建
    await userDao.create(true);
    
    // 插入数据
    await userDao.insert({
      id: "222",
      account: "5",
      age: 13
    });
    
    //  修改数据
    await userDao.update({
      data: {
        account: "李四"
      },
      where: {
        id: "5"
      }
    });
    
    // 删除表中所有数据
    await userDao.remove();
    
    // 删除表中符合条件的数据
    await userDao.remove({
      age: 15,
      userName: "zs"
    });
    
    // 查询表中符合条件的数据并排序
    await userDao.list({
      where: {
        age: {
          value: 10,
          op: ">"
        }
      },
      order: {
        columns: ["account", "id"],
        op: "desc"
      }
    });
    
    // 查询表中符合条件的单条数据
    await userDao.single({
      age: {
        value: 10,
        op: ">"
      }
    });
    
    // 分页查询表中符合条件的数据并排序
    await userDao.page({
      where: {},
      order: {
        columns: [],
        op: "asc"
      },
      pageSize: 3,
      current: 1
    });