From 68ead797a8746fe744840c18cfa81ece51b73891 Mon Sep 17 00:00:00 2001 From: lqqCp3 <1960002614@qq.com> Date: Fri, 9 Jan 2026 11:38:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20[Issues:=20#IDIOS5]=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9watermelondb=E7=9A=84=E6=8C=87=E5=AF=BC=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- en/watermelondb.md | 50 ++++++++++++++-------- zh-cn/watermelondb.md | 99 ++++++++++++++++++------------------------- 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/en/watermelondb.md b/en/watermelondb.md index 0c75c67a..6428eb49 100644 --- a/en/watermelondb.md +++ b/en/watermelondb.md @@ -10,9 +10,10 @@ Please go to the Releases page of the third-party library to check the matching information: + | Third-party library version | Release information | Supported RN version | | --------------------------- | ------------------------------------------------------------------------------------------------------ | -------------------- | -| v0.28.2-rc.1 | [@react-native-ohos/watermelondb Releases](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/tags) | 0.72/0.77 | +| v0.28.2 | [@react-native-ohos/watermelondb Releases](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/tags) | 0.72/0.77 | ## 1. Installation and Usage @@ -616,13 +617,16 @@ const styles = StyleSheet.create({ export default SchemaExampleT; ``` -## Manual Link +## 2.Manual Link This step is a guide for manually configuring native dependencies. First, you need to open the HarmonyOS project harmony in the project using DevEco Studio. ### 1.Add overrides field to oh-package.json in the project root directory +In order to make the project depend on the same version of the RN SDK, you need to add an overrides field to the oh-package.json5 file in the root directory of the project, which points to the version of the RN SDK required by the project. The version to be replaced can be either a specific version number, a version range, or a locally existing HAR package or source code directory. +For the function of this field, please refer to the official documentation.(https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-oh-package-json5-V5#zh-cn_topic_0000001792256137_overrides): + ```json { "overrides": { @@ -633,8 +637,10 @@ First, you need to open the HarmonyOS project harmony in the project using DevEc ### 2. There are currently two methods: -Import via har package (this method will be deprecated after the IDE improves related functions, and it is the preferred method currently); -Link source code directly. +There are currently two methods available: + +1. Import via HAR package (This method will be deprecated after the IDE-related features are fully implemented, but it is the preferred method at present); +2. Directly link to the source code. Method 1: Import via har package (recommended) @@ -757,7 +763,7 @@ ohpm install Then compile and run the project. -## Constraints and Limitations +## 3.Constraints and Limitations ### Compatibility Support @@ -766,6 +772,8 @@ The content of this document has been verified based on the following versions 1. RNOH:0.72.90; SDK:HarmonyOS NEXT Developer DB3; IDE: DevEco Studio: 5.0.5.220; ROM:NEXT.0.0.105; 2. RNOH:0.77.18; SDK:HarmonyOS 6.0.0 Release; IDE: DevEco Studio 6.0.0.858; ROM:6.0.0.112; +### Permission Requirements + #### Add the reason for applying for the above permissions in the entry directory Open `entry/src/main/resources/base/element/string.json`,add: @@ -790,7 +798,7 @@ Open `entry/src/main/resources/base/element/string.json`,add: } ``` -## Properties +## 4.Properties > [!TIP] "Platform"This column shows which platforms the property supports in the original third-party library。 @@ -798,6 +806,7 @@ Open `entry/src/main/resources/base/element/string.json`,add: **Collection class** + | Name | Description | Type | Required | Platform | HarmonyOS Support | | ------------------------- | ------------------------------------------------------------------------------ | -------- | -------- | ----------- | ----------------- | | db | Get the reference to the database instance that this collection belongs to | function | no | iOS/Android | yes | @@ -806,6 +815,8 @@ Open `entry/src/main/resources/base/element/string.json`,add: | create | Create and save new records in the collection | function | yes | iOS/Android | yes | | prepareCreate | Pre - create a record object without immediately persisting it to the database | function | yes | iOS/Android | yes | | prepareCreateFromDirtyRaw | Pre - create a record object from raw unvalidated data | function | yes | iOS/Android | yes | +| disposableFromDirtyRaw | Create a one - time temporary record based on raw dirty data | function | yes | iOS/Android | yes | +| table | Get the name of the database table corresponding to this collection | function | no | iOS/Android | yes | | \_fetchQuery | Execute the SQL query statement and return the complete result set | function | no | iOS/Android | no | | \_fetchIds | Get the list of all record IDs that match the query criteria | function | no | iOS/Android | no | | \_fetchCount | Count the total number of records that satisfy the query criteria | function | no | iOS/Android | no | @@ -813,13 +824,12 @@ Open `entry/src/main/resources/base/element/string.json`,add: | \_fetchRecord | Get the complete data content of a single record | function | no | iOS/Android | no | | \_applyChangesToCache | Apply the data changes to the in-memory cache system | function | no | iOS/Android | no | | \_notify | Notify all registered observers that the data has changed | function | no | iOS/Android | no | -| disposableFromDirtyRaw | Create a one - time temporary record based on raw dirty data | function | yes | iOS/Android | yes | -| table | Get the name of the database table corresponding to this collection | function | no | iOS/Android | yes | | schema | Get the data table structure definition corresponding to this collection | function | no | iOS/Android | yes | | experimentalSubscribe | Subscribe to collection - level data change notifications | function | yes | iOS/Android | yes | **Database class** + | Name | Description | Type | Required | Platform | HarmonyOS Support | | -------------------------- | -------------------------------------------------------------------------------- | -------- | -------- | ----------- | ----------------- | | get<T extends Model> | Retrieve a specific record from the database according to the model class and ID | function | yes | iOS/Android | yes | @@ -836,6 +846,7 @@ Open `entry/src/main/resources/base/element/string.json`,add: **Relation class** + | Name | Description | Type | Required | Platform | HarmonyOS Support | | ------------- | ------------------------------------------------------------------------------ | -------- | -------- | ----------- | ----------------- | | get id | Get the unique identifier of the associated target model | function | no | iOS/Android | yes | @@ -847,6 +858,7 @@ Open `entry/src/main/resources/base/element/string.json`,add: **Model class** + | Name | Description | Type | Required | Platform | HarmonyOS Support | | --------------------------------------------- | ----------------------------------------------------------------------------------- | -------- | -------- | ----------- | ----------------- | | associations | Define the association relationship mapping between the model and other data tables | function | no | iOS/Android | yes | @@ -885,6 +897,7 @@ Open `entry/src/main/resources/base/element/string.json`,add: **Query class** + | Name | Description | Type | Required | Platform | HarmonyOS Support | | -------------------------------- | ---------------------------------------------------- | -------- | -------- | ----------- | ----------------- | | pipe<T> | Perform chain conversion operations on query results | function | no | iOS/Android | yes | @@ -911,6 +924,7 @@ Open `entry/src/main/resources/base/element/string.json`,add: **Schema class** + | Name | Description | Type | Required | Platform | HarmonyOS Support | | -------------------------------- | ------------------------------------------------------------------- | -------- | -------- | ----------- | ----------------- | | tableName<T extends Model> | Define the name of the database table | function | yes | iOS/Android | yes | @@ -919,18 +933,18 @@ Open `entry/src/main/resources/base/element/string.json`,add: | validateColumnsSchema | Verify the validity of the field schema definition | function | yes | iOS/Android | yes | | tableSchema | Define the structure and field configuration of a single data table | function | yes | iOS/Android | yes | -## Pending Issues +## 5.Pending Issues -- [ ] \_fetchQuery Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -- [ ] \_fetchIds Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -- [ ] \_fetchCount Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -- [ ] \_unsafeFetchRaw Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -- [ ] \_fetchRecord Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -- [ ] \_applyChangesToCache Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -- [ ] \_notify Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_fetchQuery Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_fetchIds Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_fetchCount Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_unsafeFetchRaw Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_fetchRecord Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_applyChangesToCache Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) +- [ ] \_notify Some parameters of the interface are not supported on HarmonyOS. As an underlying method provided for other modules, it cannot be called directly. [issue#2](https://gitcode.com/OpenHarmony-RN/rntpc_watermelondb/issues/2) -## Others +## 6.Others -## Open Source License +## 7.Open Source License This project is based on [The MIT License (MIT)](https://github.com/Nozbe/WatermelonDB/blob/master/LICENSE) ,Feel free to enjoy and contribute to the open source project。 diff --git a/zh-cn/watermelondb.md b/zh-cn/watermelondb.md index 4c515492..1f643bd6 100644 --- a/zh-cn/watermelondb.md +++ b/zh-cn/watermelondb.md @@ -33,6 +33,8 @@ yarn add @react-native-ohos/watermelondb > [!WARNING] 使用时 import 的库名不变。 +### watermelondb example + ```js // jsi: false import React, { useState } from 'react'; @@ -130,39 +132,34 @@ const validateUserColumns = () => { return results; }; -// ===================== 初始化数据库改为接收表类型参数(核心修改)===================== -// 初始化数据库(新增tableType参数) const initializeDatabase = async (tableType) => { - // 根据表类型获取对应表结构 const targetTableSchema = tableSchemaMap[tableType] || userTableSchema; const adapter = new SQLiteAdapter({ - dbName: `watermelon_${tableType}_db`, // 不同表使用不同数据库名 + dbName: `watermelon_${tableType}_db`, schema: getAppDatabaseSchema(targetTableSchema), jsi: false, migrations: schemaMigrations({ migrations: [] }), }); - await adapter.initializingPromise; // 等待适配器初始化 + await adapter.initializingPromise; return new Database({ adapter, modelClasses: [] }); }; const SchemaExample = () => { const [validationResult, setValidationResult] = useState(null); const [createResult, setCreateResult] = useState(null); - const [database, setDatabase] = useState({}); // 修改为对象存储不同表的数据库实例 + const [database, setDatabase] = useState({}); + - // 验证字段结构(保留原有) const handleValidateSchema = () => { const results = validateUserColumns(); setValidationResult(results); setCreateResult(null); }; - // ===================== 修改创建方法为接收表类型参数(核心修改)===================== - // 创建数据库表(新增tableType参数) const handleCreateTables = async (tableType) => { try { - // 检查该表是否已创建 + if (database[tableType]) { const tableName = tableSchemaMap[tableType]?.name || userTable; setCreateResult({ @@ -174,7 +171,7 @@ const SchemaExample = () => { } const db = await initializeDatabase(tableType); - // 存储对应表类型的数据库实例 + setDatabase({ ...database, [tableType]: db }); const tableName = tableSchemaMap[tableType]?.name || userTable; setCreateResult({ @@ -191,11 +188,11 @@ const SchemaExample = () => { } }; - // 清空页面状态(保留原有) + const handleClear = () => { setValidationResult(null); setCreateResult(null); - setDatabase({}); // 重置为空对象 + setDatabase({}); }; return ( @@ -203,25 +200,24 @@ const SchemaExample = () => { WatermelonDB 表创建示例 - {/* 为每个按钮绑定不同的tableType参数 */}