### Runtime Setup: Manual Configuration (Rust) Source: https://context7.com/orbitinghail/graft/llms.txt Provides an example of manually configuring and creating a Graft runtime in Rust, allowing for custom Tokio runtimes and explicit configuration of remote and local storage components. This is for advanced use cases requiring fine-grained control. ```rust use graft::rt::runtime::Runtime; use graft::remote::RemoteConfig; use graft::local::fjall_storage::FjallStorage; use std::sync::Arc; use std::time::Duration; // Build Tokio runtime let tokio_rt = tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap(); // Configure remote storage let remote = Arc::new(RemoteConfig::Memory.build().unwrap()); // Configure local storage let storage = Arc::new(FjallStorage::open("/tmp/graft").unwrap()); // Create Graft runtime let runtime = Runtime::new( tokio_rt.handle().clone(), remote, storage, Some(Duration::from_secs(30)), // autosync interval ); ``` -------------------------------- ### Example Development Configuration (Filesystem) Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/config.md A TOML configuration file example for a development Graft setup using local filesystem storage. Specifies the data directory and remote storage root. ```toml data_dir = "./data" [remote] type = "fs" root = "./remote-storage" ``` -------------------------------- ### Example Production Configuration (S3) Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/config.md A TOML configuration file example for a production Graft setup using S3-compatible storage. Includes data directory, autosync interval, and remote storage details. ```toml data_dir = "/var/lib/graft" autosync = 60 [remote] type = "s3_compatible" bucket = "my-app-graft" prefix = "prod" ``` -------------------------------- ### Install Graft SQLite Extension using sqlpkg Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/direct.mdx Install the Graft SQLite extension using the sqlpkg command-line tool. This method simplifies the installation process for different operating systems. ```bash sqlpkg install orbitinghail/graft ``` ```powershell sqlpkg.exe install orbitinghail/graft ``` -------------------------------- ### Runtime Setup: Graft Initialization (Rust) Source: https://context7.com/orbitinghail/graft/llms.txt Shows how to initialize the Graft runtime with different storage backend configurations in Rust. This includes memory, S3-compatible, and filesystem backends, along with optional autosync settings. ```rust use graft::setup::{setup_graft, GraftConfig}; use graft::remote::RemoteConfig; use std::path::PathBuf; use std::num::NonZero; // Simple setup with memory backend let config = GraftConfig { remote: RemoteConfig::Memory, data_dir: PathBuf::from("/tmp/graft-data"), autosync: None, }; let runtime = setup_graft(config).unwrap(); // Setup with S3 backend and autosync let config = GraftConfig { remote: RemoteConfig::S3Compatible { bucket: "my-graft-bucket".to_string(), prefix: Some("volumes".to_string()), }, data_dir: PathBuf::from("/var/lib/graft"), autosync: Some(NonZero::new(60).unwrap()), // sync every 60 seconds }; let runtime = setup_graft(config).unwrap(); // Setup with filesystem backend let config = GraftConfig { remote: RemoteConfig::Fs { root: "/mnt/graft-remote".to_string(), }, data_dir: PathBuf::from("~/.local/share/graft"), autosync: None, }; let runtime = setup_graft(config).unwrap(); ``` -------------------------------- ### Load and Verify Graft Extension with SQLite CLI Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/direct.mdx Demonstrates how to load the Graft SQLite extension using the `.load` command in the SQLite CLI and verify its status with `pragma graft_status`. This example shows loading the extension, opening a Graft Volume, and checking the extension's status. ```sql .load PATH_TO_LIBGRAFT ``` ```console sqlite3 SQLite version 3.49.1 2025-02-18 13:38:58 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite> # load the Graft extension sqlite> .load ./libgraft_ext.so sqlite> # open a Graft Volume as a database sqlite> .open 'file:main?vfs=graft' sqlite> # verify Graft is working using pragma sqlite> pragma graft_status; +--------------------------------------------------+ | On tag main | +--------------------------------------------------+ | On tag main | | Local Log 74ggc1pWvh-3nSd2hKxw7rt7 is grafted to | | remote Log 74ggc1pWvh-3CGcHFP9HBTEK. | | | | The Volume is up to date with the remote. | +--------------------------------------------------+ ``` -------------------------------- ### Find Graft SQLite Extension Path using sqlpkg Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/direct.mdx Locate the installed Graft SQLite extension path using the sqlpkg command-line tool. This is useful for manual loading or configuration. ```bash sqlpkg which orbitinghail/graft ``` ```powershell sqlpkg.exe which orbitinghail/graft ``` -------------------------------- ### Graft SQLite Pragmas Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/swift.mdx Example SQL queries demonstrating the use of Graft's custom SQLite pragmas for managing distributed databases. ```sql PRAGMA graft_new; -- Create a new volume PRAGMA graft_info; -- Get volume information PRAGMA graft_push; -- Push local changes to remote PRAGMA graft_pull; -- Pull and merge remote changes ``` -------------------------------- ### SQL Example: Optimistic Snapshot Isolation Scenario Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/concepts/consistency.mdx Demonstrates a scenario where a client experiences optimistic snapshot isolation. This occurs when a client commits locally, but a concurrent commit from another client is accepted by the server, causing the first client's commit to be rejected upon synchronization. The example shows how replaying the transaction can lead to constraint violations after the server's accepted state is applied. ```sql create table accounts (id, bal, check(bal >= 0)); insert into accounts values (1, 10); ``` ```sql sqlite> update accounts set bal = bal - 10 where id = 1 -- accounts = [ { id: 1, bal: 0 } ] ``` ```sql sqlite> update accounts set bal = bal - 5 where id = 1 -- accounts = [ { id: 1, bal: 5 } ] ``` ```sql sqlite> select * from accounts +----+-----+ | id | bal | +----+-----+ | 1 | 5 | +----+-----+ ``` ```sql sqlite> update accounts set bal = bal - 5 where id = 1 (275) abort at 13 in [update accounts set bal = bal - 5 where id = 1;]: CHECK constraint failed: bal >= 0 Runtime error: CHECK constraint failed: bal >= 0 (19) ``` -------------------------------- ### Graft Log Operations (Fetch and Get Commit) Source: https://context7.com/orbitinghail/graft/llms.txt Demonstrates how to interact with Graft logs, including fetching log data up to a specific LSN or fetching the entire log. It also shows how to retrieve a specific commit from a log. ```rust use graft::core::{LogId, lsn::LSN}; use graft::core::commit::Commit; // Fetch log up to specific LSN let log_id = LogId::random(); runtime.fetch_log(log_id.clone(), Some(LSN::new(100))).unwrap(); // Fetch entire log runtime.fetch_log(log_id.clone(), None).unwrap(); // Get specific commit if let Some(commit) = runtime.get_commit(&log_id, LSN::new(10)).unwrap() { println!("Found commit at LSN 10"); } ``` -------------------------------- ### Install sqlite-graft Package Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/python.md Installs the necessary Python package for the Graft SQLite extension. This command uses pip, the standard package installer for Python. Ensure you have Python and pip installed. ```bash pip install sqlite-graft ``` -------------------------------- ### Get Latest Version with Git Describe Source: https://github.com/orbitinghail/graft/blob/main/RELEASE.md Retrieves the most recent tag (version) from the Git history. This is a crucial first step in determining the current release version. ```bash git describe --tags --abbrev=0 ``` -------------------------------- ### Python Usage of Graft SQLite Extension Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/python.md Demonstrates loading and using the Graft SQLite extension in Python. It initializes an in-memory SQLite database, loads the Graft extension, and then connects to a Graft volume. The example verifies the connection by executing a pragma command. ```python import sqlite3 import sqlite_graft # load graft using a temporary (empty) in-memory SQLite database db = sqlite3.connect(":memory:") db.enable_load_extension(True) sqlite_graft.load(db) # open a Graft volume as a database db = sqlite3.connect(f"file:main?vfs=graft", autocommit=True, uri=True) # use pragma to verify graft is working result = db.execute("pragma graft_status") print(result.fetchall()[0][0]) ``` -------------------------------- ### Graft Error Handling for Volume Operations Source: https://context7.com/orbitinghail/graft/llms.txt Provides an example of how to handle errors that may occur during volume operations in Graft. It specifically checks for the `VolumeNotFound` logical error and other potential Graft errors. ```rust use graft::{GraftErr, LogicalErr}; // Handle volume operations match runtime.volume_get(&volume.vid) { Ok(vol) => println!("Volume found"), Err(GraftErr::Logical(LogicalErr::VolumeNotFound)) => { println!("Volume does not exist"); } Err(e) => println!("Other error: {}", e), } ``` -------------------------------- ### Get Graft Volume Snapshot Description Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/pragmas.md The `pragma graft_snapshot` command returns a compressed description of the current connection's snapshot. This description details the structure of the snapshot, potentially spanning multiple logs. ```sql pragma graft_snapshot; ``` -------------------------------- ### Manage Volume Tags with Graft Rust API Source: https://context7.com/orbitinghail/graft/llms.txt Details how to use the Graft Rust API for tagging volumes, allowing human-readable names to be associated with volume IDs. Supports replacing, getting, checking existence, iterating, and deleting tags. ```rust // Associate a human-readable name with a volume runtime.tag_replace("production-db", volume.vid.clone()).unwrap(); // Get volume by tag let vid = runtime.tag_get("production-db").unwrap(); if let Some(vid) = vid { println!("Production DB: {}", vid.serialize()); } // Check if tag exists if runtime.tag_exists("production-db").unwrap() { println!("Tag exists"); } // List all tags for tag in runtime.tag_iter() { let (name, vid) = tag.unwrap(); println!("Tag: {} -> {}", name, vid.serialize()); } // Delete tag runtime.tag_delete("production-db").unwrap(); ``` -------------------------------- ### Create New Graft Volume and Update Tag Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/databases.md This demonstrates the use of `graft_new` to create a new, empty Graft Volume. It also updates the current tag to point to this new volume. Existing tags pointing to other volumes remain unaffected. This is useful for starting a new, independent database managed by Graft. ```sql sqlite> pragma graft_new; +-----------------------------------------------------------------------------------------------------------------------------+ | Switched to Volume 5rMJkfMogd-3bVjH8fwwX44x with local Log 74ggc1o8bK-34EknTZT8mjSx and remote Log 74ggc1o8bK-3LdWJvxfi3sPr | +-----------------------------------------------------------------------------------------------------------------------------+ | Switched to Volume 5rMJkfMogd-3bVjH8fwwX44x with local Log 74ggc1o8bK-34EknTZT8mjSx and remote Log 74ggc1o8bK-3LdWJvxfi3sPr | +-----------------------------------------------------------------------------------------------------------------------------+ ``` -------------------------------- ### Perform Snapshot Operations with Graft Rust API Source: https://context7.com/orbitinghail/graft/llms.txt Details various snapshot operations using the Graft Rust API. This includes getting a volume snapshot, checking if it's the latest, retrieving its checksum, finding missing pages, hydrating the snapshot, and creating new volumes from log references or existing snapshots. ```rust use graft::snapshot::Snapshot; use graft::core::logref::LogRef; // Get volume snapshot let snapshot = runtime.volume_snapshot(&volume.vid).unwrap(); // Check if snapshot is latest let is_latest = runtime.snapshot_is_latest(&volume.vid, &snapshot).unwrap(); // Get snapshot checksum let checksum = runtime.snapshot_checksum(&snapshot).unwrap(); println!("Checksum: {}", checksum); // Get missing pages let missing = runtime.snapshot_missing_pages(&snapshot).unwrap(); // Hydrate snapshot (fetch all missing data) runtime.snapshot_hydrate(snapshot.clone()).unwrap(); // Create volume from logref let logref = LogRef::new(LogId::random(), graft::core::lsn::LSN::new(5)); if let Some(vol) = runtime.volume_from_logref(logref).unwrap() { println!("Created volume from logref: {}", vol.vid.serialize()); } // Create volume from snapshot let new_volume = runtime.volume_from_snapshot(&snapshot).unwrap(); ``` -------------------------------- ### Prepare and Execute Release with Just Source: https://github.com/orbitinghail/graft/blob/main/RELEASE.md Executes the release process using the 'just' command. This command prepares the release, creates a release branch, and pushes it to the repository. Requires a version number as an argument. ```bash just run release --execute ``` -------------------------------- ### Open SQLite Database with Graft VFS SQL Source: https://context7.com/orbitinghail/graft/llms.txt Demonstrates how to open an in-memory SQLite database and configure it to use the Graft VFS. It shows how to open the database, switch to specific volumes and logs using `graft_switch`, and create new random volumes with `graft_new`. ```sql -- Open in-memory database with Graft VFS .open "file:main?vfs=graft" -- Switch to specific volume and logs pragma graft_switch = "5rMJkf2zHE-2xMqqKcN8RLZh:74ggc1B6R4-2kkvcy9fi4CHJ:74ggc1B6jg-2udz14pbDayZC"; -- Create new random volume pragma graft_new; ``` -------------------------------- ### Synchronize Graft Volumes with SQLite Pragmas Source: https://context7.com/orbitinghail/graft/llms.txt Explains the SQLite pragmas used for synchronizing Graft volumes. These commands allow cloning from a remote log (`graft_clone`), fetching the latest remote changes (`graft_fetch`), pulling changes from a remote repository (`graft_pull`), pushing local changes (`graft_push`), forking the current volume (`graft_fork`), and checking out a specific log reference (`graft_checkout`). ```sql -- Clone from remote log pragma graft_clone = "74ggc1B6jg-2udz14pbDayZC"; -- Fetch latest remote changes pragma graft_fetch; -- Pull changes from remote pragma graft_pull; -- Push local changes to remote pragma graft_push; -- Fork current volume pragma graft_fork; -- Checkout specific logref pragma graft_checkout = "74ggc1B6jg-2udz14pbDayZC:10"; ``` -------------------------------- ### Manage Graft Clients and AWS S3 Source: https://github.com/orbitinghail/graft/blob/main/DEMO.md This section covers command-line operations for managing Graft clients and interacting with AWS S3. It includes removing client directories, configuring AWS credentials for Tigris, listing S3 buckets, and running the SQLite shell with specified clients and S3 configurations. These commands are essential for setting up and interacting with Graft's data storage and client management. ```bash rm -r .graft/clients useaws tigris aws s3 ls 's3://graft-primary/volumes/' just run sqlite shell --release --s3 graft-primary --client f1 just run sqlite shell --release --client f1 ``` -------------------------------- ### List and Switch Graft Volumes using SQL Pragmas Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/concepts/volumes.md This snippet demonstrates how to interact with Graft Volumes using SQL pragmas. 'pragma graft_tags;' lists all available volume tags, while 'pragma graft_switch = "main";' switches the current context to the volume tagged as 'main'. These commands are essential for managing multiple databases or data partitions within Graft. ```sql -- List all tags pragma graft_tags; -- Switch to a tagged Volume pragma graft_switch = "main"; ``` -------------------------------- ### SQLite Data Operations with Graft Pragmas Source: https://context7.com/orbitinghail/graft/llms.txt Demonstrates how to perform data operations, push changes, check status, audit, and hydrate data using Graft's SQL pragmas with SQLite. This includes table creation, data insertion, querying, and synchronization commands. ```sql -- Create and populate tables CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, email TEXT); INSERT INTO users VALUES(1, 'Alice', 'alice@example.com'); INSERT INTO users VALUES(2, 'Bob', 'bob@example.com'); -- Query data SELECT * FROM users; -- Push changes to remote pragma graft_push; -- Check status after push pragma graft_status; -- Audit volume (check for missing data) pragma graft_audit; -- Hydrate volume (fetch all missing data) pragma graft_hydrate; ``` -------------------------------- ### Open and Manage Volumes with Graft Rust API Source: https://context7.com/orbitinghail/graft/llms.txt Demonstrates how to open, create, check existence, retrieve, list, and delete volumes using the Graft Rust API. It covers both auto-generated and specific ID usage. ```rust use graft::core::{VolumeId, LogId}; // Open or create a volume with specific IDs let volume = runtime.volume_open( Some(VolumeId::random()), Some(LogId::random()), Some(LogId::random()), ).unwrap(); // Open with auto-generated IDs let volume = runtime.volume_open(None, None, None).unwrap(); // Check if volume exists if runtime.volume_exists(&volume.vid).unwrap() { println!("Volume exists"); } // Get existing volume let volume = runtime.volume_get(&volume.vid).unwrap(); // List all volumes for vol in runtime.volume_iter() { let v = vol.unwrap(); println!("Volume: {}, Local: {}, Remote: {}", v.vid.serialize(), v.local.serialize(), v.remote.serialize()); } // Delete volume (keeps logs intact) runtime.volume_delete(&volume.vid).unwrap(); ``` -------------------------------- ### Read Data Pages with Graft Rust API Source: https://context7.com/orbitinghail/graft/llms.txt Shows how to read data from volumes using the Graft Rust API. It covers obtaining a volume reader, checking the total page count, and reading a specific page by its index. Accessing the snapshot head is also demonstrated. ```rust use graft::core::{PageIdx, page::Page}; use graft::volume_reader::{VolumeRead, VolumeReader}; // Get a volume reader let reader = runtime.volume_reader(volume.vid.clone()).unwrap(); // Get page count let page_count = reader.page_count(); println!("Total pages: {}", page_count); // Read a specific page let page_idx = PageIdx::new(0); let page = reader.read_page(page_idx).unwrap(); // Access snapshot let snapshot = reader.snapshot(); println!("Snapshot head: {:?}", snapshot.head()); ``` -------------------------------- ### Get Graft Volume Information Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/pragmas.md The `pragma graft_info` command displays detailed information about the current volume, including its ID, Log IDs, sync status, snapshot details, and size. This is useful for understanding the current state of the database. ```sql pragma graft_info; ``` -------------------------------- ### Add Graft.swift to Xcode Project and Initialize Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/swift.mdx Steps to add Graft.swift via Swift Package Manager and initialize the library by configuring graft.toml and setting environment variables. This allows SQLite to use Graft pragmas for distributed database management. ```swift import Foundation import Graft // Configure Graft using graft.toml and environment variables let fm = FileManager.default let supportDir = try fm.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let configPath = supportDir.appendingPathComponent("graft.toml") let dataPath = supportDir.appendingPathComponent("graft-data") // Create a graft.toml configuration file // See https://graft.rs/docs/sqlite/config/ for all configuration options let config = """ data_dir = "(dataPath.path)" make_default = true [remote] type = "memory" // Use "fs" or "s3_compatible" for production """ try config.write(to: configPath, atomically: true, encoding: .utf8) // Set the GRAFT_CONFIG environment variable setenv("GRAFT_CONFIG", configPath.path, 1) // Initialize Graft - registers graft with SQLite try Graft.static_init() // Now you can use SQLite with Graft pragmas // See https://graft.rs/docs/sqlite/pragmas/ for available pragmas ``` -------------------------------- ### Graft Import and Export Pragmas Source: https://context7.com/orbitinghail/graft/llms.txt Shows how to import and export SQLite databases using Graft pragmas, and how to retrieve the Graft version. These commands are essential for database migration and version checking. ```sql -- Import database from file pragma graft_import = "/path/to/database.db"; -- Export database to file pragma graft_export = "/path/to/output.db"; -- Get Graft version pragma graft_version; ``` -------------------------------- ### Manage Graft Volumes with SQLite Pragmas Source: https://context7.com/orbitinghail/graft/llms.txt Provides an overview of SQLite pragmas for managing Graft volumes. This includes listing all available volumes (`graft_volumes`), listing all tags (`graft_tags`), retrieving general volume information (`graft_info`), checking volume status (`graft_status`), and inspecting snapshot details (`graft_snapshot`). ```sql -- List all volumes pragma graft_volumes; -- List all tags pragma graft_tags; -- Get volume info pragma graft_info; -- Get volume status pragma graft_status; -- Get snapshot info pragma graft_snapshot; ``` -------------------------------- ### Write Data Pages with Graft Rust API Source: https://context7.com/orbitinghail/graft/llms.txt Illustrates how to write data to volumes using the Graft Rust API. This includes obtaining a volume writer, writing a page, truncating the volume, and committing the changes. The process returns a reader for the committed state. ```rust use graft::core::{PageIdx, PageCount, page::Page}; use graft::volume_writer::{VolumeWrite, VolumeWriter}; // Get a volume writer let mut writer = runtime.volume_writer(volume.vid.clone()).unwrap(); // Write a page let page_idx = PageIdx::new(0); let page = Page::from_bytes(&[0u8; 4096]).unwrap(); writer.write_page(page_idx, page).unwrap(); // Truncate volume writer.soft_truncate(PageCount::from(100)).unwrap(); // Commit changes let reader = writer.commit().unwrap(); println!("Committed with {} pages", reader.page_count()); ``` -------------------------------- ### Graft Diagnostic Pragmas Source: https://context7.com/orbitinghail/graft/llms.txt Illustrates how to use Graft's diagnostic pragmas to dump SQLite headers and specific commits. These are useful for debugging and understanding the state of the Graft database. ```sql -- Dump SQLite header pragma graft_dump_header; -- Dump specific commit pragma graft_dump_commit = "74ggc1B6jg-2udz14pbDayZC:10"; ``` -------------------------------- ### Graft SQL Basic Account Operations Source: https://github.com/orbitinghail/graft/blob/main/DEMO.md This set of SQL queries demonstrates basic account and ledger operations within the Graft database. It includes functions to sum account balances, record transfers between accounts by inserting into the ledger, and retrieve specific account balances. These queries are useful for managing financial data and auditing transactions. ```sql -- get the total balance of all accounts SELECT SUM(balance) FROM accounts; -- transfer $10 from account 1 to account 2 INSERT INTO ledger (account_id, amount) VALUES (1, -10), (2, 10); -- get the balance of account 1 and 2 SELECT * FROM accounts WHERE id IN (1, 2); ``` -------------------------------- ### Synchronize Volumes with Graft Rust API Source: https://context7.com/orbitinghail/graft/llms.txt Explains how to synchronize volume data using the Graft Rust API. Includes functions for pulling remote changes, pushing local changes, checking volume status, and fetching log updates. ```rust use graft::core::VolumeId; // Pull changes from remote runtime.volume_pull(volume.vid.clone()).unwrap(); // Push local changes to remote runtime.volume_push(volume.vid.clone()).unwrap(); // Get volume status let status = runtime.volume_status(&volume.vid).unwrap(); println!("Local status: {:?}", status.local_status); println!("Remote status: {:?}", status.remote_status); // Fetch log changes runtime.fetch_log(volume.remote.clone(), None).unwrap(); ``` -------------------------------- ### Graft Filesystem Storage Configuration Source: https://context7.com/orbitinghail/graft/llms.txt Sets up Graft to use a local filesystem as the remote storage backend. This involves specifying the root directory for storing Graft data. ```rust use graft::remote::RemoteConfig; // Filesystem backend let remote_config = RemoteConfig::Fs { root: "/mnt/shared/graft".to_string(), }; let remote = remote_config.build().unwrap(); ``` -------------------------------- ### Load Graft SQLite Extension in JavaScript Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/usage/javascript.mdx Demonstrates how to load the Graft SQLite extension using Node.js's built-in SQLite module. Requires Node.js version 23.10.0 or later due to its support for URI-formatted database connections. The code initializes a SQLite database, loads the Graft extension, and then opens a Graft volume to check its status. ```javascript import * as sqliteGraft from "sqlite-graft"; // Also should work with other javascript SQLite libraries: import { DatabaseSync } from "node:sqlite"; // load the graft extension let db = new DatabaseSync(":memory:", { allowExtension: true }); sqliteGraft.load(db); // open a Graft volume as a database and run graft_status db = new DatabaseSync("file:main?vfs=graft"); let result = db.prepare("PRAGMA graft_status"); console.log(result.all()); ``` -------------------------------- ### Graft Error Handling for Remote Operations Source: https://context7.com/orbitinghail/graft/llms.txt Illustrates how to handle remote-specific errors in Graft, such as `NotFound` or `PreconditionFailed`. This allows for granular error checking during remote data interactions. ```rust use graft::remote::RemoteErr; // Handle remote errors // Check for specific remote error types match result { Err(GraftErr::Remote(e)) if e.is_not_found() => { println!("Remote object not found"); } Err(GraftErr::Remote(e)) if e.precondition_failed() => { println!("Conditional write failed"); } _ => {} } ``` -------------------------------- ### Connect to SQLite Database by Tag using Graft VFS Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/databases.md This code snippet demonstrates how to open an SQLite database using the Graft VFS (Virtual File System). The database name is used as the tag. This method allows direct interaction with a tagged database managed by Graft. No specific inputs are required beyond the tag name. ```sql -- open the tag "main" using graft .open 'file:main?vfs=graft' ``` -------------------------------- ### Recommended SQLite Settings for Graft Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/compatibility.md These are the recommended SQLite PRAGMA settings when using Graft as the storage engine. Primarily, `journal_mode` should be set to `MEMORY` for efficiency, as Graft provides its own journaling and durability. Other settings like `synchronous`, `locking_mode`, `cache_size`, and `temp_store` are generally left at their defaults. ```sql PRAGMA journal_mode = MEMORY; PRAGMA synchronous = NORMAL; PRAGMA locking_mode = NORMAL; PRAGMA cache_size = -2000; PRAGMA temp_store = DEFAULT; ``` -------------------------------- ### Core Data Structures: Volume (Rust) Source: https://context7.com/orbitinghail/graft/llms.txt Illustrates the creation and manipulation of Graft's Volume structure in Rust. Volumes manage synchronization between local and remote logs, tracking LSN (Log Sequence Number) for consistency. ```rust use graft::volume::{Volume, SyncPoint, PendingCommit}; use graft::core::{VolumeId, LogId, lsn::LSN}; // Create a new volume let volume = Volume::new( VolumeId::random(), LogId::random(), // local log LogId::random(), // remote log None, // no sync point yet None, // no pending commit ); // Create with sync point let sync = SyncPoint { remote: LSN::new(10), local_watermark: Some(LSN::new(5)), }; let volume = volume.with_sync(Some(sync)); // Check for changes let local_changes = volume.local_changes(Some(LSN::new(8))); let remote_changes = volume.remote_changes(Some(LSN::new(15))); // Get volume status let status = volume.status(Some(LSN::new(8)), Some(LSN::new(15))); ``` -------------------------------- ### Graft SQL Volume Operations Source: https://github.com/orbitinghail/graft/blob/main/DEMO.md These SQL pragmas are used within the Graft environment to manage data volumes. They allow cloning volumes from S3 or the file system, pulling and pushing data, and retrieving information about the Graft instance's status and audit logs. These commands are fundamental for data synchronization and management in Graft. ```sql -- volume on s3 pragma graft_clone = '5rMJiBh2yA-2duiKapixrzr5'; -- volume on fs pragma graft_clone = '5rMJiK5qTN-2e3L1vhNrUdbg'; pragma graft_pull; pragma graft_push; pragma graft_info; pragma graft_status; pragma graft_audit; ``` -------------------------------- ### Clone Graft Volume from Remote Log ID Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/databases.md This command, `pragma graft_clone`, is used to create a new local Graft Volume based on a specified remote Log ID. After cloning, `pragma graft_pull` can be used to fetch any updates from the remote log. This is useful for creating a local copy of a remote database. ```sql pragma graft_clone = "74ggc1X5BE-3A7QEtHWMomvb"; +--------------------------------------------------------------------------------------+ | Created new Volume 5rMJkfNQfi-3i6P4jPw9XWVh from remote Log 74ggc1X5BE-3A7QEtHWMomvb | +--------------------------------------------------------------------------------------+ | Created new Volume 5rMJkfNQfi-3i6P4jPw9XWVh from remote Log 74ggc1X5BE-3A7QEtHWMomvb | +--------------------------------------------------------------------------------------+ sqlite> pragma graft_pull; +-----------------------------------------------------------+ | Pulled LSNs ..=1 into remote Log 74ggc1X5BE-3A7QEtHWMomvb | +-----------------------------------------------------------+ | Pulled LSNs ..=1 into remote Log 74ggc1X5BE-3A7QEtHWMomvb | +-----------------------------------------------------------+ ``` -------------------------------- ### Create a New Graft Volume Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/pragmas.md The `pragma graft_new` command creates a new, empty volume with a randomly generated Volume ID and updates the current tag to point to this new volume. It serves as a shortcut for creating a fresh database. ```sql pragma graft_new; ``` -------------------------------- ### Graft In-Memory Storage Configuration Source: https://context7.com/orbitinghail/graft/llms.txt Configures Graft to use an in-memory storage backend. This is primarily useful for testing purposes as data is not persisted. ```rust use graft::remote::RemoteConfig; // In-memory backend (for testing) let remote_config = RemoteConfig::Memory; let remote = remote_config.build().unwrap(); ``` -------------------------------- ### Fork Current Graft Database Snapshot Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/databases.md This procedure forks the current Graft database snapshot into a new, independent volume. It requires the database to be fully hydrated first using `pragma graft_hydrate`. The resulting fork is a divergent copy, isolated from the original database. This is useful for creating experimental branches or independent copies of a database. ```sql -- First ensure all pages are downloaded pragma graft_hydrate; -- Then fork pragma graft_fork; +---------------------------------------------------------------+ | Forked current snapshot into Volume: 5rMJkfPC21-3AHxh6aeSWzCp | +---------------------------------------------------------------+ | Forked current snapshot into Volume: 5rMJkfPC21-3AHxh6aeSWzCp | +---------------------------------------------------------------+ ``` -------------------------------- ### Fetch Remote Graft Log Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/pragmas.md The `pragma graft_fetch` command fetches the remote log without applying any changes to the local volume, analogous to `git fetch`. This allows inspecting remote changes before merging. ```sql pragma graft_fetch; ``` -------------------------------- ### Switch Graft Volume Connection Source: https://github.com/orbitinghail/graft/blob/main/docs/src/content/docs/docs/sqlite/pragmas.md The `pragma graft_switch` command allows switching the current connection to a different volume using its Volume ID. It can optionally specify local and remote log IDs, creating them if they don't exist. The current tag is updated accordingly. ```sql -- Switch to a specific Volume by Volume ID pragma graft_switch = "5rMJkfqcEt-2ei3bXFrcteHv"; -- Switch to a Volume and specify its local and remote Log IDs pragma graft_switch="5rMJkf2zHE-2xMqqKcN8RLZh:74ggc1B6R4-2kkvcy9fi4CHJ:74ggc1B6jg-2udz14pbDayZC"; ```