{"id":17182863,"url":"https://github.com/jin-yufeng/mplocaldb","last_synced_at":"2025-08-20T16:34:14.185Z","repository":{"id":112666409,"uuid":"262062943","full_name":"jin-yufeng/MpLocalDB","owner":"jin-yufeng","description":"微信小程序本地数据库","archived":false,"fork":false,"pushed_at":"2020-07-09T02:31:33.000Z","size":13,"stargazers_count":43,"open_issues_count":3,"forks_count":13,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-31T21:51:40.635Z","etag":null,"topics":["database","local","wxapp"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jin-yufeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-05-07T13:48:28.000Z","updated_at":"2024-06-30T12:01:04.000Z","dependencies_parsed_at":"2023-06-10T17:30:57.909Z","dependency_job_id":null,"html_url":"https://github.com/jin-yufeng/MpLocalDB","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin-yufeng%2FMpLocalDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin-yufeng%2FMpLocalDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin-yufeng%2FMpLocalDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin-yufeng%2FMpLocalDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jin-yufeng","download_url":"https://codeload.github.com/jin-yufeng/MpLocalDB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230438769,"owners_count":18225976,"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":["database","local","wxapp"],"created_at":"2024-10-15T00:38:22.725Z","updated_at":"2024-12-19T13:21:21.204Z","avatar_url":"https://github.com/jin-yufeng.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MpLocalDB\r\n\r\n\u003e 微信小程序本地数据库\r\n\r\n## 使用方法 ##\r\n1. 复制 `localDB.js`（`7.97KB`，`min` 版本 `4.26KB`）到 `utils` 目录下\r\n2. 在需要使用的页面的 `js` 文件中添加  \r\n\r\n   ```javascript\r\n   const localDB = require('utils/localDB.js')\r\n   const _ = localDB.command\r\n   ```\r\n\r\n## 示例程序 ##\r\n```javascript\r\nconst localDB = require('utils/localDB.js')\r\nconst _ = localDB.command\r\nlocalDB.init() // 初始化\r\nvar articles = localDB.collection('articles')\r\nif(!articles)\r\n  articles = localDB.createCollection('articles') // 不存在则先创建\r\n// 按文章 id 查找\r\nvar doc = articles.doc('xxx')\r\nif(doc) {\r\n  var data = doc.get() // 取得数据\r\n} else {\r\n  // 网络请求获取 data\r\n  data._timeout = Date.now() + 15 * 24 * 3600000 // 设置过期时间为 15 天\r\n  articles.add(data) // 添加到本地数据库\r\n}\r\n// 按类型查找\r\nvar data = articles.where({\r\n  type: 'xxx'\r\n}).get()\r\n// 正则查找\r\nvar data = articles.where({\r\n  title: /xxx/ // 标题中含有 xxx 的\r\n}).get()\r\n// 分页查找\r\nvar page2 = articles.skip(10).limit(10).get()\r\n// 按时间查找\r\nvar data = articles.where({\r\n  date: _.gte('20200501').and(_.lte('20200510')) // 大于等于 20200501 小于等于 20200510\r\n}).get()\r\n// 结果排序\r\nvar data = articles.orderBy('date', 'desc').get() // 按日期降序排序\r\n// 清理过期数据\r\narticles.where({\r\n  _timeout: _.lt(Date.now()) // 过期时间早于当前的\r\n}).remove()\r\n```\r\n\r\n## api ##\r\n### db ###\r\n  \r\n| 名称 | 输入值 | 返回值 | 功能 |\r\n|:---:|:---:|:---:|:---:|\r\n| init | / | / | 初始化数据库 |\r\n| collection | name | Collection | 获取名称为 name 的集合 |\r\n| createCollection | name | Collection | 创建一个名称为 name 的集合 |\r\n| removeCollection | name | / | 移除名称为 name 的集合 |\r\n\r\n### collection ###\r\n\r\n| 名称 | 输入值 | 返回值 | 功能 |\r\n|:---:|:---:|:---:|:---:|\r\n| add | data | id | 向集合中添加一条数据 |\r\n| count | / | number | 统计匹配查询条件的记录的条数 |\r\n| doc | id | document | 获取一条记录 |\r\n| get | / | array | 获取集合数据 |\r\n| limit | number | collection | 指定查询结果集数量上限 |\r\n| orderBy | field, order | collection | 指定查询排序条件 |\r\n| remove | / | / | 删除多条数据 |\r\n| skip | number | collection | 指定查询返回结果时从指定序列后的结果开始返回 |\r\n| update | newVal | / | 更新多条数据 |\r\n| where | query | collection | 进行条件查询 |\r\n\r\n附：`limit` 和 `skip` 仅对 `get` 有效\r\n\r\n### document ###\r\n\r\n| 名称 | 输入值 | 返回值 | 功能 |\r\n|:---:|:---:|:---:|:---:|\r\n| get | / | data | 获取记录数据 |\r\n| remove | / | / | 删除该记录 |\r\n| update | newVal | / | 更新记录数据 |\r\n\r\n### command ###\r\n\r\n查询指令：\r\n\r\n| 名称 | 功能 |\r\n|:---:|:---:|\r\n| eq | 等于 |\r\n| neq | 不等于 |\r\n| lt | 小于 |\r\n| lte | 小于或等于 |\r\n| gt | 大于 |\r\n| gte | 大于或等于 |\r\n| in | 字段值在给定数组中 |\r\n| nin | 字段值不在给定数组中 |\r\n| exists | 判断字段是否存在 |\r\n| or | 多字段或查询 |\r\n\r\n单个字段的条件之间还可以通过 `or` 和 `and` 进行组合，如  \r\n```javascript\r\n_.gt(30).and(_.lt(70)) // 大于 30 且小于 70\r\n_.eq(0).or(_.eq(100)) // 等于 0 或等于 100\r\n```\r\n\r\n`or` 指令用于多字段或查询（默认是与查询）  \r\n```javascript\r\n// 查询 collection 表中 a 字段为 1 或 b 字段为 2 的记录\r\ncollection.where(_.or([{\r\n  a: 1\r\n}, {\r\n  b: 2\r\n}]))\r\n```\r\n\r\n更新指令：\r\n\r\n| 名称 | 功能 |\r\n|:---:|:---:|\r\n| set | 设置字段为指定值 |\r\n| remove | 删除字段 |\r\n| inc | 原子自增字段值 |\r\n| mul | 原子自乘字段值 |\r\n| push | 如字段值为数组，往数组尾部增加指定值 |\r\n| pop | 如字段值为数组，从数组尾部删除一个元素 |\r\n| shift | 如字段值为数组，从数组头部删除一个元素 |\r\n| unshift | 如字段值为数组，往数组头部增加指定值 |\r\n\r\n## 注意事项 ##\r\n1. 数据库存储在本地 `storage` 中，账号、设备之间 **存在隔离**；最大大小为 `10MB`；**请勿覆盖或删除** `key` 为 `localDB` 的 `storage`，否则可能造成数据丢失  \r\n2. 使用前 **必须调用** `db.init` 方法（从 `storage` 中读取保存的数据，数据量较大的时候，需要选择一个合适的时机进行载入）  \r\n3. 所有数据都在内存中，存取都较快，因此所有方法 **均为同步方法**，不返回 `Promise`  \r\n4. 所有操作 **不可撤销和恢复**，尤其是 `remove` 方法需谨慎调用  \r\n5. 集合名和一个集合内的 `_id`（可自动生成）**不可重复**，否则将无法创建  \r\n6. 方法设置参考了云数据库的操作，关于各方法的详细信息可以直接参考 [云数据库的文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/Cloud.database.html)  \r\n\r\n## 更新日志 ##\r\n- 2020.7.9  \r\n  1. `F` 修复了 `remove` 后执行 `where` 可能出错的问题  \r\n\r\n- 2020.6.30  \r\n  1. `A` 增加 `or` 指令，可以实现多字段或查询  \r\n\r\n- 2020.5.13  \r\n  1. `U` 支持多字段排序（设置多个 `orderBy`）  \r\n  2. `U` 同时设置 `orderBy` 和 `skip`、`limit` 时将先进行排序再执行 `skip` 和 `limit`  \r\n\r\n- 2020.5.9  \r\n  1. `A` 添加了 `count` 方法  ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjin-yufeng%2Fmplocaldb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjin-yufeng%2Fmplocaldb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjin-yufeng%2Fmplocaldb/lists"}