Skip to content

feat: Add dbDirectory parameter for custom database storage locations#111

Open
nightfall708 wants to merge 1 commit intopowersync-ja:mainfrom
nightfall708:feat/custom-db-directory
Open

feat: Add dbDirectory parameter for custom database storage locations#111
nightfall708 wants to merge 1 commit intopowersync-ja:mainfrom
nightfall708:feat/custom-db-directory

Conversation

@nightfall708
Copy link

Summary

  • Adds a dbDirectory parameter to PowerSyncDatabase() that allows specifying a custom directory path for the database file
  • Enables storing the database in a shared App Group container, which is essential for iOS apps that need to share data between the main app and extensions (e.g. Share Extensions, widgets)
  • The underlying Kotlin SDK already supports dbDirectory — this change threads it through the Swift API layer

Motivation

Currently, dbFilename only accepts a filename (not a path), and the database is always stored in the hardcoded Application Support directory. This makes it impossible to share the database between the main app and app extensions without going through a cloud round-trip.

Before: Share Extension writes to its own PowerSync database → syncs to cloud → main app syncs from cloud (slow, requires connectivity)

After: Both main app and Share Extension point to the same database file in the App Group container (instant local access)

Usage

let containerURL = FileManager.default.containerURL(
    forSecurityApplicationGroupIdentifier: "group.com.example.app"
)

let db = PowerSyncDatabase(
    schema: schema,
    dbFilename: "powersync.db",
    dbDirectory: containerURL?.path
)

Changes

File Change
PowerSyncDatabase.swift Add dbDirectory: String? = nil parameter to the public API
KotlinPowerSyncDatabaseImpl.swift Thread dbDirectory through to the Kotlin SDK; use it in deleteDatabase()
KotlinPowerSyncDatabaseImplTests.swift Add tests for custom directory creation, file location, and deletion
CHANGELOG.md Document the new feature

Test plan

  • Verify build compiles (swift build ✅)
  • Verify tests compile (swift build --build-tests ✅)
  • testCustomDbDirectory — verifies database is created in custom directory, not the default
  • testCustomDbDirectoryCloseWithDeleteDatabase — verifies all SQLite files (db, wal, shm) are cleaned up from custom directory
  • Existing tests continue to pass (default behavior unchanged when dbDirectory is nil)

…abase storage locations

This enables storing the database in a shared App Group container,
which is essential for iOS apps that need to share data between the
main app and extensions (e.g. Share Extensions, widgets).

The Kotlin SDK already supports a dbDirectory parameter - this change
threads it through the Swift API layer and ensures deleteDatabase()
respects the custom directory.

Usage:
```swift
let containerURL = FileManager.default.containerURL(
    forSecurityApplicationGroupIdentifier: "group.com.example.app"
)

let db = PowerSyncDatabase(
    schema: schema,
    dbFilename: "powersync.db",
    dbDirectory: containerURL?.path
)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments