Use the SDK
Business data storage
The business data storage module uses Preference for in-memory storage. It supports the following data types: number, string, boolean, Array<number>, Array<boolean>, Array<Uint8Array>, and Uint8Array.
The application's memory usage increases with the amount of data stored in Preferences. Preferences is not suitable for storing large amounts of data and does not support encryption through configuration.
Constraints
Preference is not process-safe, which creates a risk of file corruption and data loss. Therefore, using Preference in multi-process scenarios is not supported.
The key must be a non-empty string with a maximum length of 1024 bytes.
If the value is a string, it must use the UTF-8 encoding format. The string can be empty, but its length cannot exceed 16 MB.
To store data that contains non-UTF-8 strings, use the Uint8Array type. This prevents formatting errors in the persistent file.
Memory usage increases with the storage data size. Keep the data volume small, preferably under 50 MB. With large data volumes, creating Preference objects and persisting data using the sync API is a time-consuming operation. Do not perform this operation in the main thread, because it may cause the application to freeze.
Create a business data table
let mpData: MPData = MPData.createData('xxx')CRUD operations
let mpData: MPData = MPData.createData('xxx')
mpData.put(key,value)
mpData.putObject(key,object)
mpData.delete(key)
mpData.get(key):preferences.ValueType
mpData.getAll(): [string,string[]]Synchronization
If you do not call the sync API, the data is stored only in memory. After you call the sync API, the data is persisted to a serialized file.
mpData.synchronize()Clear data
mpData.clear()Global User Storage
CRUD operations
import { MPData } from "@mpaas/datacenter"
// Store data
// Basic data types
MPData.put(key, value).then().catch() // Default global storage
MPData.put(key, value,{userId:""}).then().catch() // User-associated data storage
MPData.put(key, value,{encrypt: true}).then().catch() // Use encrypted storage
// object
MPData.putObject(key, value).then().catch()
MPData.putObject(key, value,{userId:""}).then().catch() // User-associated data storage
MPData.putObject(key, value,{encrypt: true}).then().catch() // Use encrypted storage
// Batch operations
MPData.putBatch(values).then().catch()
MPData.putBatch(values,{userId:""}).then().catch() // User-associated data storage
MPData.putBatch(values,{encrypt: true}).then().catch() // Use encrypted storage
// Retrieve a value
MPData.get(key).then().catch()
MPData.get(key,{userId:""}).then().catch() // User-associated data
MPData.get(key,{encrypt: true}).then().catch() // Use encryption
// Delete
await MPData.delete(key)
await MPData.delete(key,{userId : "" })
await MPData.delete(key,{encrypt:true})The optional parameters must be consistent.
Reset data
// Reset
await MPData.resetGlobalStore() // Global data
await MPData.resetUserStore(userId) // User dataQuery all data for specified parameters
// Get all data
await MPData.getAllData() // Global data
await MPData.getAllData(userId) // User dataClose the connection
Close the database connection after you finish using the data. The underlying layer of HarmonyOS limits the maximum number of concurrent database connection pools to 8. An exception is thrown if this limit is exceeded.
await MPData.close() Get a data array for a specified prefix
MPData.getEntries(keyPrefix).then((data)=>{
// data is a JSON string
}).catch()
MPData.getEntries(keyPrefix,{userId:""}).then().catch() // User-associated data
MPData.getEntries(keyPrefix,{encrypt: true}).then().catch() // Use encryptionFile storage
let fileUtil = new MPFile()
// Store a file. encrypt: specifies whether to encrypt the file.
public async saveFile(fileName: string , content: string, encrypt: boolean = false)
// Get the file content.
public async getFileContent(fileName: string, encrypt: boolean = false)
// Delete a file.
public async removeFile(fileName: string) Dataset operations
Call
getResultto retrieve the dataset.// Get the dataset for a specified prefix MPData.getResult(keyPrefix).then().catch() MPData.getResult(keyPrefix,{userId:""}).then().catch() MPData.getResult(keyPrefix,{encrypt:true}).then().catch()Call the following APIs. The parameters must be consistent.
MPData.getCount() // Current cursor position MPData.getPosition() // Move the cursor MPData.moveToFirst() MPData.moveToNext() MPData.moveToPrevious() MPData.move(offset) MPData.moveToPosition(position) // Check the cursor position MPData.isFirst() MPData.isLast() MPData.isBeforeFirst() MPData.isAfterLast() // Get the data at the current cursor position MPData.getEntry()