{"id":18769489,"url":"https://github.com/swcbo/ts-indexdb","last_synced_at":"2025-04-13T07:31:28.109Z","repository":{"id":42021347,"uuid":"254261600","full_name":"swcbo/ts-indexdb","owner":"swcbo","description":"🔨基于IndexDb 使用Ts封装🙂","archived":false,"fork":false,"pushed_at":"2024-02-29T03:31:21.000Z","size":357,"stargazers_count":45,"open_issues_count":9,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-09T23:07:12.327Z","etag":null,"topics":["indexdb","javascript","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swcbo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-09T03:26:18.000Z","updated_at":"2024-07-25T06:27:45.000Z","dependencies_parsed_at":"2024-06-19T19:15:32.238Z","dependency_job_id":null,"html_url":"https://github.com/swcbo/ts-indexdb","commit_stats":null,"previous_names":["q1104133609/ts-indexdb"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swcbo%2Fts-indexdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swcbo%2Fts-indexdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swcbo%2Fts-indexdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swcbo%2Fts-indexdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swcbo","download_url":"https://codeload.github.com/swcbo/ts-indexdb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223573771,"owners_count":17167370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["indexdb","javascript","typescript"],"created_at":"2024-11-07T19:16:01.938Z","updated_at":"2024-11-07T19:16:02.562Z","avatar_url":"https://github.com/swcbo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\n * @Description: file content\n * @Author: 小白\n * @Date: 2020-04-07 20:48:03\n * @LastEditors: 小白\n * @LastEditTime: 2020-04-09 17:59:43\n --\u003e\n# ts-indexdb \n\u003cp align=\"center\"\u003e\n \u003ca href=\"https://travis-ci.com/swcbo/rmox\"\u003e\u003cimg src=\"https://travis-ci.com/swcbo/ts-indexdb.svg?branch=master\" alt=\"npm package\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://npmjs.com/package/ts-indexdb\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/ts-indexdb.svg?logo=npm\" alt=\"npm package\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://bundlephobia.com/package/ts-indexdb@latest\"\u003e\u003cimg src=\"https://img.shields.io/bundlephobia/min/ts-indexdb.svg?logo=typescript\" alt=\"size\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n## Install\n\n```sh\nnpm install ts-indexdb\nyarn add ts-indexdb\n```\n## Usage\n### Typescript\n```\nimport { init, getInstance } from 'ts-indexdb';\nexport type Rack =  {\n    name: string\n    id?: number\n}\n```\n\n### javascript\n\n```\nimport TsIndexDb = require('ts-indexdb');\n```\n## 数据库操作方法\n### 注意\n* 当前类为单例模式只要init一次,后面直接getInstance获取实例来操作数据库\n* 操作返回的均为Promis对象\n* js不用加泛型\n### 数据库与表操作\n方法|方法名|参数|属性\n--|:--|:--:|:--\nopen_db|打开数据库|无|-\nclose_db|关闭数据库|无|-\ndelete_db|删除数据库|String|name\ndelete_table|删除表数据|String|tableName\n\n\n### 查询操作(query)\n方法|方法名|参数|属性\n--|:--|:--:|:--\nqueryAll|查询某张表的所有数据(返回具体数组)|Object|{ tableName }\nquery|查询(返回具体数组)|Object|{ tableName, condition }\nquery_by_keyValue|查询数据(更具表具体属性)返回具体某一个|Object|{ tableName, key, value }\nquery_by_primaryKey|查询数据（主键值）|Object|{ tableName, value }\ncount|查询数据（主键值）|Object|{ tableName, key, countCondition:{type,rangeValue } }\n\n### 更新操作(update)\n方法|方法名|参数|属性\n--|:--|:--:|:--\nupdate|更具条件修改数据(返回修改的数组)|Object|{ tableName, condition, handle }\nupdate_by_primaryKey|修改某条数据(主键)返回修改的对象|Object|{  tableName, value, handle }\n\n### 插入操作(insert)\n方法|方法名|参数|属性\n--|:--|:--:|:--\ninsert|增加数据|Object|{ tableName, data(数组或者单独对象) }\n\n### 删除操作(delete)\n方法|方法名|参数|属性\n--|:--|:--:|:--\ndelete|根据条件删除数据(返回删除数组)|Object|{ tableName, condition }\ndelete_by_primaryKey|删除数据(主键)|Object|{ tableName, value }\n\n\n## 例子：\n### 初始化\n```\nawait init({\n    dbName: \"books\",        // 数据库名称               \n    version: 1,             // 版本号                \n    tables: [                               \n        {\n            tableName: \"bookrackList\",         // 表名         \n            option: { keyPath: \"id\", autoIncrement: true }, // 指明主键为id\n            indexs: [    // 数据库索引\n                {\n                    key: \"id\",\n                    option: {\n                        unique: true\n                    }\n                },\n                {\n                    key: \"name\"\n                }\n            ]\n        }\n    ]\n})\n```\n### 查询\n ```\n  /**\n    * @method 查询某张表的所有数据(返回具体数组)\n    * @param {Object}\n    *   @property {String} tableName 表名\n    */\n  await getInstance().queryAll\u003cRack\u003e({\n    tableName: 'bookrackList'\n  });\n\n\n  /**\n    * @method 查询(返回具体数组)\n    * @param {Object}\n    *   @property {String} tableName 表名\n    *   @property {Function} condition 查询的条件\n    * */\n  await getInstance().query\u003cRack\u003e({\n     tableName: 'bookrackList',\n     condition: item =\u003e item.id === 3\n   });\n\n  /**\n    * @method 查询数据(更具表具体属性)返回具体某一个\n    * @param {Object}\n    *   @property {String} tableName 表名\n    *   @property {Number|String} key 名\n    *   @property {Number|String} value 值\n    *\n    * */\n  await getInstance().query_by_keyValue\u003cRack\u003e({\n     tableName: 'bookrackList',\n     key: 'name',\n     value: '我师兄实在太稳健了'\n   });\n\n  /**\n    * @method 查询数据（主键值）\n    * @param {Object}\n    *   @property {String} tableName 表名\n    *   @property {Number|String} value 主键值\n    *\n    * */ \n  await getInstance().query_by_primaryKey\u003cRack\u003e({\n     tableName: 'bookrackList',\n     value: 3\n   });\n  /**\n     * @method 查询满足key条件的个数(返回满足条件的数字个数)\n     * @param {Object}\n     *   @property {String} tableName 表名\n     *   @property {Number|String} key 查询的key\n     *   @property {Object} countCondition 查询条件\n   * */\n  /** countCondition传入方式 key 必须为已经简历索引的字段\n   *  key ≥ x\t            {key: 'gt' rangeValue: [x]}\n      key \u003e x\t            {key: 'gt' rangeValue: [x, true]}\n      key ≤ y\t            {key: 'lt' rangeValue: [y]}\n      key \u003c y\t            {key: 'lt' rangeValue: [y, true]}\n      key ≥ x \u0026\u0026 ≤ y\t    {key: 'between' rangeValue: [x, y]}\n      key \u003e x \u0026\u0026\u003c y\t    {key: 'between' rangeValue: [x, y, true, true]}\n      key \u003e x \u0026\u0026 ≤ y\t    {key: 'between' rangeValue: [x, y, true, false]}\n      key ≥ x \u0026\u0026\u003c y\t    {key: 'between' rangeValue: [x, y, false, true]}\n      key = z\t            {key: 'equal' rangeValue: [z]}\n   */\n  await getInstance().count\u003cRack\u003e({\n    tableName: 'bookrackList',\n    key: 'createdTime',\n    countCondition: {\n      type: 'between',\n      rangeValue:[1676627113088,new Date().getTime()]\n    }\n  })\n\n  ```\n\n### 更新\n```\n  /**\n     * @method 修改数据(返回修改的数组)\n     * @param {Object}\n     *   @property {String} tableName 表名\n     *   @property {Function} condition 查询的条件，遍历，与filter类似\n     *      @arg {Object} 每个元素\n     *      @return 条件\n     *   @property {Function} handle 处理函数，接收本条数据的引用，对其修改\n     * */\n  await getInstance().update\u003cRack\u003e({\n        tableName: 'bookrackList',\n        condition: item =\u003e item.id === 8,\n        handle: r =\u003e {\n          r.name = '测试修改';\n          return r;\n        }\n  })\n\n\n  /**\n  * @method 修改某条数据(主键)返回修改的对象\n  * @param {Object}\n  *   @property {String} tableName 表名\n  *   @property {String\\|Number} value 目标主键值\n  *   @property {Function} handle 处理函数，接收本条数据的引用，对其修改\n  * */\n  await getInstance().update_by_primaryKey\u003cRack\u003e({\n        tableName: 'bookrackList',\n        value: 1,\n        handle: r =\u003e {\n          r.name = '测试修改';\n          return r;\n        }\n  })\n\n```\n### 增加\n```\n  /**\n     * @method 增加数据\n     * @param {Object}\n     *   @property {String} tableName 表名\n     *   @property {Object} data 插入的数据\n     * */\n  await getInstance().insert\u003cRack\u003e({\n    tableName: 'bookrackList',\n    data: {\n      name: '测试',\n    }\n  })\n```\n\n### 删除\n\n    \n    /**\n      * @method 删除数据(返回删除数组)\n      * @param {Object}\n      *   @property {String} tableName 表名\n      *   @property {Function} condition 查询的条件，遍历，与filter类似\n      *      @arg {Object} 每个元素\n      *      @return 条件\n      * */\n    await getInstance().delete\u003cRack\u003e({\n      tableName: 'bookrackList',\n      condition: (item)=\u003e item.name === '测试',\n    })\n\n\n     /**\n      * @method 删除数据(主键)\n      * @param {Object}\n      *   @property {String} tableName 表名\n      *   @property {String\\|Number} value 目标主键值\n      * */\n    await getInstance().delete_by_primaryKey\u003cRack\u003e({\n      tableName: 'bookrackList',\n      value: 4\n    })\n\n    /**\n      * @method 删除表数据\n      * @param {String}name 数据库名称\n      */\n    await getInstance().delete_table('bookrackList')\n\n\n    /**\n      * @method 删除数据库\n      * @param {String}name 数据库名称\n      */\n    await getInstance().delete_db('bookrackList')\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswcbo%2Fts-indexdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswcbo%2Fts-indexdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswcbo%2Fts-indexdb/lists"}