Vue로 PWA 개발 - 그랜파 개발자
Firestore 데이터에 대해 CRUD(생성, 읽기, 업데이트, 삭제) 작업을 수행하는 방법에 대해 설명을 들었습니다. 이것으로는 부족합니다. firestore의 query 사용법에 대해서도 알아야 합니다.
In Firebase Firestore, a query is used to retrieve specific documents from a collection based on certain conditions. You can use queries to filter, sort, and limit the results when fetching data. Firestore provides several query methods like where, orderBy, limit, and more, to customize the results you want.
Firebase Firestore에서는 특정 조건에 따라 컬렉션에서 특정 문서를 검색하는 데 쿼리가 사용됩니다. 쿼리를 사용하여 데이터를 가져올 때 결과를 필터링, 정렬 및 제한할 수 있습니다. Firestore는 'where', 'orderBy', 'limit' 등과 같은 여러 쿼리 메서드를 제공하여 원하는 결과를 맞춤설정합니다.
Assuming you've already initialized Firestore using the Firebase Admin SDK:
Firebase Admin SDK를 사용하여 Firestore를 이미 초기화했다고 가정합니다.
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
To retrieve documents based on a condition, you can use the where method.
조건에 따라 문서를 검색하려면 where 메소드를 사용할 수 있습니다.
exports.getPostsByAuthor = onRequest(async (req, res) => {
const authorId = req.query.authorId; // Assume authorId is passed as a query parameter
try {
const postsSnapshot = await db.collection('posts')
.where('authorId', '==', authorId) // Query for posts where 'authorId' matches
.get();
const posts = [];
postsSnapshot.forEach((doc) => {
posts.push({ id: doc.id, ...doc.data() });
});
res.send(posts);
} catch (error) {
console.error('Error getting posts:', error);
res.status(500).send('Error getting posts');
}
});
In this example:
이 예에서는 다음과 같습니다.
You can chain multiple where methods to apply more conditions.
여러 개의 'where' 메서드를 연결하여 더 많은 조건을 적용할 수 있습니다.
exports.getRecentPostsByAuthor = onRequest(async (req, res) => {
const authorId = req.query.authorId;
const startDate = req.query.startDate; // Example: "2023-01-01T00:00:00Z"
try {
const postsSnapshot = await db.collection('posts')
.where('authorId', '==', authorId)
.where('createdAt', '>=', new Date(startDate))
.get();
const posts = [];
postsSnapshot.forEach((doc) => {
posts.push({ id: doc.id, ...doc.data() });
});
res.send(posts);
} catch (error) {
console.error('Error getting posts:', error);
res.status(500).send('Error getting posts');
}
});
In this case, the query filters posts by authorId and returns only those created after a certain date (createdAt >= startDate).
이 경우 쿼리는 authorId를 기준으로 게시물을 필터링하고 특정 날짜(createdAt >= startDate) 이후에 생성된 게시물만 반환합니다.
You can sort the results by a specific field using the orderBy method.
orderBy 메소드를 사용하여 특정 필드를 기준으로 결과를 정렬할 수 있습니다.
exports.getPostsSortedByDate = onRequest(async (req, res) => {
try {
const postsSnapshot = await db.collection('posts')
.orderBy('createdAt', 'desc') // Sort by 'createdAt' in descending order
.get();
const posts = [];
postsSnapshot.forEach((doc) => {
posts.push({ id: doc.id, ...doc.data() });
});
res.send(posts);
} catch (error) {
console.error('Error getting posts:', error);
res.status(500).send('Error getting posts');
}
});
Here: 여기:
orderBy('createdAt', 'desc') sorts the posts by their creation date in descending order, showing the most recent posts first.
orderBy('createdAt', 'desc')는 게시물을 생성 날짜별로 내림차순으로 정렬하여 가장 최근 게시물을 먼저 표시합니다
You can restrict the number of documents returned using the limit method.
limit 메소드를 사용하여 반환되는 문서 수를 제한할 수 있습니다.
exports.getTopFivePosts = onRequest(async (req, res) => {
try {
const postsSnapshot = await db.collection('posts')
.orderBy('createdAt', 'desc') // Sort by the most recent
.limit(5) // Limit the results to 5 documents
.get();
const posts = [];
postsSnapshot.forEach((doc) => {
posts.push({ id: doc.id, ...doc.data() });
});
res.send(posts);
} catch (error) {
console.error('Error getting posts:', error);
res.status(500).send('Error getting posts');
}
});
This query limits the result to only 5 documents, even if there are more matching posts.
이 쿼리는 일치하는 게시물이 더 있더라도 결과를 5개의 문서로만 제한합니다.
You can combine filtering and sorting by chaining where and orderBy together.
where와 orderBy를 함께 연결하여 필터링과 정렬을 결합할 수 있습니다.
exports.getSortedPostsByAuthor = onRequest(async (req, res) => {
const authorId = req.query.authorId;
try {
const postsSnapshot = await db.collection('posts')
.where('authorId', '==', authorId)
.orderBy('createdAt', 'desc') // Sort the results by 'createdAt'
.get();
const posts = [];
postsSnapshot.forEach((doc) => {
posts.push({ id: doc.id, ...doc.data() });
});
res.send(posts);
} catch (error) {
console.error('Error getting posts:', error);
res.status(500).send('Error getting posts');
}
});
Here, the query:
여기서 쿼리는 다음과 같습니다.
To paginate results, use the startAfter method in combination with orderBy.
결과를 페이지로 나누려면 orderBy와 함께 startAfter 메소드를 사용하세요.
exports.getPaginatedPosts = onRequest(async (req, res) => {
const lastPostId = req.query.lastPostId; // The ID of the last post from the previous page
const pageSize = 10; // Number of posts per page
try {
let query = db.collection('posts').orderBy('createdAt', 'desc').limit(pageSize);
// If there is a lastPostId, fetch the post and start after it
if (lastPostId) {
const lastPost = await db.collection('posts').doc(lastPostId).get();
query = query.startAfter(lastPost);
}
const postsSnapshot = await query.get();
const posts = [];
postsSnapshot.forEach((doc) => {
posts.push({ id: doc.id, ...doc.data() });
});
res.send(posts);
} catch (error) {
console.error('Error getting posts:', error);
res.status(500).send('Error getting posts');
}
});
In this example:
이 예에서는 다음과 같습니다.
By using these methods, you can customize your queries to retrieve exactly the data you need from Firestore!
이러한 방법을 사용하면 쿼리를 맞춤설정하여 Firestore에서 필요한 데이터를 정확하게 검색할 수 있습니다.