quicklite
Version:
A lightweight ORM toolkit for SQLite in Node.js applications
112 lines (111 loc) • 3.2 kB
TypeScript
import Database from 'better-sqlite3';
import { BaseEntity, TableInfo } from './BaseEntity';
/**
* Query options for finding entities
*/
export interface QueryOptions {
/**
* WHERE conditions as key-value pairs
*/
where?: {
[key: string]: any;
};
/**
* ORDER BY clause (e.g., 'id DESC')
*/
orderBy?: string;
/**
* Maximum number of results to return
*/
limit?: number;
/**
* Number of results to skip
*/
offset?: number;
}
/**
* Base service class for CRUD operations on entities
*/
export declare class BaseService<T extends object> {
protected db: Database.Database;
protected entityClass: new () => T & BaseEntity;
protected tableInfo: TableInfo;
/**
* Creates a new service for the specified entity
* @param db Database instance
* @param entityClass Entity class
*/
constructor(db: Database.Database, entityClass: any);
/**
* Inserts a new entity into the database
* @param entity Entity to insert
* @returns ID of the inserted entity
*/
insert(entity: Partial<T>): number;
/**
* Inserts multiple entities in a single transaction
* @param entities Array of entities to insert
*/
batchInsert(entities: Partial<T>[]): void;
/**
* Retrieves an entity by its ID
* @param id Entity ID
* @returns Entity or null if not found
*/
getById(id: number | string): T | null;
/**
* Finds entities matching the specified criteria
* @param options Query options
* @returns Array of matching entities
*/
find(options?: QueryOptions): T[];
/**
* Finds a single entity matching the specified criteria
* @param options Query options
* @returns Matching entity or null if not found
*/
findOne(options?: QueryOptions): T | null;
/**
* Counts entities matching the specified criteria
* @param options Query options
* @returns Count of matching entities
*/
count(options?: QueryOptions): number;
/**
* Updates an entity in the database
* @param entity Entity data to update
* @param whereCondition Optional WHERE condition (defaults to primary key)
* @returns Number of updated rows
*/
update(entity: Partial<T>, whereCondition?: {
[key: string]: any;
}): number;
/**
* Deletes entities matching the specified condition
* @param whereCondition WHERE condition
* @returns Number of deleted rows
*/
delete(whereCondition: {
[key: string]: any;
}): number;
/**
* Deletes an entity by its ID
* @param id Entity ID
* @returns Number of deleted rows
*/
deleteById(id: number | string): number;
/**
* Executes a custom SQL query
* @param sql SQL statement
* @param params Query parameters
* @returns Query results
*/
query<R = any>(sql: string, params?: any[]): R[];
/**
* Executes a custom SQL statement
* @param sql SQL statement
* @param params Statement parameters
* @returns Statement result
*/
execute(sql: string, params?: any[]): Database.RunResult;
}