Skip to content

Commit

Permalink
fixup! feat: Support updates with readonly array values
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanbeevers committed May 29, 2024
1 parent 4afb35e commit 18e7b16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
12 changes: 8 additions & 4 deletions src/__tests__/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe('model', () => {

describe('bulkWrite', () => {
test('simple schema', async () => {
const operations: PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
const operations = [
{
insertOne: {
document: {
Expand Down Expand Up @@ -283,7 +283,9 @@ describe('model', () => {
filter: { foo: 'foo' },
},
},
];
] as const;

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

await simpleModel.bulkWrite(operations);

Expand All @@ -293,7 +295,7 @@ describe('model', () => {
});

test('schema with defaults', async () => {
const operations: PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
const operations = [
{
insertOne: {
document: {
Expand Down Expand Up @@ -367,7 +369,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
42 changes: 27 additions & 15 deletions 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 Expand Up @@ -171,7 +171,7 @@ export type PaprAllProperties<TSchema> = {
* }
*/
export type PaprArrayElementsProperties<TSchema> = {
[Property in `${KeysOfAType<PaprAllProperties<TSchema>, any[]>}.$${
[Property in `${KeysOfAType<PaprAllProperties<TSchema>, readonly any[]>}.$${
| ''
| `[${string}]`}`]?: ArrayElement<
PropertyType<TSchema, Property extends `${infer Key}.$${string}` ? Key : never>
Expand Down Expand Up @@ -204,6 +204,18 @@ export type PaprMatchKeysAndValues<TSchema> = PaprAllProperties<TSchema> &
PaprArrayElementsProperties<TSchema> &
PaprArrayNestedProperties<TSchema>;

type DeepReadonly<TSchema> = TSchema extends (infer Member)[]
? DeepReadonlyArray<Member>
: TSchema extends object
? DeepReadonlyObject<TSchema>
: TSchema;

interface DeepReadonlyArray<TSchema> extends ReadonlyArray<DeepReadonly<TSchema>> {}

type DeepReadonlyObject<TSchema> = {
readonly [Prop in keyof TSchema]: DeepReadonly<TSchema[Prop]>;
};

export interface PaprUpdateFilter<TSchema> {
$currentDate?: OnlyFieldsOfType<
TSchema,
Expand All @@ -213,21 +225,21 @@ export interface PaprUpdateFilter<TSchema> {
$type: 'date' | 'timestamp';
}
>;
$inc?: OnlyFieldsOfType<TSchema, NumericType | undefined>;
$min?: PaprMatchKeysAndValues<TSchema>;
$max?: PaprMatchKeysAndValues<TSchema>;
$mul?: OnlyFieldsOfType<TSchema, NumericType | undefined>;
$inc?: OnlyFieldsOfType<DeepReadonly<TSchema>, NumericType | undefined>;
$min?: PaprMatchKeysAndValues<DeepReadonly<TSchema>>;
$max?: PaprMatchKeysAndValues<DeepReadonly<TSchema>>;
$mul?: OnlyFieldsOfType<DeepReadonly<TSchema>, NumericType | undefined>;
$rename?: Record<string, string>;
$set?: PaprMatchKeysAndValues<TSchema>;
$setOnInsert?: PaprMatchKeysAndValues<TSchema>;
$unset?: OnlyFieldsOfType<TSchema, any, '' | 1 | true>;
$addToSet?: SetFields<TSchema>;
$pop?: OnlyFieldsOfType<TSchema, readonly any[], -1 | 1>;
$pull?: PullOperator<TSchema>;
$push?: PushOperator<TSchema>;
$pullAll?: PullAllOperator<TSchema>;
$set?: PaprMatchKeysAndValues<DeepReadonly<TSchema>>;
$setOnInsert?: PaprMatchKeysAndValues<DeepReadonly<TSchema>>;
$unset?: OnlyFieldsOfType<DeepReadonly<TSchema>, any, '' | 1 | true>;
$addToSet?: SetFields<DeepReadonly<TSchema>>;
$pop?: OnlyFieldsOfType<DeepReadonly<TSchema>, readonly any[], -1 | 1>;
$pull?: PullOperator<DeepReadonly<TSchema>>;
$push?: PushOperator<DeepReadonly<TSchema>>;
$pullAll?: PullAllOperator<DeepReadonly<TSchema>>;
$bit?: OnlyFieldsOfType<
TSchema,
DeepReadonly<TSchema>,
NumericType | undefined,
| {
and: IntegerType;
Expand Down

0 comments on commit 18e7b16

Please sign in to comment.