0

I tried the following basic example from the online;

var loki = require('lokijs');
var lokiDatabase = new loki('oops.json');

var collection = lokiDatabase.addCollection('props',{
    indices: ['name']
});


function hello() {
    var insertjson = {name:'Ram'};
    collection.insert(insertjson);
    lokiDatabase.saveDatabase();
    var select = collection.findOne({name:'Ram'});
    console.log('select response:'+ select.name);
 }

hello();

I am able to get the output with findOne method;

But here, the question is; as tutorials said, LokiJS is an in-memory database; wheras, I can see all the inserts & updates are presenting in the oops.json file.

Where we are storing/from to in-memory here?

Did I understood the concepts wrong?

Ram Kowsu
  • 711
  • 2
  • 10
  • 30
  • This is desirable when calling the saveDatabase method, don't you think? I mean if you don't persist the db in a file, it still works in memory, right? – gorhawk Oct 29 '18 at 13:20
  • @gorhawk, in-memory in the sense, we are not gonna save the records in a file right? It should be in the RAM/some where!!! – Ram Kowsu Oct 29 '18 at 13:35
  • Just don't call the saveDatabase method. Your findOne query will still work. That is what you want, no? – gorhawk Oct 29 '18 at 13:38
  • what if I didn't call saveDatabase method, it will save in in-memory, so whenever I call findOne(), will it retrun data always? – Ram Kowsu Oct 29 '18 at 15:46
  • yes, but it's not a server, so the db only lasts as long as your program is running, depending on the environment (for example I have used it in browser env, that means as long as the page is open) Sorry but I really don't know what level of familiarity you have with these concepts so I don't know how to answer it. – gorhawk Oct 29 '18 at 15:54

2 Answers2

0

enter image description here

//lokirouter.js
const db=require('./lokidb')
const router=require('express').Router
class Erouter{
static get(){
router.get("/",(req,res)=>{
db.loadDatabase({},function(){
    try{
    const data=db.getCollection('items').find({})
    res.send(data).status(200)
    }
    catch(r){
        res.status(500).send(`${r}`)
    }
})
})
router.post("/",(req,res)=>{
db.loadDatabase({},()=>{
    try{
    const data=db.getCollection('items').insert(req.body.criteria)
    db.saveDatabase(data)
    db.save(data)
    res.send(data).status(200)
    }
    catch(r){
    res.status(500).send(`${r}`)
    }
    })
    })
return router
}}
module.exports=Erouter

//lokidb.js
var loki=require('lokijs')
var db = new loki('loki.db');
var items = db.addCollection('items');
module.exports=db

//lokiapp.js
const lokirouter=require('./lokirouter')
const express =require("express")
const bodyParser = require('body-parser')
const app=express()
const db=require('./lokidb')
const port=8000;
app.listen(port)
console.log("Server Started"+ " "+port)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use("/",lokirouter.get())
Jai Jadhav
  • 11
  • 3
0

Lokijs is a document driven database,besides not necessary to store records only a json file,can also store in local database file creating local_database.db for instance. As previous mentioned answer below you have to run it using postman. when you insert records in request body in json format ex:{ "criteria":{"name":"jason"} } it will get inserted into the local_database.db file.Similarly to retrive the records you can call get api. Since to find a particular record you can use findOne({name:"jason"}).

Jai Jadhav
  • 11
  • 3