This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
forked from grammyjs/storages
-
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.
feat(firestore): add adapter constructor
- Loading branch information
1 parent
830f31d
commit 68b00b3
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
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,31 @@ | ||
import { CollectionReference } from './collection-reference'; | ||
import { Document, createDocument, getDocumentValue } from './document'; | ||
import { Encoder } from './encoder'; | ||
import { Storage } from './storage'; | ||
|
||
export const createAdapter = | ||
<A, B>(encoder: Encoder<A, B>) => | ||
(collection: CollectionReference<Document<B>>): Storage<A> => { | ||
return { | ||
read: async (key: string) => { | ||
const snapshot = await collection.doc(key).get(); | ||
const document = snapshot.data(); | ||
if (!document) { | ||
return undefined; | ||
} | ||
const encoded = getDocumentValue(document); | ||
if (!encoded) { | ||
return undefined; | ||
} | ||
return encoder.decode(encoded); | ||
}, | ||
write: async (key: string, value: A) => { | ||
const encoded = encoder.encode(value); | ||
const doc: Document<B> = createDocument(encoded); | ||
await collection.doc(key).set(doc); | ||
}, | ||
delete: async (key: string) => { | ||
await collection.doc(key).delete(); | ||
}, | ||
}; | ||
}; |