Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
feat(firestore): add adapter constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgubaidullin committed Oct 3, 2023
1 parent 830f31d commit 68b00b3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/firestore/src/adapter.ts
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();
},
};
};

0 comments on commit 68b00b3

Please sign in to comment.