@capgo/capacitor-data-storage-sqlite
CAPACITOR 6
Note from the Owner
This Plugin has been transfered to Capgo org after his original creator @jepiqueau decide to retire.
We will forever be thankful for the work he did.
Capacitor Data Storage SQlite Plugin is a custom Native Capacitor plugin providing a key-value permanent store for simple data of type string only to SQLite on IOS, Android and Electron platforms and to IndexDB for the Web platform.
Maintainer | GitHub | Social |
---|---|---|
Martin Donadieu | riderx | |
Quéau Jean Pierre | jepiqueau |
The plugin follows the guidelines from the Capacitor Team
,
meaning that it will not work in IE11 without additional JavaScript transformations, e.g. with Babel.
npm install --save @capgo/capacitor-data-storage-sqlite
npx cap sync
On iOS, no further steps are needed.
On Android, no further steps are needed.
On Web,
npm install --save localforage
npm install --save @capacitor-community/electron
npx cap add @capacitor-community/electron
Go to the Electron folder of your application
cd electron
npm install --save sqlite3
npm install --save-dev @types/sqlite3
npm run build
cd ..
npx cap sync @capacitor-community/electron
Then build YOUR_APPLICATION
npm run build
npx cap copy
npx cap copy @capacitor-community/electron
npx cap open ios
npx cap open android
npx cap open @capacitor-community/electron
ionic serve
No configuration required for this plugin
Name | Android | iOS | Electron | Web |
---|---|---|---|---|
openStore (non-encrypted DB) | ✅ | ✅ | ✅ | ✅ |
openStore (encrypted DB) | ✅ | ✅ | ❌ | ❌ |
closeStore | ✅ | ✅ | ✅ | ❌ |
isStoreOpen | ✅ | ✅ | ✅ | ❌ |
isStoreExists | ✅ | ✅ | ✅ | ❌ |
deleteStore | ✅ | ✅ | ✅ | ❌ |
setTable | ✅ | ✅ | ✅ | ✅ |
set | ✅ | ✅ | ✅ | ✅ |
get | ✅ | ✅ | ✅ | ✅ |
iskey | ✅ | ✅ | ✅ | ✅ |
keys | ✅ | ✅ | ✅ | ✅ |
values | ✅ | ✅ | ✅ | ✅ |
filtervalues | ✅ | ✅ | ✅ | ✅ |
keysvalues | ✅ | ✅ | ✅ | ✅ |
remove | ✅ | ✅ | ✅ | ✅ |
clear | ✅ | ✅ | ✅ | ✅ |
isTable | ✅ | ✅ | ✅ | ✅ |
tables | ✅ | ✅ | ✅ | ✅ |
deleteTable | ✅ | ✅ | ✅ | ❌ |
isJsonValid | ✅ | ✅ | ✅ | ✅ |
importFromJson | ✅ | ✅ | ✅ | ✅ |
exportToJson | ✅ | ✅ | ✅ | ✅ |
The IOS & Android code use SQLCipher allowing for database encryption.
The Android code is now based on androidx.sqlite
. The database is not closed anymore after each transaction for performance improvement.
You must manage the close
of the database before opening a new database.
The Web code use localforage
package to store the datastore in the Browser.
The Electron code use sqlite3
package
Thanks goes to these wonderful people (emoji key):
Jean Pierre Quéau 💻 |
Matthew Burke 📖 |
Kevin van Schaijk 💻 |
Andy Garbett 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!
Retirement message of @jepiqueau -->
I have been dedicated to developing and maintaining this plugin for many years since the inception of Ionic Capacitor. Now, at 73+ years old, and with my MacBook Pro becoming obsolete for running Capacitor 6 for iOS, I have made the decision to cease maintenance of the plugin. If anyone wishes to take ownership of this plugin, they are welcome to do so.
It has been a great honor to be part of this development journey alongside the developer community. I am grateful to see many of you following me on this path and incorporating the plugin into your applications. Your comments and suggestions have motivated me to continuously improve it.
I have made this decision due to several family-related troubles that require my full attention and time. Therefore, I will not be stepping back. Thank you to all of you for your support.
End <--
This tutorial will guide you through the process of using the @capgo/capacitor-data-storage-sqlite
package to implement a key-value permanent store for simple string data in your Ionic Capacitor app.
Before we start, make sure you have the following installed:
Open your terminal or command prompt and navigate to your project directory.
Run the following command to install the package:
npm install --save @capgo/capacitor-data-storage-sqlite
npx cap sync
npm install --save localforage
npm install --save @capacitor-community/electron
npx cap add @capacitor-community/electron
cd electron
npm install --save sqlite3
npm install --save-dev @types/sqlite3
npm run build
cd ..
npx cap sync @capacitor-community/electron
Now that we have installed the package, let's look at how to use it in your app.
First, import the plugin in your TypeScript file:
import { Capacitor } from '@capacitor/core';
import { CapacitorDataStorageSqlite, capDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
To start using the storage, you need to open a store:
async function openStore() {
const store = new CapacitorDataStorageSqlite(Capacitor.getPlatform());
await store.openStore({ database: "my_db", table: "my_table" });
return store;
}
To set a value in the store:
async function setValue(store, key: string, value: string) {
await store.set(key, value);
}
To retrieve a value from the store:
async function getValue(store, key: string) {
const result = await store.get(key);
return result.value;
}
To check if a key exists in the store:
async function isKeyExists(store, key: string) {
const result = await store.iskey(key);
return result.result;
}
To remove a key from the store:
async function removeKey(store, key: string) {
await store.remove(key);
}
To clear all data from the store:
async function clearStore(store) {
await store.clear();
}
When you're done using the store, it's good practice to close it:
async function closeStore(store) {
await store.closeStore();
}
Here's a complete example of how to use the plugin:
import { Capacitor } from '@capacitor/core';
import { CapacitorDataStorageSqlite, capDataStorageSqlite } from '@capgo/capacitor-data-storage-sqlite';
async function dataStorageExample() {
const store = new CapacitorDataStorageSqlite(Capacitor.getPlatform());
try {
// Open the store
await store.openStore({ database: "my_db", table: "my_table" });
// Set a value
await store.set("myKey", "Hello, Capacitor!");
// Get the value
const result = await store.get("myKey");
console.log("Value:", result.value);
// Check if key exists
const keyExists = await store.iskey("myKey");
console.log("Key exists:", keyExists.result);
// Remove the key
await store.remove("myKey");
// Clear the store
await store.clear();
} catch (error) {
console.error("Error:", error);
} finally {
// Close the store
await store.closeStore();
}
}
dataStorageExample();
You've now learned how to use the @capgo/capacitor-data-storage-sqlite
package to implement a key-value storage system in your Ionic Capacitor app. This plugin provides a simple way to store and retrieve string data across different platforms, including iOS, Android, Electron, and Web.
Remember to handle errors appropriately and close the store when you're done using it. For more advanced usage, including working with encrypted databases, multiple tables, and JSON import/export, refer to the plugin's full API documentation.
For more detailed information about the API and available options, refer to the package's README or documentation.