joshuaquek/lokijs-promise

Thank you!

Closed this issue ยท 4 comments

Hi,

I just wanted to say thank you!

I was pulling my hair out trying to get LokiJS to work like a "normal" database connector. Your little script does the trick! I haven't gotten into depth with it yet, but first tests and Loki is behaving. ๐Ÿ˜„

Just one thing. In newer Nodes versions, you don't need Bluebird and even then, I don't believe in a try/ catch block there should be a reject? At any rate, Node says it isn't defined.

  try {
    db = new Loki(dbName, {
      adapter: new Lfsa(),
      autoload: true,
      autoloadCallback: () => {
        isLoaded = true
      },
      autosave: true,
      autosaveInterval: autosaveInterval || 1000
    })
  } catch (error) {
    reject(error) // <--this is causing a "reject is not defined" error
  }

Thanks again!

Scott

Hi @smolinari thanks for highlighting this!

Yes I've just done a push to fix this issue. (v1.0.4 on npm)

Also I have added in a way for you to set the Promise library of this package to the native NodeJs Promise library.

const { setPromiseLibrary } = require('lokijs-promise')
setPromiseLibrary(global.Promise) // This sets the Promise library used in lokijs-promise to the native NodeJs Promise library

I have also updated the Readme to instruct users how this can be done too.

The default Promise library of this package will be Bluebird, as it is faster than Native Nodejs Promises for older versions of Node.

Let me know if you have any more good ideas or fixes that I could make to lokijs-promise, will be glad to add more new features to it if they are useful.

Thanks Scott!

Sure. As I work with it now and anything comes up, I'll let you know.

And no. Thank you! ๐Ÿ‘

Scott

Hey @joshuaquek

My JS experience is showing.....

I'm working with GraphQL and I'm trying to make a "models" like functionality with the resolvers and Loki. Currently I have this in my index.js.

initDB()
let models = new Models()
models.initCollection('users')
//......

// and in the resolvers for users this

const resolvers = {
  Query: {
    getUser: (_, args ) => models.findOne('users', args.id)
  },
}

This is working.

This is my "Models" object

import { getCollection } from './db.js' // db.js is taken from lokijs-promise

let Models = function() {
  this.collections = {}
  this.results = {}
}

Models.prototype.initCollection = async function(collectionName) {
  try {
    this.collections[collectionName] = await getCollection(collectionName)
    return this
  }
  catch (err) {
    console.log('Oops! Database has a problem: ', err)
  }
}

Models.prototype.findOne = function (collectionName, filter) {
  this.results = this.collections[collectionName].findOne(filter)
  return this.results 
}

export default Models

Thing is, what I'd like to avoid is the initialization part for each collection and have the following kind of functionality in the resolver.

const resolvers = {
  Query: {
    getUser: (_, args ) => models.users.findOne(args.id)
  },
}

or

const resolvers = {
  Query: {
    getPosts: (_, args ) => models.posts.findAll()
  },
}

If I end up with more collections, the collection initialization stuff could get old (old as in always having to always initialize) and I'd also like to not have to wrap each Loki collection function in my own functions.

That is where I am lacking in JS knowledge.

I was able to get part of the way, but always ended up with "Promise pending" and couldn't for the life of me resolve it to get the Loki collection functionality to work. ๐Ÿ˜Š

Any ideas? Am I overthinking Loki?

Scott

Actually thinking about this some more, in the end, for the purposes I need Loki, the initialization isn't all that bad. I could theoretically automate that too and have it be just one call for all the available collections and it's done. Hmm....

Ok. Nevermind. I'll just leave it as it. It works. But, if you have any ideas to do this better, I'm all ears. ๐Ÿ˜„

Scott