Skip to content

Commit

Permalink
feat: bulkWrite method accepts readonly operations (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanbeevers authored May 30, 2024
1 parent 479fc65 commit f517739
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
91 changes: 89 additions & 2 deletions src/__tests__/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,93 @@ describe('model', () => {
});
});

test('simple schema with inline operations', async () => {
await simpleModel.bulkWrite([
{
insertOne: {
document: {
bar: 123,
foo: 'foo',
},
},
},
]);

expect(collection.bulkWrite).toHaveBeenCalledWith(
[
{
insertOne: {
document: {
bar: 123,
foo: 'foo',
},
},
},
],
{
ignoreUndefined: true,
}
);
});

test('simple schema with readonly operations', async () => {
const operations = [
{
insertOne: {
document: {
bar: 123,
foo: 'foo',
},
},
},
{
updateOne: {
filter: { foo: 'foo' },
update: {
$set: { bar: 123 },
},
},
},
{
updateMany: {
filter: { foo: 'foo' },
update: {
$set: { bar: 123 },
},
},
},
{
replaceOne: {
filter: { foo: 'foo' },
replacement: {
bar: 123,
foo: 'foo',
},
},
},
{
deleteOne: {
filter: { foo: 'foo' },
},
},
{
deleteMany: {
filter: { foo: 'foo' },
},
},
] as const;

expectType<readonly PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[]>(operations);

await simpleModel.bulkWrite(operations);

expect(collection.bulkWrite).toHaveBeenCalledWith(operations, {
ignoreUndefined: true,
});
});

test('schema with defaults', async () => {
const operations: PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
const operations = [
{
insertOne: {
document: {
Expand Down Expand Up @@ -366,7 +451,9 @@ describe('model', () => {
upsert: true,
},
},
];
] as const;

expectType<readonly PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[]>(operations);

await simpleModel.bulkWrite(operations);

Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface Model<TSchema extends BaseSchema, TOptions extends SchemaOption
) => Promise<TResult[]>;

bulkWrite: (
operations: PaprBulkWriteOperation<TSchema, TOptions>[],
operations: readonly PaprBulkWriteOperation<TSchema, TOptions>[],
options?: BulkWriteOptions
) => Promise<BulkWriteResult | void>;

Expand Down Expand Up @@ -376,7 +376,7 @@ export function build<TSchema extends BaseSchema, TOptions extends SchemaOptions
model.bulkWrite = wrap(
model,
async function bulkWrite(
operations: PaprBulkWriteOperation<TSchema, TOptions>[],
operations: readonly PaprBulkWriteOperation<TSchema, TOptions>[],
options?: BulkWriteOptions
): Promise<BulkWriteResult | void> {
if (operations.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/mongodbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { DocumentForInsert, NestedPaths, PropertyType } from './utils';
// We've adopted these types in this repository and made some improvements to them.
// See: https://github.com/plexinc/papr/issues/410

// These buik operation types need our own `PaprFilter` and `PaprUpdateFilter` in their definition
// These bulk operation types need our own `PaprFilter` and `PaprUpdateFilter` in their definition
export type PaprBulkWriteOperation<TSchema, TOptions extends SchemaOptions<TSchema>> =
| {
// @ts-expect-error Type expects a Document extended type, but Document is too generic
Expand Down

0 comments on commit f517739

Please sign in to comment.