{"id":5292,"date":"2016-11-23T15:42:38","date_gmt":"2016-11-23T15:42:38","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5292"},"modified":"2016-11-23T15:42:38","modified_gmt":"2016-11-23T15:42:38","slug":"using-loki-js-node-scripts","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/using-loki-js-node-scripts\/","title":{"rendered":"Using Loki.js in Node scripts"},"content":{"rendered":"<p>Loki.js is a filesystem based database (like Sqllite), but that serializes to Javascript.<\/p>\n<p>To create an empty database in Node, you can do the following &#8211; this creates an empty database.<\/p>\n<pre lang=\"javascript\">\nconst loki = require(\"lokijs\");\nconst db = new loki(\"db.json\");\ndb.save();\n<\/pre>\n<p>Once you have a database, you can import existing files into it. If you want to do this without erasing the database each time, you have to check if the data collection exists each time.<\/p>\n<pre lang=\"javascript\">\nconst fs = require(\"fs\");\nconst files = fs.readdirSync(\"1\/\");\n\nconst talks = db.addCollection(\"talks\");\nfunction loadCollection(colName, callback) {\n  db.loadDatabase({}, () => {\n    let existingCollection = db.getCollection(colName);\n\n    if (!existingCollection) {\n      existingCollection = db.addCollection(\"talks\");\n    }\n\n    callback(existingCollection);\n  });\n}\n\nloadCollection(\"talks\", (talks) => {\n  const allFiles = fs.readdirSync(\".\/1\");\n\n  for (let i in allFiles) {\n    let filename = \"1\/\" + files[i];\n    const talk = JSON.parse(fs.readFileSync(filename));\n    talks.insert(talk);\n  }      \n \n  db.saveDatabase();\n});\n<\/pre>\n<p>Once you do this, you&#8217;ll have a massive JSON file to work with further.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Loki.js from within Node<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[160,302,387],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5292"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5292"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5292\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}