You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It is only 2.6mb gzipped.
36
+
PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It is only 3mb gzipped and has support for many Postgres extensions, including [pgvector](https://github.com/pgvector/pgvector).
37
37
38
38
```javascript
39
39
import { PGlite } from"@electric-sql/pglite";
@@ -47,23 +47,7 @@ It can be used as an ephemeral in-memory database, or with persistence either to
47
47
48
48
Unlike previous "Postgres in the browser" projects, PGlite does not use a Linux virtual machine - it is simply Postgres in WASM.
49
49
50
-
It is being developed at [ElectricSQL](http://electric-sql.com) in collaboration with [Neon](http://neon.tech). We will continue to build on this experiment with the aim of creating a fully capable lightweight WASM Postgres with support for extensions such as pgvector.
51
-
52
-
## Whats new in V0.1
53
-
54
-
Version 0.1 (up from 0.0.2) includes significant changes to the Postgres build - it's about 1/3 smaller at 2.6mb gzipped, and up to 2-3 times faster. We have also found a way to statically compile Postgres extensions into the build - the first of these is pl/pgsql with more coming soon.
55
-
56
-
Key changes in this release are:
57
-
58
-
- Support for [parameterised queries](#querytquery-string-params-any-options-queryoptions-promiseresultst)#39
59
-
- An interactive [transaction API](#transactiontcallback-tx-transaction--promiset)#39
60
-
- pl/pgsql support #48
61
-
- Additional [query options](#queryoptions)#51
62
-
- Run PGlite in a [Web Workers](#web-workers)#49
63
-
- Fix for running on Windows #54
64
-
- Fix for missing `pg_catalog` and `information_schema` tables and view #41
65
-
66
-
We have also [published some benchmarks](https://github.com/electric-sql/pglite/blob/main/packages/benchmark/README.md) in comparison to a WASM SQLite build, and both native Postgres and SQLite. While PGlite is currently a little slower than WASM SQLite we have plans for further optimisations, including OPFS support and removing some the the Emscripten options that can add overhead.
50
+
For full documentation and user guides see [pglite.dev](https://pglite.dev).
67
51
68
52
## Browser
69
53
@@ -116,201 +100,6 @@ or to persist to the filesystem:
116
100
constdb=newPGlite("./path/to/pgdata");
117
101
```
118
102
119
-
## Deno
120
-
121
-
To use the in-memory Postgres, create a file `server.ts`:
122
-
123
-
```typescript
124
-
import { PGlite } from"npm:@electric-sql/pglite";
125
-
126
-
Deno.serve(async (_request:Request) => {
127
-
const db =newPGlite();
128
-
const query =awaitdb.query("select 'Hello world' as message;");
129
-
130
-
returnnewResponse(JSON.stringify(query));
131
-
});
132
-
```
133
-
134
-
Then run the file with `deno run --allow-net --allow-read server.ts`.
A new pglite instance is created using the `new PGlite()` constructor.
143
-
144
-
##### `dataDir`
145
-
146
-
Path to the directory to store the Postgres database. You can provide a url scheme for various storage backends:
147
-
148
-
-`file://` or unprefixed: File system storage, available in Node and Bun.
149
-
-`idb://`: IndexedDB storage, available in the browser.
150
-
-`memory://`: In-memory ephemeral storage, available in all platforms.
151
-
152
-
##### `options`:
153
-
154
-
-`debug`: 1-5 - the Postgres debug level. Logs are sent to the console.
155
-
-`relaxedDurability`: boolean - under relaxed durability mode PGlite will not wait for flushes to storage to complete when using the indexedDB file system.
To start an interactive transaction pass a callback to the transaction method. It is passed a `Transaction` object which can be used to perform operations within the transaction.
The same as the main [`.exec` method](#execquery-string-promisearrayresults).
246
-
-`tx.rollback()`
247
-
Rollback and close the current transaction.
248
-
249
-
##### Example:
250
-
251
-
```ts
252
-
awaitpg.transaction(async (tx) => {
253
-
awaittx.query(
254
-
'INSERT INTO test (name) VALUES ('$1');',
255
-
[ 'test' ]
256
-
);
257
-
returnawaitts.query('SELECT * FROM test;');
258
-
});
259
-
```
260
-
261
-
#### `.close(): Promise<void>`
262
-
263
-
Close the database, ensuring it is shut down cleanly.
264
-
265
-
### Properties:
266
-
267
-
-`.ready`*boolean (read only)*: Whether the database is ready to accept queries.
268
-
-`.closed`*boolean (read only)*: Whether the database is closed and no longer accepting queries.
269
-
-`.waitReady`*Promise<void>*: Promise that resolves when the database is ready to use. Note that queries will wait for this if called before the database has fully initialised, and so it's not necessary to wait for it explicitly.
270
-
271
-
### Results<T> Objects:
272
-
273
-
Result objects have the following properties:
274
-
275
-
-`rows: Row<T>[]` - The rows retuned by the query
276
-
-`affectedRows?: number` - Count of the rows affected by the query. Note this is *not* the count of rows returned, it is the number or rows in the database changed by the query.
277
-
-`fields: { name: string; dataTypeID: number }[]` - Field name and Postgres data type ID for each field returned.
278
-
279
-
280
-
### Row<T> Objects:
281
-
282
-
Rows objects are a key / value mapping for each row returned by the query.
283
-
284
-
The `.query<T>()` method can take a TypeScript type describing the expected shape of the returned rows. *(Note: this is not validated at run time, the result only cast to the provided type)*
285
-
286
-
### Web Workers:
287
-
288
-
It's likely that you will want to run PGlite in a Web Worker so that it doesn't block the main thread. To aid in this we provide a `PGliteWorker` with the same API as the core `PGlite` but it runs Postgres in a dedicated Web Worker. To use, import from the `/worker` export:
*Work in progress: We plan to expand this API to allow sharing of the worker PGlite across browser tabs.*
303
-
304
-
## Extensions
305
-
306
-
PGlite supports the pl/pgsql procedural language extension, this is included and enabled by default.
307
-
308
-
In future we plan to support additional extensions, see the [roadmap](#roadmap).
309
-
310
-
## ORM support.
311
-
312
-
- Drizzle ORM supports PGlite, see [their docs here](https://orm.drizzle.team/docs/get-started-postgresql#pglite).
313
-
314
103
## How it works
315
104
316
105
PostgreSQL typically operates using a process forking model; whenever a client initiates a connection, a new process is forked to manage that connection. However, programs compiled with Emscripten - a C to WebAssembly (WASM) compiler - cannot fork new processes, and operates strictly in a single-process mode. As a result, PostgreSQL cannot be directly compiled to WASM for conventional operation.
@@ -321,48 +110,6 @@ Fortunately, PostgreSQL includes a "single user mode" primarily intended for com
321
110
322
111
- PGlite is single user/connection.
323
112
324
-
## Roadmap
325
-
326
-
PGlite is *Alpha* and under active development, the current roadmap is:
327
-
328
-
- CI builds [#19](https://github.com/electric-sql/pglite/issues/19)
Please use the [issues](https://github.com/electric-sql/pglite/issues/) in this main repository for filing issues related to either part of PGlite. Changes that affect both the TypeScript package and the Postgres source should be filed as two pull requests - one for each repository, and they should reference each other.
347
-
348
-
## Building
349
-
350
-
There are a couple of prerequisites:
351
-
352
-
- the Postgres build toolchain - https://www.postgresql.org/download/
353
-
- emscripten version 3.1.56 - https://emscripten.org/docs/getting_started/downloads.html
354
-
355
-
To build, checkout the repo, then:
356
-
357
-
```
358
-
git submodule update --init
359
-
cd ./pglite/packages/pglite
360
-
emsdk install 3.1.56
361
-
emsdk activate 3.1.56
362
-
pnpm install
363
-
pnpm build
364
-
```
365
-
366
113
## Acknowledgments
367
114
368
115
PGlite builds on the work of [Stas Kelvich](https://github.com/kelvich) of [Neon](https://neon.tech) in this [Postgres fork](https://github.com/electric-sql/postgres-wasm).
This package implements React hooks for [PGLite](https://pglite.dev/) on top of the [live query plugin](https://pglite.dev/docs/live-queries). Full documentation is available at [pglite.dev/docs/framework-hooks](https://pglite.dev/docs/framework-hooks#react).
4
+
5
+
To install:
6
+
7
+
```sh
8
+
npm install @electric-sql/pglite-react
9
+
```
10
+
11
+
The hooks this package provides are:
12
+
13
+
-[PGliteProvider](https://pglite.dev/docs/framework-hooks#pgliteprovider): A Provider component to pass a PGlite instance to all child components for use with the other hooks.
14
+
-[usePGlite](https://pglite.dev/docs/framework-hooks#usepglite): Retrieve the provided PGlite instance.
15
+
-[useLiveQuery](https://pglite.dev/docs/framework-hooks#uselivequery): Reactively re-render your component whenever the results of a live query change
16
+
-[useLiveIncrementalQuery](https://pglite.dev/docs/framework-hooks#useliveincrementalquery): Reactively re-render your component whenever the results of a live query change by offloading the diff to PGlite
A [sync plugin](https://pglite.dev/docs/sync) for [PGlite](https://pglite.dev/) using [ElectricSQL](https://electric-sql.com/). Full documentation is available at [pglite.dev/docs/sync](https://pglite.dev/docs/sync).
4
+
5
+
To install:
6
+
7
+
```sh
8
+
npm install @electric-sql/pglite-sync
9
+
```
10
+
11
+
Then add it to you PGlite instance and create any local tables needed:
0 commit comments