-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #552 from ONEARMY/master
v0.5.0
- Loading branch information
Showing
42 changed files
with
1,123 additions
and
255 deletions.
There are no files selected for viewing
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,63 @@ | ||
// tslint:disable no-implicit-dependencies | ||
// import { IDBEndpoint } from '@OAModels/common.models' | ||
import * as rtdb from './realtimeDB' | ||
import * as firestore from './firestoreDB' | ||
import { IDBEndpoint, IDbDoc } from '../models' | ||
|
||
/* Functions in this folder are used to sync data between firestore and firebase realtime databases | ||
The reason for this is to allow large collections to be 'cached' for cheap retrieval | ||
An example would be the map pins. When a user first visits the map all pins are downloaded | ||
The previous map had 30,000 pins, so this would cost roughly $0.02c, or $20 per 1000 users | ||
By pulling the data from firebase realtime instead and only subscribing to updates, this is | ||
dramatically reduced. For more information about the various database strategies see: | ||
https://github.com/OneArmyWorld/onearmy/wiki/Backend-Database | ||
*/ | ||
|
||
const endpoints: IDBEndpoint[] = [ | ||
'v2_discussions', | ||
'v2_events', | ||
'v2_howtos', | ||
'v2_mappins', | ||
'v2_tags', | ||
// NOTE - do not want to keep list of sync'd users | ||
// 'v2_users', | ||
] | ||
|
||
export const syncAll = async () => { | ||
const promises = endpoints.map(async endpoint => await sync(endpoint)) | ||
const results = await Promise.all(promises) | ||
const response = {} | ||
endpoints.forEach((endpoint, i) => (response[endpoint] = results[i])) | ||
return response | ||
} | ||
|
||
// for given endpoint, query rtdb for all records, determin latest, | ||
// query firestore for newer records, add to rtdb | ||
export const sync = async (endpoint: IDBEndpoint) => { | ||
const existing = await rtdb.get(endpoint) | ||
const latest = | ||
existing && Object.keys(existing).length > 1 | ||
? Object.values<IDbDoc>(existing).sort((a, b) => _sortByModified(a, b))[0] | ||
._modified | ||
: '' | ||
|
||
const update = await firestore.db | ||
.collection(endpoint) | ||
.where('_modified', '>', latest) | ||
.get() | ||
const docs = update.empty ? [] : update.docs.map(d => d.data()) | ||
const json = {} | ||
docs.forEach(doc => (json[doc._id] = doc)) | ||
await rtdb.update(endpoint, json) | ||
return json | ||
} | ||
|
||
function _sortByModified(a: IDbDoc, b: IDbDoc) { | ||
return a._modified > b._modified ? -1 : 1 | ||
} | ||
|
||
export const test = async () => { | ||
const endpoint: IDBEndpoint = 'v2_howtos' | ||
const data = await rtdb.get(endpoint) | ||
return Object.values(data) | ||
} |
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
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
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
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
Oops, something went wrong.