@magnetarjs/types
Advanced tools
Comparing version 0.15.0 to 0.15.1
@@ -5,2 +5,3 @@ import { Merge } from 'merge-anything'; | ||
type TupleOf<Value, Length extends number, Result extends readonly Value[] = []> = Result['length'] extends Length ? Result : TupleOf<Value, Length, [...Result, Value]>; | ||
type TupleFirst<Tuple extends readonly unknown[]> = Tuple extends Readonly<[ | ||
@@ -14,2 +15,10 @@ infer Head, | ||
]> ? Tail : never; | ||
type TupleLast<Tuple extends readonly unknown[]> = Tuple extends Readonly<[ | ||
...infer _, | ||
infer Last | ||
]> ? Last : never; | ||
type TupleExcludingLast<Tuple extends readonly unknown[]> = Tuple extends Readonly<[ | ||
...infer Rest, | ||
unknown | ||
]> ? Rest : never; | ||
@@ -153,3 +162,17 @@ type DeepGet<T, K extends readonly unknown[]> = K extends [] ? T : T extends { | ||
type CollectionInstance<DocDataType extends Record<string, any> = Record<string, any>, GranularTypes extends { | ||
type StoreName = string; | ||
type DocMetadata = { | ||
data: Record<string, any> | undefined; | ||
id: string; | ||
/** | ||
* In case the doc was returned optimisticly (from the local store data) then `exists` will be `'unknown'` | ||
*/ | ||
exists: boolean | 'unknown'; | ||
/** | ||
* In case the doc was returned optimisticly (from the local store data) then `metadata` will be absent | ||
*/ | ||
metadata?: Record<string, any>; | ||
}; | ||
type DocInstance<DocDataType extends Record<string, any> = Record<string, any>, GranularTypes extends { | ||
insert: Record<string, any>; | ||
@@ -162,34 +185,41 @@ } = { | ||
*/ | ||
data: Map<string, DocDataType>; | ||
data: DocDataType | undefined; | ||
/** | ||
* Represents `data.size` of your collection, however, calling `fetchCount()` will update just this `count`, from where on it will no longer be linked to `data.size`. | ||
* `collection` is available on every document for chaining | ||
* @example doc('001').collection('items') | ||
*/ | ||
count: number; | ||
collection: CollectionFn; | ||
/** | ||
* `doc` is available on every collection for chaining | ||
* @see {@link DocFn} | ||
* @example collection('pokedex').doc('001') | ||
* The id of the document. When this is a nested document, it will not include the full path, only the final part | ||
* @example '001' | ||
*/ | ||
doc: DocFn<DocDataType>; | ||
/** | ||
* The id of the collection. When this is a nested collection, it will not include the full path, only the final part | ||
* @example 'items' | ||
*/ | ||
id: string; | ||
/** | ||
* The full path of the collection | ||
* @example 'pokedex/001/items' | ||
* @example 'pokedex' | ||
* The full path of the document | ||
* @example 'pokedex/001' | ||
*/ | ||
path: string; | ||
/** | ||
* Returns the open stream promise of this collection, dependant on which `where`/`query`/`limit`/`orderBy` filters was used. | ||
* Meta data on the doc's existence | ||
* ```js | ||
* await magnetar.db('users/abc123').fetch() // this will update the `exists` metadata | ||
* console.log(`magnetar.db('users/abc123').exists → `, magnetar.db('users/abc123').exists) | ||
* ``` | ||
* This way you can easily display in your UI if a document exists or not without having to manage that state yourself | ||
* - `undefined` at first | ||
* - `'error'` when fetched but the API call returned an error | ||
* - `false` when fetched once and the document does not exist | ||
* - `true` when successfully fetched | ||
*/ | ||
exists: undefined | 'error' | boolean; | ||
/** | ||
* Returns the open stream promise of this doc. | ||
* | ||
* Returns `null` when there is no open stream. | ||
* | ||
* This promise will resolve when `collection().closeStream()` is called, or when the stream closed because of an error. | ||
* This promise will resolve when `doc().closeStream()` is called, or when the stream closed because of an error. | ||
*/ | ||
streaming: () => Promise<void> | null; | ||
/** | ||
* Close the stream of this collection, dependant on which `where`/`query`/`limit`/`orderBy` filters was used. | ||
* Close the stream of this doc. | ||
* | ||
@@ -200,15 +230,5 @@ * Does nothing if there is no open stream. | ||
/** | ||
* Close all streams of this collection, no matter which `where`/`query`/`limit`/`orderBy` filters were used. | ||
* | ||
* Does nothing if there are no open streams. | ||
*/ | ||
closeAllStreams: () => void; | ||
/** | ||
* @see {@link MagnetarFetchCountAction} | ||
*/ | ||
fetchCount: MagnetarFetchCountAction; | ||
/** | ||
* @see {@link MagnetarFetchAction} | ||
*/ | ||
fetch: MagnetarFetchAction<DocDataType, 'collection'>; | ||
fetch: MagnetarFetchAction<DocDataType, 'doc'>; | ||
/** | ||
@@ -223,46 +243,23 @@ * @see {@link MagnetarStreamAction} | ||
/** | ||
* @see {@link MagnetarDeleteAction} | ||
* @see {@link MagnetarWriteAction} | ||
*/ | ||
delete: MagnetarDeleteAction; | ||
merge: MagnetarWriteAction<DocDataType>; | ||
/** | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
* @see {@link MagnetarWriteAction} | ||
*/ | ||
orderBy: (fieldPath: OPathsWithOptional<DocDataType>, direction?: 'asc' | 'desc') => CollectionInstance<DocDataType, GranularTypes>; | ||
assign: MagnetarWriteAction<DocDataType>; | ||
/** | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
* @see {@link MagnetarWriteAction} | ||
*/ | ||
limit: (limitCount: number) => CollectionInstance<DocDataType, GranularTypes>; | ||
replace: MagnetarWriteAction<DocDataType>; | ||
/** | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
* @see {@link MagnetarDeletePropAction} | ||
*/ | ||
where: <Path extends OPathsWithOptional<DocDataType>, WhereOp extends WhereFilterOp>(fieldPath: Path, operator: WhereOp, value: WhereFilterValue<WhereOp, DefaultTo<DeepPropType<DocDataType, Path>, any>>) => CollectionInstance<DocDataType, GranularTypes>; | ||
deleteProp: MagnetarDeletePropAction<DocDataType>; | ||
/** | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
* @see {@link MagnetarDeleteAction} | ||
*/ | ||
query: (query: Query<DocDataType>) => CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
*/ | ||
startAfter(docSnapshot: Record<string, any>): CollectionInstance<DocDataType, GranularTypes>; | ||
startAfter(...fieldValues: unknown[]): CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* Meta data from the last fetch call | ||
*/ | ||
fetched: FetchMetaDataCollection; | ||
delete: MagnetarDeleteAction; | ||
}; | ||
type StoreName = string; | ||
type DocMetadata = { | ||
data: Record<string, any> | undefined; | ||
id: string; | ||
/** | ||
* In case the doc was returned optimisticly (from the local store data) then `exists` will be `'unknown'` | ||
*/ | ||
exists: boolean | 'unknown'; | ||
/** | ||
* In case the doc was returned optimisticly (from the local store data) then `metadata` will be absent | ||
*/ | ||
metadata?: Record<string, any>; | ||
}; | ||
/** | ||
@@ -285,2 +282,44 @@ * Returns a _deep_ Partial of `O`, where all props & nested props are optional | ||
/** | ||
* This function will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyWritePayload<DocDataType extends Record<string, any> = Record<string, any>> = (payload: PartialDeep<DocDataType>, docId?: string | void) => PartialDeep<DocDataType>; | ||
/** | ||
* This function will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyDeletePropPayload = (payload: string | string[]) => string | string[]; | ||
/** | ||
* This function will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyReadPayload = (payload: Record<string, any> | void, docId?: string | void) => Record<string, any> | void; | ||
/** | ||
* These functions will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyPayloadFnMap<DocDataType extends Record<string, any> = Record<string, any>> = { | ||
insert?: ModifyWritePayload<DocDataType>; | ||
merge?: ModifyWritePayload<DocDataType>; | ||
assign?: ModifyWritePayload<DocDataType>; | ||
replace?: ModifyWritePayload<DocDataType>; | ||
write?: ModifyWritePayload<DocDataType>; | ||
deleteProp?: ModifyDeletePropPayload; | ||
read?: ModifyReadPayload; | ||
stream?: ModifyReadPayload; | ||
fetch?: ModifyReadPayload; | ||
}; | ||
/** | ||
* These functions will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyPayloadFnsMap = { | ||
insert: ModifyWritePayload[]; | ||
merge: ModifyWritePayload[]; | ||
assign: ModifyWritePayload[]; | ||
replace: ModifyWritePayload[]; | ||
write: ModifyWritePayload[]; | ||
deleteProp: ModifyDeletePropPayload[]; | ||
delete: never[]; | ||
read: ModifyReadPayload[]; | ||
stream: ModifyReadPayload[]; | ||
fetch: ModifyReadPayload[]; | ||
}; | ||
/** | ||
* Can be used to modify docs that come in from 'stream' or 'fetch' actions, before they are added to your store data. When returning `undefined` they will be discarded & won't be added to the store data. | ||
@@ -323,2 +362,140 @@ */ | ||
/** | ||
* these are all the actions that Magnetar streamlines, whichever plugin is used | ||
* these actions are executable from a `MagnetarModule` and handled by each plugin individually | ||
*/ | ||
type ActionName = 'fetch' | 'fetchCount' | 'stream' | 'insert' | 'merge' | 'assign' | 'replace' | 'deleteProp' | 'delete'; | ||
/** | ||
* You can pass options to this action specifically; | ||
* This is what the dev can provide as second param when executing any action in addition to the payload. | ||
* @example | ||
* // first update the server and await that before updating the local store: | ||
* { executionOrder: ['remote', 'local'] } | ||
* @example | ||
* // don't throw errors for this action, wherever it might fail | ||
* { onError: 'continue' } | ||
*/ | ||
type ActionConfig<DocDataType extends Record<string, any> = Record<string, any>> = { | ||
executionOrder?: StoreName[]; | ||
onError?: 'revert' | 'continue' | 'stop'; | ||
modifyPayloadOn?: ModifyPayloadFnMap<DocDataType>; | ||
modifyReadResponseOn?: ModifyReadResponseFnMap<DocDataType>; | ||
on?: EventNameFnMap; | ||
/** | ||
* An option for remote stores like Firestore to delay a sync to the server and batch any additional actions made during the `syncDebounceMs`. | ||
*/ | ||
syncDebounceMs?: number; | ||
}; | ||
/** | ||
* Opens a continuous stream to a document or collection. | ||
* @returns the open stream promise. This will never resolve as long as the stream is open. | ||
*/ | ||
type MagnetarStreamAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload?: any | void, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<void>; | ||
/** | ||
* Fetches document(s) and adds the data to your local store's state. | ||
* Fetch is optimistic by default — if it can find the doc's data in your local state, it will return that and prevent any remote fetches. | ||
* You can force a re-fetch by passing `{ force: true }` | ||
* @returns the document(s) data that was fetched. If you need to access other metadata that was retrieved during fetching, you can use `modifyReadResponse.added`. | ||
* @example | ||
* const bulbasaur = magnetar.collection('pokedex').doc('001') | ||
* bulbasaur.fetch() // does nothing if already fetched once | ||
* @example | ||
* const bulbasaur = magnetar.collection('pokedex').doc('001') | ||
* bulbasaur.fetch({ force: true }) // makes API call to remote store | ||
* @example | ||
* const pokedex = magnetar.collection('pokedex') | ||
* pokedex.fetch() // does nothing if already fetched once | ||
* @example | ||
* const pokedex = magnetar.collection('pokedex') | ||
* pokedex.fetch({ force: true }) // makes API call to remote store | ||
*/ | ||
type MagnetarFetchAction<DocDataType extends Record<string, any> = Record<string, any>, calledFrom extends 'collection' | 'doc' = 'collection' | 'doc'> = (payload?: { | ||
force?: boolean; | ||
} | Record<string, any> | void, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<calledFrom extends 'collection' ? Map<string, DocDataType> : DocDataType | undefined>; | ||
/** | ||
* Fetches a collection's document count and caches this count to your local store's state. | ||
* @returns the document count that was fetched. | ||
* @example | ||
* magnetar.collection('pokedex').count // 0 | ||
* | ||
* const count = await magnetar.collection('pokedex').fetchCount() | ||
* count // 151 | ||
* magnetar.collection('pokedex').count // 151 | ||
*/ | ||
type MagnetarFetchCountAction = () => Promise<number>; | ||
/** | ||
* @returns The new `doc()` instance after inserting. You can access the inserted `id` by checking this returned instance. | ||
* @example | ||
* const newDoc = collection('myDocs').insert({ some: 'payload' }) | ||
* newDoc.id // the generated id | ||
* newDoc.data // { some: 'payload' } | ||
*/ | ||
type MagnetarInsertAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload: DocDataType, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<DocInstance<DocDataType>>; | ||
/** | ||
* @returns the new document data after applying the changes to the local document (including any modifications from modifyPayloadOn) | ||
*/ | ||
type MagnetarWriteAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload: PartialDeep<DocDataType>, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<DocDataType>; | ||
/** | ||
* @returns the new document data after applying the changes to the local document (including any modifications from modifyPayloadOn) | ||
*/ | ||
type MagnetarDeletePropAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload: keyof DocDataType | string | (keyof DocDataType | string)[], | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<Partial<DocDataType>>; | ||
/** | ||
* @param {*} [payload] When executing on a doc: no payload needed. When executing on a collection: you need to pass the document ID you want to delete. | ||
* @param {ActionConfig} [actionConfig] | ||
* @example collection('pokedex').delete('001') | ||
* @example doc('pokedex/001').delete() | ||
*/ | ||
type MagnetarDeleteAction = (payload?: any, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig) => Promise<void>; | ||
/** | ||
* All fetch promises with the payload passed to `fetch(payload)` as key (JSON.stringify) and the "fetch promise" as value. In case `fetch()` had no payload, use `undefined` | ||
*/ | ||
type FetchPromises = Map<string, Promise<any>>; | ||
/** | ||
* Meta data on the last fetch call for a collection(). | ||
* - easily fetch more data with: | ||
* ```js | ||
* dbMyCollection.startAfter(dbMyCollection.fetched.cursor).fetch()` | ||
* ``` | ||
* - easily know if you fetched everything when using `startAfter` fetches: | ||
* ```js | ||
* if (dbMyCollection.fetched.reachedEnd) alert('fetched everything already') | ||
* ``` | ||
*/ | ||
type FetchMetaDataCollection = { | ||
/** Wether or not the end was reached, in case there is no `limit` this is always true */ | ||
reachedEnd: boolean; | ||
/** The last fetched doc, in a format defined by the Plugin */ | ||
cursor: unknown; | ||
}; | ||
type MergeDeep<A0 extends Record<string | number | symbol, unknown>, A1 extends Record<string | number | symbol, unknown>> = Merge<A0, [A1]>; | ||
@@ -673,44 +850,2 @@ | ||
/** | ||
* This function will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyWritePayload<DocDataType extends Record<string, any> = Record<string, any>> = (payload: PartialDeep<DocDataType>, docId?: string | void) => PartialDeep<DocDataType>; | ||
/** | ||
* This function will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyDeletePropPayload = (payload: string | string[]) => string | string[]; | ||
/** | ||
* This function will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyReadPayload = (payload: Record<string, any> | void, docId?: string | void) => Record<string, any> | void; | ||
/** | ||
* These functions will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyPayloadFnMap<DocDataType extends Record<string, any> = Record<string, any>> = { | ||
insert?: ModifyWritePayload<DocDataType>; | ||
merge?: ModifyWritePayload<DocDataType>; | ||
assign?: ModifyWritePayload<DocDataType>; | ||
replace?: ModifyWritePayload<DocDataType>; | ||
write?: ModifyWritePayload<DocDataType>; | ||
deleteProp?: ModifyDeletePropPayload; | ||
read?: ModifyReadPayload; | ||
stream?: ModifyReadPayload; | ||
fetch?: ModifyReadPayload; | ||
}; | ||
/** | ||
* These functions will be executed everytime BEFORE the related action is triggered. The function defined will receive the payload of the action. You can then modify and return this payload. | ||
*/ | ||
type ModifyPayloadFnsMap = { | ||
insert: ModifyWritePayload[]; | ||
merge: ModifyWritePayload[]; | ||
assign: ModifyWritePayload[]; | ||
replace: ModifyWritePayload[]; | ||
write: ModifyWritePayload[]; | ||
deleteProp: ModifyDeletePropPayload[]; | ||
delete: never[]; | ||
read: ModifyReadPayload[]; | ||
stream: ModifyReadPayload[]; | ||
fetch: ModifyReadPayload[]; | ||
}; | ||
/** | ||
* Execution order per action or action type. | ||
@@ -855,3 +990,3 @@ */ | ||
type DocInstance<DocDataType extends Record<string, any> = Record<string, any>, GranularTypes extends { | ||
type CollectionInstance<DocDataType extends Record<string, any> = Record<string, any>, GranularTypes extends { | ||
insert: Record<string, any>; | ||
@@ -864,41 +999,34 @@ } = { | ||
*/ | ||
data: DocDataType | undefined; | ||
data: Map<string, DocDataType>; | ||
/** | ||
* `collection` is available on every document for chaining | ||
* @example doc('001').collection('items') | ||
* Represents `data.size` of your collection, however, calling `fetchCount()` will update just this `count`, from where on it will no longer be linked to `data.size`. | ||
*/ | ||
collection: CollectionFn; | ||
count: number; | ||
/** | ||
* The id of the document. When this is a nested document, it will not include the full path, only the final part | ||
* @example '001' | ||
* `doc` is available on every collection for chaining | ||
* @see {@link DocFn} | ||
* @example collection('pokedex').doc('001') | ||
*/ | ||
doc: DocFn<DocDataType>; | ||
/** | ||
* The id of the collection. When this is a nested collection, it will not include the full path, only the final part | ||
* @example 'items' | ||
*/ | ||
id: string; | ||
/** | ||
* The full path of the document | ||
* @example 'pokedex/001' | ||
* The full path of the collection | ||
* @example 'pokedex/001/items' | ||
* @example 'pokedex' | ||
*/ | ||
path: string; | ||
/** | ||
* Meta data on the doc's existence | ||
* ```js | ||
* await magnetar.db('users/abc123').fetch() // this will update the `exists` metadata | ||
* console.log(`magnetar.db('users/abc123').exists → `, magnetar.db('users/abc123').exists) | ||
* ``` | ||
* This way you can easily display in your UI if a document exists or not without having to manage that state yourself | ||
* - `undefined` at first | ||
* - `'error'` when fetched but the API call returned an error | ||
* - `false` when fetched once and the document does not exist | ||
* - `true` when successfully fetched | ||
*/ | ||
exists: undefined | 'error' | boolean; | ||
/** | ||
* Returns the open stream promise of this doc. | ||
* Returns the open stream promise of this collection, dependant on which `where`/`query`/`limit`/`orderBy` filters was used. | ||
* | ||
* Returns `null` when there is no open stream. | ||
* | ||
* This promise will resolve when `doc().closeStream()` is called, or when the stream closed because of an error. | ||
* This promise will resolve when `collection().closeStream()` is called, or when the stream closed because of an error. | ||
*/ | ||
streaming: () => Promise<void> | null; | ||
/** | ||
* Close the stream of this doc. | ||
* Close the stream of this collection, dependant on which `where`/`query`/`limit`/`orderBy` filters was used. | ||
* | ||
@@ -909,5 +1037,15 @@ * Does nothing if there is no open stream. | ||
/** | ||
* Close all streams of this collection, no matter which `where`/`query`/`limit`/`orderBy` filters were used. | ||
* | ||
* Does nothing if there are no open streams. | ||
*/ | ||
closeAllStreams: () => void; | ||
/** | ||
* @see {@link MagnetarFetchCountAction} | ||
*/ | ||
fetchCount: MagnetarFetchCountAction; | ||
/** | ||
* @see {@link MagnetarFetchAction} | ||
*/ | ||
fetch: MagnetarFetchAction<DocDataType, 'doc'>; | ||
fetch: MagnetarFetchAction<DocDataType, 'collection'>; | ||
/** | ||
@@ -922,160 +1060,31 @@ * @see {@link MagnetarStreamAction} | ||
/** | ||
* @see {@link MagnetarWriteAction} | ||
* @see {@link MagnetarDeleteAction} | ||
*/ | ||
merge: MagnetarWriteAction<DocDataType>; | ||
delete: MagnetarDeleteAction; | ||
/** | ||
* @see {@link MagnetarWriteAction} | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
*/ | ||
assign: MagnetarWriteAction<DocDataType>; | ||
orderBy: (fieldPath: OPathsWithOptional<DocDataType>, direction?: 'asc' | 'desc') => CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* @see {@link MagnetarWriteAction} | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
*/ | ||
replace: MagnetarWriteAction<DocDataType>; | ||
limit: (limitCount: number) => CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* @see {@link MagnetarDeletePropAction} | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
*/ | ||
deleteProp: MagnetarDeletePropAction<DocDataType>; | ||
where: <Path extends OPathsWithOptional<DocDataType>, WhereOp extends WhereFilterOp>(fieldPath: Path, operator: WhereOp, value: WhereFilterValue<WhereOp, DefaultTo<DeepPropType<DocDataType, Path>, any>>) => CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* @see {@link MagnetarDeleteAction} | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
*/ | ||
delete: MagnetarDeleteAction; | ||
}; | ||
/** | ||
* these are all the actions that Magnetar streamlines, whichever plugin is used | ||
* these actions are executable from a `MagnetarModule` and handled by each plugin individually | ||
*/ | ||
type ActionName = 'fetch' | 'fetchCount' | 'stream' | 'insert' | 'merge' | 'assign' | 'replace' | 'deleteProp' | 'delete'; | ||
/** | ||
* You can pass options to this action specifically; | ||
* This is what the dev can provide as second param when executing any action in addition to the payload. | ||
* @example | ||
* // first update the server and await that before updating the local store: | ||
* { executionOrder: ['remote', 'local'] } | ||
* @example | ||
* // don't throw errors for this action, wherever it might fail | ||
* { onError: 'continue' } | ||
*/ | ||
type ActionConfig<DocDataType extends Record<string, any> = Record<string, any>> = { | ||
executionOrder?: StoreName[]; | ||
onError?: 'revert' | 'continue' | 'stop'; | ||
modifyPayloadOn?: ModifyPayloadFnMap<DocDataType>; | ||
modifyReadResponseOn?: ModifyReadResponseFnMap<DocDataType>; | ||
on?: EventNameFnMap; | ||
query: (query: Query<DocDataType>) => CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* An option for remote stores like Firestore to delay a sync to the server and batch any additional actions made during the `syncDebounceMs`. | ||
* Chainable filter. Returns {@link CollectionInstance} with filter applied. | ||
*/ | ||
syncDebounceMs?: number; | ||
startAfter(docSnapshot: Record<string, any>): CollectionInstance<DocDataType, GranularTypes>; | ||
startAfter(...fieldValues: unknown[]): CollectionInstance<DocDataType, GranularTypes>; | ||
/** | ||
* Meta data from the last fetch call | ||
*/ | ||
fetched: FetchMetaDataCollection; | ||
}; | ||
/** | ||
* Opens a continuous stream to a document or collection. | ||
* @returns the open stream promise. This will never resolve as long as the stream is open. | ||
*/ | ||
type MagnetarStreamAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload?: any | void, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<void>; | ||
/** | ||
* Fetches document(s) and adds the data to your local store's state. | ||
* Fetch is optimistic by default — if it can find the doc's data in your local state, it will return that and prevent any remote fetches. | ||
* You can force a re-fetch by passing `{ force: true }` | ||
* @returns the document(s) data that was fetched. If you need to access other metadata that was retrieved during fetching, you can use `modifyReadResponse.added`. | ||
* @example | ||
* const bulbasaur = magnetar.collection('pokedex').doc('001') | ||
* bulbasaur.fetch() // does nothing if already fetched once | ||
* @example | ||
* const bulbasaur = magnetar.collection('pokedex').doc('001') | ||
* bulbasaur.fetch({ force: true }) // makes API call to remote store | ||
* @example | ||
* const pokedex = magnetar.collection('pokedex') | ||
* pokedex.fetch() // does nothing if already fetched once | ||
* @example | ||
* const pokedex = magnetar.collection('pokedex') | ||
* pokedex.fetch({ force: true }) // makes API call to remote store | ||
*/ | ||
type MagnetarFetchAction<DocDataType extends Record<string, any> = Record<string, any>, calledFrom extends 'collection' | 'doc' = 'collection' | 'doc'> = (payload?: { | ||
force?: boolean; | ||
} | Record<string, any> | void, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<calledFrom extends 'collection' ? Map<string, DocDataType> : DocDataType | undefined>; | ||
/** | ||
* Fetches a collection's document count and caches this count to your local store's state. | ||
* @returns the document count that was fetched. | ||
* @example | ||
* magnetar.collection('pokedex').count // 0 | ||
* | ||
* const count = await magnetar.collection('pokedex').fetchCount() | ||
* count // 151 | ||
* magnetar.collection('pokedex').count // 151 | ||
*/ | ||
type MagnetarFetchCountAction = () => Promise<number>; | ||
/** | ||
* @returns The new `doc()` instance after inserting. You can access the inserted `id` by checking this returned instance. | ||
* @example | ||
* const newDoc = collection('myDocs').insert({ some: 'payload' }) | ||
* newDoc.id // the generated id | ||
* newDoc.data // { some: 'payload' } | ||
*/ | ||
type MagnetarInsertAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload: DocDataType, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<DocInstance<DocDataType>>; | ||
/** | ||
* @returns the new document data after applying the changes to the local document (including any modifications from modifyPayloadOn) | ||
*/ | ||
type MagnetarWriteAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload: PartialDeep<DocDataType>, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<DocDataType>; | ||
/** | ||
* @returns the new document data after applying the changes to the local document (including any modifications from modifyPayloadOn) | ||
*/ | ||
type MagnetarDeletePropAction<DocDataType extends Record<string, any> = Record<string, any>> = (payload: keyof DocDataType | string | (keyof DocDataType | string)[], | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig<DocDataType>) => Promise<Partial<DocDataType>>; | ||
/** | ||
* @param {*} [payload] When executing on a doc: no payload needed. When executing on a collection: you need to pass the document ID you want to delete. | ||
* @param {ActionConfig} [actionConfig] | ||
* @example collection('pokedex').delete('001') | ||
* @example doc('pokedex/001').delete() | ||
*/ | ||
type MagnetarDeleteAction = (payload?: any, | ||
/** | ||
* TODO | ||
* @deprecated — should deprecated this "general" action config and replace with one specific for this action | ||
*/ | ||
actionConfig?: ActionConfig) => Promise<void>; | ||
/** | ||
* All fetch promises with the payload passed to `fetch(payload)` as key (JSON.stringify) and the "fetch promise" as value. In case `fetch()` had no payload, use `undefined` | ||
*/ | ||
type FetchPromises = Map<string, Promise<any>>; | ||
/** | ||
* Meta data on the last fetch call for a collection(). | ||
* - easily fetch more data with: | ||
* ```js | ||
* dbMyCollection.startAfter(dbMyCollection.fetched.cursor).fetch()` | ||
* ``` | ||
* - easily know if you fetched everything when using `startAfter` fetches: | ||
* ```js | ||
* if (dbMyCollection.fetched.reachedEnd) alert('fetched everything already') | ||
* ``` | ||
*/ | ||
type FetchMetaDataCollection = { | ||
/** Wether or not the end was reached, in case there is no `limit` this is always true */ | ||
reachedEnd: boolean; | ||
/** The last fetched doc, in a format defined by the Plugin */ | ||
cursor: unknown; | ||
}; | ||
@@ -1126,2 +1135,2 @@ /** | ||
export { ActionConfig, ActionName, ActionTernary, ActionType, Clauses, CollectionFn, CollectionInstance, CollectionName, DoOnFetch, DoOnFetchCount, DoOnStream, DoOnStreamFns, DocFn, DocInstance, DocMetadata, Equals, EqualsAnyOfUnion, EventFn, EventFnBefore, EventFnError, EventFnRevert, EventFnSuccess, EventName, EventNameFnMap, EventNameFnsMap, ExecutionOrderConfig, FetchCountResponse, FetchMetaDataCollection, FetchPromises, FetchResponse, GlobalConfig, Intersect, IsFullStringLiteral, Limit, MODULE_IDENTIFIER_SPLIT, MagnetarDeleteAction, MagnetarDeletePropAction, MagnetarFetchAction, MagnetarFetchCountAction, MagnetarInsertAction, MagnetarInstance, MagnetarPlugin, MagnetarStreamAction, MagnetarWriteAction, MergeDeep, ModifyDeletePropPayload, ModifyPayloadFnMap, ModifyPayloadFnsMap, ModifyReadPayload, ModifyReadResponseFnMap, ModifyReadResponseFnsMap, ModifyWritePayload, ModuleConfig, MustExecuteOnRead, OLeaves, OPathsWithOptional, OPathsWithoutOptional, OnAddedFn, OnModifiedFn, OnRemovedFn, OrderByClause, PartialDeep, PathFilterIdentifier, PathWhereIdentifier, PathWhereOrderByIdentifier, PluginActionPayloadBase, PluginActionTernary, PluginDeleteAction, PluginDeleteActionPayload, PluginDeletePropAction, PluginDeletePropActionPayload, PluginFetchAction, PluginFetchActionPayload, PluginFetchCountAction, PluginFetchCountActionPayload, PluginInsertAction, PluginInsertActionPayload, PluginInstance, PluginModuleConfig, PluginModuleSetupPayload, PluginRevertAction, PluginRevertActionPayload, PluginStreamAction, PluginStreamActionPayload, PluginWriteAction, PluginWriteActionPayload, Query, QueryClause, StoreName, StreamResponse, SyncBatch, WhereClause, WhereClauseTuple, WhereFilterOp, WhereFilterValue, WriteLock, actionNameTypeMap, getPathFilterIdentifier, getPathWhereIdentifier, getPathWhereOrderByIdentifier }; | ||
export { ActionConfig, ActionName, ActionTernary, ActionType, ArrayValues, Clauses, CollectionFn, CollectionInstance, CollectionName, DeepGet, DeepPropType, DefaultTo, DoOnFetch, DoOnFetchCount, DoOnStream, DoOnStreamFns, DocFn, DocInstance, DocMetadata, Equals, EqualsAnyOfUnion, EventFn, EventFnBefore, EventFnError, EventFnRevert, EventFnSuccess, EventName, EventNameFnMap, EventNameFnsMap, ExecutionOrderConfig, FetchCountResponse, FetchMetaDataCollection, FetchPromises, FetchResponse, GlobalConfig, Intersect, IsFullStringLiteral, Limit, MODULE_IDENTIFIER_SPLIT, MagnetarDeleteAction, MagnetarDeletePropAction, MagnetarFetchAction, MagnetarFetchCountAction, MagnetarInsertAction, MagnetarInstance, MagnetarPlugin, MagnetarStreamAction, MagnetarWriteAction, MergeDeep, ModifyDeletePropPayload, ModifyPayloadFnMap, ModifyPayloadFnsMap, ModifyReadPayload, ModifyReadResponseFnMap, ModifyReadResponseFnsMap, ModifyWritePayload, ModuleConfig, MustExecuteOnRead, OLeaves, OPathsWithOptional, OPathsWithoutOptional, OnAddedFn, OnModifiedFn, OnRemovedFn, OrderByClause, PartialDeep, PathFilterIdentifier, PathWhereIdentifier, PathWhereOrderByIdentifier, PluginActionPayloadBase, PluginActionTernary, PluginDeleteAction, PluginDeleteActionPayload, PluginDeletePropAction, PluginDeletePropActionPayload, PluginFetchAction, PluginFetchActionPayload, PluginFetchCountAction, PluginFetchCountActionPayload, PluginInsertAction, PluginInsertActionPayload, PluginInstance, PluginModuleConfig, PluginModuleSetupPayload, PluginRevertAction, PluginRevertActionPayload, PluginStreamAction, PluginStreamActionPayload, PluginWriteAction, PluginWriteActionPayload, Query, QueryClause, Split, StoreName, StreamResponse, SyncBatch, TupleExcludingFirst, TupleExcludingLast, TupleFirst, TupleLast, TupleOf, WhereClause, WhereClauseTuple, WhereFilterOp, WhereFilterValue, WriteLock, actionNameTypeMap, getPathFilterIdentifier, getPathWhereIdentifier, getPathWhereOrderByIdentifier }; |
{ | ||
"name": "@magnetarjs/types", | ||
"version": "0.15.0", | ||
"version": "0.15.1", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "description": "Magnetar shared types", |
58735
1242