-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added FirestoreDataConverterFactory support to firestore accessor factory - added example usage of system state
- Loading branch information
Showing
12 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
15 changes: 13 additions & 2 deletions
15
apps/demo-api/src/app/function/example/example.schedule.ts
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,5 +1,16 @@ | ||
import { loadExampleSystemState } from '@dereekb/demo-firebase'; | ||
import { DemoScheduleFunction } from '../function'; | ||
|
||
export const exampleUsageOfSchedule: DemoScheduleFunction = (request) => { | ||
console.log('exampleUsageOfSchedule() was called!'); | ||
export const exampleUsageOfSchedule: DemoScheduleFunction = async (request) => { | ||
const exampleSystemStateDocument = loadExampleSystemState(request.nest.demoFirestoreCollections.systemStateCollection.documentAccessor()); | ||
|
||
const currentSystemState = await exampleSystemStateDocument.snapshotData(); | ||
|
||
console.log(`exampleUsageOfSchedule() was called! Last update was at ${currentSystemState?.data.lastUpdate}`); | ||
|
||
await exampleSystemStateDocument.accessor.set({ | ||
data: { | ||
lastUpdate: new Date() | ||
} | ||
}); | ||
}; |
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,3 +1,4 @@ | ||
export * from './guestbook'; | ||
export * from './profile'; | ||
export * from './service'; | ||
export * from './system'; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './system'; |
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,19 @@ | ||
import { firestoreSubObject, firestoreDate, snapshotConverterFunctions, SystemStateStoredData, SystemStateStoredDataFieldConverterConfig, SystemStateStoredDataConverterMap } from '@dereekb/firebase'; | ||
|
||
export const EXAMPLE_SYSTEM_DATA_SYSTEM_STATE_TYPE = 'example'; | ||
|
||
export interface ExampleSystemData extends SystemStateStoredData { | ||
lastUpdate: Date; | ||
} | ||
|
||
export const exampleSystemDataConverter: SystemStateStoredDataFieldConverterConfig<ExampleSystemData> = firestoreSubObject<ExampleSystemData>({ | ||
objectField: { | ||
fields: { | ||
lastUpdate: firestoreDate({ saveDefaultAsNow: true }) | ||
} | ||
} | ||
}); | ||
|
||
export const demoSystemStateStoredDataConverterMap: SystemStateStoredDataConverterMap = { | ||
[EXAMPLE_SYSTEM_DATA_SYSTEM_STATE_TYPE]: exampleSystemDataConverter | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/firebase/src/lib/common/firestore/accessor/converter.ts
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,12 @@ | ||
import { FactoryWithInput, FactoryWithRequiredInput, Maybe } from '@dereekb/util'; | ||
import { DocumentReference, FirestoreDataConverter } from '../types'; | ||
|
||
/** | ||
* Factory used to provide an FirestoreDataConverter based on the input reference. | ||
*/ | ||
export type FirestoreDataConverterFactory<T> = FactoryWithInput<FirestoreDataConverter<T>, DocumentReference<T>>; | ||
|
||
/** | ||
* Factory used to provide an optional custom FirestoreDataConverter based on the input reference. | ||
*/ | ||
export type InterceptFirestoreDataConverterFactory<T> = FactoryWithRequiredInput<Maybe<FirestoreDataConverter<T>>, DocumentReference<T>>; |
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 +1,2 @@ | ||
export * from './user'; | ||
export * from './system'; |
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 @@ | ||
export * from './system'; |
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,100 @@ | ||
import { GrantedSysAdminRole } from '@dereekb/model'; | ||
import { AbstractFirestoreDocument } from '../../common/firestore/accessor/document'; | ||
import { FirestoreCollection, firestoreModelIdentity } from '../../common/firestore/collection/collection'; | ||
import { FirestoreContext } from '../../common/firestore/context'; | ||
import { snapshotConverterFunctions } from '../../common/firestore/snapshot/snapshot'; | ||
import { CollectionReference } from '../../common/firestore/types'; | ||
import { firestorePassThroughField } from '../../common/firestore/snapshot/snapshot.field'; | ||
import { mapObjectMap, ModelFieldMapFunctionsConfig, cachedGetter } from '@dereekb/util'; | ||
|
||
// MARK: Collection | ||
export interface SystemStateFirestoreCollections { | ||
readonly systemStateCollection: SystemStateFirestoreCollection; | ||
} | ||
|
||
export type SystemStateTypes = typeof systemStateIdentity; | ||
|
||
// MARK: Mock Item | ||
export const systemStateIdentity = firestoreModelIdentity('systemState', 'sys'); | ||
|
||
/** | ||
* Used to identify a SystemStateId. | ||
*/ | ||
export type SystemStateTypeIdentifier = string; | ||
|
||
/** | ||
* Used to identify a SystemStateId. | ||
*/ | ||
export type SystemStateId = SystemStateTypeIdentifier; | ||
|
||
/** | ||
* Arbitrary data stored within a SystemState. Stored values should always be either a string, number, or boolean. | ||
*/ | ||
export type SystemStateStoredData = Record<string, any>; | ||
|
||
/** | ||
* A collection used for recording the current state of system subcomponents. System states for a given identifier are treated as a system-wide singleton/state/setting. | ||
* | ||
* For example, a SystemState with a specific SystemStateId may be relied on for information about the previous update, etc. | ||
*/ | ||
export interface SystemState<T extends SystemStateStoredData = SystemStateStoredData> { | ||
data: T; | ||
} | ||
|
||
export type SystemStateRoles = GrantedSysAdminRole; | ||
|
||
/** | ||
* Refers to a singleton SystemState based on this model's identifier. | ||
*/ | ||
export class SystemStateDocument<T extends SystemStateStoredData = SystemStateStoredData> extends AbstractFirestoreDocument<SystemState<T>, SystemStateDocument<T>, typeof systemStateIdentity> { | ||
get modelIdentity() { | ||
return systemStateIdentity; | ||
} | ||
} | ||
|
||
export const systemStateConverter = snapshotConverterFunctions<SystemState>({ | ||
fields: { | ||
data: firestorePassThroughField() | ||
} | ||
}); | ||
|
||
export function systemStateCollectionReference(context: FirestoreContext): CollectionReference<SystemState> { | ||
return context.collection(systemStateIdentity.collectionName); | ||
} | ||
|
||
export type SystemStateFirestoreCollection = FirestoreCollection<SystemState, SystemStateDocument>; | ||
|
||
/** | ||
* A ModelFieldMapFunctionsConfig used for data conversion. | ||
*/ | ||
export type SystemStateStoredDataFieldConverterConfig<T extends SystemStateStoredData = SystemStateStoredData> = ModelFieldMapFunctionsConfig<T, any>; | ||
|
||
export type SystemStateStoredDataConverterMap = { | ||
[key: string]: SystemStateStoredDataFieldConverterConfig<any>; | ||
}; | ||
|
||
export function systemStateFirestoreCollection(firestoreContext: FirestoreContext, converters: SystemStateStoredDataConverterMap): SystemStateFirestoreCollection { | ||
const mappedConvertersGetter = cachedGetter(() => | ||
mapObjectMap(converters, (dataConverter) => { | ||
return snapshotConverterFunctions<SystemState>({ | ||
fields: { | ||
data: dataConverter | ||
} | ||
}); | ||
}) | ||
); | ||
|
||
return firestoreContext.firestoreCollection({ | ||
converter: systemStateConverter, | ||
converterFactory: (ref) => { | ||
const type: SystemStateTypeIdentifier = ref.id; | ||
return mappedConvertersGetter()[type]; | ||
}, | ||
modelIdentity: systemStateIdentity, | ||
collection: systemStateCollectionReference(firestoreContext), | ||
makeDocument: (a, d) => { | ||
return new SystemStateDocument(a, d); | ||
}, | ||
firestoreContext | ||
}); | ||
} |