0.9.2 • Published 3 years ago
@peregrine/mongo-connect v0.9.2
Mongo Connect
Experimental MongoDB lib for node. Wraps the mongoose library.
Notes
- All objects internally have an
_id
field for the ID. Don't specify the primary ID in the Schema. - When passing or retreiving an object, the
_id
field will be renamed toid
Demo
import { MongoDB, MutableRepository, required } from "@peregrine/mongo-connect"
type Nullable<T> = T | null
interface Pet {
name: string
kind: string
dateOfBirth?: Date
}
interface PetWithId extends Pet {
id: string
}
const db = await MongoDB.connect(process.env.MONGO_URL)
const pets: MutableRepository<Pet> = db.getMutableRepository<Pet>("pets", {
name: required(String),
kind: required(String),
dateOfBirth: Date
})
const maya: PetWithId = await pets.add({
name: "Maya",
kind: "Parrot"
})
const updatedMaya: Nullable<PetWithId> = await pets.update(maya.id, {
name: maya.name,
kind: "Macaw"
})
// OR
const patchedMaya: Nullable<PetWithId> = await pets.patch(maya.id, { kind: "Macaw" })