Comments (10)
Hey @Neo-Ciber94
I've found the problem, it's due to React strict mode. Essentially PGlite is initiated twice for the same database, the first time it creates the indexeddb database to store the files, the second time it sees the database is there and so thinks it doesn't have to initiate it. However the first run hasn't completed and so the database doesn't exist.
I commented out the strict mode on this fork: https://stackblitz.com/edit/react-pglite-bug-rcynzw-cwupa2?file=src%2FApp.tsx
It's one of the occasions where you almost certainly don't want strict mode to init the postgres twice as it's such a cpu heavy operation, even for development. I would recommend explicitly only having a single instance of PGlite, example here (and below): https://stackblitz.com/edit/react-pglite-bug-rcynzw-bzsd5x?file=src%2FApp.tsx,src%2Fmain.tsx,src%2Findex.css
We should look at having a lock so that only one instance of the database can be opened at once with indexeddb and throw an error if it can't be acquired. I'm going to use this issue to track that.
import { PGlite } from '@electric-sql/pglite';
import { useState, useEffect } from 'react';
const DATA_DIR = 'idb://somedatabase2';
let client: PGlite;
export default function App() {
const [data, setData] = useState<unknown>('PENDING');
useEffect(() => {
(async () => {
client ??= new PGlite(DATA_DIR, { debug: 1 });
const result = await client.exec('SELECT NOW()');
setData(result);
})();
}, []);
return (
<div>
<h1>PGLite</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
from pglite.
Perhaps we could provide a PGlite.createOnce() function or similar, which would return the same instance no matter how many times it’s called.
from pglite.
Could you check if there is an indexeddb via the Chrome dev tools, if there is one delete it and refresh the app. It may be that the state of the database is broken, I'm suspicious that the initdb phase didn't complete when you first ran the code. Alternatively change the name of the database so that it creates a new one.
from pglite.
Thanks for the report, could you let me know what React framework and build tooling you are using?
the "use client"
is making me think you may be using Next.js and RSC?
Does this happen in all web browsers or only a specific one?
from pglite.
Thanks for the report, could you let me know what React framework and build tooling you are using?
the
"use client"
is making me think you may be using Next.js and RSC?Does this happen in all web browsers or only a specific one?
I test the code on NextJS, RemixJS and Vite+React, all 3 the same error.
I was using Chrome (Windows).
from pglite.
Removing or changing the name of the Indexeddb is not working for me either, is also happening in Firefox:
from pglite.
Not sure where the errors is comming from, is also happening on stackblitz:
https://stackblitz.com/edit/react-pglite-bug-rcynzw?file=src%2FApp.tsx
Be aware that this halts my browser.
from pglite.
@samwillis You are totally right, looks like useEffect
took other victim.
Ensuring the instance is created only once solve the issue for me.
I really appreciate your help, Thank you!
from pglite.
@thruflo i like that. Ideally we should use a weak map to store the references to previously open databases so they can be cleanly garbage collected without having to explicitly close it to remove any reference.
from pglite.
If a createOnce()
is added, would there be any use for the current syntax that allows multiple instances to be created?
Also is there anything to consider to support workers? Maybe we need a createOnceWithWorkers()
too? Even though I don't really know why you would ever need an instance on the main thread in the first place—unless there's a good reason it sounds like an antipattern to even allow it.
from pglite.
Related Issues (20)
- `affectedRows` not being returned in UPDATE `.query` method
- initdb failing to complete in Bun HOT 1
- Generic Extension Strategy HOT 6
- `import("module")).default` is undefined HOT 3
- Calling `execProtocol` when PGlite is not ready HOT 2
- Throw error when main db instance method used inside an interactive transaction HOT 1
- Options for WASM and shareDir bundle location
- Feature: Add foreign data wrapper HOT 1
- Attempting to change column type throws error HOT 3
- Able to create custom fs
- React native support
- OOB when select() with over about 1k response? HOT 2
- PGlite bindings for other runtimes? HOT 1
- missing WebSocket dependency for node environments HOT 2
- `PGlite.exec()` returns result objects with misplaced `affectedRows`
- "memory access out of bounds" or "null function or function signature mismatch" with plpgsql; but works in Postgres 13/14.5/15.1 HOT 4
- error performing snapshot: error: unrecognized node type: 67143218
- db enters invalid state, causing many subsequent errors and requiring a reload HOT 1
- Error: expected magic word 00 61 73 6d, found 3c 21 44 4f @+0 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pglite.