HarmonyOS 鸿蒙Next 便捷开发的本地关系数据操作框架
HarmonyOS 鸿蒙Next 便捷开发的本地关系数据操作框架
cjdb
简介
cjdb 是一套简化数据库操作,面相程序开发的简洁架构,正在不断完善中,欢迎各位大佬指正
下载安装
ohpm install cjdb
依赖
ohpm install cjcommon
ohpm install reflect-metadata
CJDBUtil API
- init(config: { context: any, dbName?: string, securityLevel?: relationalStore.SecurityLevel }): Promise
- query<T>(clazz: T, conditions?: Condition[]): Promise<T[]>
- getById<T>(clazz: T, id: number): Promise
- all<T>(clazz: T): Promise<T[]>
- save<T>(instance: T): Promise
- saveList<T>(instance: T): Promise
- update<T>(clazz: T, values, conditions?: Condition[]): Promise
- delete<T>(clazz: T, conditions?: Condition[]): Promise
- executeSql<T>(sql: string, params: Array<relationalStore.ValueType>): Promise
- querySql(sql: string, params: Array<relationalStore.ValueType>): Promise<relationalStore.ResultSet>
数据类CJBaseModel 公用API
- save(): Promise
- update(): Promise
- delete(): Promise
使用说明
1、初始化
async onCreate(want, launchParam) {
await CJDBUtil.init({
context: this.context, // 必填,上下文
dbName: 'cjdb.db', // 非必填(默认:'cjdb.db')
securityLevel: relationalStore.SecurityLevel.S1 // 非必填 (默认 S1)
})
}
2、数据类格式
import { Table, Id, ColumnType, CJBaseModel, Columns } from 'cjdb';
@Table("Person") // @Table("表名")
export default class Person extends CJBaseModel {
// ==> id属性都必填 <==
//@Id = {
//columnName?: string,列名
//types?: ColumnType,类型
//autoincrement?: boolean,是否自增
//}
@Id()
id: number
//Columns 参数:
//columnName?: string, 对应列名,非必填,默认属性名
//types: ColumnType 对应列类型,必填,
@Columns({columnName: "name",types: ColumnType.TEXT})
name: string
@Columns({types: ColumnType.INTEGER})
old: number
constructor(name?: string, old?: number) {
super();
this.name = name
this.old = old
}
}
数据的增、删、改、查
1、新增
a、采用 CJDBUtil
import { CJDBUtil } from 'cjdb'
let person: Person = new Person(`哈哈`, 23)
CJDBUtil.save(person)
.then(id => {
CJLogUtil.info(TAG, "保存成功", id)
})
.catch(err => {
CJLogUtil.error(TAG, "保存异常", JSON.stringify(err))
})
b、继承 CJBaseModel
export default class Person extends CJBaseModel {}
let person: Person = new Person(`哈哈`, 22)
CJLogUtil.info(`保存:`, JSON.stringify(person))
person.save()
.then(res => {
CJLogUtil.info(TAG, "保存成功")
})
.catch(err => {
CJLogUtil.info(TAG, "保存失败")
})
2、修改
a、采用 CJDBUtil
import { CJDBUtil,OperatorType,Condition } from 'cjdb'
CJDBUtil.update(Person, { name: "测试" }, [
// 修改Person类 id == 2 的数据,可以无限制添加条件
new Condition('id', OperatorType.EQUAL_TO, 2),
])
.then(ids => {
CJLogUtil.info(TAG, "更新结果:", JSON.stringify(ids))
})
.catch(err => {
CJLogUtil.error(TAG, "更新异常", JSON.stringify(err))
})
b、继承 CJBaseModel
let person: Person = new Person(`哈哈`, 56)
person.id = 1 // id 必须存才
// 更新id为1的数据
person.update()
.then(res => {
CJLogUtil.info(TAG, "更新成功")
})
.catch(err => {
CJLogUtil.info(TAG, "更新失败")
})
3、删除
a、采用 CJDBUtil
import { CJDBUtil,OperatorType,Condition } from 'cjdb'
CJDBUtil.delete(Person, [
new Condition('id', OperatorType.EQUAL_TO, 2),
])
.then(results => {
CJLogUtil.info(TAG, "删除结果:", JSON.stringify(results))
})
.catch(err => {
CJLogUtil.error(TAG, "删除异常", JSON.stringify(err))
})
b、继承 CJBaseModel
let person: Person = new Person(`哈哈`, 68)
person.id = 1
person
.delete()
.then(res => {
CJLogUtil.info(TAG, "删除成功")
})
.catch(err => {
CJLogUtil.info(TAG, "删除失败")
})
4、查询 (采用 CJDBUtil)
a、查询类所有数据
CJDBUtil.all<Person>(clazz:Person)
CJDBUtil.all(Person)
.then(results => {
CJLogUtil.info(TAG, "查询结果:", JSON.stringify(results))
})
.catch(err => {
CJLogUtil.error(TAG, "查询异常", JSON.stringify(err))
})
b、根据条件查询类所有数据
CJDBUtil.query<Person>(clazz: Person, conditions?: Condition[])
CJDBUtil.query(Person, [new Condition('id', OperatorType.EQUAL_TO, 2)])
.then(results => {
CJLogUtil.info(TAG, "根据条件查询成功:", JSON.stringify(results))
})
.catch(err => {
CJLogUtil.error(TAG, "根据条件查询异常", JSON.stringify(err))
})
c、根据id查询指定数据
CJDBUtil.getById(Person, 1)
.then(results => {
CJLogUtil.info(TAG, "根据ID查询成功:", JSON.stringify(results))
})
.catch(err => {
CJLogUtil.error(TAG, "根据ID查询异常", JSON.stringify(err))
})
更多关于HarmonyOS 鸿蒙Next 便捷开发的本地关系数据操作框架的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
你好,请问怎么支持 对象嵌套对象,存储到数据库中呢
更多关于HarmonyOS 鸿蒙Next 便捷开发的本地关系数据操作框架的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
正在不断更新中,各位大佬多多给点意见
HarmonyOS鸿蒙Next的本地关系数据操作框架是基于分布式数据管理技术,支持设备间数据同步与共享。该框架采用SQLite作为本地数据库引擎,提供了轻量级的ORM(对象关系映射)支持,开发者可以通过简洁的API进行数据的增删改查操作。框架支持事务处理,确保数据操作的原子性、一致性、隔离性和持久性。此外,鸿蒙Next的本地关系数据操作框架还支持数据加密,保障用户数据的安全性。通过该框架,开发者可以高效地进行本地数据管理,提升应用的性能和用户体验。