diff --git a/en/application-dev/database/database-storage-guidelines.md b/en/application-dev/database/database-storage-guidelines.md index 00f56f8853b93fc058f7e66c8fb3a2517cf5d7d0..9375ed470629cba6112c1897db9f7b475d43cb3d 100644 --- a/en/application-dev/database/database-storage-guidelines.md +++ b/en/application-dev/database/database-storage-guidelines.md @@ -1,10 +1,10 @@ -# Lightweight Data Store Development +# Lightweight Data Store Development -## When to Use +## When to Use The lightweight data store is ideal for storing lightweight and frequently used data, but not for storing a large amount of data or data with frequent changes. The application data is persistently stored on a device in the form of files. Note that the instance accessed by an application contains all data of the file. The data is always loaded to the memory of the device until the application removes it from the memory. The application can perform data operations using the **Storage** APIs. -## Available APIs +## Available APIs The lightweight data store provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value pairs. Keys are of the string type, and values can be of the number, string, or Boolean type. @@ -14,24 +14,9 @@ Create a **Storage** instance for data operations. A **Storage** instance is **Table 1** API for creating a **Storage** instance - - - - - - - - - - - -

Package Name

-

Method

-

Description

-

ohos.data.storage

-

getStorage(path: string): Promise<Storage>;

-

Obtains the Storage singleton corresponding to a file for data operations.

-
+| Package Name | Method | Description | +| ----------------- | ------------------------------------------- | ------------------------------------------------------------ | +| ohos.data.storage | getStorage(path: string): Promise\ | Obtains the **Storage** singleton corresponding to a file for data operations. | **Writing Data** @@ -39,24 +24,9 @@ Call the **put\(\)** method to add or modify data in a **Storage** instance. **Table 2** API for writing data - - - - - - - - - - - -

Class

-

Method

-

Description

-

Storage

-

put(key: string, value: ValueType): Promise<void>;

-

Writes data of the number, string, and Boolean types.

-
+| Class | Method | Description | +| ------- | -------------------------------------------------- | ----------------------------------------------------- | +| Storage | put(key: string, value: ValueType): Promise\ | Writes data of the number, string, and Boolean types. | **Reading Data** @@ -64,24 +34,9 @@ Call the **get\(\)** method to read data from a **Storage** instance. **Table 3** API for reading data - - - - - - - - - - - -

Class

-

Method

-

Description

-

Storage

-

get(key: string, defValue: ValueType): Promise<ValueType>;

-

Reads data of the number, string, and Boolean types.

-
+| Class | Method | Description | +| ------- | ---------------------------------------------------------- | ---------------------------------------------------- | +| Storage | get(key: string, defValue: ValueType): Promise\ | Reads data of the number, string, and Boolean types. | **Storing Data Persistently** @@ -89,24 +44,9 @@ Call the **flush\(\)** method to write the cached data back to its text file f **Table 4** API for data persistence - - - - - - - - - - - -

Class

-

Method

-

Description

-

Storage

-

flush(): Promise<void>;

-

Writes data in the Storage instance back to its file through an asynchronous thread.

-
+| Class | Method | Description | +| ------- | ----------------------- | ------------------------------------------------------------ | +| Storage | flush(): Promise\ | Writes data in the **Storage** instance back to its file through an asynchronous thread. | **Observing Data Changes** @@ -114,31 +54,10 @@ Specify **StorageObserver** as the callback to subscribe to data changes. When **Table 5** APIs for subscribing to data changes - - - - - - - - - - - - - - - -

Class

-

Method

-

Description

-

Storage

-

on(type: 'change', callback: Callback<StorageObserver>): void;

-

Subscribes to data changes.

-

Storage

-

off(type: 'change', callback: Callback<StorageObserver>): void;

-

Unsubscribes from data changes.

-
+| Class | Method | Description | +| ------- | ------------------------------------------------------------ | ------------------------------- | +| Storage | on(type: 'change', callback: Callback\): void | Subscribes to data changes. | +| Storage | off(type: 'change', callback: Callback\): void | Unsubscribes from data changes. | **Deleting Data** @@ -146,33 +65,13 @@ Use the following APIs to delete a **Storage** instance or data file. **Table 6** APIs for deleting data - - - - - - - - - - - - - - - -

Package Name

-

Method

-

Description

-

ohos.data.storage

-

deleteStorage(path: string): Promise<void>;

-

Deletes a Storage instance from the cache and deletes its file from the device.

-

ohos.data.storage

-

removeStorageFromCache(path: string): Promise<void>;

-

Deletes a Storage instance from the cache to release memory.

-
- -## How to Develop +| Package Name | Method | Description | +| ----------------- | ---------------------------------------------------- | ------------------------------------------------------------ | +| ohos.data.storage | deleteStorage(path: string): Promise\ | Deletes a **Storage** instance from the cache and deletes its file from the device. | +| ohos.data.storage | removeStorageFromCache(path: string): Promise\ | Deletes a **Storage** instance from the cache to release memory. | + + +## How to Develop 1. Import **@ohos.data.storage** and related modules to the development environment. @@ -192,7 +91,7 @@ Use the following APIs to delete a **Storage** instance or data file. ``` -1. Write data. +3. Write data. Use the **put\(\)** method of the **Storage** class to write data to the cached **Storage** instance. @@ -210,7 +109,7 @@ Use the following APIs to delete a **Storage** instance or data file. ``` -1. Read data. +4. Read data. Use the **get\(\)** method of the **Storage** class to read data. @@ -228,7 +127,7 @@ Use the following APIs to delete a **Storage** instance or data file. ``` -1. Store data persistently. +5. Store data persistently. Use the **flush** or **flushSync** method to flush data in the **Storage** instance to its file. @@ -236,7 +135,7 @@ Use the following APIs to delete a **Storage** instance or data file. storage.flush(); ``` -2. Observe data changes. +6. Observe data changes. Specify **StorageObserver** as the callback to subscribe to data changes for an application. When the value of the subscribed key is changed and the **flush\(\)** method is executed, **StorageObserver** will be invoked. Unregister the **StorageObserver** when it is no longer required. @@ -256,7 +155,7 @@ Use the following APIs to delete a **Storage** instance or data file. ``` -1. Delete the specified file. +7. Delete the specified file. Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored. @@ -269,4 +168,3 @@ Use the following APIs to delete a **Storage** instance or data file. }) ``` -