typesafe-mongo
v0.1.12
Published
TypeSafe utilities for official MongoDB Node.js driver.
Downloads
6
Readme
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.