Skip to content

Commit e7f429c

Browse files
committed
Used countDocuments for count and estimated count
1 parent 7ab1049 commit e7f429c

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/packages/pongo/src/main/typing/operations.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export interface PongoCollection<T extends PongoDocument> {
2828
filter: PongoFilter<T>,
2929
update: PongoUpdate<T>,
3030
): Promise<PongoUpdateResult>;
31-
deleteOne(filter: PongoFilter<T>): Promise<PongoDeleteResult>;
32-
deleteMany(filter: PongoFilter<T>): Promise<PongoDeleteResult>;
33-
findOne(filter: PongoFilter<T>): Promise<T | null>;
34-
find(filter: PongoFilter<T>): Promise<T[]>;
35-
countDocuments(filter: PongoFilter<T>): Promise<number>;
31+
deleteOne(filter?: PongoFilter<T>): Promise<PongoDeleteResult>;
32+
deleteMany(filter?: PongoFilter<T>): Promise<PongoDeleteResult>;
33+
findOne(filter?: PongoFilter<T>): Promise<T | null>;
34+
find(filter?: PongoFilter<T>): Promise<T[]>;
35+
countDocuments(filter?: PongoFilter<T>): Promise<number>;
3636
drop(): Promise<boolean>;
3737
rename(newName: string): Promise<PongoCollection<T>>;
3838
handle(id: string, handle: DocumentHandler<T>): Promise<T | null>;

src/packages/pongo/src/mongo/mongoCollection.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
177177
filter?: Filter<T> | undefined,
178178
_options?: DeleteOptions | undefined,
179179
): Promise<DeleteResult> {
180-
const result = await this.collection.deleteOne(
181-
filter as unknown as PongoFilter<T>,
182-
);
180+
const result = await this.collection.deleteOne(filter as PongoFilter<T>);
183181

184182
return {
185183
acknowledged: result.acknowledged,
@@ -190,9 +188,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
190188
filter?: Filter<T> | undefined,
191189
_options?: DeleteOptions | undefined,
192190
): Promise<DeleteResult> {
193-
const result = await this.collection.deleteMany(
194-
filter as unknown as PongoFilter<T>,
195-
);
191+
const result = await this.collection.deleteMany(filter as PongoFilter<T>);
196192

197193
return {
198194
acknowledged: result.acknowledged,
@@ -307,7 +303,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
307303
estimatedDocumentCount(
308304
_options?: EstimatedDocumentCountOptions | undefined,
309305
): Promise<number> {
310-
throw new Error('Method not implemented.');
306+
return this.collection.countDocuments();
311307
}
312308
countDocuments(
313309
filter?: Filter<T> | undefined,
@@ -471,10 +467,10 @@ export class Collection<T extends Document> implements MongoCollection<T> {
471467
throw new Error('Method not implemented.');
472468
}
473469
count(
474-
_filter?: Filter<T> | undefined,
470+
filter?: Filter<T> | undefined,
475471
_options?: CountOptions | undefined,
476472
): Promise<number> {
477-
throw new Error('Method not implemented.');
473+
return this.collection.countDocuments((filter as PongoFilter<T>) ?? {});
478474
}
479475
listSearchIndexes(
480476
options?: ListSearchIndexesOptions | undefined,

src/packages/pongo/src/postgres/postgresCollection.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,26 @@ export const postgresCollection = <T extends PongoDocument>(
9797
? { acknowledged: true, modifiedCount: result.rowCount }
9898
: { acknowledged: false, modifiedCount: 0 };
9999
},
100-
deleteOne: async (filter: PongoFilter<T>): Promise<PongoDeleteResult> => {
100+
deleteOne: async (filter?: PongoFilter<T>): Promise<PongoDeleteResult> => {
101101
await createCollection;
102102

103-
const result = await execute(SqlFor.deleteOne(filter));
103+
const result = await execute(SqlFor.deleteOne(filter ?? {}));
104104
return result.rowCount
105105
? { acknowledged: true, deletedCount: result.rowCount }
106106
: { acknowledged: false, deletedCount: 0 };
107107
},
108-
deleteMany: async (filter: PongoFilter<T>): Promise<PongoDeleteResult> => {
108+
deleteMany: async (filter?: PongoFilter<T>): Promise<PongoDeleteResult> => {
109109
await createCollection;
110110

111-
const result = await execute(SqlFor.deleteMany(filter));
111+
const result = await execute(SqlFor.deleteMany(filter ?? {}));
112112
return result.rowCount
113113
? { acknowledged: true, deletedCount: result.rowCount }
114114
: { acknowledged: false, deletedCount: 0 };
115115
},
116-
findOne: async (filter: PongoFilter<T>): Promise<T | null> => {
116+
findOne: async (filter?: PongoFilter<T>): Promise<T | null> => {
117117
await createCollection;
118118

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

147147
return result;
148148
},
149-
find: async (filter: PongoFilter<T>): Promise<T[]> => {
149+
find: async (filter?: PongoFilter<T>): Promise<T[]> => {
150150
await createCollection;
151151

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

158158
const { count } = await single(
159-
execute<{ count: number }>(SqlFor.countDocuments(filter)),
159+
execute<{ count: number }>(SqlFor.countDocuments(filter ?? {})),
160160
);
161161
return count;
162162
},

0 commit comments

Comments
 (0)