krakenmongo
v0.1.7
Published
This driver contains functions to help connecting things to mongoDB.
Downloads
14
Readme
KrMongo - Driver to contect Thing to mongoDB
This driver contains functions to help connecting things to mongoDB.
Main methods:
- get
- search
- post
- delete
- deleteAll
- getCollections
Running tests
node --experimental-vm-modules node_modules/.bin/jest
Publish
npm adduser
npx parcel build
npm publish
Setup
Set mongoDb login parameters
Through environement variables
process.env.dbAppName = "APP_NAME"
process.env.dbUrl = "mongodb.net"
process.env.dbName = "DB_NAME";
process.env.dbUserid = "USER_ID"
process.env.dbPassword = "PASSWORD";
process.env.dbCollection = "COLLECTION_NAME";
Directly to the KrMongo object
let k = new KrMongo()
k.dbAppName = "APP_NAME"
k.dbUrl = "mongodb.net"
k.dbName = "DB_NAME";
k.dbUserid = "USER_ID"
k.dbPassword = "PASSWORD";
k.collection = "COLLECTION_NAME"
How to use
Get
let k = new KrMongo()
let t = new KrThing("Thing", "Thing1")
let action = await k.get(t) // Returns action thing
if(action.a.isSuccess() == false){ throw(action.a.error) }
let result = action.p.get('result').value // Requested thing
console.log(result.record)
Search
let k = new KrMongo()
let limit = 10
let offset = 0
let orderBy = "name"
let orderDirection = -1
let action = await k.search({"@type": "Thing"}, limit, offset, orderBy, orderDirection)
if(action.a.isSuccess() == false){ throw(action.a.error) }
let results = action.p.get('result').values // Requested thing
console.log(results.length)
Post
let k = new KrMongo()
ler record = {
"@context": "https://schema.org/",
"@type": "Thing",
"@id": "thing1",
"name": "thing1"
}
let t = new KrThing(record)
let action = await k.post(t) // Returns action thing
if(action.a.isSuccess() == false){ throw(action.a.error) }
Delete
let k = new KrMongo()
let t = new KrThing("Thing", "Thing1")
let action = await k.delete(t) // Returns action thing
if(action.a.isSuccess() == false){ throw(action.a.error) }
DeleteAll
let k = new KrMongo()
let t = new KrThing("Thing", "Thing1")
let action = await k.deleteAll() // Returns action thing
if(action.a.isSuccess() == false){ throw(action.a.error) }