Files
Overheard/src/lib/Firebase/messages.js

16 lines
626 B
JavaScript

import { collection, query, where, getDocs } from 'firebase/firestore'; // tools for building and running db queries
import { db } from './config'; // database connection
import { getQueryPrefix } from '$lib/utils/geohash'; // convert coordinates into geohash string
export async function getNearbyMessages(lat, lng) {
const prefix = getQueryPrefix(lat, lng);
const q = query(
collection(db, 'messages'),
where('geohash', '>=', prefix),
where('geohash', '<', prefix + 'z')
);
const snapshot = await getDocs(q);
return snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
}