Skip to content

Commit 97067d8

Browse files
committed
Handle empty/null post embeddings for similar posts and createPost
Avoid pgvector error when query vector is empty; persist null instead of [] when embedding computation fails; align types with nullable embeddings. Made-with: Cursor
1 parent ffe4a76 commit 97067d8

4 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/models/PostModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class PostModel {
4949
archive: boolean;
5050

5151
@Column("float", { array: true, nullable: true })
52-
embedding: number[];
52+
embedding: number[] | null;
5353

5454
@ManyToOne(() => UserModel, (user) => user.posts, { onDelete: "CASCADE" })
5555
@JoinColumn({ name: "userId" })

src/repositories/PostRepository.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
9090
price: number,
9191
images: string[],
9292
user: UserModel,
93-
embedding: number[],
93+
embedding: number[] | null,
9494
): Promise<PostModel> {
9595
const post = new PostModel();
9696
post.title = title;
@@ -561,6 +561,9 @@ export class PostRepository extends AbstractRepository<PostModel> {
561561
excludeUserId: string,
562562
limit = 20,
563563
): Promise<PostModel[]> {
564+
if (!Array.isArray(queryEmbedding) || queryEmbedding.length === 0) {
565+
return [];
566+
}
564567
const lit = `[${queryEmbedding.join(",")}]`;
565568
return await this.repository
566569
.createQueryBuilder("post")
@@ -583,6 +586,9 @@ export class PostRepository extends AbstractRepository<PostModel> {
583586
excludeUserId: string,
584587
limit = 10,
585588
): Promise<PostModel[]> {
589+
if (!Array.isArray(embedding) || embedding.length === 0) {
590+
return [];
591+
}
586592
const lit = `[${embedding.join(",")}]`;
587593
return await this.repository
588594
.createQueryBuilder("post")
@@ -703,6 +709,9 @@ export class PostRepository extends AbstractRepository<PostModel> {
703709
excludeUserId: string,
704710
limit = 10,
705711
): Promise<PostModel[]> {
712+
if (!Array.isArray(avgEmbedding) || avgEmbedding.length === 0) {
713+
return [];
714+
}
706715
const lit = `[${avgEmbedding.join(",")}]`;
707716
return await this.repository
708717
.createQueryBuilder("post")

src/services/PostService.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,26 @@ export class PostService {
114114
console.error("Error computing embedding:", error);
115115
embedding = null;
116116
}
117-
const freshPost = await postRepository.createPost(post.title, post.description, categories, eventTags, post.condition, post.originalPrice, images, user, (embedding ?? []) as number[]);
118-
if (embedding && Array.isArray(embedding) && embedding.length > 0) {
117+
const validEmbedding =
118+
Array.isArray(embedding) && embedding.length > 0 ? embedding : null;
119+
const freshPost = await postRepository.createPost(
120+
post.title,
121+
post.description,
122+
categories,
123+
eventTags,
124+
post.condition,
125+
post.originalPrice,
126+
images,
127+
user,
128+
validEmbedding,
129+
);
130+
if (validEmbedding) {
119131
const requestRepository = Repositories.request(
120132
transactionalEntityManager,
121133
);
122134
// TODO: how many should we get?
123135
const similarRequests = await requestRepository.findSimilarRequests(
124-
embedding,
136+
validEmbedding,
125137
user.firebaseUid,
126138
10,
127139
);
@@ -584,9 +596,12 @@ export class PostService {
584596
const postRepository = Repositories.post(transactionalEntityManager);
585597
const post = await postRepository.getPostById(params.id);
586598
if (!post) throw new NotFoundError("Post not found!");
587-
const embedding = post.embedding;
599+
const embedding =
600+
Array.isArray(post.embedding) && post.embedding.length > 0
601+
? post.embedding
602+
: null;
588603

589-
if (embedding == null) {
604+
if (!embedding) {
590605
let fallback = await postRepository.getSimilarPostsFallback(
591606
post.id,
592607
user.firebaseUid,

src/types/ApiResponses.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export interface Post {
9393
categories: Category[];
9494
eventTags: EventTag[];
9595
sold: boolean;
96-
embedding: number[];
96+
embedding: number[] | null;
9797
}
9898

9999
export interface GetPostsResponse {
@@ -157,7 +157,7 @@ export interface Request {
157157
title: string;
158158
description: string;
159159
archive: boolean;
160-
embedding: number[];
160+
embedding: number[] | null;
161161
user: PrivateProfile;
162162
matches: PostModel[];
163163
}

0 commit comments

Comments
 (0)