0.1.12 • Published 9 months ago

typesafe-mongo v0.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

NPM version Repository package.json version MIT license

TypeSafe Mongo

TypeSafe utilities for official MongoDB Node.js driver.

Installation

npm install typesafe-mongo

Usage

Filter and Update

import {filter, update} from 'typesafe-mongo';

interface AwesomeDocument {
  foo: {
    bar: string;
  };
  objects: {
    name: string;
    value: number;
  }[];
}

declare const collection: Collection<AwesomeDocument>;

await collection.findOne(
  filter({
    foo: {
      bar: {
        $eq: 'abc',
      },
    },
  }),
);

await collection.updateOne(
  filter({
    objects: {
      name: 'abc',
    },
  }),
  update({
    $set: {
      objects: {
        $: {
          value: 123,
        },
      },
    },
  }),
);

await collection.updateOne(
  filter({
    // Using `atomic()` to prevent a plain object from being flattened:
    objects: atomic({
      name: 'abc',
      value: 123,
    }),
  }),
  update({
    $set: {
      // Arrays will never be flattened, so in this case no `atomic()` needed.
      objects: [
        {
          name: 'abc',
          value: 123,
        },
      ],
    },
  }),
);

Flatten Utilities

import {flatten, sort, project} from 'typesafe-mongo';

interface AwesomeDocument {
  foo: {
    bar: string;
  };
  objects: {
    name: string;
    value: number;
  }[];
}

flatten<AwesomeDocument, 'asc' | 'desc'>({
  foo: {
    bar: 'asc',
  },
  objects: {
    name: 'desc',
  },
});

sort<AwesomeDocument>({
  foo: {
    bar: 1,
  },
});

project<AwesomeDocument>({
  objects: {
    name: true,
  },
});

License

MIT License.

0.1.12

9 months ago

0.1.10

1 year ago

0.1.11

12 months ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago