-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Carlos Lopez edited this page Jul 16, 2026
·
1 revision
Documentación completa de la API de GitDB.
export { GitDB, gitDb } from './core/gitdb.ts';
export { defineRelations } from './core/relations.ts';
export type { /* tipos */ } from './core/relations.ts';
export { /* schema builders */ } from './core/schema.ts';
export { DeleteQuery } from './queries/delete-query.ts';
export { InsertQuery } from './queries/insert-query.ts';
export { and, eq, gte, ilike, lt, ne, not, or } from './queries/where-operators.ts';
export { UpdateQuery } from './queries/update-query.ts';Inicializa una instancia de GitDB.
function gitDb(options: GitDbOptions): Promise<GitDB>interface GitDbOptions {
dir: string; // Directorio del repositorio Git
author?: {
name: string; // Nombre del autor (commits)
email: string; // Email del autor
};
logger?: GitDbLogger; // Logger personalizado (opcional)
}const db = await gitDb({
dir: './data',
author: {
name: 'My App',
email: 'app@example.com'
}
});Inicia una query SELECT.
select(): SelectQuery
select(fields: (keyof Entity)[]): SelectQueryEjemplo:
const allUsers = await db.select().from(User);
const emails = await db.select(['email', 'name']).from(User);Inicia una query INSERT.
insert(entity: EntityDefinition): InsertQueryEjemplo:
await db.insert(User).values({ id: '1', name: 'John' });Inicia una query UPDATE.
update(entity: EntityDefinition): UpdateQueryEjemplo:
await db.update(User).set({ name: 'Jane' }).where(eq('id', '1'));Inicia una query DELETE.
delete(): DeleteQueryEjemplo:
await db.delete().from(User).where(eq('id', '1'));Cuenta registros.
$count(entity: EntityDefinition, where?: WhereInput): Promise<number>Ejemplo:
const total = await db.$count(User);
const adults = await db.$count(User, gte('age', 18));Suma valores.
$sum(entity: EntityDefinition, field: string, where?: WhereInput): Promise<number>Ejemplo:
const total = await db.$sum(Product, 'price');Calcula promedio.
$avg(entity: EntityDefinition, field: string, where?: WhereInput): Promise<number | null>Ejemplo:
const avg = await db.$avg(User, 'age');Configura relaciones para cargar.
with(relationsRegistry: RelationsRegistry, includeRelations?: IncludeRelationsInput): GitDB
with(includeRelations?: IncludeRelationsInput): GitDBEjemplo:
const result = await db.with({
posts: true,
comments: true
}).select().from(User).where(eq('id', 'user-1'));Cierra la conexión.
close(): Promise<void>Ejemplo:
await db.close();Define una entidad.
function entity(name: string, columns: ColumnDefinitions): EntityDefinitiontext() // Texto ilimitado
varchar(length?) // Texto con límite
char(length) // Texto fijoint() // 32-bit integer
integer() // Alias de int()
bigint() // 64-bit integer
numeric(p, s) // Decimal preciso
real() // Float
double() // Double
doublePrecision() // Alias de double()bool() // Booleano
boolean() // Alias de bool()date() // Solo fecha
timestamp() // Fecha y horauuid() // UUID
json() // JSON object.primary() // Primary key
.unique() // Unique constraintfrom(entity: EntityDefinition): SelectQuery
where(condition: WhereInput): SelectQuery
include(relations: IncludeRelationsInput): SelectQueryEjemplo:
const result = await db
.select(['id', 'name'])
.from(User)
.where(eq('age', 30))
.include({ posts: true });values(data: Entity | Entity[]): Promise<Entity | Entity[]>Ejemplo:
const user = await db.insert(User).values({ id: '1', name: 'John' });set(data: Partial<Entity>): UpdateQuery
where(condition: WhereInput): Promise<void>Ejemplo:
await db.update(User).set({ name: 'Jane' }).where(eq('id', '1'));from(entity: EntityDefinition): DeleteQuery
where(condition: WhereInput): Promise<void>Ejemplo:
await db.delete().from(User).where(eq('id', '1'));eq(field, value) // Equals
ne(field, value) // Not equals
gt(field, value) // Greater than
gte(field, value) // Greater or equal
lt(field, value) // Less than
lte(field, value) // Less or equal
ilike(field, pattern) // Case-insensitive LIKE
and(...predicates) // Logical AND
or(...predicates) // Logical OR
not(predicate) // Logical NOTinterface EntityDefinition {
name: string;
columns: Record<string, ColumnType>;
}type WhereInput = Predicate | Predicate[];type IncludeRelationsInput = Record<string, boolean | IncludeRelationsInput>;GitDB puede lanzar errores en las siguientes situaciones:
try {
await db.insert(User).values(invalid);
} catch (error) {
if (error instanceof ValidationError) {
console.log('Validation failed:', error.message);
} else if (error instanceof GitError) {
console.log('Git error:', error.message);
} else {
throw error;
}
}- Getting Started - Tutorial
- Examples - Ejemplos prácticos
- FAQ - Preguntas frecuentes