Skip to content

Commit

Permalink
Addec capability to throw errors on operation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Sep 27, 2024
1 parent ca58842 commit a103912
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
18 changes: 10 additions & 8 deletions src/packages/pongo/src/core/collection/pongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type PongoCollectionOptions<ConnectorType extends string = string> = {
pool: Dumbo;
sqlBuilder: PongoCollectionSQLBuilder;
schema?: { autoMigration?: MigrationStyle };
errors?: { throwOnOperationFailures?: boolean };
};

const enlistIntoTransactionIfActive = async <
Expand Down Expand Up @@ -79,6 +80,7 @@ export const pongoCollection = <
pool,
sqlBuilder: SqlFor,
schema,
errors,
}: PongoCollectionOptions<ConnectorType>): PongoCollection<T> => {
const sqlExecutor = pool.execute;
const command = async <Result extends QueryResultRow = QueryResultRow>(
Expand Down Expand Up @@ -144,7 +146,7 @@ export const pongoCollection = <
successful,
insertedId: successful ? _id : null,
},
{ operationName: 'insertOne', collectionName },
{ operationName: 'insertOne', collectionName, errors },
);
},
insertMany: async (
Expand All @@ -170,7 +172,7 @@ export const pongoCollection = <
insertedCount: result.rowCount ?? 0,
insertedIds: rows.map((d) => d._id as string),
},
{ operationName: 'insertMany', collectionName },
{ operationName: 'insertMany', collectionName, errors },
);
},
updateOne: async (
Expand All @@ -191,7 +193,7 @@ export const pongoCollection = <
modifiedCount: result.rows[0]!.modified!,
matchedCount: result.rows[0]!.matched!,
},
{ operationName: 'updateOne', collectionName },
{ operationName: 'updateOne', collectionName, errors },
);
},
upsertOne: async (
Expand All @@ -212,7 +214,7 @@ export const pongoCollection = <
modifiedCount: result.rows[0]!.modified!,
matchedCount: result.rows[0]!.matched!,
},
{ operationName: 'upsertOne', collectionName },
{ operationName: 'upsertOne', collectionName, errors },
);
},
replaceOne: async (
Expand All @@ -232,7 +234,7 @@ export const pongoCollection = <
modifiedCount: result.rows[0]!.modified!,
matchedCount: result.rows[0]!.matched!,
},
{ operationName: 'replaceOne', collectionName },
{ operationName: 'replaceOne', collectionName, errors },
);
},
updateMany: async (
Expand All @@ -250,7 +252,7 @@ export const pongoCollection = <
modifiedCount: result.rowCount ?? 0,
matchedCount: result.rowCount ?? 0,
},
{ operationName: 'updateMany', collectionName },
{ operationName: 'updateMany', collectionName, errors },
);
},
deleteOne: async (
Expand All @@ -269,7 +271,7 @@ export const pongoCollection = <
deletedCount: result.rows[0]!.deleted!,
matchedCount: result.rows[0]!.matched!,
},
{ operationName: 'deleteOne', collectionName },
{ operationName: 'deleteOne', collectionName, errors },
);
},
deleteMany: async (
Expand All @@ -286,7 +288,7 @@ export const pongoCollection = <
deletedCount: result.rowCount ?? 0,
matchedCount: result.rowCount ?? 0,
},
{ operationName: 'deleteOne', collectionName },
{ operationName: 'deleteMany', collectionName, errors },
);
},
findOne: async (
Expand Down
1 change: 1 addition & 0 deletions src/packages/pongo/src/core/pongoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type PongoClientOptions<
TypedClientSchema extends PongoClientSchema = PongoClientSchema,
> = {
schema?: { autoMigration?: MigrationStyle; definition?: TypedClientSchema };
errors?: { throwOnOperationFailures?: boolean };
connectionOptions?: PooledPongoClientOptions | NotPooledPongoOptions;
};

Expand Down
26 changes: 15 additions & 11 deletions src/packages/pongo/src/core/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,34 +385,38 @@ export type OperationResult = {
acknowledged: boolean;
successful: boolean;

assertSuccessful: () => void;
assertSuccessful: (errorMessage?: string) => void;
};

export const operationResult = <T extends OperationResult>(
result: Omit<T, 'assertSuccess' | 'acknowledged' | 'assertSuccessful'>,
options: {
check?: (
result: Omit<T, 'assertSuccess' | 'acknowledged' | 'assertSuccessful'>,
) => boolean;
operationName: string;
collectionName: string;
errors?: { throwOnOperationFailures?: boolean } | undefined;
},
): T =>
({
): T => {
const operationResult: T = {
...result,
acknowledged: true,
successful: result.successful,
assertSuccessful: (errorMessage?: string) => {
const { successful } = result;
const { check, operationName, collectionName } = options;
const isOk = successful && (!check || check(result));
const { operationName, collectionName } = options;

if (!isOk)
if (!successful)
throw new Error(
errorMessage ?? `${operationName} on ${collectionName} failed!`,
errorMessage ??
`${operationName} on ${collectionName} failed with ${JSON.stringify(result)}!`,
);
},
}) as T;
} as T;

if (options.errors?.throwOnOperationFailures)
operationResult.assertSuccessful();

return operationResult;
};

export interface PongoInsertOneResult extends OperationResult {
insertedId: string | null;
Expand Down
1 change: 1 addition & 0 deletions src/packages/pongo/src/postgres/dbClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const postgresDb = (
pool,
sqlBuilder: postgresSQLBuilder(collectionName),
...(options.schema ? options.schema : {}),
...(options.errors ? options.errors : {}),
}),
transaction: () => pool.transaction(),
withTransaction: (handle) => pool.withTransaction(handle),
Expand Down

0 comments on commit a103912

Please sign in to comment.