Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jsdoc comments for database package #425

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/database/src/clients/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ const migrationsDirectory = path.resolve(
'../../migrations'
);

export const createKnex = (config: Knex.Config): Knex => knex(config);

/**
* Creates and configures a query builder instance configured for a PostgreSQL database.
*
* @param {string} connectionString - The connection string for the PostgreSQL database.
* @param {boolean} [ssl=false] - Indicates whether SSL should be enabled for the connection.
* If enabled, `rejectUnauthorized` will be set to `false` to allow self-signed certificates.
*/
export const getPostgresKnex = (
connectionString: string,
ssl: boolean = false
Expand All @@ -33,10 +38,11 @@ export const getInMemoryKnex = (): Knex => {
return getSqlite3Knex(':memory:');
};

export const getFileSystemKnex = (path: string): Knex => {
return getSqlite3Knex(path);
};

/**
* Creates and configures a query builder instance configured for a SQLite database.
*
* @param {string} filename - The path to the SQLite database file.
*/
const getSqlite3Knex = (filename: string): Knex => {
return knex({
client: 'better-sqlite3',
Expand Down
7 changes: 7 additions & 0 deletions packages/database/src/clients/kysely/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import pg from 'pg';

import { type Database } from './types.js';

/**
* Creates a new Postgres database connection.
*
* @param {string} connectionString - The connection string to connect to the Postgres database.
* @param {boolean} ssl - A boolean indicating whether SSL should be used for the connection.
* If true, SSL is enabled with the option to not reject unauthorized certificates.
*/
export const createPostgresDatabase = (
connectionString: string,
ssl: boolean
Expand Down
26 changes: 6 additions & 20 deletions packages/database/src/clients/kysely/sqlite3.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
import { Kysely, SqliteDialect } from 'kysely';
import BetterSqliteDatabase, {
type Database as SqliteDatabase,
} from 'better-sqlite3';
import { type Database as SqliteDatabase } from 'better-sqlite3';

import { type Database } from './types.js';

type TestDatabase = {
kysely: Kysely<Database>;
sqlite: SqliteDatabase;
};

export const createInMemoryDatabase = (): TestDatabase => {
const database = new BetterSqliteDatabase(':memory:');
return {
kysely: new Kysely<Database>({
dialect: new SqliteDialect({
database,
}),
}),
sqlite: database,
};
};

/**
* Creates a query builder instance configured to use SQLite as the database dialect.
*
* @param {SqliteDatabase} database - The SQLite database connection or configuration object.
*/
export const createSqliteDatabase = (database: SqliteDatabase) => {
return new Kysely<Database>({
dialect: new SqliteDialect({
Expand Down
3 changes: 3 additions & 0 deletions packages/database/src/context/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const migrationsDirectory = path.resolve(
'../../migrations'
);

/**
* Provides a context for accessing and managing a SQLite database stored on the filesystem.
*/
export class FilesystemDatabaseContext implements DatabaseContext {
public readonly engine = 'sqlite';
knex?: Knex;
Expand Down
5 changes: 5 additions & 0 deletions packages/database/src/context/in-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { migrateDatabase } from '../management/migrate-database.js';

import { type DatabaseContext } from './types.js';

/**
* Provides a context for accessing and managing a SQLite database stored in memory.
*
* This context is implemented for testing or temporary use cases where persistence is not required.
*/
export class InMemoryDatabaseContext implements DatabaseContext {
public readonly engine = 'sqlite';
knex?: Knex;
Expand Down
3 changes: 3 additions & 0 deletions packages/database/src/context/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { migrateDatabase } from '../management/migrate-database.js';
import { type DatabaseContext } from './types.js';
import { Pool } from 'pg';

/**
* Provides a context for accessing and managing a PostgreSQL database.
*/
export class PostgresDatabaseContext implements DatabaseContext {
public readonly engine = 'postgres';
knex?: Knex;
Expand Down
11 changes: 11 additions & 0 deletions packages/database/src/management/migrate-database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { type DatabaseContext } from '../context/types.js';

/**
* Handles database migration operations.
*
* Executes the latest database migrations for the given context and
* returns a function to roll back these migrations if needed.
*
* @param {DatabaseContext} ctx - The database context providing access to
* the database instance and migration utilities.
*
* @returns {Function} A function to roll back the last applied migrations.
*/
export const migrateDatabase = async (ctx: DatabaseContext) => {
const db = await ctx.getKnex();
await db.migrate.latest();
Expand Down
Loading