Skip to content

Commit

Permalink
Used countDocuments for count and estimated count
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jul 12, 2024
1 parent 9f67741 commit 4bd5dea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/packages/pongo/src/main/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export interface PongoCollection<T extends PongoDocument> {
filter: PongoFilter<T>,
update: PongoUpdate<T>,
): Promise<PongoUpdateResult>;
deleteOne(filter: PongoFilter<T>): Promise<PongoDeleteResult>;
deleteMany(filter: PongoFilter<T>): Promise<PongoDeleteResult>;
findOne(filter: PongoFilter<T>): Promise<T | null>;
find(filter: PongoFilter<T>): Promise<T[]>;
countDocuments(filter: PongoFilter<T>): Promise<number>;
deleteOne(filter?: PongoFilter<T>): Promise<PongoDeleteResult>;
deleteMany(filter?: PongoFilter<T>): Promise<PongoDeleteResult>;
findOne(filter?: PongoFilter<T>): Promise<T | null>;
find(filter?: PongoFilter<T>): Promise<T[]>;
countDocuments(filter?: PongoFilter<T>): Promise<number>;
drop(): Promise<boolean>;
rename(newName: string): Promise<PongoCollection<T>>;
handle(id: string, handle: DocumentHandler<T>): Promise<T | null>;
Expand Down
14 changes: 5 additions & 9 deletions src/packages/pongo/src/mongo/mongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
filter?: Filter<T> | undefined,
_options?: DeleteOptions | undefined,
): Promise<DeleteResult> {
const result = await this.collection.deleteOne(
filter as unknown as PongoFilter<T>,
);
const result = await this.collection.deleteOne(filter as PongoFilter<T>);

return {
acknowledged: result.acknowledged,
Expand All @@ -190,9 +188,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
filter?: Filter<T> | undefined,
_options?: DeleteOptions | undefined,
): Promise<DeleteResult> {
const result = await this.collection.deleteMany(
filter as unknown as PongoFilter<T>,
);
const result = await this.collection.deleteMany(filter as PongoFilter<T>);

return {
acknowledged: result.acknowledged,
Expand Down Expand Up @@ -307,7 +303,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
estimatedDocumentCount(
_options?: EstimatedDocumentCountOptions | undefined,
): Promise<number> {
throw new Error('Method not implemented.');
return this.collection.countDocuments();
}
countDocuments(
filter?: Filter<T> | undefined,
Expand Down Expand Up @@ -471,10 +467,10 @@ export class Collection<T extends Document> implements MongoCollection<T> {
throw new Error('Method not implemented.');
}
count(
_filter?: Filter<T> | undefined,
filter?: Filter<T> | undefined,
_options?: CountOptions | undefined,
): Promise<number> {
throw new Error('Method not implemented.');
return this.collection.countDocuments((filter as PongoFilter<T>) ?? {});
}
listSearchIndexes(
options?: ListSearchIndexesOptions | undefined,
Expand Down
20 changes: 10 additions & 10 deletions src/packages/pongo/src/postgres/postgresCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,26 @@ export const postgresCollection = <T extends PongoDocument>(
? { acknowledged: true, modifiedCount: result.rowCount }
: { acknowledged: false, modifiedCount: 0 };
},
deleteOne: async (filter: PongoFilter<T>): Promise<PongoDeleteResult> => {
deleteOne: async (filter?: PongoFilter<T>): Promise<PongoDeleteResult> => {
await createCollection;

const result = await execute(SqlFor.deleteOne(filter));
const result = await execute(SqlFor.deleteOne(filter ?? {}));
return result.rowCount
? { acknowledged: true, deletedCount: result.rowCount }
: { acknowledged: false, deletedCount: 0 };
},
deleteMany: async (filter: PongoFilter<T>): Promise<PongoDeleteResult> => {
deleteMany: async (filter?: PongoFilter<T>): Promise<PongoDeleteResult> => {
await createCollection;

const result = await execute(SqlFor.deleteMany(filter));
const result = await execute(SqlFor.deleteMany(filter ?? {}));
return result.rowCount
? { acknowledged: true, deletedCount: result.rowCount }
: { acknowledged: false, deletedCount: 0 };
},
findOne: async (filter: PongoFilter<T>): Promise<T | null> => {
findOne: async (filter?: PongoFilter<T>): Promise<T | null> => {
await createCollection;

const result = await execute(SqlFor.findOne(filter));
const result = await execute(SqlFor.findOne(filter ?? {}));
return (result.rows[0]?.data ?? null) as T | null;
},
handle: async (
Expand Down Expand Up @@ -146,17 +146,17 @@ export const postgresCollection = <T extends PongoDocument>(

return result;
},
find: async (filter: PongoFilter<T>): Promise<T[]> => {
find: async (filter?: PongoFilter<T>): Promise<T[]> => {
await createCollection;

const result = await execute(SqlFor.find(filter));
const result = await execute(SqlFor.find(filter ?? {}));
return result.rows.map((row) => row.data as T);
},
countDocuments: async (filter: PongoFilter<T> = {}): Promise<number> => {
countDocuments: async (filter?: PongoFilter<T>): Promise<number> => {
await createCollection;

const { count } = await single(
execute<{ count: number }>(SqlFor.countDocuments(filter)),
execute<{ count: number }>(SqlFor.countDocuments(filter ?? {})),
);
return count;
},
Expand Down

0 comments on commit 4bd5dea

Please sign in to comment.