Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Refactor configuration #26

Merged
merged 6 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SQLite.enablePromise(true)

// Open a database connection and electrify it.
SQLite.openDatabase('example.db')
.then(db => electrify(db))
.then(db => electrify(db, { app: "my-app", env: "prod", token: "token", migrations: [] }))
.then(db => { // Use as normal, e.g.:
db.transaction(tx => tx.executeSql('SELECT 1'))
})
Expand Down Expand Up @@ -78,7 +78,7 @@ const worker = new Worker(url, {type: "module"})
// Electrify the SQL.js / absurd-sql machinery and then open
// a persistent, named database.
initElectricSqlJs(worker, {locateFile: file => `/${file}`})
.then(SQL => SQL.openDatabase('example.db'))
.then(SQL => SQL.openDatabase('example.db', { app: "my-app", env: "prod", token: "token", migrations: [] }))
.then(db => db.exec('SELECT 1'))
```

Expand Down Expand Up @@ -112,7 +112,7 @@ export default const App = () => {

useEffect(() => {
SQLite.openDatabase('example.db')
.then(db => electrify(db))
.then(db => electrify(db, { app: "my-app", env: "prod", token: "token" }))
.then(db => setDb(db))
}, [])

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"test": "ava",
"pretest": "yarn build",
"prepublishOnly": "yarn test",
"lint": "eslint src --fix"
"lint": "eslint src --fix",
"prepare": "npm run build"
},
"ava": {
"timeout": "10m",
Expand Down
4 changes: 2 additions & 2 deletions src/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ export abstract class WorkerServer {
SQL?: any

worker: Worker
opts: ElectrifyOptions
opts?: ElectrifyOptions

_dbs: {
[key: DbName]: AnyWorkerThreadElectricDatabase
}

constructor(worker: Worker, opts: ElectrifyOptions) {
constructor(worker: Worker, opts?: ElectrifyOptions) {
this.worker = worker
this.opts = opts
this._dbs = {}
Expand Down
14 changes: 7 additions & 7 deletions src/drivers/absurd-sql/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { initBackend } from '@aphro/absurd-sql/dist/indexeddb-main-thread'

import { ServerMethod, WorkerClient } from '../../bridge/index'
import { ElectricNamespace } from '../../electric/index'
import { ElectricNamespace, ElectrifyOptions } from '../../electric/index'
import { MainThreadBridgeNotifier } from '../../notifiers/bridge'
import { proxyOriginal } from '../../proxy/original'
import { ElectricConfig } from '../../satellite/config'
import { DbName } from '../../util/types'

import { DatabaseAdapter } from './adapter'
Expand Down Expand Up @@ -33,7 +34,7 @@ export { resultToRows } from './result'
export { ElectricWorker } from './worker'

export interface SQL {
openDatabase(dbName: DbName): Promise<ElectrifiedDatabase>
openDatabase(dbName: DbName, config: ElectricConfig): Promise<ElectrifiedDatabase>
}

export const initElectricSqlJs = async (worker: Worker, locateOpts: LocateFileOpts = {}): Promise<SQL> => {
Expand All @@ -47,17 +48,16 @@ export const initElectricSqlJs = async (worker: Worker, locateOpts: LocateFileOp
}
await workerClient.request(init, locator.serialise())

// we remove opts type info here to implement target interface
const openDatabase = async (dbName: DbName, opts?: any): Promise<ElectrifiedDatabase> => {
const openDatabase = async (dbName: DbName, config: ElectricConfig, opts?: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
const open: ServerMethod = {
target: 'server',
name: 'open'
}
await workerClient.request(open, dbName)
await workerClient.request(open, dbName, config)

const db = new MainThreadDatabaseProxy(dbName, workerClient)
const adapter = opts && opts.adapter || new DatabaseAdapter(db)
const notifier = opts && opts.notifier || new MainThreadBridgeNotifier(dbName, workerClient)
const adapter = opts?.adapter || new DatabaseAdapter(db)
const notifier = opts?.notifier || new MainThreadBridgeNotifier(dbName, workerClient)
const namespace = new ElectricNamespace(adapter, notifier)

return proxyOriginal(db, {electric: namespace}) as ElectrifiedDatabase
Expand Down
15 changes: 8 additions & 7 deletions src/drivers/absurd-sql/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MockRegistry } from '../../satellite/mock'
import { DatabaseAdapter } from './adapter'
import { Config, Database, ElectricDatabase, QueryExecResult, Statement } from './database'
import { MockSocket } from '../../sockets/mock'
import { ElectricConfig } from '../../satellite/config'

interface TestData {
notifications: Notification[]
Expand Down Expand Up @@ -112,25 +113,25 @@ export class MockElectricWorker extends WorkerServer {
return true
}

async open(dbName: DbName): Promise<boolean> {
async open(dbName: DbName, config: ElectricConfig): Promise<boolean> {
if (!this.SQL) {
throw new RequestError(400, 'Must init before opening')
}

const opts = this.opts
const registry = opts.registry || new MockRegistry()
const registry = opts?.registry || new MockRegistry()

if (!(dbName in this._dbs)) {
const db = new MockDatabase(dbName)
const adapter = opts.adapter || new DatabaseAdapter(db)
const migrator = opts.migrator || new MockMigrator()
const notifier = opts.notifier || new MockNotifier(dbName)
const socket = opts.socket || new MockSocket()
const adapter = opts?.adapter || new DatabaseAdapter(db)
const migrator = opts?.migrator || new MockMigrator()
const notifier = opts?.notifier || new MockNotifier(dbName)
const socket = opts?.socket || new MockSocket()

const namespace = new ElectricNamespace(adapter, notifier)
this._dbs[dbName] = new ElectricDatabase(db, namespace, this.worker.user_defined_functions)

await registry.ensureStarted(dbName, adapter, migrator, notifier, socket, opts)
await registry.ensureStarted(dbName, adapter, migrator, notifier, socket, config)
}
else {
await registry.ensureAlreadyStarted(dbName)
Expand Down
17 changes: 9 additions & 8 deletions src/drivers/absurd-sql/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ElectricNamespace, ElectrifyOptions } from '../../electric/index'
import { BundleMigrator } from '../../migrators/bundle'
import { WorkerBridgeNotifier } from '../../notifiers/bridge'
import { globalRegistry } from '../../satellite/registry'
import { ElectricConfig } from '../../satellite/config'
import { DbName } from '../../util/types'

import { DatabaseAdapter } from './adapter'
Expand Down Expand Up @@ -36,13 +37,13 @@ export class ElectricWorker extends WorkerServer {
return true
}

async open(dbName: DbName): Promise<boolean> {
async open(dbName: DbName, config: ElectricConfig): Promise<boolean> {
if (this.SQL === undefined) {
throw new RequestError(400, 'Must init before opening')
}

const opts = this.opts
const registry = opts.registry || globalRegistry
const registry = opts?.registry || globalRegistry

if (!(dbName in this._dbs)) {
const SQL = this.SQL
Expand All @@ -57,15 +58,15 @@ export class ElectricWorker extends WorkerServer {
const db = new SQL.Database(path, {filename: true})
db.exec(`PRAGMA journal_mode=MEMORY; PRAGMA page_size=8192;`)

const adapter = opts.adapter || new DatabaseAdapter(db)
const migrator = opts.migrator || new BundleMigrator(adapter, opts.migrations)
const notifier = opts.notifier || new WorkerBridgeNotifier(dbName, this)
const socket = opts.socket || new WebSocketWeb()
const adapter = opts?.adapter || new DatabaseAdapter(db)
const migrator = opts?.migrator || new BundleMigrator(adapter, config.migrations)
const notifier = opts?.notifier || new WorkerBridgeNotifier(dbName, this)
const socket = opts?.socket || new WebSocketWeb()

const namespace = new ElectricNamespace(adapter, notifier)
this._dbs[dbName] = new ElectricDatabase(db, namespace, this.worker.user_defined_functions)

await registry.ensureStarted(dbName, adapter, migrator, notifier, socket, this.opts)
await registry.ensureStarted(dbName, adapter, migrator, notifier, socket, config)
}
else {
await registry.ensureAlreadyStarted(dbName)
Expand All @@ -76,7 +77,7 @@ export class ElectricWorker extends WorkerServer {

// Static entrypoint allows us to maintain a reference to the
// instance. Passing opts allows the user to configure.
static start(worker: Worker, opts: ElectrifyOptions): void {
static start(worker: Worker, opts?: ElectrifyOptions): void {
const ref = new ElectricWorker(worker, opts)

refs.push(ref)
Expand Down
15 changes: 8 additions & 7 deletions src/drivers/better-sqlite3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { BundleMigrator } from '../../migrators/bundle'
import { EventNotifier } from '../../notifiers/event'
import { globalRegistry } from '../../satellite/registry'
import { ElectricConfig } from '../../satellite/config'
import { WebSocketNode } from '../../sockets/node'
import { DbName } from '../../util/types'

Expand All @@ -18,18 +19,18 @@ import { Database, ElectricDatabase, ElectrifiedDatabase } from './database'
export { ElectricDatabase, DatabaseAdapter }
export type { Database, ElectrifiedDatabase }

export const electrify = async (db: Database, opts: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
export const electrify = async (db: Database, config: ElectricConfig, opts?: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
const dbName: DbName = db.name

const adapter = opts.adapter || new DatabaseAdapter(db)
const migrator = opts.migrator || new BundleMigrator(adapter, opts.migrations)
const notifier = opts.notifier || new EventNotifier(dbName)
const socket = opts.socket || new WebSocketNode()
const registry = opts.registry || globalRegistry
const adapter = opts?.adapter || new DatabaseAdapter(db)
const migrator = opts?.migrator || new BundleMigrator(adapter, config.migrations)
const notifier = opts?.notifier || new EventNotifier(dbName)
const socket = opts?.socket || new WebSocketNode()
const registry = opts?.registry || globalRegistry

const namespace = new ElectricNamespace(adapter, notifier)
const electric = new ElectricDatabase(db, namespace)

const electrified = await baseElectrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, opts)
const electrified = await baseElectrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, config)
return electrified as unknown as ElectrifiedDatabase
}
15 changes: 8 additions & 7 deletions src/drivers/cordova-sqlite-storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { BundleMigrator } from '../../migrators/bundle'
import { EventNotifier } from '../../notifiers/event'
import { globalRegistry } from '../../satellite/registry'
import { ElectricConfig } from '../../satellite/config'

import { DatabaseAdapter } from './adapter'
import { Database, ElectricDatabase, ElectrifiedDatabase } from './database'
Expand All @@ -20,18 +21,18 @@ import { MockSocket } from '../../sockets/mock'
export { DatabaseAdapter, ElectricDatabase }
export type { Database, ElectrifiedDatabase }

export const electrify = async (db: Database, opts: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
export const electrify = async (db: Database, config: ElectricConfig, opts?: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
const dbName: DbName = db.dbname

const adapter = opts.adapter || new DatabaseAdapter(db)
const migrator = opts.migrator || new BundleMigrator(adapter, opts.migrations)
const notifier = opts.notifier || new EventNotifier(dbName)
const socket = opts.socket || new MockSocket() // TODO
const registry = opts.registry || globalRegistry
const adapter = opts?.adapter || new DatabaseAdapter(db)
const migrator = opts?.migrator || new BundleMigrator(adapter, config.migrations)
const notifier = opts?.notifier || new EventNotifier(dbName)
const socket = opts?.socket || new MockSocket() // TODO
const registry = opts?.registry || globalRegistry

const namespace = new ElectricNamespace(adapter, notifier)
const electric = new ElectricDatabase(db, namespace)

const electrified = await baseElectrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, opts)
const electrified = await baseElectrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, config)
return electrified as unknown as ElectrifiedDatabase
}
21 changes: 11 additions & 10 deletions src/drivers/cordova-sqlite-storage/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { ElectricNamespace, electrify, ElectrifyOptions } from '../../electric/i

import { MockMigrator } from '../../migrators/mock'
import { Notifier } from '../../notifiers/index'
import { MockNotifier } from '../../notifiers/mock'
import { MockRegistry } from '../../satellite/mock'
import { MockNotifier } from '../../notifiers/mock'
import { ElectricConfig } from '../../satellite/config'
import { MockRegistry } from '../../satellite/mock'

import { DatabaseAdapter } from './adapter'
import { Database, ElectricDatabase, ElectrifiedDatabase } from './database'
Expand All @@ -16,20 +17,20 @@ import { MockSocket } from '../../sockets/mock'

type RetVal = Promise<[Database, Notifier, ElectrifiedDatabase]>

const testOpts = { config: {app: "app", replication: {address: "", port: 0}}}
const testConfig = {app: "app", env: "test", token: "token", replication: {address: "", port: 0}}

export const initTestable = async (dbName: DbName, opts: ElectrifyOptions = testOpts): RetVal => {
export const initTestable = async (dbName: DbName, config: ElectricConfig = testConfig, opts?: ElectrifyOptions): RetVal => {
const db = new MockDatabase(dbName)

const adapter = opts.adapter || new DatabaseAdapter(db)
const notifier = opts.notifier || new MockNotifier(dbName)
const migrator = opts.migrator || new MockMigrator()
const socket = opts.socket || new MockSocket()
const registry = opts.registry || new MockRegistry()
const adapter = opts?.adapter || new DatabaseAdapter(db)
const notifier = opts?.notifier || new MockNotifier(dbName)
const migrator = opts?.migrator || new MockMigrator()
const socket = opts?.socket || new MockSocket()
const registry = opts?.registry || new MockRegistry()

const namespace = new ElectricNamespace(adapter, notifier)
const electric = new ElectricDatabase(db, namespace)

const electrified = await electrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, opts)
const electrified = await electrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, config)
return [db, notifier, electrified as unknown as ElectrifiedDatabase]
}
15 changes: 8 additions & 7 deletions src/drivers/expo-sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { BundleMigrator } from '../../migrators/bundle'
import { EventNotifier } from '../../notifiers/event'
import { globalRegistry } from '../../satellite/registry'
import { ElectricConfig } from '../../satellite/config'

import { DatabaseAdapter } from './adapter'
import { Database, ElectricDatabase, ElectricWebSQLDatabase, ElectrifiedDatabase } from './database'
Expand All @@ -23,14 +24,14 @@ import 'fastestsmallesttextencoderdecoder'
export { DatabaseAdapter, ElectricDatabase, ElectricWebSQLDatabase }
export type { Database, ElectrifiedDatabase }

export const electrify = async (db: Database, opts: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
export const electrify = async (db: Database, config: ElectricConfig, opts?: ElectrifyOptions): Promise<ElectrifiedDatabase> => {
const dbName: DbName = db._name

const adapter = opts.adapter || new DatabaseAdapter(db)
const migrator = opts.migrator || new BundleMigrator(adapter, opts.migrations)
const notifier = opts.notifier || new EventNotifier(dbName)
const socket = opts.socket || new WebSocketReactNative()
const registry = opts.registry || globalRegistry
const adapter = opts?.adapter || new DatabaseAdapter(db)
const migrator = opts?.migrator || new BundleMigrator(adapter, config.migrations)
const notifier = opts?.notifier || new EventNotifier(dbName)
const socket = opts?.socket || new WebSocketReactNative()
const registry = opts?.registry || globalRegistry

const namespace = new ElectricNamespace(adapter, notifier)

Expand All @@ -42,6 +43,6 @@ export const electrify = async (db: Database, opts: ElectrifyOptions): Promise<E
electric = new ElectricDatabase(db, namespace)
}

const electrified = await baseElectrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, opts)
const electrified = await baseElectrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, config)
return electrified as unknown as ElectrifiedDatabase
}
19 changes: 10 additions & 9 deletions src/drivers/expo-sqlite/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ElectricNamespace, ElectrifyOptions, electrify } from '../../electric/i

import { MockMigrator } from '../../migrators/mock'
import { Notifier } from '../../notifiers/index'
import { MockNotifier } from '../../notifiers/mock'
import { MockNotifier} from '../../notifiers/mock'
import { ElectricConfig } from '../../satellite/config'
import { MockRegistry } from '../../satellite/mock'

import { DatabaseAdapter } from './adapter'
Expand All @@ -15,18 +16,18 @@ import { MockDatabase, MockWebSQLDatabase } from './mock'
import { MockSocket } from '../../sockets/mock'

type RetVal = Promise<[Database, Notifier, ElectrifiedDatabase]>
const testOpts = { config: {app: "app", replication: {address: "", port: 0}}}
const testConfig = {app: "app", env: "test", token: "token", replication: {address: "", port: 0}}

export const initTestable = async (dbName: DbName, useWebSQLDatabase: boolean = false, opts: ElectrifyOptions = testOpts): RetVal => {
export const initTestable = async (dbName: DbName, useWebSQLDatabase: boolean = false, config: ElectricConfig = testConfig, opts?: ElectrifyOptions): RetVal => {
const db = useWebSQLDatabase
? new MockWebSQLDatabase(dbName)
: new MockDatabase(dbName)

const adapter = opts.adapter || new DatabaseAdapter(db)
const migrator = opts.migrator || new MockMigrator()
const notifier = opts.notifier || new MockNotifier(dbName)
const socket = opts.socket || new MockSocket()
const registry = opts.registry || new MockRegistry()
const adapter = opts?.adapter || new DatabaseAdapter(db)
const migrator = opts?.migrator || new MockMigrator()
const notifier = opts?.notifier || new MockNotifier(dbName)
const socket = opts?.socket || new MockSocket()
const registry = opts?.registry || new MockRegistry()

const namespace = new ElectricNamespace(adapter, notifier)

Expand All @@ -38,6 +39,6 @@ export const initTestable = async (dbName: DbName, useWebSQLDatabase: boolean =
electric = new ElectricDatabase(db, namespace)
}

const electrified = await electrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, opts)
const electrified = await electrify(dbName, db, electric, adapter, migrator, notifier, socket, registry, config)
return [db, notifier, electrified as unknown as ElectrifiedDatabase]
}
Loading