Alfredo Perez
11/12/2024, 11:28 PMAlfredo Perez
11/12/2024, 11:29 PMexport const pollDatabaseAndSyncTypesense = async () => {
try {
if (!lastSyncedAt) {
lastSyncedAt = await getLastSyncedAt();
}
const models: Array<{ model: typeof Model; collectionName: string }> = [
{ model: User, collectionName: "users" },
{ model: Community, collectionName: "communities" },
{ model: Event, collectionName: "events" },
{ model: Message, collectionName: "messages" },
{ model: Group, collectionName: "groups" },
{ model: Project, collectionName: "projects" },
{ model: Membership, collectionName: "membership" },
{ model: MembershipRequest, collectionName: "membership_request" },
];
// Process each model for updated and deleted records
for (const { model, collectionName } of models) {
const config = CollectionConfig[collectionName as CollectionKey];
// Query updated records
const updatedRecords = await model.query()
.where("updatedAt", ">", lastSyncedAt)
.select(...config.fields);
const mappedRecords = updatedRecords.map(config.mapObject);
if (mappedRecords.length > 0) {
await typesense
.collections(collectionName)
.documents()
.import(mappedRecords, { action: "upsert" });
<http://log.info|log.info>(`Synced ${mappedRecords.length} updated records for ${collectionName}`);
}
}
// Update lastSyncedAt
await updateLastSyncedAt();
} catch (error) {
Sentry.captureException(error);
log.error("Error syncing data to Typesense:", error);
}
};
Jason Bosco
11/13/2024, 1:30 AMgetLastSyncedAt
Jason Bosco
11/13/2024, 1:31 AMJason Bosco
11/13/2024, 1:33 AMAlfredo Perez
11/13/2024, 6:18 PMlast_synced
to the collections
2. Periodically (lets say every 5 mins) use similar code as above to do comparison of db records updatedAt
comparing to last_synced
for the collection the record belongs to.
3. Update the collection's last_synced
to the current time. (Question: does updating metadata work the same as updating other fields in the collection or how would that look)?Alfredo Perez
11/13/2024, 6:22 PMJason Bosco
11/14/2024, 3:27 AMfields
in the PATCH endpoint, you'll send metadataJason Bosco
11/14/2024, 3:27 AM