feat: add queries to search for users by username and for parent posts based on content

This commit is contained in:
Stevan Freeborn
2023-09-15 22:16:21 -05:00
parent c0aec83868
commit f63da9792d
3 changed files with 35 additions and 4 deletions
+12 -2
View File
@@ -1,4 +1,4 @@
import { PaginationResult, paginationOptsValidator } from 'convex/server'; import { paginationOptsValidator } from 'convex/server';
import { v } from 'convex/values'; import { v } from 'convex/values';
import { PostWithUserDto } from '../src/app/types'; import { PostWithUserDto } from '../src/app/types';
import { Id } from './_generated/dataModel'; import { Id } from './_generated/dataModel';
@@ -206,7 +206,7 @@ export const getAllPostsWithUser = query({
export const getAllPostsForFollowings = query({ export const getAllPostsForFollowings = query({
args: { paginationOpts: paginationOptsValidator }, args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args): Promise<PaginationResult<PostWithUserDto>> => { handler: async (ctx, args) => {
const currentUser = await ctx.auth.getUserIdentity(); const currentUser = await ctx.auth.getUserIdentity();
if (currentUser === null) { if (currentUser === null) {
@@ -272,3 +272,13 @@ export const getAllPostsForFollowings = query({
return { ...posts, page: postsWithUser }; return { ...posts, page: postsWithUser };
}, },
}); });
export const getPostsBySearchTerm = query({
args: { term: v.string(), paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
return await ctx.db
.query('posts')
.withSearchIndex('search_by_content', q => q.search('content', args.term))
.paginate(args.paginationOpts);
},
});
+10 -2
View File
@@ -34,14 +34,22 @@ export default defineSchema(
created_at: v.number(), created_at: v.number(),
updated_at: v.number(), updated_at: v.number(),
}), }),
}).index('by_clerk_id', ['clerkUser.id']), })
.index('by_clerk_id', ['clerkUser.id'])
.searchIndex('search_by_username', {
searchField: 'clerkUser.username',
}),
posts: defineTable({ posts: defineTable({
parentPostId: v.optional(v.id('posts')), parentPostId: v.optional(v.id('posts')),
userId: v.id('users'), userId: v.id('users'),
content: v.array(v.string()), content: v.array(v.string()),
}) })
.index('by_user_id', ['userId']) .index('by_user_id', ['userId'])
.index('by_parent_id', ['parentPostId']), .index('by_parent_id', ['parentPostId'])
.searchIndex('search_by_content', {
searchField: 'content',
filterFields: ['parentPostId'],
}),
likes: defineTable({ likes: defineTable({
postId: v.id('posts'), postId: v.id('posts'),
userId: v.id('users'), userId: v.id('users'),
+13
View File
@@ -1,3 +1,4 @@
import { paginationOptsValidator } from 'convex/server';
import { v } from 'convex/values'; import { v } from 'convex/values';
import { UserDto } from './../src/app/types/index'; import { UserDto } from './../src/app/types/index';
import { import {
@@ -121,3 +122,15 @@ export const deleteUser = internalMutation({
await ctx.db.delete(userRecord._id); await ctx.db.delete(userRecord._id);
}, },
}); });
export const getUsersBySearchTerm = query({
args: { term: v.string(), paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
return await ctx.db
.query('users')
.withSearchIndex('search_by_username', q =>
q.search('clerkUser.username', args.term)
)
.paginate(args.paginationOpts);
},
});