import {
BaseDirectory,
Configurate,
JsonProvider,
defineConfig,
keyring,
optional,
} from "tauri-plugin-configurate-api";
// 1. Define schema
const schema = defineConfig({
theme: String,
fontSize: Number,
notifications: optional(Boolean),
server: {
host: String,
apiKey: keyring(String, { id: "server-api-key" }),
},
});
// 2. Create a Configurate instance
const config = new Configurate({
schema,
fileName: "app.json",
baseDir: BaseDirectory.AppConfig,
provider: JsonProvider(),
});
// Keyring identity — reuse this object across all operations
const KEYRING = { service: "my-app", account: "default" };
// 3. Write the initial config (apiKey stored in OS keyring)
await config
.create({
theme: "dark",
fontSize: 14,
notifications: true,
server: { host: "api.example.com", apiKey: "sk-secret" },
})
.lock(KEYRING)
.run();
// 4a. Load with secrets populated
const { data } = await config.load().unlock(KEYRING);
console.log(data.theme); // "dark"
console.log(data.server.apiKey); // "sk-secret"
// 4b. Load locked (apiKey is null)
const locked = await config.load().run();
console.log(locked.data.server.apiKey); // null
// 5. Save — full replacement
await config
.save({
theme: "light",
fontSize: 16,
notifications: false,
server: { host: "api.example.com", apiKey: "sk-secret" },
})
.lock(KEYRING)
.run();
// 6. Patch — only 'theme' is updated
await config.patch({ theme: "dark" }).run();
// Verify the patch
const updated = await config.load().unlock(KEYRING);
console.log(updated.data.theme); // "dark"
console.log(updated.data.fontSize); // 16 (unchanged)
// 7. Check existence
const exists = await config.exists();
console.log(exists); // true
// 8. Delete the config and its keyring entries
await config.delete(KEYRING);