feat: add queries to search for users by username and for parent posts based on content
This commit is contained in:
+12
-2
@@ -1,4 +1,4 @@
|
||||
import { PaginationResult, paginationOptsValidator } from 'convex/server';
|
||||
import { paginationOptsValidator } from 'convex/server';
|
||||
import { v } from 'convex/values';
|
||||
import { PostWithUserDto } from '../src/app/types';
|
||||
import { Id } from './_generated/dataModel';
|
||||
@@ -206,7 +206,7 @@ export const getAllPostsWithUser = query({
|
||||
|
||||
export const getAllPostsForFollowings = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args): Promise<PaginationResult<PostWithUserDto>> => {
|
||||
handler: async (ctx, args) => {
|
||||
const currentUser = await ctx.auth.getUserIdentity();
|
||||
|
||||
if (currentUser === null) {
|
||||
@@ -272,3 +272,13 @@ export const getAllPostsForFollowings = query({
|
||||
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
@@ -34,14 +34,22 @@ export default defineSchema(
|
||||
created_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({
|
||||
parentPostId: v.optional(v.id('posts')),
|
||||
userId: v.id('users'),
|
||||
content: v.array(v.string()),
|
||||
})
|
||||
.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({
|
||||
postId: v.id('posts'),
|
||||
userId: v.id('users'),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { paginationOptsValidator } from 'convex/server';
|
||||
import { v } from 'convex/values';
|
||||
import { UserDto } from './../src/app/types/index';
|
||||
import {
|
||||
@@ -121,3 +122,15 @@ export const deleteUser = internalMutation({
|
||||
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);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user