mevn-orm
Version:
simple ORM for express js
130 lines (129 loc) • 4.96 kB
TypeScript
import type { Knex } from 'knex';
/** Active Knex instance. Available after calling {@link configure} or {@link configureDatabase}. */
declare let DB: Knex | undefined;
type SupportedClient = 'sqlite3' | 'sqlite' | 'better-sqlite3' | 'mysql' | 'mysql2' | 'postgres' | 'postgresql' | 'pg' | 'pgnative' | 'cockroachdb' | 'redshift' | 'mssql' | 'oracledb' | 'oracle';
interface SimpleDatabaseConfig {
client?: SupportedClient;
dialect?: SupportedClient;
connection?: NonNullable<Knex.Config['connection']>;
connectionString?: string;
filename?: string;
host?: string;
port?: number;
user?: string;
password?: string;
database?: string;
ssl?: boolean | Record<string, unknown>;
debug?: boolean;
pool?: Knex.PoolConfig;
}
interface MigrationResult {
batch: number;
log: string[];
}
/**
* Returns the active Knex instance.
*
* @returns Configured Knex client.
* @throws When {@link configure} or {@link configureDatabase} has not been called.
*/
declare const getDB: () => Knex;
/**
* Initialises the ORM with a raw Knex config object or an existing Knex instance.
*
* @param config - Knex configuration or a pre-built Knex instance.
* @returns The active Knex client (also available as {@link DB}).
*/
declare const configure: (config: Knex.Config | Knex) => Knex;
/**
* Sets default migration options used by {@link makeMigration}, {@link migrateLatest},
* and other migration helpers.
*
* @param config - Knex migrator config (typically `directory` and `extension`).
* @returns A copy of the stored migration config.
*
* @example
* ```ts
* setMigrationConfig({ directory: './migrations', extension: 'ts' })
* ```
*/
declare const setMigrationConfig: (config: Knex.MigratorConfig) => Knex.MigratorConfig;
/**
* Returns the currently configured default migration options.
*
* @returns Copy of the migration config set via {@link setMigrationConfig}.
*/
declare const getMigrationConfig: () => Knex.MigratorConfig;
/**
* Builds a Knex config from simple database options.
*
* Prefer `client` over the deprecated `dialect` field. Connection details can be
* supplied as a `connection` object/string or as top-level `host`/`filename` fields.
*
* @param config - Simplified database configuration.
* @returns Knex config ready for {@link configure}.
*/
declare const createKnexConfig: (config: SimpleDatabaseConfig) => Knex.Config;
/**
* Initialises the ORM from simple database options.
*
* This is the recommended entry point for most applications.
*
* @param config - Simplified database configuration with `client` and connection fields.
* @returns The active Knex client (also available as {@link DB}).
*
* @example
* ```ts
* configureDatabase({
* client: 'better-sqlite3',
* connection: { filename: './dev.sqlite' }
* })
* ```
*
* SQLite note: `client: 'sqlite3'` and `client: 'sqlite'` are accepted for
* compatibility but resolve to the Knex `better-sqlite3` driver. Prefer
* installing and configuring `better-sqlite3`; the older `sqlite3` package is
* discouraged because it requires native builds.
*/
declare const configureDatabase: (config: SimpleDatabaseConfig) => Knex;
/**
* Generates a Knex migration file and returns its absolute path.
*
* @param name - Migration name (e.g. `create_users_table`).
* @param config - Optional per-call migrator overrides.
* @returns Path to the created migration file.
*/
declare const makeMigration: (name: string, config?: Knex.MigratorConfig) => Promise<string>;
/**
* Runs all pending migrations.
*
* @param config - Optional per-call migrator overrides.
* @returns Batch number and filenames of migrations executed in this run.
*/
declare const migrateLatest: (config?: Knex.MigratorConfig) => Promise<MigrationResult>;
/**
* Rolls back the most recent migration batch.
*
* @param config - Optional per-call migrator overrides.
* @param all - When `true`, rolls back all completed batches.
* @returns Batch number and filenames of migrations rolled back.
*/
declare const migrateRollback: (config?: Knex.MigratorConfig, all?: boolean) => Promise<MigrationResult>;
/**
* Returns the current migration version recorded by Knex.
*
* @param config - Optional per-call migrator overrides.
* @returns Latest applied migration name, or `'none'` when no migrations have run.
*/
declare const migrateCurrentVersion: (config?: Knex.MigratorConfig) => Promise<string>;
/**
* Lists completed and pending migration filenames.
*
* @param config - Optional per-call migrator overrides.
* @returns Object with `completed` and `pending` migration filename arrays.
*/
declare const migrateList: (config?: Knex.MigratorConfig) => Promise<{
completed: string[];
pending: string[];
}>;
export { DB, getDB, configure, createKnexConfig, configureDatabase, setMigrationConfig, getMigrationConfig, makeMigration, migrateLatest, migrateRollback, migrateCurrentVersion, migrateList, };