diff --git a/src/__tests__/model.test.ts b/src/__tests__/model.test.ts index 84ca35050..a278c373c 100644 --- a/src/__tests__/model.test.ts +++ b/src/__tests__/model.test.ts @@ -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[]>(operations); + + await simpleModel.bulkWrite(operations); + + expect(collection.bulkWrite).toHaveBeenCalledWith(operations, { + ignoreUndefined: true, + }); + }); + test('schema with defaults', async () => { - const operations: PaprBulkWriteOperation[] = [ + const operations = [ { insertOne: { document: { @@ -366,7 +451,9 @@ describe('model', () => { upsert: true, }, }, - ]; + ] as const; + + expectType[]>(operations); await simpleModel.bulkWrite(operations); diff --git a/src/model.ts b/src/model.ts index 9a4a4b90c..2512f822d 100644 --- a/src/model.ts +++ b/src/model.ts @@ -57,7 +57,7 @@ export interface Model Promise; bulkWrite: ( - operations: PaprBulkWriteOperation[], + operations: readonly PaprBulkWriteOperation[], options?: BulkWriteOptions ) => Promise; @@ -376,7 +376,7 @@ export function build[], + operations: readonly PaprBulkWriteOperation[], options?: BulkWriteOptions ): Promise { if (operations.length === 0) { diff --git a/src/mongodbTypes.ts b/src/mongodbTypes.ts index 7ba9c4768..6429feba4 100644 --- a/src/mongodbTypes.ts +++ b/src/mongodbTypes.ts @@ -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> = | { // @ts-expect-error Type expects a Document extended type, but Document is too generic