-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
410 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
# What is PGlite | ||
# What is PGlite | ||
|
||
PGlite is a WASM Postgres build packaged into a TypeScript/JavaScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It's under 3mb gzipped, and has support for many [Postgres extensions](../extensions/), including [pgvector](../extensions/#pgvector). | ||
|
||
Unlike previous "Postgres in the browser" projects, PGlite does not use a Linux virtual machine - it is simply Postgres in WASM. | ||
|
||
It's being developed by [ElectricSQL](https://electric-sql.com/) for our use case of embedding into applications, either locally or at the edge, allowing users to sync a subset of their Postgres database. | ||
|
||
However, there are many more use cases for PGlite beyond it's use as an embedded application databases: | ||
|
||
- Unit and CI testing<br> | ||
PGlite is very fast to start and tare down, perfect for unit tests, you can a unique fresh Postgres for each test. | ||
|
||
- Local development<br> | ||
You can use PGlite as an alternative to a full local Postgres for local development, masivly simplifyinf your development environmant. | ||
|
||
- Remote development, or local web containers<br> | ||
As PGlite is so light weight it can be easily embedded into remote containerised development environments, or in-browser [web containers](https://webcontainers.io). | ||
|
||
- On-device or edge AI and RAG<br> | ||
PGlite has full support for [pgvector](../extensions/#pgvector), enabling a local or edge retrieval augmented generation (RAG) workflow. | ||
|
||
We are very keen to establish PGlite as an open source, and open contribution, project, working to build a community around it to develop its capabilities for all use cases. | ||
|
||
Getting started with PGlite is super easy, just install and import the NPM package, then create a your embded database: | ||
|
||
```js | ||
import { PGlite } from "@electric-sql/pglite"; | ||
|
||
const db = new PGlite(); | ||
await db.query("select 'Hello world' as message;"); | ||
// -> { rows: [ { message: "Hello world" } ] } | ||
``` | ||
|
||
It can be used as an ephemeral in-memory database, or with persistence either to the file system (Node/Bun) or indexedDB (Browser). | ||
|
||
Read more in our [getting started guide](./index.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,62 @@ | ||
# Extension Development | ||
# Extension Development | ||
|
||
PGlite has support for both Postgres extensions and it's own plugin api that allows a developer to augment a PGlite instance with an additional API. | ||
|
||
## Extension API | ||
|
||
::: warning | ||
The extension API is not yet stable and may change in a future release. | ||
::: | ||
|
||
PGlite extensions are an object with the following interface: | ||
|
||
```ts | ||
export interface Extension { | ||
name: string; | ||
setup: ExtensionSetup; | ||
} | ||
|
||
export type ExtensionSetup = ( | ||
pg: PGliteInterface, | ||
emscriptenOpts: any, | ||
clientOnly?: boolean, | ||
) => Promise<ExtensionSetupResult>; | ||
|
||
export interface ExtensionSetupResult { | ||
emscriptenOpts?: any; | ||
namespaceObj?: any; | ||
bundlePath?: URL; | ||
init?: () => Promise<void>; | ||
close?: () => Promise<void>; | ||
} | ||
``` | ||
|
||
`name` is the human readable name of the extension. | ||
|
||
`setup` is a function that receives the following parameters, and and returns a promise that resolves to an object conforming to `ExtensionSetupResult`: | ||
|
||
- `pg`<br> | ||
the [PGlite](../docs/api.md) instance that the enstension is being added to | ||
- `emscriptenOpts`<br> | ||
the options currently configured to pass to the [Emscrption Module factory](https://emscripten.org/docs/api_reference/module.html), including any [Emscript FS](https://emscripten.org/docs/api_reference/Filesystem-API.html). | ||
- `clientOnly`<br> | ||
A boolean indicating if this instace of the extension is "client only", meaning that it is on the main thread and doesnt have direct access to the underlying WASM. When true `emscriptenOpts` and `bundlePath` should not re returned as they will have no affect. | ||
|
||
The returned object has these properties - all are optional: | ||
|
||
- `emscriptenOpts`<br> | ||
Any augmented or altered configuration to pass to the [Emscrption Module factory](https://emscripten.org/docs/api_reference/module.html). | ||
- `namespaceObj`<br> | ||
An object to add as a namespace to the PGlite instance, this can provide access to additional methods or properties that your extension would like to expose. | ||
- `bundlePath`<br> | ||
The path to the Postgres extension tarball - see [Building Postgres Extensions](#building-postgres-extensions) | ||
- `init`<br> | ||
An initiation function that will be run after the PGlite instance and Postgres runtime has started, but before the instance is marked as ready for external usage. You can use this to perform any initiation your extension needs to perform on the database at startup. | ||
- `close`<br> | ||
A function that will be called when the user calls `close()` on their PGlite instance, this is called before the database has been shut down. | ||
|
||
An example of a PGlite extension that auments the PGlite instance is the [live query extension](../docs/live-queries.md). | ||
|
||
## Building Postgres Extensions | ||
|
||
We are still working on documentation, and examples, showing how to build Postgres extensions for use with PGlite. Please check back soon, or reach out on [Discord](https://discord.com/channels/933657521581858818/1212676471588520006) if you would like to try building a particular extension for PGlite. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>PGlite Dump Datadir Example</title> | ||
<link rel="stylesheet" href="./styles.css" /> | ||
<script src="./utils.js"></script> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@electric-sql/pglite": "../dist/index.js" | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1>PGlite Dump Datadir Example</h1> | ||
<div class="script-plus-log"> | ||
<script type="module" src="./dumpDataDir.js"></script> | ||
<div id="log"></div> | ||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.