Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom File Path to store database file for Android #933

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion CHANGELOG-Unreleased.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## Unreleased

- [Android] Provides the user with the ability to pass in a file path instead of the database name only and creates the *.db file under a 'databases' sub-folder under the provided path. Pattern for file path is *file:///data/user/0/com.mattermost.rnbeta/files/xxx.db*
### BREAKING CHANGES
- [LokiJS] `useWebWorker` and `useIncrementalIndexedDB` options are now required (previously, skipping them would only trigger a warning)

Expand Down
15 changes: 14 additions & 1 deletion native/android/src/main/java/com/nozbe/watermelondb/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ class Database(private val name: String, private val context: Context) {
if (name == ":memory:" || name.contains("mode=memory")) {
context.cacheDir.delete()
File(context.cacheDir, name).path
} else if (name.startsWith("/") || name.startsWith("file")) {
avinashlng1080 marked this conversation as resolved.
Show resolved Hide resolved
// Extracts the database name from the path
val dbName = name.substringAfterLast("/")

// Extracts the real path where the *.db file will be created
val truePath = name.substringAfterLast("file://").substringBeforeLast("/")

// Creates the directory
val fileObj = File(truePath, "databases")
fileObj.mkdir()


File("${truePath}/databases", dbName).path
} else
// On some systems there is some kind of lock on `/databases` folder ¯\_(ツ)_/¯
// On some systems there is some kind of lock on `/databases` folder ¯\_(ツ)_/¯
context.getDatabasePath("$name.db").path.replace("/databases", ""),
null)
}
Expand Down