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

Exposed countDocuments method #29

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions src/packages/pongo/src/main/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +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[]>;
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
18 changes: 7 additions & 11 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,13 +303,13 @@ 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,
filter?: Filter<T> | undefined,
_options?: CountDocumentsOptions | undefined,
): Promise<number> {
throw new Error('Method not implemented.');
return this.collection.countDocuments(filter as PongoFilter<T>);
}
distinct<Key extends '_id' | keyof EnhancedOmit<T, '_id'>>(
key: Key,
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