CrudRepositoryBase
Overview
CrudRepositoryBase è la classe base astratta che implementa l'interfaccia ICrudRepository e definisce il contratto comune di tutti i repository CRUD del package. PostgRESTRepository è la sua unica implementazione concreta inclusa nel package.
abstract class CrudRepositoryBase implements ICrudRepository
Quando estenderla
Estendi CrudRepositoryBase quando devi implementare un backend non-PostgREST (REST custom, GraphQL, gRPC, mock in-memory) mantenendo la stessa API CRUD usata dal resto dell'ecosistema PZeta. Tutte le facade (PostgRESTClient, BasePostgrestService) e gli helper di paginazione/filtri continueranno a funzionare se il tuo repository soddisfa il contratto.
PostgRESTRepository / EnhancedPostgRESTRepository. Estendere CrudRepositoryBase è giustificato solo per integrazioni custom o test doubles.Stato protetto
| Campo | Tipo | Descrizione |
|---|---|---|
baseUrl | string | URL base dell'API |
isAuthRequired | boolean | Le richieste richiedono autenticazione |
useCache | boolean | Abilita caching delle risposte |
Costruttore
constructor(baseUrl: string, isAuthRequired: boolean, useCache: boolean)
Metodi astratti da implementare
| Metodo | Signature |
|---|---|
create | create<T>(path, element: Partial<T>, keyName?: keyof T): Promise<T | undefined> |
read | read<T, K>(path, key: K, keyName: keyof T, forceRefresh?: boolean): Promise<T | undefined> |
update | update<T, K>(path, key: K, element: Partial<T>, keyName: keyof T): Promise<T | undefined> |
delete | delete<T, K>(path, key: K, keyName: keyof T): Promise<T | undefined> |
createMany | createMany<T>(path, elements: Partial<T>[], keyName?): Promise<T[] | undefined> |
readAll | readAll<T>(path, filters?: FilterCriteria<T>, forceRefresh?): Promise<T[] | PaginatedResponse<T> | undefined> |
updateMany | updateMany<T>(path, element: Partial<T>, filters?, keyName?): Promise<T[] | undefined> |
deleteMany | deleteMany<T, K>(path, keys: K[], keyName: keyof T): Promise<T[] | undefined> |
Esempio: backend REST custom
import { CrudRepositoryBase } from '@pzeta/postgrest'
import type {
FilterCriteria,
PaginatedResponse,
} from '@pzeta/postgrest'
import { axios } from 'axios'
class CustomRestRepository extends CrudRepositoryBase {
constructor(baseUrl: string) {
super(baseUrl, true, false)
}
async create<T extends Record<string, unknown>>(
path: string,
element: Partial<T>,
): Promise<T | undefined> {
const { data } = await axios.post<T>(`${this.baseUrl}${path}`, element)
return data
}
async read<T extends Record<string, unknown>, K>(
path: string,
key: K,
_keyName: keyof T,
): Promise<T | undefined> {
const { data } = await axios.get<T>(`${this.baseUrl}${path}/${String(key)}`)
return data
}
async update<T extends Record<string, unknown>, K>(
path: string,
key: K,
element: Partial<T>,
): Promise<T | undefined> {
const { data } = await axios.put<T>(
`${this.baseUrl}${path}/${String(key)}`,
element,
)
return data
}
async delete<T extends Record<string, unknown>, K>(
path: string,
key: K,
): Promise<T | undefined> {
const { data } = await axios.delete<T>(
`${this.baseUrl}${path}/${String(key)}`,
)
return data
}
async createMany<T extends Record<string, unknown>>(
path: string,
elements: Partial<T>[],
): Promise<T[] | undefined> {
const { data } = await axios.post<T[]>(`${this.baseUrl}${path}/bulk`, elements)
return data
}
async readAll<T extends Record<string, unknown>>(
path: string,
filters?: FilterCriteria<T>,
): Promise<T[] | PaginatedResponse<T> | undefined> {
const { data } = await axios.get<T[]>(`${this.baseUrl}${path}`, {
params: filters,
})
return data
}
async updateMany<T extends Record<string, unknown>>(
path: string,
element: Partial<T>,
filters?: FilterCriteria<T>,
): Promise<T[] | undefined> {
const { data } = await axios.patch<T[]>(`${this.baseUrl}${path}`, element, {
params: filters,
})
return data
}
async deleteMany<T extends Record<string, unknown>, K>(
path: string,
keys: K[],
keyName: keyof T,
): Promise<T[] | undefined> {
const { data } = await axios.delete<T[]>(`${this.baseUrl}${path}`, {
data: { [String(keyName)]: keys },
})
return data
}
}
PostgRESTClient / BasePostgrestService si aspettano che create/update/delete mantengano coerenza con readAll.Cross-reference
ICrudRepository— contratto formalePostgRESTRepository— implementazione concreta per PostgRESTFilterCriteria— operatori filtro standardRepositoryUtils— utility perRestErrore parsing path riusabili
BaseEnhancedRepository
Classe base estendibile che fornisce CRUD + RPC + cache su path e keyName fissi — punto di estensione per repository di dominio con metodi custom type-safe.
EnhancedPostgRESTRepository
Wrapper semplificato di PostgRESTRepository con path e keyName fissi nel costruttore — API minimale type-safe per singola entità.