3

Right now in my index.html, I need to do

<script src="node_modules/nedb/browser-version/out/nedb.min.js"> </script>

<script>var nedb = new Nedb({filename: 'someFile2', autoload: true});
</script>

This loads the persistent nedb database "someFile2" under indexeddb in the Google Chrome browser.

How do I bootstrap nedb via the main app component or module? I want to be able to create an nedb database via the application code.

I use Systemjs with angular 2.

coder
  • 353
  • 5
  • 18

2 Answers2

3

I recently spent some time importing NeDB into an Angular2 + Electron project. Eventually I was able to do it, so here is how I did it. I hope it will be close enough to doing it in a non-Electron project.

First, make sure both nedb and @types/nedb are installed:

npm install -S nedb
npm install -S @types/nedb

You need to tell SystemJS where to find nedb. To do that, add the following line to the map object in systemjs.config.js (this assumes you specified an 'npm' path in the 'paths' object):

'nedb': 'npm:nedb/browser-version/out/nedb.min.js'

Import the Datastore module in your Angular 2 file:

import * as Datastore from 'nedb';

You should now be able to create a new instance of Datastore in your code, e.g.:

constructor() {
  let db = new Datastore();
  db.loadDatabase(function() {
    // ...
  });
}

I hope this helps.

2

In addition to Alberto Venturini's answer.

For Angular2 + Electron project using Angular2 CLI

Add path to NeDB in .angular-cli.json in your root folder, to tell where to find NeDB.

"scripts": [
        "../node_modules/nedb/browser-version/out/nedb.min.js"
]
oreofeolurin
  • 636
  • 3
  • 16