1.4.1 • Published 2 years ago

@chipsgg/mongo-table v1.4.1

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

Mongo Table

Simplifies initializing database, collection and indexes while providing common basic mongo calls.

Install

yarn add @chipsgg/mongo-table

API

Include in your code: const Mongo = require('@chipsgg/mongo-table')

If you just want to connect to the db use: `const DB = require('@chipsgg/mongo-table/db').

Init

Initializes through mongo uri and passes through options to the standard mongo driver. Mongo Connection Docs.

//You can spawn tables with the Tables function
require('mongo-table')({
  uri:'your mongo db uri',
}).then(async Tables=>{
  const userTable = await Tables({
    name:'users',
    indices:['email'],
  })
  //do something with user table
})

Schema

Theres a really basic schema object which you can pass to the table to initialize indices and other collection options. See Mongo Collection Options for additional options.

{
  name:string //the name of the collection, required,
  indices:string[], //array of keys on the data which you want to index
  compound:[     //array of array of data keys you want to index
    string[]
  ],
  ...collectionOptions
}

async get(id) => doc

Get a single document

async set(id,doc) =>doc

Set a single document (upsert)

async has(id) => boolean

Check if a document exists by id

async delete(id) => {_id:string}

Delete single document

async upsert(doc) => doc

Upsert (update or create if does not exist) document. Assigns id if not provided.

async update(doc) => doc

Update an existing document, requires an _id.

async insert(doc) => doc

Insert a new document.

async getBy(query) => doc[]

Get documents by filtering properties. Uses indexes if they exist. Same as col.find(query).

async getAll(ids:[]) => doc[]

Get documents by array of _ids.

async deleteAll(ids:[]) => {_id:string}[]

Delete documents by an array of ids.

async insertMany(docs:[]) => doc[]

Insert many documents by array of ids. Requres _id on each document.

async list => doc[]

List all documents in collection as an array.

async drop() => undefined

Drop documents from collection.

async close() => undefined

Close Database connection. All collections on this connection will be affectect.

readStream(query:object) => highland:cursor

Read mongo query as a node compatible stream.

db() => database

Get the underlying Mongo database object.

collection() => collection

Get the underlying Mongo collection.

getBySortedBetween(filter = {}, max = Date.now(), min = 0, skip = 0, limit = 100) => doc[]

List all recods matching the filter predicate, sorted ascending, between max and min, starting at index 0 to 100.

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.1.2

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago