Merge branch 'pglite' into 'main'

Add support for pglite

See merge request soapbox-pub/ditto!483
This commit is contained in:
Alex Gleason 2024-09-10 21:12:47 +00:00
commit a888f43288
7 changed files with 135 additions and 10 deletions

View file

@ -1,4 +1,4 @@
image: denoland/deno:1.45.5
image: denoland/deno:1.46.3
default:
interruptible: true

View file

@ -1 +1 @@
deno 1.45.5
deno 1.46.3

View file

@ -28,6 +28,7 @@
"@b-fuze/deno-dom": "jsr:@b-fuze/deno-dom@^0.1.47",
"@bradenmacdonald/s3-lite-client": "jsr:@bradenmacdonald/s3-lite-client@^0.7.4",
"@db/sqlite": "jsr:@db/sqlite@^0.11.1",
"@electric-sql/pglite": "npm:@soapbox.pub/pglite@^0.2.10",
"@hono/hono": "jsr:@hono/hono@^4.4.6",
"@isaacs/ttlcache": "npm:@isaacs/ttlcache@^1.4.1",
"@lambdalisue/async": "jsr:@lambdalisue/async@^2.1.1",
@ -37,6 +38,7 @@
"@scure/base": "npm:@scure/base@^1.1.6",
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",
"@soapbox/kysely-deno-sqlite": "jsr:@soapbox/kysely-deno-sqlite@^2.1.0",
"@soapbox/kysely-pglite": "jsr:@soapbox/kysely-pglite@^0.0.1",
"@soapbox/stickynotes": "jsr:@soapbox/stickynotes@^0.4.0",
"@std/assert": "jsr:@std/assert@^0.225.1",
"@std/cli": "jsr:@std/cli@^0.223.0",

68
deno.lock generated
View file

@ -2,7 +2,7 @@
"version": "3",
"packages": {
"specifiers": {
"jsr:@b-fuze/deno-dom@^0.1.47": "jsr:@b-fuze/deno-dom@0.1.47",
"jsr:@b-fuze/deno-dom@^0.1.47": "jsr:@b-fuze/deno-dom@0.1.48",
"jsr:@bradenmacdonald/s3-lite-client@^0.7.4": "jsr:@bradenmacdonald/s3-lite-client@0.7.6",
"jsr:@db/sqlite@^0.11.1": "jsr:@db/sqlite@0.11.1",
"jsr:@denosaurs/plug@1": "jsr:@denosaurs/plug@1.0.6",
@ -12,7 +12,8 @@
"jsr:@gleasonator/policy@0.4.0": "jsr:@gleasonator/policy@0.4.0",
"jsr:@gleasonator/policy@0.4.1": "jsr:@gleasonator/policy@0.4.1",
"jsr:@gleasonator/policy@0.4.2": "jsr:@gleasonator/policy@0.4.2",
"jsr:@hono/hono@^4.4.6": "jsr:@hono/hono@4.5.9",
"jsr:@gleasonator/policy@0.5.0": "jsr:@gleasonator/policy@0.5.0",
"jsr:@hono/hono@^4.4.6": "jsr:@hono/hono@4.5.11",
"jsr:@lambdalisue/async@^2.1.1": "jsr:@lambdalisue/async@2.1.1",
"jsr:@nostrify/db@^0.31.2": "jsr:@nostrify/db@0.31.2",
"jsr:@nostrify/nostrify@^0.22.1": "jsr:@nostrify/nostrify@0.22.5",
@ -20,8 +21,12 @@
"jsr:@nostrify/nostrify@^0.22.5": "jsr:@nostrify/nostrify@0.22.5",
"jsr:@nostrify/nostrify@^0.30.0": "jsr:@nostrify/nostrify@0.30.1",
"jsr:@nostrify/nostrify@^0.30.1": "jsr:@nostrify/nostrify@0.30.1",
"jsr:@nostrify/types@^0.30.0": "jsr:@nostrify/types@0.30.0",
"jsr:@nostrify/nostrify@^0.31.0": "jsr:@nostrify/nostrify@0.31.0",
"jsr:@nostrify/policies@^0.33.0": "jsr:@nostrify/policies@0.33.0",
"jsr:@nostrify/types@^0.30.0": "jsr:@nostrify/types@0.30.1",
"jsr:@nostrify/types@^0.30.1": "jsr:@nostrify/types@0.30.1",
"jsr:@soapbox/kysely-deno-sqlite@^2.1.0": "jsr:@soapbox/kysely-deno-sqlite@2.2.0",
"jsr:@soapbox/kysely-pglite@^0.0.1": "jsr:@soapbox/kysely-pglite@0.0.1",
"jsr:@soapbox/stickynotes@^0.4.0": "jsr:@soapbox/stickynotes@0.4.0",
"jsr:@std/assert@^0.213.1": "jsr:@std/assert@0.213.1",
"jsr:@std/assert@^0.217.0": "jsr:@std/assert@0.217.0",
@ -45,7 +50,7 @@
"jsr:@std/fs@^0.221.0": "jsr:@std/fs@0.221.0",
"jsr:@std/fs@^0.229.3": "jsr:@std/fs@0.229.3",
"jsr:@std/internal@^1.0.0": "jsr:@std/internal@1.0.1",
"jsr:@std/io@^0.224": "jsr:@std/io@0.224.6",
"jsr:@std/io@^0.224": "jsr:@std/io@0.224.7",
"jsr:@std/json@^0.223.0": "jsr:@std/json@0.223.0",
"jsr:@std/media-types@^0.224.1": "jsr:@std/media-types@0.224.1",
"jsr:@std/path@0.213.1": "jsr:@std/path@0.213.1",
@ -58,6 +63,7 @@
"npm:@scure/base@^1.1.6": "npm:@scure/base@1.1.6",
"npm:@scure/bip32@^1.4.0": "npm:@scure/bip32@1.4.0",
"npm:@scure/bip39@^1.3.0": "npm:@scure/bip39@1.3.0",
"npm:@soapbox.pub/pglite@^0.2.10": "npm:@soapbox.pub/pglite@0.2.10",
"npm:@types/node": "npm:@types/node@18.16.19",
"npm:comlink-async-generator": "npm:comlink-async-generator@0.0.1",
"npm:comlink-async-generator@^0.0.1": "npm:comlink-async-generator@0.0.1",
@ -101,6 +107,9 @@
"jsr:@denosaurs/plug@1.0.3"
]
},
"@b-fuze/deno-dom@0.1.48": {
"integrity": "bf5b591aef2e9e9c59adfcbb93a9ecd45bab5b7c8263625beafa5c8f1662e7da"
},
"@bradenmacdonald/s3-lite-client@0.7.6": {
"integrity": "2b5976dca95d207dc88e23f9807e3eecbc441b0cf547dcda5784afe6668404d1",
"dependencies": [
@ -156,6 +165,13 @@
"jsr:@nostrify/nostrify@^0.22.1"
]
},
"@gleasonator/policy@0.5.0": {
"integrity": "c2882eb3b4147dfe96b6ec2870b012b5a614f686770d1d4b2f778fdc44e8b1f5",
"dependencies": [
"jsr:@nostrify/nostrify@^0.31.0",
"jsr:@nostrify/policies@^0.33.0"
]
},
"@hono/hono@4.4.6": {
"integrity": "aa557ca9930787ee86b9ca1730691f1ce1c379174c2cb244d5934db2b6314453"
},
@ -165,6 +181,9 @@
"@hono/hono@4.5.1": {
"integrity": "459748ed4d4146c6e4bdff0213ff1ac44749904066ae02e7550d6c7f28c9bc4c"
},
"@hono/hono@4.5.11": {
"integrity": "5bd6b1a3a503efb746fdcf0aae3ac536dd09229d372988bde5db0798ef64ae4f"
},
"@hono/hono@4.5.3": {
"integrity": "429923b2b3c6586a1450862328d61a1346fee5841e8ae86c494250475057213c"
},
@ -248,15 +267,44 @@
"npm:zod@^3.23.8"
]
},
"@nostrify/nostrify@0.31.0": {
"integrity": "1c1b686bb9ca3ad8d19807e3b96ef3793a65d70fd0f433fe6ef8b3fdb9f45557",
"dependencies": [
"jsr:@nostrify/types@^0.30.1",
"jsr:@std/encoding@^0.224.1",
"npm:@scure/bip32@^1.4.0",
"npm:@scure/bip39@^1.3.0",
"npm:lru-cache@^10.2.0",
"npm:nostr-tools@^2.7.0",
"npm:websocket-ts@^2.1.5",
"npm:zod@^3.23.8"
]
},
"@nostrify/policies@0.33.0": {
"integrity": "c946b06d0527298b4d7c9819d142a10f522ba09eee76c37525aa4acfc5d87aee",
"dependencies": [
"jsr:@nostrify/types@^0.30.1",
"npm:nostr-tools@^2.7.0"
]
},
"@nostrify/types@0.30.0": {
"integrity": "1f38fa849cff930bd709edbf94ef9ac02f46afb8b851f86c8736517b354616da"
},
"@nostrify/types@0.30.1": {
"integrity": "245da176f6893a43250697db51ad32bfa29bf9b1cdc1ca218043d9abf6de5ae5"
},
"@soapbox/kysely-deno-sqlite@2.2.0": {
"integrity": "668ec94600bc4b4d7bd618dd7ca65d4ef30ee61c46ffcb379b6f45203c08517a",
"dependencies": [
"npm:kysely@^0.27.2"
]
},
"@soapbox/kysely-pglite@0.0.1": {
"integrity": "7a4221aa780aad6fba9747c45c59dfb1c62017ba8cad9db5607f6e5822c058d5",
"dependencies": [
"npm:kysely@^0.27.4"
]
},
"@soapbox/stickynotes@0.4.0": {
"integrity": "60bfe61ab3d7e04bf708273b1e2d391a59534bdf29e54160e98d7afd328ca1ec"
},
@ -368,6 +416,12 @@
"jsr:@std/bytes@^1.0.2"
]
},
"@std/io@0.224.7": {
"integrity": "a70848793c44a7c100926571a8c9be68ba85487bfcd4d0540d86deabe1123dc9",
"dependencies": [
"jsr:@std/bytes@^1.0.2"
]
},
"@std/json@0.223.0": {
"integrity": "9a4a255931dd0397924c6b10bb6a72fe3e28ddd876b981ada2e3b8dd0764163f"
},
@ -481,6 +535,10 @@
"@scure/base": "@scure/base@1.1.6"
}
},
"@soapbox.pub/pglite@0.2.10": {
"integrity": "sha512-DEHejCr+R99RNdyOo34Nbl1FKLmpBCc0pMlPhH3yTyc/KH5HV7dPYbTGCgqRXPxODVkQhvaEuIF2266KsUlZcg==",
"dependencies": {}
},
"@types/dompurify@3.0.5": {
"integrity": "sha512-1Wg0g3BtQF7sSb27fJQAKck1HECM6zV1EB66j8JH9i3LCjYabJa0FSdiSgsD5K/RbrsR0SiraKacLB+T8ZVYAg==",
"dependencies": {
@ -1920,6 +1978,7 @@
"jsr:@nostrify/db@^0.31.2",
"jsr:@nostrify/nostrify@^0.30.1",
"jsr:@soapbox/kysely-deno-sqlite@^2.1.0",
"jsr:@soapbox/kysely-pglite@^0.0.1",
"jsr:@soapbox/stickynotes@^0.4.0",
"jsr:@std/assert@^0.225.1",
"jsr:@std/cli@^0.223.0",
@ -1933,6 +1992,7 @@
"npm:@isaacs/ttlcache@^1.4.1",
"npm:@noble/secp256k1@^2.0.0",
"npm:@scure/base@^1.1.6",
"npm:@soapbox.pub/pglite@^0.2.10",
"npm:comlink-async-generator@^0.0.1",
"npm:comlink@^4.4.1",
"npm:commander@12.1.0",

View file

@ -82,7 +82,7 @@ class Conf {
* ```
*/
static get databaseUrl(): string {
return Deno.env.get('DATABASE_URL') ?? 'sqlite://data/db.sqlite3';
return Deno.env.get('DATABASE_URL') ?? 'pglite://data/pgdata';
}
static db = {
get url(): url.UrlWithStringQuery {
@ -92,6 +92,7 @@ class Conf {
switch (Conf.db.url.protocol) {
case 'sqlite:':
return 'sqlite';
case 'pglite:':
case 'postgres:':
case 'postgresql:':
return 'postgres';

View file

@ -5,6 +5,7 @@ import { NDatabaseSchema, NPostgresSchema } from '@nostrify/db';
import { FileMigrationProvider, Kysely, Migrator } from 'kysely';
import { Conf } from '@/config.ts';
import { DittoPglite } from '@/db/adapters/DittoPglite.ts';
import { DittoPostgres } from '@/db/adapters/DittoPostgres.ts';
import { DittoSQLite } from '@/db/adapters/DittoSQLite.ts';
import { DittoTables } from '@/db/DittoTables.ts';
@ -30,12 +31,17 @@ export class DittoDB {
static async _getInstance(): Promise<DittoDatabase> {
const result = {} as DittoDatabase;
switch (Conf.db.dialect) {
case 'sqlite':
switch (Conf.db.url.protocol) {
case 'sqlite:':
result.dialect = 'sqlite';
result.kysely = await DittoSQLite.getInstance();
break;
case 'postgres':
case 'pglite:':
result.dialect = 'postgres';
result.kysely = await DittoPglite.getInstance();
break;
case 'postgres:':
case 'postgresql:':
result.dialect = 'postgres';
result.kysely = await DittoPostgres.getInstance();
break;

View file

@ -0,0 +1,56 @@
import { PGlite } from '@electric-sql/pglite';
import { NPostgresSchema } from '@nostrify/db';
import { PgliteDialect } from '@soapbox/kysely-pglite';
import { Kysely } from 'kysely';
import { Conf } from '@/config.ts';
import { DittoTables } from '@/db/DittoTables.ts';
import { KyselyLogger } from '@/db/KyselyLogger.ts';
export class DittoPglite {
static db: Kysely<DittoTables> & Kysely<NPostgresSchema> | undefined;
// deno-lint-ignore require-await
static async getInstance(): Promise<Kysely<DittoTables> & Kysely<NPostgresSchema>> {
if (!this.db) {
this.db = new Kysely({
dialect: new PgliteDialect({
database: new PGlite(this.path),
}),
log: KyselyLogger,
}) as Kysely<DittoTables> & Kysely<NPostgresSchema>;
}
return this.db;
}
static get poolSize() {
return 1;
}
static get availableConnections(): number {
return 1;
}
/** Get the relative or absolute path based on the `DATABASE_URL`. */
static get path(): string | undefined {
if (Conf.databaseUrl === 'pglite://:memory:') {
return undefined;
}
const { host, pathname } = Conf.db.url;
if (!pathname) return '';
// Get relative path.
if (host === '') {
return pathname;
} else if (host === '.') {
return pathname;
} else if (host) {
return host + pathname;
}
return '';
}
}