Data Storage - Locally stored
Datastore provides local key-value storage. It is similar to the first-generation storages module, but with asynchronous APIs, optional encryption, and customizable serialization.
Datastore supports number, boolean, string, Array, Object, null, and undefined (values are stored through JSON-compatible serialization).
Data stored in the datastore will remain unless the application is unloaded or actively deleted.
Data is scoped by app: the Auto.js Pro IDE build and a packaged build do not share the same files. Within one app install, every script that uses the same datastore name sees the same keys (unless you use encryption with different keys).
If you need to store relatively sensitive data, you can pass encryptionKey when creating a datastore to encrypt local storage. However, note that others may still obtain the key from your code and decrypt the data. See encryptionKey.
Datastore is not storages. The v1 storages module and v2 datastore use different storage backends. Reusing the same logical name does not migrate or share data between v1 and v2.
Example
const { createDatastore } = require("datastore");
// Create a local datastore named "example.test"
const datastore = createDatastore("example.test");
async function main() {
// Write a value
await datastore.set("hello", "world");
// Read a value
console.log("get hello:", await datastore.get("hello"));
// Remove a value and return the removed value
console.log("remove hello:", await datastore.remove("hello"));
// Check whether a key exists
console.log("contains hello:", await datastore.contains("hello"));
// Store and read complex objects (must be JSON-serializable)
await datastore.set("versions", { autojspro: process.versions.autojspro, nodejs: process.version });
const versions = await datastore.get("versions");
console.log("versions:", versions);
// Clear datastore
await datastore.clear();
}
main().catch(console.error);Table of contents
Interfaces
Functions
Function
createDatastore
createDatastore(name, options?): Datastore
Creates a named on-disk store. Different names are isolated; the same name in the same app is shared across scripts.
If you pass encryptionKey, reads and writes use that key. Opening an encrypted store with the wrong key (or no key) usually does not throw at open time—you may read garbage, get null, or hit JSON parse errors when values are deserialized.
Example
// Create an encrypted datastore; the key length must be 16, 32, or 64 characters
const encrptedDatastore = createDatastore("example.encrypted", { encryptionKey: "bCGwOgwzsCqXQFaW" });
async function main() {
// Write with encrypted datastore
await encrptedDatastore.set("timestamp", Date.now());
// Read with encrypted datastore
console.log("timestamp:", await encrptedDatastore.get("timestamp"));
}
main().catch(console.error);Parameters
name:string- Local datastore name.options?:DatastoreOptions- Options.
