Skip to content

Configuration

createRepositoryConfig()

ParameterTypeDefaultDescription
pathstringFirestore collection path
isGroupbooleantrue for collection group queries
foreignKeysstring[]Keys for get.by* (unique retrieval)
queryKeysstring[]Keys for query.by* (list retrieval)
refCbFunctionBuilds the DocumentReference
documentKeystring"docId"Field auto-injected with the Firestore document ID
pathKeystring"documentPath"Field auto-injected with the full Firestore path
createdKeystring"createdAt"Field auto-set on creation
updatedKeystring"updatedAt"Field auto-updated on every write

Simple collection

typescript
users: createRepositoryConfig(userSchema)({
  path:        "users",
  isGroup:     false,
  foreignKeys: ["docId", "email"] as const,
  queryKeys:   ["isActive", "name"] as const,
  documentKey: "docId",
  pathKey:     "documentPath",
  createdKey:  "createdAt",
  updatedKey:  "updatedAt",
  refCb: (db: Firestore, docId: string) => db.collection("users").doc(docId),
});

Sub-collection

The refCb receives parent IDs in order, then the document ID last.

typescript
comments: createRepositoryConfig<CommentModel>()({
  path:        "comments",
  isGroup:     true,
  foreignKeys: ["docId", "postId", "userId"] as const,
  queryKeys:   ["postId", "userId"] as const,
  documentKey: "docId",
  pathKey:     "documentPath",
  createdKey:  "createdAt",
  updatedKey:  "updatedAt",
  refCb: (db: Firestore, postId: string, commentId: string) =>
    db.collection("posts").doc(postId).collection("comments").doc(commentId),
});

buildRepositoryRelations()

Declares relationships between repositories.

typescript
const mappingWithRelations = buildRepositoryRelations(repositoryMapping, {
  users: {
    docId:  { repo: "posts",    key: "userId", type: "many" as const },
  },
  posts: {
    userId: { repo: "users",    key: "docId",  type: "one"  as const },
    docId:  { repo: "comments", key: "postId", type: "many" as const },
  },
  comments: {
    postId: { repo: "posts",    key: "docId",  type: "one"  as const },
    userId: { repo: "users",    key: "docId",  type: "one"  as const },
  },
});

Validation

TypeScript validates that repository names, foreign keys, and relation keys all exist in your mapping.

createRepositoryMapping()

typescript
import { getFirestore } from "firebase-admin/firestore";

const db = getFirestore();
export const repos = createRepositoryMapping(db, mappingWithRelations);

Generated methods overview

NamespaceMethodDescription
(root)create(data)Create with auto ID
(root)set(id, data, options?)Create / replace with specific ID
(root)update(id, data)Partial update
(root)delete(id)Delete document
getby{ForeignKey}(value)Get single document
getbyList(key, values[])Get multiple documents by value list
queryby{QueryKey}(value, options?)Query by key
queryby(options)Generic query (full options)
querygetAll(options?)Get all documents
querypaginate(options)Cursor-based pagination
querypaginateAll(options)Async generator over all pages
queryonSnapshot(options, cb, errCb?)Real-time listener
batchcreate()Create a batch builder (max 500 ops)
bulkset / update / deleteBulk operations (auto-split)
populate(doc, key | options)Populate related documents
aggregatecount / sum / averageServer-side aggregations
transactionrun(callback)Firestore transaction