68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import type { PoolConfig } from "pg";
|
|
import { createD1Driver, D1Driver } from "./adapters/d1.ts";
|
|
import { createPostgresDriver, PostgresDriver } from "./adapters/pg.ts";
|
|
import { createSqliteDriver } from "./adapters/sqlite.ts";
|
|
import { Database } from "./database.ts";
|
|
|
|
export * from "./adapters/d1.ts";
|
|
export * from "./adapters/driver.ts";
|
|
export * from "./adapters/pg.ts";
|
|
export * from "./adapters/sqlite.ts";
|
|
export * from "./database.ts";
|
|
export * from "./decorators/Field.ts";
|
|
export * from "./decorators/Hydrate.ts";
|
|
export * from "./decorators/Table.ts";
|
|
export * from "./hydration.ts";
|
|
export * from "./repository.ts";
|
|
export * from "./migrations.ts";
|
|
|
|
export interface SqliteDatabaseOptions {
|
|
dialect: "sqlite";
|
|
/** Path of the database file, `:memory:` for an in-memory database. */
|
|
path?: string;
|
|
readonly?: boolean;
|
|
}
|
|
|
|
export interface PostgresDatabaseOptions {
|
|
dialect: "postgres";
|
|
/** Connection string, e.g. `postgres://user:pass@localhost:5432/db`. */
|
|
url?: string;
|
|
/** Connection options, used when no url is given. */
|
|
connection?: PoolConfig;
|
|
}
|
|
|
|
export interface D1DatabaseOptions {
|
|
dialect: "d1";
|
|
/** Account the database belongs to. */
|
|
accountId: string;
|
|
/** Identifier of the database. */
|
|
databaseId: string;
|
|
/** Api token of the account. */
|
|
apiToken: string;
|
|
/** Endpoint of the api, useful for tests. */
|
|
endpoint?: string;
|
|
}
|
|
|
|
/** Creates a database backed by SQLite or PostgreSQL. */
|
|
export function createDatabase(
|
|
options: SqliteDatabaseOptions | PostgresDatabaseOptions | D1DatabaseOptions,
|
|
): Database {
|
|
if (options.dialect === "sqlite") {
|
|
return new Database(
|
|
createSqliteDriver({ path: options.path, readonly: options.readonly }),
|
|
);
|
|
}
|
|
if (options.dialect === "d1") {
|
|
const driver: D1Driver = createD1Driver({
|
|
accountId: options.accountId,
|
|
databaseId: options.databaseId,
|
|
apiToken: options.apiToken,
|
|
endpoint: options.endpoint,
|
|
});
|
|
return new Database(driver);
|
|
}
|
|
const driver: PostgresDriver = options.url
|
|
? createPostgresDriver(options.url)
|
|
: createPostgresDriver(options.connection ?? {});
|
|
return new Database(driver);
|
|
}
|