-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added methods for transaction to easily work with runTransaction
- Loading branch information
1 parent
9025b5c
commit ca1188e
Showing
9 changed files
with
149 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import { DocumentData } from "../types/DocumentTypes" | ||
import { AdminDocumentReference, AdminWriteBatch, DocumentReference, FirestoreDocumentData, ReactNativeDocumentReference, ReactNativeWriteBatch, WebDocumentReference, WebWriteBatch, WriteBatch } from "../types/FirestoreTypes" | ||
import { DocumentReference, WriteBatch } from "../types/FirestoreTypes" | ||
|
||
export type CommitResult = Promise<unknown> | ||
export interface DocumentBatchHandler<WB extends WriteBatch, DR extends DocumentReference> { | ||
export interface DocumentBatchHandler { | ||
create(): WriteBatch | ||
set(batch: WB, ref: DR, data: DocumentData): void | ||
update(batch: WB, ref: DR, data: DocumentData): void | ||
delete(batch: WB, ref: DR): void | ||
commit(batch: WB): CommitResult | ||
set(batch: WriteBatch, ref: DocumentReference, data: DocumentData): void | ||
update(batch: WriteBatch, ref: DocumentReference, data: DocumentData): void | ||
delete(batch: WriteBatch, ref: DocumentReference): void | ||
commit(batch: WriteBatch): CommitResult | ||
} | ||
export interface WebDocumentBatchHandler extends DocumentBatchHandler<WebWriteBatch, WebDocumentReference<FirestoreDocumentData>> { | ||
export interface WebDocumentBatchHandler extends DocumentBatchHandler { | ||
|
||
} | ||
export interface AdminDocumentBatchHandler extends DocumentBatchHandler<AdminWriteBatch, AdminDocumentReference<FirestoreDocumentData>> { | ||
export interface AdminDocumentBatchHandler extends DocumentBatchHandler { | ||
|
||
} | ||
export interface ReactNativeDocumentBatchHandler extends DocumentBatchHandler<ReactNativeWriteBatch, ReactNativeDocumentReference<FirestoreDocumentData>> { | ||
export interface ReactNativeDocumentBatchHandler extends DocumentBatchHandler { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,64 @@ | ||
import { getFirestore } from "firebase-admin/firestore"; | ||
import { getFirestore, Transaction, CollectionReference, DocumentReference, WriteBatch, SetOptions } from "firebase-admin/firestore"; | ||
import { | ||
AdminDocumentBatchHandler, | ||
AdminDocumentClassHandler, | ||
AdminWriteBatch, | ||
AdminDocumentData, | ||
AdminDocumentReference, | ||
AdminCollectionReference, | ||
AdminSetOptions | ||
DocumentBatchHandler, | ||
DocumentClassHandler, | ||
DocumentData | ||
} from "../src" | ||
|
||
export class DocumentHandler implements AdminDocumentClassHandler { | ||
|
||
export class DocumentHandler implements DocumentClassHandler { | ||
collection(name: string) { | ||
return getFirestore().collection(name) | ||
} | ||
getDoc(ref: AdminDocumentReference<AdminDocumentData>) { | ||
getDoc(ref: DocumentReference) { | ||
return ref.get() | ||
} | ||
addDoc(collectionRef: AdminCollectionReference<AdminDocumentData>, data: Partial<unknown>) { | ||
addDoc(collectionRef: CollectionReference, data: Partial<unknown>) { | ||
return collectionRef.add(data) | ||
} | ||
setDoc(ref: AdminDocumentReference<AdminDocumentData>, data: Partial<unknown>, options: AdminSetOptions) { | ||
setDoc(ref: DocumentReference, data: Partial<unknown>, options: SetOptions) { | ||
return ref.set(data, options) | ||
} | ||
updateDoc(ref: AdminDocumentReference<AdminDocumentData>, data: Partial<unknown>) { | ||
updateDoc(ref: DocumentReference, data: Partial<unknown>) { | ||
return ref.update(data) | ||
} | ||
deleteDoc(ref: AdminDocumentReference<AdminDocumentData>) { | ||
deleteDoc(ref: DocumentReference) { | ||
return ref.delete() | ||
} | ||
doc(ref: AdminCollectionReference<AdminDocumentData>, id: string) { | ||
doc(ref: CollectionReference, id: string) { | ||
return ref.doc(id) | ||
} | ||
|
||
transactionCreate(transaction: Transaction, ref: any, data: Partial<unknown>) { | ||
return transaction.create(ref, data) | ||
} | ||
transactionDelete(transaction: Transaction, ref: any) { | ||
return transaction.delete(ref) | ||
} | ||
transactionGet(transaction: Transaction, ref: any) { | ||
return transaction.get(ref) | ||
} | ||
transactionSet(transaction: Transaction, ref: any, data: Partial<unknown>, options: SetOptions) { | ||
return transaction.set(ref, data, options) | ||
} | ||
transactionUpdate(transaction: Transaction, ref: any, data: Partial<unknown>) { | ||
return transaction.update(ref, data) | ||
} | ||
} | ||
export class BatchHandler implements AdminDocumentBatchHandler { | ||
export class BatchHandler implements DocumentBatchHandler { | ||
create() { | ||
return getFirestore().batch() | ||
} | ||
set(batch: AdminWriteBatch, ref: AdminDocumentReference<AdminDocumentData>, data: AdminDocumentData) { | ||
set(batch: WriteBatch, ref: DocumentReference, data: DocumentData) { | ||
batch.set(ref, data) | ||
} | ||
update(batch: AdminWriteBatch, ref: AdminDocumentReference<AdminDocumentData>, data: AdminDocumentData) { | ||
update(batch: WriteBatch, ref: DocumentReference, data: DocumentData) { | ||
batch.update(ref, data) | ||
} | ||
delete(batch: AdminWriteBatch, ref: AdminDocumentReference<AdminDocumentData>) { | ||
delete(batch: WriteBatch, ref: DocumentReference) { | ||
batch.delete(ref) | ||
} | ||
commit(batch: AdminWriteBatch) { | ||
commit(batch: WriteBatch) { | ||
return batch.commit() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { expect, test } from "@jest/globals" | ||
import { credential } from "firebase-admin" | ||
import { initializeApp } from "firebase-admin/app" | ||
import { getFirestore } from "firebase-admin/firestore" | ||
import { DocumentClass, DocumentClassDefineProps } from "../src" | ||
import { DocumentHandler } from "./handler" | ||
|
||
initializeApp({ | ||
credential: credential.cert("serviceAccountKey.json") | ||
}) | ||
|
||
class Country extends DocumentClass { | ||
name!: string | ||
code!: string | ||
|
||
definition({ define, defineCollection, defineHandler }: DocumentClassDefineProps): void { | ||
define<typeof this.name>("name", "string") | ||
define<typeof this.code>("code", "string") | ||
|
||
defineCollection(() => getFirestore().collection("country")) | ||
defineHandler(new DocumentHandler()) | ||
} | ||
} | ||
|
||
test("runTransaction set", async () => { | ||
await getFirestore().runTransaction(async (transaction) => { | ||
const country = new Country("DK") | ||
country.code = "DKE" | ||
country.name = "Denmark" | ||
await country.withTransaction(transaction).set() | ||
}) | ||
}) | ||
test("runTransaction update", async () => { | ||
await getFirestore().runTransaction(async (transaction) => { | ||
const country = new Country("DK") | ||
expect(await country.withTransaction(transaction).get()).toBeTruthy() | ||
country.code = "DK" | ||
await country.withTransaction(transaction).update() | ||
}) | ||
}) | ||
test("runTransaction get check", async () => { | ||
await getFirestore().runTransaction(async (transaction) => { | ||
const country = new Country("DKE") | ||
expect(await country.withTransaction(transaction).get()).toBeFalsy() | ||
}) | ||
}) | ||
test("runTransaction delete", async () => { | ||
await getFirestore().runTransaction(async (transaction) => { | ||
const country = new Country("DK") | ||
expect(await country.withTransaction(transaction).get()).toBeTruthy() | ||
country.withTransaction(transaction).delete() | ||
}) | ||
}) |