Skip to content

Swift

Follow these steps to add Graft.swift to your Xcode project using Swift Package Manager:

  1. Open your Xcode project.

  2. Go to File > Add Package Dependencies...

  3. Enter the repository URL for Graft.swift in the search bar (top right):

    https://github.com/orbitinghail/Graft.swift.git
  4. Select the version you want to use (usually the latest).

  5. Click “Add Package”.

  6. Add the Graft “Package Product” to your application target on the next screen.

  7. Import and initialize the library during application startup:

    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 is configured via a graft.toml file. Key configuration options:

  • data_dir: Where Graft stores local LSM storage data
  • remote: Configuration for remote object storage (memory, filesystem, or S3-compatible)
  • autosync: Background synchronization interval in seconds
  • make_default: Makes Graft VFS the default for new connections
  • log_file: Path to write verbose operation logs

See the configuration documentation for complete details.

Once initialized, you can use Graft’s custom SQLite pragmas to manage distributed databases:

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

See the pragmas documentation for all available commands.