jok-mango
v2.2.1
Published
Simplified way to work with MongoDB
Downloads
97
Readme
jok-mango
Simplified way to work with MongoDB
Features
✅ Stores _id: ObjectId
field, but wraps it to the id: string
✅ Documents have version: number
out of the box, and its increased by 1
every time you call update
✅ Every document has createdAt
and updatedAt
props
✅ Soft Delete & Hard Delete ability for documents
How to use
- Declare data structure
import { DocumentBase } from 'jok-mango'
export interface User extends DocumentBase {
email: string
passwordHash: string
fullname?: string
}
- Create repository object for declared type
import { getRepository } from 'jok-mango'
const connectionString = `mongodb://mongo:mongo@localhost:27017/test?authSource=admin`
const client = new MongoClient(connectionString, { useNewUrlParser: true })
await client.connect()
const db = client.db()
const users = getRepository<User>(db, 'users')
- Use repository object
const user = await users.create({
email: '[email protected]',
passwordHash: 'strong-hash',
})
const { id, version } = user
const updatedUser = await users.update(
{ id, version },
{
email: '[email protected]',
},
)
Performance Cost
Library calls _id.toHexString()
for all retrieved items to have id: string
and make developers life easier, so logical quetion is: How expensive operation is it.
Hardware: MacBook Pro (Retina, 15-inch, Mid 2014) Processor: 2.2 GHz Intel Core i7
| Number of Operations | Duration | | -------------------: | -------- | | 1 | ~20 μs | | 1 000 | ~829 μs | | 1 000 000 | ~250 ms |