# Lightweight Data Store Development<a name="EN-US_TOPIC_0000001230830543"></a>
# Lightweight Data Store Development
## When to Use<a name="section13841104521714"></a>
## 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<a name="section15173156141712"></a>
## 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.
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 (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
**Creating a Storage Instance**
...
...
@@ -14,131 +14,50 @@ Create a **Storage** instance for data operations. A **Storage** instance is
**Table 1** API for creating a **Storage** instance
<tdclass="cellrowborder"valign="top"width="54.059999999999995%"headers="mcps1.2.4.1.3 "><pid="p4311132162417"><aname="p4311132162417"></a><aname="p4311132162417"></a>Obtains the <strongid="b193911821194211"><aname="b193911821194211"></a><aname="b193911821194211"></a>Storage</strong> singleton corresponding to a file for data operations.</p>
<tdclass="cellrowborder"valign="top"width="43.21%"headers="mcps1.2.4.1.3 "><pid="p127261044489"><aname="p127261044489"></a><aname="p127261044489"></a>Writes data of the number, string, and Boolean types.</p>
<tdclass="cellrowborder"valign="top"width="44.13%"headers="mcps1.2.4.1.3 "><pid="p11862123993920"><aname="p11862123993920"></a><aname="p11862123993920"></a>Reads data of the number, string, and Boolean types.</p>
<tdclass="cellrowborder"valign="top"width="44.13%"headers="mcps1.2.4.1.3 "><pid="p1234935810316"><aname="p1234935810316"></a><aname="p1234935810316"></a>Writes data in the <strongid="b263252724411"><aname="b263252724411"></a><aname="b263252724411"></a>Storage</strong> instance back to its file through an asynchronous thread.</p>
| Storage | flush(): Promise\<void> | Writes data in the **Storage** instance back to its file through an asynchronous thread.|
**Observing Data Changes**
Specify **StorageObserver** as the callback to subscribe to data changes. When the value of the subscribed key is changed and the **flush\(\)** method is executed, **StorageObserver** will be invoked.
<tdclass="cellrowborder"valign="top"width="39.81%"headers="mcps1.2.4.1.3 "><pid="p1148505452114"><aname="p1148505452114"></a><aname="p1148505452114"></a>Subscribes to data changes.</p>
<tdclass="cellrowborder"valign="top"width="39.81%"headers="mcps1.2.4.1.3 "><pid="p54853548219"><aname="p54853548219"></a><aname="p54853548219"></a>Unsubscribes from data changes.</p>
</td>
</tr>
</tbody>
</table>
Specify **StorageObserver** as the callback to subscribe to data changes. When the value of the subscribed key is changed and the **flush()** method is executed, **StorageObserver** will be invoked.
<tdclass="cellrowborder"valign="top"width="38.3%"headers="mcps1.2.4.1.3 "><pid="p19291149141220"><aname="p19291149141220"></a><aname="p19291149141220"></a>Deletes a <strongid="b16577722115210"><aname="b16577722115210"></a><aname="b16577722115210"></a>Storage</strong> instance from the cache and deletes its file from the device.</p>
<tdclass="cellrowborder"valign="top"width="38.3%"headers="mcps1.2.4.1.3 "><pid="p164461159247"><aname="p164461159247"></a><aname="p164461159247"></a>Deletes a <strongid="b12971117115417"><aname="b12971117115417"></a><aname="b12971117115417"></a>Storage</strong> instance from the cache to release memory.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section146940394256"></a>
1. Import **@ohos.data.storage** and related modules to the development environment.
| 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
1. Import @ohos.data.storage and related modules to the development environment.
```
import dataStorage from '@ohos.data.storage'
...
...
@@ -184,17 +82,18 @@ Use the following APIs to delete a **Storage** instance or data file.
2. Create a **Storage** instance.
Read the specified file and load its data to the **Storage** instance for data operations.
let promise = dataStorage.getStorage(path + '/mystore')
```
3. Write data.
1. Write data.
Use the **put\(\)** method of the **Storage** class to write data to the cached **Storage** instance.
Use the **put()** method of the **Storage** class to write data to the cached **Storage** instance.
```
promise.then((storage) => {
...
...
@@ -209,10 +108,9 @@ Use the following APIs to delete a **Storage** instance or data file.
})
```
4. Read data.
1. Read data.
Use the **get\(\)** method of the **Storage** class to read data.
Use the **get()** method of the **Storage** class to read data.
```
promise.then((storage) => {
...
...
@@ -223,22 +121,20 @@ Use the following APIs to delete a **Storage** instance or data file.
console.info("Get the value of startup failed with err: " + err)
})
}).catch((err) => {
console.info("Get the storage failed")
})
console.info("Get the storage failed")})
```
5. Store data persistently.
1. Store data persistently.
Use the **flush** or **flushSync** method to flush data in the **Storage** instance to its file.
Use the **flush()** or **flushSync()** method to flush data in the **Storage** instance to its 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.
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.
```
promise.then((storage) => {
...
...
@@ -255,8 +151,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.
...
...
@@ -265,8 +160,5 @@ Use the following APIs to delete a **Storage** instance or data file.