Skip to main content

Overview

The RecordRepository class handles all database operations for Record entities. It provides methods for creating, reading, updating, and soft-deleting records in the SQLite database. Location: src/infraestructure/repositories/RecordRepository.ts

Methods

create

Creates a new record in the database.
async create(record: Record): Promise<void>
record
Record
required
The record object to create
return
Promise<void>
Resolves when the record is successfully created
Database Interaction:
INSERT INTO records (
  id, title, subtitle, metadata, type, userId, createdAt, updatedAt, isDeleted
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

findAll

Retrieves all non-deleted records from the database.
async findAll(): Promise<Record[]>
return
Promise<Record[]>
Array of all active (non-deleted) records. The isDeleted field is converted from numeric (0/1) to boolean.
Database Interaction:
SELECT * FROM records WHERE isDeleted = 0
Note: This method automatically filters out soft-deleted records and converts the isDeleted field from integer to boolean.

update

Updates an existing record in the database.
async update(record: Record): Promise<void>
record
Record
required
The record object with updated values. The id field is used to identify which record to update.
return
Promise<void>
Resolves when the record is successfully updated
Database Interaction:
UPDATE records SET
  title = ?,
  subtitle = ?,
  metadata = ?,
  type = ?,
  userId = ?,
  updatedAt = ?
WHERE id = ?
Note: The createdAt and isDeleted fields are not updated by this method.

softDelete

Marks a record as deleted without removing it from the database.
async softDelete(id: string): Promise<void>
id
string
required
The unique identifier of the record to soft delete
return
Promise<void>
Resolves when the record is successfully marked as deleted
Database Interaction:
UPDATE records SET isDeleted = 1 WHERE id = ?
Note: Soft-deleted records are excluded from findAll() results but remain in the database for potential recovery or audit purposes.

Usage Example

import { RecordRepository } from './infraestructure/repositories/RecordRepository';
import { Record } from './domain/entities/Record';

const recordRepo = new RecordRepository();

// Create a new record
const newRecord: Record = {
  id: 'rec_123',
  title: 'My Record',
  subtitle: 'A sample record',
  metadata: JSON.stringify({ key: 'value' }),
  type: 'standard',
  userId: 'user_456',
  createdAt: new Date().toISOString(),
  updatedAt: new Date().toISOString(),
  isDeleted: false
};

await recordRepo.create(newRecord);

// Get all records
const records = await recordRepo.findAll();

// Update a record
newRecord.title = 'Updated Title';
newRecord.updatedAt = new Date().toISOString();
await recordRepo.update(newRecord);

// Soft delete a record
await recordRepo.softDelete('rec_123');

Database Schema

The repository interacts with the records table with the following structure:
ColumnTypeConstraints
idTEXTPRIMARY KEY
titleTEXTNOT NULL
subtitleTEXTNULLABLE
metadataTEXTNULLABLE
typeTEXTNOT NULL
userIdTEXTNULLABLE
createdAtTEXTNOT NULL
updatedAtTEXTNOT NULL
isDeletedINTEGERNOT NULL (0 or 1)

Build docs developers (and LLMs) love