Skip to content

Commit

Permalink
client: rename dbSchema to schema. (#235)
Browse files Browse the repository at this point in the history
This makes the initial instantiation syntax nicer:

```tsx
await electrify(conn, schema, config)
```

As opposed to:

```tsx
await electrify(conn, dbSchema, config)
```
  • Loading branch information
thruflo authored Jul 12, 2023
1 parent 3f305bf commit 23e6ff1
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions clients/typescript/test/client/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3348,5 +3348,5 @@ export const tableSchemas = {
>,
}

export const dbSchema = new DbSchema(tableSchemas, [])
export type Electric = ElectricClient<typeof dbSchema>
export const schema = new DbSchema(tableSchemas, [])
export type Electric = ElectricClient<typeof schema>
12 changes: 6 additions & 6 deletions clients/typescript/test/client/model/shapes.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import Log from 'loglevel'
import Database from 'better-sqlite3'
import { dbSchema } from '../generated'
import { schema } from '../generated'
import { DatabaseAdapter } from '../../../src/drivers/better-sqlite3'
import {
shapeManager,
Expand Down Expand Up @@ -51,7 +51,7 @@ const config = {
async function makeContext(t: ExecutionContext) {
const client = new MockSatelliteClient()
const adapter = new DatabaseAdapter(db)
const migrations = dbSchema.migrations
const migrations = schema.migrations
const migrator = new BundleMigrator(adapter, migrations)
const dbName = `.tmp/test-${randomValue()}.db`
const notifier = new MockNotifier(dbName)
Expand All @@ -67,7 +67,7 @@ async function makeContext(t: ExecutionContext) {

const ns = new ElectricNamespace(adapter, notifier)
shapeManager.init(satellite)
const electric = ElectricClient.create(dbSchema, ns)
const electric = ElectricClient.create(schema, ns)
const Post = electric.db.Post
const Items = electric.db.Items
const User = electric.db.User
Expand All @@ -91,15 +91,15 @@ async function makeContext(t: ExecutionContext) {
init()
}

type TableType<T extends keyof ElectricClient<typeof dbSchema>['db']> =
ElectricClient<typeof dbSchema>['db'][T]
type TableType<T extends keyof ElectricClient<typeof schema>['db']> =
ElectricClient<typeof schema>['db'][T]
type ContextType = {
dbName: string
db: typeof db
satellite: SatelliteProcess
client: MockSatelliteClient
runMigrations: () => Promise<number>
electric: ElectricClient<typeof dbSchema>
electric: ElectricClient<typeof schema>
Post: TableType<'Post'>
Items: TableType<'Items'>
User: TableType<'User'>
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/test/client/model/table.errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import Database from 'better-sqlite3'
import { electrify } from '../../../src/drivers/better-sqlite3'
import { dbSchema } from '../generated'
import { schema } from '../generated'
import { ZodError } from 'zod'
import { InvalidArgumentError } from '../../../src/client/validation/errors/invalidArgumentError'
import {
Expand All @@ -16,7 +16,7 @@ import {
*/

const db = new Database(':memory:')
const electric = await electrify(db, dbSchema, {
const electric = await electrify(db, schema, {
auth: {
token: 'test-token',
},
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/test/client/model/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
_NOT_UNIQUE_,
_RECORD_NOT_FOUND_,
} from '../../../src/client/validation/errors/messages'
import { dbSchema, Post } from '../generated'
import { schema, Post } from '../generated'
import {
shapeManager,
ShapeManagerMock,
} from '../../../src/client/model/shapes'

const db = new Database(':memory:')
const electric = await electrify(db, dbSchema, {
const electric = await electrify(db, schema, {
auth: {
token: 'test-token',
},
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/test/client/notifications.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import Database from 'better-sqlite3'
import { electrify } from '../../src/drivers/better-sqlite3'
import { dbSchema } from './generated'
import { schema } from './generated'
import { shapeManager, ShapeManagerMock } from '../../src/client/model/shapes'

// Use a mocked shape manager for these tests
Expand All @@ -16,7 +16,7 @@ const config = {
},
}

const { notifier, adapter, db } = await electrify(conn, dbSchema, config)
const { notifier, adapter, db } = await electrify(conn, schema, config)
await db.Items.sync() // sync the Items table

async function runAndCheckNotifications(f: () => Promise<void>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Database from 'better-sqlite3'

import { electrify } from '../../src/drivers/better-sqlite3'
import { dbSchema } from '../client/generated'
import { schema } from '../client/generated'

const config = {
auth: {
Expand All @@ -12,7 +12,7 @@ const config = {
const original = new Database('example.db')

// Electrify the DB and use the DAL to query the `Items` table
const { db } = await electrify(original, dbSchema, config)
const { db } = await electrify(original, schema, config)
await db.Items.findMany({
select: {
value: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { electrify } from '../../src/drivers/cordova-sqlite-storage'
import { dbSchema } from '../client/generated'
import { schema } from '../client/generated'

const config = {
auth: {
Expand All @@ -14,7 +14,7 @@ const opts = {

document.addEventListener('deviceready', () => {
window.sqlitePlugin.openDatabase(opts, async (original) => {
const { db } = await electrify(original, dbSchema, config)
const { db } = await electrify(original, schema, config)
await db.Items.findMany({
select: {
value: true,
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/test/example-typing-usage/expo-sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as SQLite from 'expo-sqlite'

import { electrify } from '../../src/drivers/expo-sqlite'
import { dbSchema } from '../client/generated'
import { schema } from '../client/generated'

const config = {
auth: {
Expand All @@ -11,7 +11,7 @@ const config = {

const original = SQLite.openDatabase('example.db')

const { db } = await electrify(original, dbSchema, config)
const { db } = await electrify(original, schema, config)
await db.Items.findMany({
select: {
value: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SQLite from 'react-native-sqlite-storage'
import { electrify } from '../../src/drivers/react-native-sqlite-storage'
import { dbSchema } from '../client/generated'
import { schema } from '../client/generated'

// You can use the driver with or without the promise API enabled.
// Either way, you need to pass in a flag to the `electrify` function
Expand All @@ -15,7 +15,7 @@ const config = {
}

const original = await SQLite.openDatabase({ name: 'example.db' })
const { db } = await electrify(original, dbSchema, promisesEnabled, config)
const { db } = await electrify(original, schema, promisesEnabled, config)

await db.Items.findMany({
select: {
Expand Down
10 changes: 5 additions & 5 deletions clients/typescript/test/frameworks/react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { QualifiedTablename } from '../../src/util/tablename'
import { useLiveQuery } from '../../src/frameworks/react/hooks'
import { makeElectricContext } from '../../src/frameworks/react/provider'
import { ElectricClient } from '../../src/client/model/client'
import { dbSchema, Electric } from '../client/generated'
import { schema, Electric } from '../client/generated'

const assert = (stmt: any, msg: string = 'Assertion failed.'): void => {
if (!stmt) {
Expand All @@ -35,7 +35,7 @@ test('useLiveQuery returns query results', async (t) => {
const adapter = new DatabaseAdapter(original, false)
const notifier = new MockNotifier('test.db')
const namespace = new ElectricNamespace(adapter, notifier)
const dal = ElectricClient.create(dbSchema, namespace)
const dal = ElectricClient.create(schema, namespace)

const query = 'select i from bars'
const liveQuery = dal.db.liveRaw({
Expand All @@ -58,7 +58,7 @@ test('useLiveQuery returns error when query errors', async (t) => {

const notifier = new MockNotifier('test.db')
const namespace = new ElectricNamespace(adapter, notifier)
const dal = ElectricClient.create(dbSchema, namespace)
const dal = ElectricClient.create(schema, namespace)

const wrapper: FC = ({ children }) => {
return <ElectricProvider db={dal}>{children}</ElectricProvider>
Expand All @@ -79,7 +79,7 @@ test('useLiveQuery re-runs query when data changes', async (t) => {
const adapter = new DatabaseAdapter(original, false)
const notifier = new MockNotifier('test.db')
const namespace = new ElectricNamespace(adapter, notifier)
const dal = ElectricClient.create(dbSchema, namespace)
const dal = ElectricClient.create(schema, namespace)

const query = 'select foo from bars'
const liveQuery = dal.db.liveRaw({
Expand Down Expand Up @@ -115,7 +115,7 @@ test('useLiveQuery re-runs query when *aliased* data changes', async (t) => {
const adapter = new DatabaseAdapter(original, false)
const notifier = new MockNotifier('test.db')
const namespace = new ElectricNamespace(adapter, notifier)
const dal = ElectricClient.create(dbSchema, namespace)
const dal = ElectricClient.create(schema, namespace)

await notifier.attach('baz.db', 'baz')

Expand Down
6 changes: 3 additions & 3 deletions e2e/satellite_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { authToken } from 'electric-sql/auth'
import { setLogLevel } from 'electric-sql/debug'
import { electrify } from 'electric-sql/node'
import { v4 as uuidv4 } from 'uuid'
import { dbSchema, Electric } from './generated/models'
import { schema, Electric } from './generated/models'

setLogLevel('DEBUG')

Expand All @@ -24,8 +24,8 @@ export const open_db = async (
}
}
console.log(`config: ${JSON.stringify(config)}`)
dbSchema.migrations = migrations
return await electrify(original, dbSchema, config)
schema.migrations = migrations
return await electrify(original, schema, config)
}

export const set_subscribers = (db: Electric) => {
Expand Down
4 changes: 2 additions & 2 deletions e2e/satellite_client/src/generated/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,5 +795,5 @@ export const tableSchemas = {
>,
}

export const dbSchema = new DbSchema(tableSchemas, migrations)
export type Electric = ElectricClient<typeof dbSchema>
export const schema = new DbSchema(tableSchemas, migrations)
export type Electric = ElectricClient<typeof schema>
2 changes: 1 addition & 1 deletion examples/web-wa-sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const Example = () => {
useEffect(() => {
const init = async () => {
const conn = await ElectricDatabase.init('electric.db', '')
const db = await electrify(conn, dbSchema, config)
const db = await electrify(conn, schema, config)
setElectric(db)
}
init()
Expand Down
4 changes: 2 additions & 2 deletions examples/web-wa-sqlite/src/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react'
import './Example.css'

import { dbSchema, Electric } from './generated/client'
import { schema, Electric } from './generated/client'
import { electrify, ElectricDatabase } from 'electric-sql/wa-sqlite'
import { makeElectricContext, useLiveQuery } from 'electric-sql/react'

Expand All @@ -19,7 +19,7 @@ export const Example = () => {
useEffect(() => {
const init = async () => {
const conn = await ElectricDatabase.init('electric.db', '')
const db = await electrify(conn, dbSchema, config)
const db = await electrify(conn, schema, config)
setElectric(db)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export function writeTableSchemas(
.blankLine()

writer
.writeLine('export const dbSchema = new DbSchema(tableSchemas, migrations)')
.writeLine('export type Electric = ElectricClient<typeof dbSchema>')
.writeLine('export const schema = new DbSchema(tableSchemas, migrations)')
.writeLine('export type Electric = ElectricClient<typeof schema>')
}

export function writeFieldNamesArray(
Expand Down

0 comments on commit 23e6ff1

Please sign in to comment.