@premiuz/odm
v0.3.0
Published
ODM do Premiuz para Firestore. Encapsula os modelos de domínio e expõe repositórios type-safe. Construído sobre [`@vorisc/firestore-odm`](https://github.com/brunovorisc/firestore-odm).
Readme
@premiuz/odm
ODM do Premiuz para Firestore. Encapsula os modelos de domínio e expõe repositórios type-safe. Construído sobre @vorisc/firestore-odm.
Setup
import { createOdmClient } from '@premiuz/odm'
export const odm = createOdmClient({
serviceAccount: process.env.FIREBASE_SERVICE_ACCOUNT!,
database: '(default)', // opcional
emulator: false, // ou true / { host, port }
})Métodos do BaseRepository
Todos os repositórios expõem a mesma interface:
| Método | Descrição |
| -------------------------------------------------- | ------------------------------------------- |
| findUnique({ where }) | Busca um documento ou retorna null |
| findUniqueOrThrow({ where }) | Busca um documento ou lança NotFoundError |
| findFirst({ where, orderBy }) | Primeiro resultado ou null |
| findMany({ where, orderBy, take, skip, cursor }) | Lista com filtros e paginação |
| create({ data }) | Cria um documento |
| createMany({ data }) | Cria vários em lote |
| update({ where, data }) | Atualiza um documento |
| updateMany({ where, data }) | Atualiza vários em lote |
| upsert({ where, create, update }) | Cria ou atualiza |
| delete({ where }) | Remove por id |
| deleteMany({ where? }) | Remove vários (ou todos) |
| count({ where? }) | Conta documentos |
| aggregate({ where?, _count, _sum, _avg }) | Agregações nativas |
Transações
await odm.runTransaction(async (tx) => {
const wallet = await tx.wallet.findUniqueOrThrow({
where: { userId }
})
await tx.wallet.update({
where: { id: wallet.id },
data: { balance: wallet.balance - amount }
})
await tx.creditTransaction.create({ data: { ... } })
})