{"size": 2010, "ext": "js", "max_stars_repo_path": "lokijs.js", "max_stars_repo_name": "ianngn/node-red-contrib-lokijs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lokijs.js", "max_issues_repo_name": "ianngn/node-red-contrib-lokijs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lokijs.js", "max_forks_repo_name": "ianngn/node-red-contrib-lokijs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.4430379747, "max_line_length": 81, "alphanum_fraction": 0.547761194}
/* eslint-disable */

'use strict';

/* eslint-disable */

const Loki = require('lokijs');

module.exports = (RED) => {
  function init(config) {
    const lokidb = new Loki(config.filename);
    let coll = lokidb.getCollection(config.collection);
    if (!coll) {
      coll = lokidb.addCollection(config.collection);
    }
    return lokidb;
  }

  function lokijsConfig(n) {
    RED.nodes.createNode(this, n);
    this.filename = n.filename;
    this.collection = n.collection;
    this.lokidb = init(this);
  }
  RED.nodes.registerType('lokijs-config', lokijsConfig);

  function lokijsOp(n) {
    RED.nodes.createNode(this, n);
    this.config = RED.nodes.getNode(n.config);
    this.method = n.method;
    this.name = n.name;
    this.input = n.input;
    const node = this;

    const connect = (nd) => {
      const coll = nd.config.lokidb.getCollection(nd.config.collection);

      nd.on('input', (msg) => {
        let input = {};

        if (nd.input === 'true') {
          input = msg;
        } else if (this.input !== 'false' && typeof this.input !== 'undefined') {
          try {
            input = RED.util.getMessageProperty(msg, this.input);
          } catch (err) {
            input = {};
          }
        }

        let message = msg;

        if (nd.method === 'find') {
          message.payload = coll.find(input);
          delete message.payload.meta;
          delete message.payload.$loki;
        } else if (nd.method === 'insert') {
          if (nd.input === 'true') {
            message = coll.insert(input);
            delete message.meta;
            delete message.$loki;
          } else {
            message.payload = coll.insert(input);
            delete message.payload.meta;
            delete message.payload.$loki;
          }
        }
        nd.send(message);
      });
    };
    if (node.config) {
      connect(node);
    } else {
      node.error(RED._('lokijs.errors.missingconfig'));
    }
  }
  RED.nodes.registerType('lokijs-op', lokijsOp);
};
