first commit

This commit is contained in:
Matt Kane
2026-04-01 10:44:22 +01:00
commit 43fcb9a131
1789 changed files with 395041 additions and 0 deletions

View File

@@ -0,0 +1,125 @@
/**
* Database Adapter Functions
*
* These run at config time (astro.config.mjs) and return serializable descriptors.
* The actual dialect is created at runtime by loading the entrypoint.
*
* @example
* ```ts
* // astro.config.mjs
* import emdash from "emdash/astro";
* import { sqlite } from "emdash/db";
*
* export default defineConfig({
* integrations: [
* emdash({
* database: sqlite({ url: "file:./data.db" }),
* }),
* ],
* });
* ```
*/
/**
* Dialect family identifier.
* Used at runtime to select dialect-specific SQL fragments.
*/
export type DatabaseDialectType = "sqlite" | "postgres";
/**
* Database descriptor - serializable config for virtual modules
*/
export interface DatabaseDescriptor {
entrypoint: string;
config: unknown;
type: DatabaseDialectType;
}
export interface SqliteConfig {
/**
* Database URL (e.g., "file:./data.db")
*/
url: string;
}
export interface LibsqlConfig {
/**
* Database URL (e.g., "file:./data.db" or "libsql://...")
*/
url: string;
/**
* Auth token for remote libSQL
*/
authToken?: string;
}
/**
* SQLite database adapter (better-sqlite3)
*
* For local development and Node.js deployments.
*
* @example
* ```ts
* database: sqlite({ url: "file:./data.db" })
* ```
*/
export function sqlite(config: SqliteConfig): DatabaseDescriptor {
return {
entrypoint: "emdash/db/sqlite",
config,
type: "sqlite",
};
}
/**
* libSQL database adapter (Turso)
*
* For Turso hosted databases or local libSQL.
*
* @example
* ```ts
* database: libsql({
* url: "libsql://my-db.turso.io",
* authToken: process.env.TURSO_AUTH_TOKEN,
* })
* ```
*/
export function libsql(config: LibsqlConfig): DatabaseDescriptor {
return {
entrypoint: "emdash/db/libsql",
config,
type: "sqlite",
};
}
/**
* PostgreSQL connection configuration
*/
export interface PostgresConfig {
connectionString?: string;
host?: string;
port?: number;
database?: string;
user?: string;
password?: string;
ssl?: boolean;
pool?: { min?: number; max?: number };
}
/**
* PostgreSQL database adapter
*
* For PostgreSQL deployments with connection pooling.
*
* @example
* ```ts
* database: postgres({ connectionString: process.env.DATABASE_URL })
* ```
*/
export function postgres(config: PostgresConfig): DatabaseDescriptor {
return {
entrypoint: "emdash/db/postgres",
config,
type: "postgres",
};
}

View File

@@ -0,0 +1,37 @@
/**
* emdash/db
*
* Database adapters for EmDash CMS.
* Use these in astro.config.mjs to configure the database.
*
* @example
* ```ts
* import emdash from "emdash/astro";
* import { sqlite } from "emdash/db";
*
* export default defineConfig({
* integrations: [
* emdash({
* database: sqlite({ url: "file:./data.db" }),
* }),
* ],
* });
* ```
*/
export { sqlite, libsql, postgres } from "./adapters.js";
export type {
DatabaseDescriptor,
DatabaseDialectType,
SqliteConfig,
LibsqlConfig,
PostgresConfig,
} from "./adapters.js";
// Migration utilities (used by playground, preview, and custom deployment scripts)
export {
runMigrations,
getMigrationStatus,
rollbackMigration,
} from "../database/migrations/runner.js";
export type { MigrationStatus } from "../database/migrations/runner.js";

View File

@@ -0,0 +1,23 @@
/**
* libSQL runtime adapter
*
* Creates a Kysely dialect for libSQL/Turso.
* Loaded at runtime via virtual module.
*/
import type { Dialect } from "kysely";
import type { LibsqlConfig } from "./adapters.js";
/**
* Create a libSQL dialect from config
*/
export function createDialect(config: LibsqlConfig): Dialect {
// Dynamic import to avoid loading @libsql/kysely-libsql at config time
const { LibsqlDialect } = require("@libsql/kysely-libsql");
return new LibsqlDialect({
url: config.url,
authToken: config.authToken,
});
}

View File

@@ -0,0 +1,30 @@
/**
* PostgreSQL runtime adapter
*
* Creates a Kysely dialect for PostgreSQL via pg.
* Loaded at runtime via virtual module.
*/
import { PostgresDialect } from "kysely";
import { Pool } from "pg";
import type { PostgresConfig } from "./adapters.js";
/**
* Create a PostgreSQL dialect from config
*/
export function createDialect(config: PostgresConfig): PostgresDialect {
const pool = new Pool({
connectionString: config.connectionString,
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: config.ssl,
min: config.pool?.min ?? 0,
max: config.pool?.max ?? 10,
});
return new PostgresDialect({ pool });
}

View File

@@ -0,0 +1,27 @@
/**
* SQLite runtime adapter
*
* Creates a Kysely dialect for better-sqlite3.
* Loaded at runtime via virtual module.
*/
import type { Dialect } from "kysely";
import type { SqliteConfig } from "./adapters.js";
/**
* Create a SQLite dialect from config
*/
export function createDialect(config: SqliteConfig): Dialect {
// Dynamic import to avoid loading better-sqlite3 at config time
const BetterSqlite3 = require("better-sqlite3");
const { SqliteDialect } = require("kysely");
// Parse URL to get file path
const url = config.url;
const filePath = url.startsWith("file:") ? url.slice(5) : url;
const database = new BetterSqlite3(filePath);
return new SqliteDialect({ database });
}