React Native Quick SQLite

A fast react-native SQLite library built using JSI

README

screenshot

    yarn add react-native-quick-sqlite
npx pod-install



Quick SQLite embeds the latest version of SQLite and provides a low-level JSI-backed API to execute SQL queries.

Performance metrics are intentionally not presented, anecdotic testimonies suggest anywhere between 2x and 5x speed improvement. On small queries you might not notice a difference with the old bridge but as you send large data to JS the speed increase is considerable.

Starting on version 8.0.0 only React-Native 0.71 onwards is supported. This is due to internal changes to React-Native artifacts. If you are on < 0.71 use the latest 7.x.x version.

TypeORM is officially supported, however, there is currently a parsing issue with React-Native 0.71 and its babel configuration and therefore it will not work, nothing wrong with this package, this is purely an issue on TypeORM.

API


  1. ```typescript
  2. import {open} from 'react-native-quick-sqlite'

  3. const db = open('myDb.sqlite')

  4. // The db object now contains the following methods:

  5. db = {
  6.   close: () => void,
  7.   delete: () => void,
  8.   attach: (dbNameToAttach: string, alias: string, location?: string) => void,
  9.   detach: (alias: string) => void,
  10.   transaction: (fn: (tx: Transaction) => void) => Promise<void>,
  11.   execute: (query: string, params?: any[]) => QueryResult,
  12.   executeAsync: (
  13.     query: string,
  14.     params?: any[]
  15.   ) => Promise<QueryResult>,
  16.   executeBatch: (commands: SQLBatchParams[]) => BatchQueryResult,
  17.   executeBatchAsync: (commands: SQLBatchParams[]) => Promise<BatchQueryResult>,
  18.   loadFile: (location: string) => FileLoadResult;,
  19.   loadFileAsync: (location: string) => Promise<FileLoadResult>
  20. }
  21. ```

Simple queries


The basic query is synchronous, it will block rendering on large operations, further below you will find async versions.

  1. ```typescript
  2. import { open } from 'react-native-quick-sqlite';

  3. try {
  4.   const db = open('myDb.sqlite');

  5.   let { rows } = db.execute('SELECT somevalue FROM sometable');

  6.   rows.forEach((row) => {
  7.     console.log(row);
  8.   });

  9.   let { rowsAffected } = await db.executeAsync(
  10.     'UPDATE sometable SET somecolumn = ? where somekey = ?',
  11.     [0, 1]
  12.   );

  13.   console.log(`Update affected ${rowsAffected} rows`);
  14. } catch (e) {
  15.   console.error('Something went wrong executing SQL commands:', e.message);
  16. }
  17. ```

Transactions


Throwing an error inside the callback will ROLLBACK the transaction.

If you want to execute a large set of commands as fast as possible you should use the executeBatch method, it wraps all the commands in a transaction and has less overhead.

  1. ```typescript
  2. await QuickSQLite.transaction('myDatabase', (tx) => {
  3.   const { status } = tx.execute(
  4.     'UPDATE sometable SET somecolumn = ? where somekey = ?',
  5.     [0, 1]
  6.   );

  7.   // offload from JS thread
  8.   await tx.executeAsync = tx.executeAsync(
  9.     'UPDATE sometable SET somecolumn = ? where somekey = ?',
  10.     [0, 1]
  11.   );

  12.   // Any uncatched error ROLLBACK transaction
  13.   throw new Error('Random Error!');

  14.   // You can manually commit or rollback
  15.   tx.commit();
  16.   // or
  17.   tx.rollback();
  18. });
  19. ```

Batch operation


Batch execution allows the transactional execution of a set of commands

  1. ```typescript
  2. const commands = [
  3.   ['CREATE TABLE TEST (id integer)'],
  4.   ['INSERT INTO TEST (id) VALUES (?)', [1]],
  5.   [('INSERT INTO TEST (id) VALUES (?)', [2])],
  6.   [('INSERT INTO TEST (id) VALUES (?)', [[3], [4], [5], [6]])],
  7. ];

  8. const res = QuickSQLite.executeSqlBatch('myDatabase', commands);

  9. console.log(`Batch affected ${result.rowsAffected} rows`);
  10. ```

Dynamic Column Metadata


In some scenarios, dynamic applications may need to get some metadata information about the returned result set.

This can be done by testing the returned data directly, but in some cases may not be enough, for example when data is stored outside
SQLite datatypes. When fetching data directly from tables or views linked to table columns, SQLite can identify the table declared types:

  1. ```typescript
  2. let { metadata } = QuickSQLite.executeSql(
  3.   'myDatabase',
  4.   'SELECT int_column_1, bol_column_2 FROM sometable'
  5. );

  6. metadata.forEach((column) => {
  7.   // Output:
  8.   // int_column_1 - INTEGER
  9.   // bol_column_2 - BOOLEAN
  10.   console.log(`${column.columnName} - ${column.columnDeclaredType}`);
  11. });
  12. ```

Async operations


You might have too much SQL to process and it will cause your application to freeze. There are async versions for some of the operations. This will offload the SQLite processing to a different thread.

  1. ```ts
  2. QuickSQLite.executeAsync(
  3.   'myDatabase',
  4.   'SELECT * FROM "User";',
  5.   []).then(({rows}) => {
  6.     console.log('users', rows);
  7.   })
  8. );
  9. ```

Attach or Detach other databases


SQLite supports attaching or detaching other database files into your main database connection through an alias.
You can do any operation you like on this attached database like JOIN results across tables in different schemas, or update data or objects.
These databases can have different configurations, like journal modes, and cache settings.

You can, at any moment, detach a database that you don't need anymore. You don't need to detach an attached database before closing your connection. Closing the main connection will detach any attached databases.

SQLite has a limit for attached databases: A default of 10, and a global max of 125

References: Attach - Detach

  1. ```ts
  2. QuickSQLite.attach('mainDatabase', 'statistics', 'stats', '../databases');

  3. const res = QuickSQLite.executeSql(
  4.   'mainDatabase',
  5.   'SELECT * FROM some_table_from_mainschema a INNER JOIN stats.some_table b on a.id_column = b.id_column'
  6. );

  7. // You can detach databases at any moment
  8. QuickSQLite.detach('mainDatabase', 'stats');
  9. if (!detachResult.status) {
  10.   // Database de-attached
  11. }
  12. ```

Loading SQL Dump Files


If you have a plain SQL file, you can load it directly, with low memory consumption.

  1. ```typescript
  2. const { rowsAffected, commands } = QuickSQLite.loadFile(
  3.   'myDatabase',
  4.   '/absolute/path/to/file.sql'
  5. );
  6. ```

Or use the async version which will load the file in another native thread

  1. ```typescript
  2. QuickSQLite.loadFileAsync('myDatabase', '/absolute/path/to/file.sql').then(
  3.   (res) => {
  4.     const { rowsAffected, commands } = res;
  5.   }
  6. );
  7. ```

Use built-in SQLite


On iOS you can use the embedded SQLite, when running pod-install add an environment flag:

  1. ```
  2. QUICK_SQLITE_USE_PHONE_VERSION=1 npx pod-install
  3. ```

On Android, it is not possible to link (using C++) the embedded SQLite. It is also a bad idea due to vendor changes, old android bugs, etc. Unfortunately, this means this library will add some megabytes to your app size.

TypeORM


This library is pretty barebones, you can write all your SQL queries manually but for any large application, an ORM is recommended.

You can use this library as a driver for TypeORM. However, there are some incompatibilities you need to take care of first.

Starting on Node14 all files that need to be accessed by third-party modules need to be explicitly declared, TypeORM does not export its package.json which is needed by Metro, we need to expose it and make those changes "permanent" by using patch-package:

  1. ```json
  2. // package.json stuff up here
  3. "exports": {
  4.     "./package.json": "./package.json", // ADD THIS
  5.     ".": {
  6.       "types": "./index.d.ts",
  7. // The rest of the package json here
  8. ```

After you have applied that change, do:

  1. ```sh
  2. yarn patch-package --exclude 'nothing' typeorm
  3. ```

Now every time you install your node_modules that line will be added.

Next, we need to trick TypeORM to resolve the dependency of react-native-sqlite-storage to react-native-quick-sqlite, on your babel.config.js add the following:

  1. ```js
  2. plugins: [
  3.   // w/e plugin you already have
  4.   ...,
  5.   [
  6.     'module-resolver',
  7.     {
  8.       alias: {
  9.         "react-native-sqlite-storage": "react-native-quick-sqlite"
  10.       },
  11.     },
  12.   ],
  13. ]
  14. ```

You will need to install the babel module-resolver plugin:

  1. ```sh
  2. yarn add babel-plugin-module-resolver
  3. ```

Finally, you will now be able to start the app without any metro/babel errors (you will also need to follow the instructions on how to setup TypeORM), now we can feed the driver into TypeORM:

  1. ```ts
  2. import { typeORMDriver } from 'react-native-quick-sqlite'

  3. datasource = new DataSource({
  4.   type: 'react-native',
  5.   database: 'typeormdb',
  6.   location: '.',
  7.   driver: typeORMDriver,
  8.   entities: [...],
  9.   synchronize: true,
  10. });
  11. ```

Loading existing DBs


The library creates/opens databases by appending the passed name plus, the documents directory on iOS and the files directory on Android, this differs from other SQL libraries (some place it in awww folder, some in androids databases folder, etc.).

If you have an existing database file you want to load you can navigate from these directories using dot notation. e.g. ../www/myDb.sqlite. Note that on iOS the file system is sand-boxed, so you cannot access files/directories outside your app bundle directories.

Alternatively, you can place/move your database file using one of the many react-native fs libraries.

Enable compile-time options


By specifying pre-processor flags, you can enable optional features like FTS5, Geopoly, etc.

iOS


Add a `post_install` block to your `/ios/Podfile` like so:

  1. ```ruby
  2. post_install do |installer|
  3.   installer.pods_project.targets.each do |target|
  4.     if target.name == "react-native-quick-sqlite" then
  5.       target.build_configurations.each do |config|
  6.         config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', '<SQLITE_FLAGS>']
  7.       end
  8.     end
  9.   end
  10. end
  11. ```

Replace the `` part with the flags you want to add.
For example, you could add SQLITE_ENABLE_FTS5=1 to GCC_PREPROCESSOR_DEFINITIONS to enable FTS5 in the iOS project.

Android


You can specify flags via `/android/gradle.properties` like so:

  1. ```
  2. quickSqliteFlags="<SQLITE_FLAGS>"
  3. ```

Oscar


react-native-quick-sqlite was originally created by Oscar Franco. Thanks Oscar!

License


MIT License.