firestore-auto-sync
Version:
<a href="https://www.npmjs.com/package/firestore-auto-sync"><img src="https://img.shields.io/npm/v/firestore-auto-sync.svg" alt="Total Downloads"></a> <a href="https://www.npmjs.com/package/firestore-auto-sync"><img src="https://img.shields.io/npm/dw/fire
89 lines (88 loc) • 3.19 kB
TypeScript
import { DocumentSnapshot } from "firebase-admin/firestore";
import type { Change } from "firebase-functions";
import type { OPaths } from "./helpers/types";
import { WhereClause } from "./helpers/typesFirestore";
export declare type UpdateFoundDocs<T = Record<string, any>> = {
/**
* The target collection to grab documents in to update
*/
collection: string;
/**
* The name of the prop on the origin document that is should be equal to `keyTarget`
* @example 'id'
*/
keyOrigin: OPaths<T>;
/**
* The name of the prop on the target document in this collection that is should be equal to `keyOrigin`
*
* Can be left out if `{keyTarget}` is part of the `collection` path
* @example 'userId'
*/
keyTarget?: string;
/**
* A custom where function to search docs by
*
* Can be left out if `keyTarget` is provided
* @example ['userIds.{keyTarget}', '==', true]
*/
where?: WhereClause[];
};
export declare type UpdateSpecificDoc = {
/**
* The target doc that must be updated in reaction
*
* Can be have `{keyTarget}` is part of the `collection` path
*/
doc: string;
};
export declare type Reaction<T extends Record<string, any> = Record<string, any>> = {
/**
* The name of the prop which when updated will cause all `updates` to be triggered in reaction
*/
prop: OPaths<T>;
/**
* When set, it will only do reactive updates if this `value`
*/
value?: string;
/**
* The updates you want to make
*/
updates: ((UpdateSpecificDoc | UpdateFoundDocs<T>) & {
/**
* The name of the prop that must be updated to be the same value as the `prop` defined in this Reaction
*/
propToUpdate: string;
})[];
};
export declare type Increment<T extends Record<string, any> = Record<string, any>> = {
/**
* @param newData — undefined if this is a deletion, otherwise the updated/created record
* @param oldData — undefined if this is a creation, otherwise the updated/deleted record
* - returning a positive number will increment the `propToIncrement` by this amount
* - returning a positive number will decrement the `propToIncrement` by this amount
* - returning 0 will stop execution
*/
incrementBy: (newData?: T, oldData?: T) => number;
/**
* 変更先の情報
*/
increments: ((UpdateSpecificDoc | UpdateFoundDocs<T>) & {
/**
* The name of the prop that must be incremented with Firestore's increment helper fn
*/
propToIncrement: string;
})[];
};
export declare const executeReaction: ({ change, reaction, }: {
change: Change<DocumentSnapshot>;
reaction: Reaction<any>;
}) => Promise<Promise<any>>;
export declare const executeIncrement: ({ change, increment, }: {
change: Change<DocumentSnapshot>;
increment: Increment<any>;
}) => Promise<Promise<any>>;
export declare const firebaseAutoSync: ({ change, reaction, increment, }: {
change: Change<DocumentSnapshot>;
reaction?: Reaction<any> | undefined;
increment?: Increment<any> | undefined;
}) => Promise<any>;