0.10.1 • Published 2 days ago

electric-sql v0.10.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 days ago

License npm Tests

ElectricSQL Typescript Client

ElectricSQL is a local-first SQL system. It provides active-active cloud sync for embedded SQLite databases and a reactive programming model to bind components to live database queries.

The ElectricSQL Typescript Client is the main ElectricSQL client library for developing node, web and JavaScript-based mobile applications. It's designed to work with any SQLite driver or bindings, with convienience functions to integrate with the most popular ones, including the primary drivers for Expo, React Native, SQL.js and Node.js.

See the:

Install

Using yarn:

yarn add electric-sql

Or using npm:

npm install --save electric-sql

Usage

Instantiate and use your SQLite driver as normal and call electrify when opening a new database connection. For example using react-native-sqlite-storage:

import { electrify } from 'electric-sql/react-native'

// Import your SQLite driver
import SQLite from 'react-native-sqlite-storage'
SQLite.enablePromise(true)

// Import your app config and migrations
import config from '.electric/@config'

// Open an SQLite database connection
const original = await SQLite.openDatabase('example.db')

// ⚡ Electrify it
const db = electrify(original, config)

// Use as normal, e.g.:
db.transaction((tx) => tx.executeSql('SELECT 1'))

Using in the web browser

Electric uses SQL.js in the browser with absurd-sql for persistence. This runs in a web worker (which we also use to keep background replication off the main thread). As a result, the electrified db client provides an asynchronous version of a subset of the SQL.js driver interface.

First create a worker.js file that imports and starts an ElectricWorker process:

import { ElectricWorker } from 'electric-sql/browser'

ElectricWorker.start(self)

Then, in your main application:

import { initElectricSqlJs } from 'electric-sql/browser'

// Import your app config and migrations
import config from '.electric/@config'

// Start the background worker
const url = new URL('./worker.js', import.meta.url)
const worker = new Worker(url, { type: 'module' })

// Electrify the SQL.js / absurd-sql machinery
const SQL = await initElectricSqlJs(worker, {
  locateFile: (file) => `/${file}`,
})

// Open a named database connection
const db = await SQL.openDatabase('example.db', config)

This gives you persistent, local-first SQL with active-active replication in your web browser 🤯. Use the db client as normal, with the proviso that the methods are now async (they return promises rather than direct values).

See the Quickstart guide for more information.

Reactivity

Once electrified, you can bind live database queries to your reactive components, so they automatically update when data changes or comes in over the replication stream. For example:

import React from 'react'
import { Pressable, Text, View } from 'react-native'

import { useElectric, useElectricQuery } from 'electric-sql/react'

export const LiveComponent = () => {
  const db = useElectric()
  const { results } = useElectricQuery('SELECT value FROM items')

  const addItem = () => {
    sql.execute('INSERT INTO items VALUES(?)', [crypto.randomUUID()])
  }

  return (
    <View>
      {results.map((item, index) => (
        <Text key={index}>Item: {item.value}</Text>
      ))}

      <Pressable onPress={addItem}>
        <Text>Add</Text>
      </Pressable>
    </View>
  )
}

See the Reactivity guide for more information.

Issues

Please raise any bugs, issues and feature requests on GitHub Issues.

Contributing

See the Community Guidelines including the Guide to Contributing and Contributor License Agreement.

Support

We have an open community Discord. If you’re interested in the project, please come and say hello and let us know if you have any questions or need any help or support getting things running.

0.10.1

15 days ago

0.10.0

16 days ago

0.9.7-canary.0

1 month ago

0.9.6

1 month ago

0.9.5

1 month ago

0.9.4

2 months ago

0.9.3

2 months ago

0.9.2

3 months ago

0.9.1

3 months ago

0.9.0

3 months ago

0.8.2

5 months ago

0.8.1

5 months ago

0.8.0

5 months ago

0.7.1

6 months ago

0.7.0

6 months ago

0.6.3

7 months ago

0.6.2

7 months ago

0.5.3

8 months ago

0.6.4

7 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.5.0-next.6

8 months ago

0.5.0-next.5

9 months ago

0.5.0-next.8

8 months ago

0.5.0-next.7

8 months ago

0.5.0

8 months ago

0.5.2

8 months ago

0.5.1

8 months ago

0.5.0-next.2

9 months ago

0.5.0-next.1

9 months ago

0.5.0-next.0

10 months ago

0.5.0-next.4

9 months ago

0.5.0-next.3

9 months ago

0.4.3

1 year ago

0.4.2

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.10

1 year ago

0.3.9

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.3

1 year ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.1-0

2 years ago

0.1.0

2 years ago