Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

245: add image comments index and image comments filter #267

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/@types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ export type Filters = {
addedEnd?: Maybe<Scalars['Date']['output']>;
addedStart?: Maybe<Scalars['Date']['output']>;
cameras?: Maybe<Array<Scalars['String']['output']>>;
comments?: Maybe<Scalars['String']['output']>;
createdEnd?: Maybe<Scalars['Date']['output']>;
createdStart?: Maybe<Scalars['Date']['output']>;
custom?: Maybe<Scalars['String']['output']>;
Expand All @@ -372,6 +373,7 @@ export type FiltersInput = {
addedEnd?: InputMaybe<Scalars['Date']['input']>;
addedStart?: InputMaybe<Scalars['Date']['input']>;
cameras?: InputMaybe<Array<Scalars['String']['input']>>;
comments?: InputMaybe<Scalars['String']['input']>;
createdEnd?: InputMaybe<Scalars['Date']['input']>;
createdStart?: InputMaybe<Scalars['Date']['input']>;
custom?: InputMaybe<Scalars['String']['input']>;
Expand Down
2 changes: 1 addition & 1 deletion src/api/db/models/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class ImageModel {
hasNext: false,
} as AggregationOutput<ImageSchema>;
}
return await MongoPaging.aggregate(Image.collection, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JesseLeung97, can you elaborate on the warning you were seeing and where it was coming from? It looks like the MongoPaging.aggregate() function is asynchronous, and this would make sense as it's a DB query, so I do think awaiting it is important.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what my lsp is saying.

Screenshot 2024-10-08 at 22 17 48

Here's the source code. It seems weird that it would be synchronous but the types.d.ts file isn't declaring it async.

// src/@types/mongo-cursor-pagination.d.ts

  const defaultExport: {
    mongoosePlugin: any;
    aggregate: <T extends Model>(
      model: T['schema'],
      input: AggregationInput,
    ) => AggregationOutput<T>;
  };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah I think that defaultExport.aggregate return type needs to be wrapped in a Promise type. mongo-cursor-pagination doesn't include Typescript types so we wrote these ones ourselves and this must have been an oversight on our part. I think the following should resolve that warning:

  const defaultExport: {
    mongoosePlugin: any;
    aggregate: <T extends Model>(
      model: T['schema'],
      input: AggregationInput,
    ) => Promise<AggregationOutput<T>>;
  };

return MongoPaging.aggregate(Image.collection, {
aggregation: buildPipeline(input.filters, context.user['curr_project']!),
limit: input.limit,
paginatedField: input.paginatedField,
Expand Down
12 changes: 12 additions & 0 deletions src/api/db/models/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,22 @@ export function buildPipeline(
labels,
reviewed,
custom,
comments
}: gql.Filters,
projectId?: string,
): PipelineStage[] {
const pipeline: PipelineStage[] = [];

if (comments) {
pipeline.push({
$match: {
$text: {
$search: `"${comments}"`
nathanielrindlaub marked this conversation as resolved.
Show resolved Hide resolved
}
}
})
}

// match current project
if (projectId) {
pipeline.push({ $match: { projectId: projectId } });
Expand Down Expand Up @@ -200,6 +211,7 @@ export function buildPipeline(
pipeline.push({ $match: isFilterValid(custom) });
}


console.log('utils.buildPipeline() - pipeline: ', JSON.stringify(pipeline));
return pipeline;
}
Expand Down
2 changes: 2 additions & 0 deletions src/api/db/schemas/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const ImageSchema = new Schema({

ImageSchema.plugin(MongoPaging.mongoosePlugin);

ImageSchema.index({ 'comments.comment': 'text' });

export default mongoose.model('Image', ImageSchema);

export type ImageSchema = mongoose.InferSchemaType<typeof ImageSchema>;
Expand Down
1 change: 1 addition & 0 deletions src/api/type-defs/inputs/QueryImagesInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default /* GraphQL */ `
labels: [String!]
reviewed: Boolean
custom: String
comments: String
}

input QueryImagesInput {
Expand Down
1 change: 1 addition & 0 deletions src/api/type-defs/objects/Filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default /* GraphQL */ `
reviewed: Boolean
notReviewed: Boolean
custom: String
comments: String
}
`;
Loading