美文网首页
rdbStore 鸿蒙数据库组件

rdbStore 鸿蒙数据库组件

作者: 壹零二肆 | 来源:发表于2025-05-13 20:22 被阅读0次

简介

鸿蒙生态数据库组件,支撑大量鸿蒙应用数据库相关能力。

鸿蒙平台数据库能力以 relationalStore 相关接口提供给开发者,rdbStore
以DTO对象形式来进行数据库操作,封装数据库创建和自动升级、数据库谓词构建、查询结果反序列化、品质调优等能力,实现简单高效地进行数据库操作。

运维方面:完备单元测试、品质数据打点上报、全链路日志等。

仓库地址:https://github.com/bytedance/rdbStore

接入

ohpm install rdbstore

更多技术支持: 邮件 gongshijie.gongsj@bytedance.com

能力简介

可参考单元测试代码进行编码

数据库创建

this.database = Rdb.databaseBuilder(context, {
  version: DBConstants.DB_VERSION,
  dbName: DBConstants.NEW_DB,
  entities: [XXXX, XXXX, XXXX, XXXX],
  migrations: [],
  encrypt: false,
  securityLevel: relationalStore.SecurityLevel.S1,
  autoMigrate: true
})
  .setLogger(new DBLog())
  .setEvent(new DBEventSender())
  .build()

数据库操作

插入

RdbDao#batchInsert 批量插入数据(默认为insert or replace)
RdbDao#insert 插入数据
RdbDao#insertSync 同步主线程插入数据

代码示范:

it('insert', 0, async (done: Function) => {

  const model = new Sample()
  model.id = Date.now()
  model.age = Date.now()
  model.bigId = BigInt(Date.now())
  await dao.insert(model)

  const model2 = new Sample()
  model2.id = Date.now()
  model2.age = Date.now()
  model2.bigId = BigInt(Date.now())
  dao.insertSync(model2)

  await dao.batchInsert([model, model2])
})

删除

RdbDao#delete 删除数据
RdbDao#deleteSync 同步主线程删除
RdbDao#batchDelete 批量通过谓词删除
RdbDao#batchDeleteSync 批量通过谓词删除同步
RdbDao#deleteAll 删除所有
RdbDatabase#deleteDb 删除整个 db 文件

代码示范:

it('remove', 0, async (done: Function) => {
  const model = new Sample()
  model.id = Date.now()
  model.age = Date.now()
  model.bigId = BigInt(Date.now())
  model.name = 'a'
  await dao.insert(model)
  
  await dao.delete(model)

  const builder = dao.predicatesBuilder()
  const nameProp = dao.getProperty('name')
  const ageProp = dao.getProperty('age')

  // (b and (c or d))
  const predicates =
    builder.where(nameProp.equalTo('b'), ...builder.or(nameProp.equalTo('c'), nameProp.equalTo('d'))).predicates
  const deleteCnt = await dao.batchDelete(predicates)

  // (b or (c and d))
  const builder2 = dao.predicatesBuilder()
  const predicates2 = builder2.where(...builder2.or(nameProp.equalTo('b'),
    ...builder2.and(nameProp.equalTo('c'), nameProp.equalTo('d')))).predicates
  const deleteCnt2 = await dao.batchDelete(predicates2)
})

修改

RdbDao#update 完整更新数据
RdbDao#updatePartial 局部更新数据列
RdbDao#updateSync 同步更新数据
代码示范:

it('update', 0, async (done: Function) => {
  const model = new Sample()
  model.id = Date.now()
  model.age = 12
  model.bigId = BigInt(Date.now())
  await dao.insert(model)
  model.bigId = 3n
  model.child.name = 'j'
  
  // 异步
  await dao.update(model)
  // 同步
  dao.updateSync(model)
  // 局部更新
  model.child.name = 'cc'
  model.child.sun.name = 'ss'
  const values: relationalStore.ValuesBucket = {
    'cname': 'cc',
    'sname': 'ss'
  }
  await dao.updatePartial(values, model)
})

查找

RdbDao#predicatesBuilder 复杂查询通过 builder 构建
RdbDao#queryPredicate 通过传入谓词来查询且解析为对应 model
RdbDao#querySync 同步进行查询且解析为对应 model
RdbDao#querySql Sql 语句查询且解析为对应 model
PredicatesBuilder#where 构建查询谓词

代码示范:

it('query', 0, async (done: Function) => {

  // 推荐 predicatesBuilder 查询
  const groupIdProp = dao.getProperty('groupID')
  dao.predicatesBuilder()
    .where(groupIdProp.equalTo(groupId))
    .limit(9)
    .build()
    .run()

  // sql 查询
  const queryResult = dao.queryRawSqlSync('select * from SampleEntity where name = "d"')
  const queryResult2 = await dao.querySql('select * from SampleEntity where name = "d"')
})

运维

RdbDao#getAllColumns 获取表中所有列
RdbDao#getTableCreateSql 获取表创建语句
RdbDao#getAllIndex 获取所有表索引
RdbDao#getCount 查询数据库中行数量
RdbDao#getPredicatesByKey 获取对应 model 主键的谓词
RdbDatabase#getAllTable 获取所有表

品质

TuneDbParamPlugin#setJournalMode
设置数据库的日志模式

  • DELETE:默认模式,在事务提交时删除日志。
  • TRUNCATE:事务提交时截断日志,而不是删除。
  • MEMORY:日志仅保存在内存中。
  • WAL(Write-Ahead Logging):变更预写日志中,再异步地应用到数据库

TuneDbParamPlugin#setPageSize
设置数据库页大小,页是 SQLite 用于存储和管理数据的基本单位

TuneDbParamPlugin#setSynchronous
插入操作时的同步模式

TuneDbParamPlugin#setJournalSizeLimit
设置日志文件大小限制

升级

推荐开启自动升级,之后只需要修改版本号即可,无需手写 Migration

若仍想手动升级,可以通过配置 migration 进行升级

const migration1_2 = new Migration(1, 2).addColumn('table_x', 'column_x', ColumnType.TEXT)
const migration2_4 = new Migration(2, 4).addIndex('index_x', 'table_x', ['column_a', 'column_b'])

this.db = Rdb.databaseBuilder(this.context, {
version: 1,
dbName: 'stream.db',
entities: [SampleEntity],
migrations: [migration1_2, migration2_4],
encrypt: false,
securityLevel: relationalStore.SecurityLevel.S1,
autoMigrate: false
}).setLogger(new DBLog())
.setEvent(new DBEventSender())
.build()

License

Copyright (2024) Bytedance Ltd. and/or its affiliates

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

相关文章

  • 鸿蒙应用配置文件简介

    鸿蒙应用的配置文件 安卓有个清单文件,四大组件、权限等都需要在清单文件中注册,清单文件是个xml文件。类似的,鸿蒙...

  • 零基础学鸿蒙编程-关系数据库

    什么是关系数据库 鸿蒙关系数据库是适合在移动设备上使用的轻量型关系型数据库,速度快,占用空间小.与轻量数据库适合存...

  • WCDB相关文章链接

    微信移动端数据库组件 WCDB 系列:iOS 基础篇(一)微信移动端数据库组件 WCDB 系列:数据库修复三板斧(...

  • 常用SDK

    AFNetworking网络请求组件SDWebImage多个缩略图缓存组件FMDB本地数据库组件UICKeyCha...

  • iOS各种不要不要的开源项目集合2

    通过CocoaPods安装 AFNetworking网络请求组件 FMDB本地数据库组件 SDWebImage多个...

  • iOS开源库和第三方组件(1)

    通过CocoaPods安装 AFNetworking 网络请求组件FMDB 本地数据库组件SDWebImage 多...

  • MySql之表设计原则

    1)不应该针对整个系统进行数据库设计,而应该根据系统架构中的组件划分,针对每个组件所处理的业务进行组件单元的数据库...

  • iOS中常见的第三方组件

    1、使用CocoaPods安装 AFNetworking 网络请求组件 FMDB 本地数据库组件 SDWebIma...

  • 鸿蒙 PageSlider 滑动组件基础用法

    前言 : 各位同学大家好 有断时间没有给大家更新文章了具体多久我也记得了。最近还在学习鸿蒙开发, 学到了PageS...

  • iOS开发 三方库、插件、大牛博客等

    用到的组件1、通过CocoaPods安装项目名称项目信息AFNetworking网络请求组件FMDB本地数据库组件...

网友评论

      本文标题:rdbStore 鸿蒙数据库组件

      本文链接:https://www.haomeiwen.com/subject/srwvijtx.html