0.1.1 • Published 2 years ago

@gallofeliz/documents-collection v0.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Documents Collection

npm.io

Note: This module is part of @gallofeliz/js-libs that is a personal project. It is not developed nor tested for applications that need high security or scalability.

Mongodb compatible documents collection interface with various implementations (for the moment only NeDB) :

  • find, findOne
    • Sort
    • Limit, Skip
    • Projection
    • Cursor
  • insert (one, multi)
  • update, updateOne
  • remove, removeOne
  • count, has
  • Aggregate
  • Fix typescript some invalidations patched by ts-ignore and any
  • ? Use Mingo Projection to better projection than NeDB ?

See DocumentCollection type for more informations.

import {DocumentCollection, NeDbDocumentCollection} from '@gallofeliz/documents-collection'

interface Person {
    _id: string
    name: string
    age: number
}

const collection = new NeDbDocumentCollection<Person>({
    filePath: null
})

await collection.insert([
    {name: 'Mélanie', age: 45},
    {name: 'Paul', age: 44},
    {name: 'Thierry', age: 45},
    {name: 'Lucie', age: 12},
    {name: 'Guillaume', age: 32},
    {name: 'Paulette', age: 77},
])

/*
    Paulette is 77 years old { name: 'Paulette', age: 77 }
    Thierry is 45 years old { name: 'Thierry', age: 45 }
    Mélanie is 45 years old { name: 'Mélanie', age: 45 }
    Paul is 44 years old { name: 'Paul', age: 44 }
    Guillaume is 32 years old { name: 'Guillaume', age: 32 }
    Lucie is 12 years old { name: 'Lucie', age: 12 }
*/
for await (const person of collection.find({}, {sort: {age: -1}, projection: { _id: 0 }})) {
    console.log(`${person.name} is ${person.age} years old`, person)
}

/*
    [
      { age: 32, names: [ 'Guillaume' ] },
      { age: 44, names: [ 'Paul' ] },
      { age: 45, names: [ 'Thierry', 'Mélanie' ] },
      { age: 77, names: [ 'Paulette' ] }
    ]
*/
console.log(await collection.aggregate([
    { $match: { age: { $gte: 18 } } },
    { $group: { _id: "$age", names: { $push: "$name" } } },
    { $project: { age: '$_id', _id: 0, names: 1 } },
    { $sort: { age: 1 } }
]).toArray())
0.1.1

2 years ago