@goatlab/fluent-firebase
v0.9.27
Published
Readable query Interface & API generator for TS and Node
Downloads
560
Readme
@goatlab/fluent-firebase
Firebase/Firestore connector for the Goat Fluent query interface. Provides a type-safe, schema-validated query builder for Firebase Firestore with support for complex queries, batch operations, and relations.
Installation
npm install @goatlab/fluent-firebase
# or
yarn add @goatlab/fluent-firebase
# or
pnpm add @goatlab/fluent-firebaseBasic Usage
import { FirebaseInit, FirebaseConnector } from '@goatlab/fluent-firebase'
import { Entity, ObjectType, f } from '@goatlab/fluent'
import { z } from 'zod'
// Initialize Firebase
FirebaseInit({
databaseName: 'your-project-id',
serviceAccount: './path/to/service-account.json', // optional
emulator: false // set to true for local development
})
// Define your entity
@Entity('users')
@ObjectType()
export class UserEntity {
@f.Column()
id: string
@f.Column()
name: string
@f.Column()
email: string
@f.Column()
created: Date
}
// Define your schema
const UserSchema = z.object({
id: z.string().optional(),
name: z.string(),
email: z.string().email(),
created: z.date().optional()
})
// Create a repository
class UserRepository extends FirebaseConnector<UserEntity> {
constructor() {
super({
entity: UserEntity,
inputSchema: UserSchema,
outputSchema: UserSchema // optional, defaults to inputSchema
})
}
}
// Use the repository
const userRepo = new UserRepository()
// Insert
const user = await userRepo.insert({
name: 'John Doe',
email: '[email protected]'
})
// Query
const users = await userRepo.findMany({
where: { email: '[email protected]' },
limit: 10,
orderBy: [{ created: 'desc' }]
})
// Update
await userRepo.updateById(user.id, { name: 'Jane Doe' })
// Delete
await userRepo.deleteById(user.id)Key Features
- Type-safe queries with TypeScript and Zod schema validation
- Fluent query interface compatible with other Goat Fluent connectors
- Complex query support including AND/OR conditions and multiple operators
- Batch operations for efficient bulk inserts and updates
- Relations support for loading related data
- Firebase Emulator support for local development and testing
- Raw access to Firebase Admin SDK when needed
License
MIT
