Skip to content

Commit 0700c43

Browse files
committed
HTTP FS demo
1 parent c16a1ce commit 0700c43

19 files changed

+1179
-242
lines changed

demos/pagila-httpvfs/.gitignore

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
26+
public/pagila
27+
public/pagila.tar.gz

demos/pagila-httpvfs/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# PGlite + HttpFs Demo
2+
3+
This demo shows how to use PGlite, a WASM build of Postgres running entirely in the browser, with the WIP HttpFs to connect to a remote PGlite database. It's using HTTP range requests to fetch database file pages from the remote server on demand.
4+
5+
The database in this demo is the Pagila sample database.
6+
7+
## Development
8+
9+
Install the dependencies:
10+
11+
```
12+
pnpm install
13+
```
14+
15+
Build the database:
16+
17+
```
18+
pnpm make-database
19+
```
20+
21+
Start the dev server:
22+
23+
```
24+
pnpm dev
25+
```

demos/pagila-httpvfs/eslint.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import reactHooks from 'eslint-plugin-react-hooks'
4+
import reactRefresh from 'eslint-plugin-react-refresh'
5+
import tseslint from 'typescript-eslint'
6+
7+
export default tseslint.config(
8+
{ ignores: ['dist'] },
9+
{
10+
extends: [js.configs.recommended, ...tseslint.configs.recommended],
11+
files: ['**/*.{ts,tsx}'],
12+
languageOptions: {
13+
ecmaVersion: 2020,
14+
globals: globals.browser,
15+
},
16+
plugins: {
17+
'react-hooks': reactHooks,
18+
'react-refresh': reactRefresh,
19+
},
20+
rules: {
21+
...reactHooks.configs.recommended.rules,
22+
'react-refresh/only-export-components': [
23+
'warn',
24+
{ allowConstantExport: true },
25+
],
26+
},
27+
},
28+
)

demos/pagila-httpvfs/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/icon.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>PGlite + HttpFs Demo</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

demos/pagila-httpvfs/package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "pagila-httpvfs",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc -b && vite build",
9+
"lint": "eslint .",
10+
"preview": "vite preview",
11+
"make-database": "tsx scripts/make-database.ts"
12+
},
13+
"dependencies": {
14+
"@electric-sql/pglite": "workspace:*",
15+
"@electric-sql/pglite-react": "workspace:*",
16+
"@electric-sql/pglite-repl": "workspace:*",
17+
"react": "^18.3.1",
18+
"react-dom": "^18.3.1",
19+
"tar": "^7.4.3",
20+
"tsx": "^4.19.1"
21+
},
22+
"devDependencies": {
23+
"@eslint/js": "^9.9.0",
24+
"@types/node": "^20.11.18",
25+
"@types/react": "^18.3.3",
26+
"@types/react-dom": "^18.3.0",
27+
"@vitejs/plugin-react": "^4.3.1",
28+
"eslint": "^9.9.0",
29+
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
30+
"eslint-plugin-react-refresh": "^0.4.9",
31+
"globals": "^15.9.0",
32+
"typescript": "^5.5.3",
33+
"typescript-eslint": "^8.0.1",
34+
"vite": "^5.4.1"
35+
}
36+
}

demos/pagila-httpvfs/public/icon.svg

+4
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { PGlite } from '@electric-sql/pglite'
2+
import fs from 'node:fs/promises'
3+
import path from 'node:path'
4+
import { fileURLToPath } from 'node:url'
5+
import { extract as tarExtract } from 'tar'
6+
7+
const SCHEMA_URL =
8+
'https://raw.githubusercontent.com/devrimgunduz/pagila/refs/heads/master/pagila-schema.sql'
9+
const DATA_URL =
10+
'https://raw.githubusercontent.com/devrimgunduz/pagila/refs/heads/master/pagila-insert-data.sql'
11+
const THIS_DIR = path.dirname(fileURLToPath(import.meta.url))
12+
13+
// Download the schema and data from the internet
14+
console.log('Downloading schema...')
15+
const schema = await fetch(SCHEMA_URL).then((r) => r.text())
16+
console.log('Downloading data...')
17+
const data = await fetch(DATA_URL).then((r) => r.text())
18+
19+
// Create a new PGlite instance
20+
console.log('Creating database...')
21+
const pg = await PGlite.create()
22+
23+
// Initialize the schema
24+
console.log('Initializing database schema...')
25+
await pg.exec(schema)
26+
27+
// Split the data into lines and execute each line so as to not run out of memory
28+
console.log('Inserting database data...')
29+
const dataLines = data.split('\n').filter((line) => line.trim().length > 0 && !line.startsWith('--'))
30+
for (const line of dataLines) {
31+
try {
32+
await pg.exec(line)
33+
} catch (e) {
34+
console.error(line)
35+
console.error(e)
36+
process.exit(1)
37+
}
38+
}
39+
40+
console.log('Vacuuming database...')
41+
await pg.exec('VACUUM ANALYZE')
42+
await pg.exec('CHECKPOINT')
43+
44+
console.log('Dumping database...')
45+
const file = await pg.dumpDataDir()
46+
47+
console.log('Writing database...')
48+
await fs.writeFile(
49+
path.join(THIS_DIR, '..', 'public', 'pagila.tar.gz'),
50+
Buffer.from(await file.arrayBuffer()),
51+
)
52+
53+
console.log('Extracting database...')
54+
await fs.mkdir(path.join(THIS_DIR, '..', 'public', 'pagila'))
55+
await tarExtract({
56+
file: path.join(THIS_DIR, '..', 'public', 'pagila.tar.gz'),
57+
cwd: path.join(THIS_DIR, '..', 'public', 'pagila'),
58+
})
59+
60+
console.log('Done!')

demos/pagila-httpvfs/src/App.css

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 1rem;
5+
text-align: center;
6+
width: 100%;
7+
display: flex;
8+
flex-direction: column;
9+
align-items: center;
10+
}
11+
12+
.PGliteRepl-root {
13+
height: 60vh;
14+
width: 100%;
15+
text-align: left;
16+
}
17+
18+
.intro {
19+
max-width: 60rem;
20+
}

demos/pagila-httpvfs/src/App.tsx

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { PGliteWorker } from '@electric-sql/pglite/worker'
2+
import { Repl } from '@electric-sql/pglite-repl'
3+
import { useEffect, useState } from 'react'
4+
import PGWorker from './pglite-worker.js?worker'
5+
import './App.css'
6+
7+
let pgPromise: Promise<PGliteWorker>
8+
9+
function App() {
10+
pgPromise ??= PGliteWorker.create(
11+
new PGWorker({
12+
name: 'pglite-worker',
13+
}),
14+
)
15+
const [pg, setPg] = useState<PGliteWorker | null>(null)
16+
useEffect(() => {
17+
pgPromise.then(setPg)
18+
}, [])
19+
20+
return (
21+
<>
22+
<h1>
23+
<a href="https://pglite.dev">PGlite</a> +{' '}
24+
<a href="https://github.com/electric-sql/pglite/tree/main/packages/httpfs">
25+
HttpFs
26+
</a>
27+
</h1>
28+
<div className="intro">
29+
<p>
30+
This demo shows how to use <a href="https://pglite.dev">PGlite</a>, a
31+
WASM build of Postgres running entirely in the browser, with the WIP
32+
HttpFs to connect to a remote PGlite database. It's using HTTP range
33+
requests to fetch database file pages from the remote server on
34+
demand.
35+
</p>
36+
<p>
37+
The database in this demo is the{' '}
38+
<a href="https://github.com/devrimgunduz/pagila">Pagila</a> sample
39+
database.
40+
</p>
41+
<p>
42+
The REPL below supports the same <code>\d</code> commands as{' '}
43+
<code>psql</code>.
44+
</p>
45+
</div>
46+
{pg ? <Repl pg={pg} border={true} /> : <p>Loading...</p>}
47+
</>
48+
)
49+
}
50+
51+
export default App

demos/pagila-httpvfs/src/index.css

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
:root {
2+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3+
line-height: 1.5;
4+
font-weight: 400;
5+
font-size: 14px;
6+
7+
color-scheme: light dark;
8+
color: rgba(255, 255, 255, 0.87);
9+
background-color: #242424;
10+
11+
font-synthesis: none;
12+
text-rendering: optimizeLegibility;
13+
-webkit-font-smoothing: antialiased;
14+
-moz-osx-font-smoothing: grayscale;
15+
}
16+
17+
a {
18+
font-weight: 500;
19+
color: #646cff;
20+
text-decoration: inherit;
21+
}
22+
a:hover {
23+
color: #535bf2;
24+
}
25+
26+
body {
27+
margin: 0;
28+
display: flex;
29+
place-items: center;
30+
}
31+
32+
h1 {
33+
font-size: 1.5em;
34+
line-height: 1.1;
35+
font-weight: 600;
36+
}
37+
38+
h1 a {
39+
font-weight: 600;
40+
}
41+
42+
button {
43+
border-radius: 8px;
44+
border: 1px solid transparent;
45+
padding: 0.6em 1.2em;
46+
font-size: 1em;
47+
font-weight: 500;
48+
font-family: inherit;
49+
background-color: #1a1a1a;
50+
cursor: pointer;
51+
transition: border-color 0.25s;
52+
}
53+
button:hover {
54+
border-color: #646cff;
55+
}
56+
button:focus,
57+
button:focus-visible {
58+
outline: 4px auto -webkit-focus-ring-color;
59+
}
60+
61+
@media (prefers-color-scheme: light) {
62+
:root {
63+
color: #213547;
64+
background-color: #ffffff;
65+
}
66+
a:hover {
67+
color: #747bff;
68+
}
69+
button {
70+
background-color: #f9f9f9;
71+
}
72+
}

demos/pagila-httpvfs/src/main.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { StrictMode } from 'react'
2+
import { createRoot } from 'react-dom/client'
3+
import App from './App.tsx'
4+
import './index.css'
5+
6+
createRoot(document.getElementById('root')!).render(
7+
<StrictMode>
8+
<App />
9+
</StrictMode>,
10+
)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { worker } from "@electric-sql/pglite/worker";
2+
import { HttpFs } from "@electric-sql/pglite/httpfs/browser";
3+
import { PGlite } from "@electric-sql/pglite";
4+
5+
worker({
6+
async init() {
7+
const pg = await PGlite.create({
8+
fs: new HttpFs("/pagila", {
9+
fetchGranularity: 'page', // 'file' or 'page'
10+
}),
11+
});
12+
return pg;
13+
},
14+
});
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
6+
"module": "ESNext",
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"isolatedModules": true,
13+
"moduleDetection": "force",
14+
"noEmit": true,
15+
"jsx": "react-jsx",
16+
17+
/* Linting */
18+
"strict": true,
19+
"noUnusedLocals": true,
20+
"noUnusedParameters": true,
21+
"noFallthroughCasesInSwitch": true
22+
},
23+
"include": ["src"]
24+
}

0 commit comments

Comments
 (0)