Plugins teach Lix how to understand specific file formats by detecting changes at the entity level and reconstructing files from entity states.
A plugin handles two core responsibilities:
1. Detect changes
2. Apply changes
Optional: Render diffs
When you insert or update a file, Lix automatically invokes the matching plugin based on the file path:
File updated
↓
Plugin matched by glob pattern (*.json, *.md, etc.)
↓
Plugin.detectChanges() identifies entity changes
↓
Changes stored in databaseWhen you read a file back:
Query entities from database
↓
Plugin.applyChanges() reconstructs file
↓
File data returnedRegister plugins when opening a lix:
import { openLix } from "@lix-js/sdk";
import { plugin as jsonPlugin } from "@lix-js/plugin-json";
import { plugin as markdownPlugin } from "@lix-js/plugin-md";
const lix = await openLix({
providePlugins: [jsonPlugin, markdownPlugin],
});
// Insert a JSON file - plugin automatically detected by *.json pattern
await lix.db
.insertInto("file")
.values({
path: "/config.json",
data: new TextEncoder().encode(JSON.stringify({ theme: "dark" })),
})
.execute();The plugin is automatically matched based on the file path and the plugin's detectChangesGlob pattern.
Plugins implement the LixPlugin interface with two main methods:
detectChanges({ before, after, querySync }) - Detects entity changes between file statesapplyChanges({ file, changes }) - Reconstructs files from entity statesFor implementation examples, see the official plugin source code:
querySyncThese implementations demonstrate:
querySync to preserve stable entity IDsUse Stable Entity IDs
querySync to preserve existing IDs across changesChoose the Right Granularity
Handle Edge Cases
Optimize Performance
querySync sparingly - it's synchronous but has overheadTest Thoroughly
detectChanges and applyChanges are inversesRegister multiple plugins to handle different file types:
import { plugin as jsonPlugin } from "@lix-js/plugin-json";
import { plugin as csvPlugin } from "@lix-js/plugin-csv";
import { plugin as markdownPlugin } from "@lix-js/plugin-md";
const lix = await openLix({
providePlugins: [jsonPlugin, csvPlugin, markdownPlugin],
});Each file is automatically processed by the plugin whose detectChangesGlob pattern matches the file path.
Official plugins:
Community plugins: Check the Lix GitHub organization