Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Apr 16, 2024
1 parent 80fc6b6 commit 81c0732
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
4 changes: 0 additions & 4 deletions clients/typescript/src/drivers/pglite/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ export class DatabaseAdapter extends GenericDatabaseAdapter {
}

async _run(statement: Statement): Promise<RunResult> {
console.log('_run:', statement.sql)
const res = await this.db.query(statement.sql, statement.args)
console.log('res:', res)
return {
rowsAffected: res.affectedRows ?? 0,
}
}

async _query(statement: Statement): Promise<Row[]> {
console.log('_query:', statement.sql)
const ret = (await this.db.query<Row>(statement.sql, statement.args)).rows
console.log('ret:', ret)
return ret
}
}
20 changes: 10 additions & 10 deletions examples/web-pglite/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/web-pglite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"preview": "vite preview"
},
"dependencies": {
"@electric-sql/pglite": "^0.1.1",
"electric-sql": "file:../../clients/typescript/electric-sql-0.9.4.tgz",
"@electric-sql/pglite": "^0.1.3",
"electric-sql": "file:../../clients/typescript/electric-sql-0.9.6.tgz",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
20 changes: 15 additions & 5 deletions examples/web-pglite/src/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ import './Example.css'

const { ElectricProvider, useElectric } = makeElectricContext<Electric>()

// We use a global database instance to avoid reinitializing the database
// when the component re-renders under React strict mode.
let db: PGlite

export const Example = () => {
const [ electric, setElectric ] = useState<Electric>()

useEffect(() => {
let isMounted = true
let electric: Electric | undefined

const init = async () => {
const config = {
debug: import.meta.env.DEV,
debug: false,//import.meta.env.DEV,
url: import.meta.env.ELECTRIC_SERVICE
}

const { tabId } = uniqueTabId()
const scopedDbName = `idb://basic-${LIB_VERSION}-${tabId}.db`

const db = new PGlite(scopedDbName)
await db.waitReady;
await db.exec('SET search_path TO main, public;') // TODO: remove when schemas are fixed
const electric = await electrify(db, schema, config)
db ??= new PGlite(scopedDbName)
electric = await electrify(db, schema, config)
await electric.connect(authToken())

if (!isMounted) {
Expand All @@ -41,9 +44,16 @@ export const Example = () => {
setElectric(electric)
}

const cleanup = async () => {
if (electric) {
await electric.close()
}
}

init()

return () => {
cleanup()
isMounted = false
}
}, [])
Expand Down

0 comments on commit 81c0732

Please sign in to comment.