0

I have looked at linvodb3 and pouchdb for a project that I am working on. According to https://stackoverflow.com/a/35142175, I cannot access a IndexDB database from CLI directly when created through angular-cli.

Is there a NoSQL database usable in NodeJS/Angular2 app that creates an instance reachable from the CLI and web page without using a server instance?

I need to pre-ship a database with information that will not be updated on the client (at least in the early stages) but allows searching and other database features for a offline document library.

Edit 1

let PouchDB = require('pouchdb');
let db = PouchDB('./db');

This gives me a folder named "db" which has the database files it when testing directly from NodeJS CLI. As, long as I call the same db folder from both Angular2 and NodeJS then I can pre-publish data.

For Angular2:

import * as PouchDB from 'pouchdb';
import { Injectable } from '@angular/core';

@Injectable()
export class PouchdbService {

   private _db: any; // <-- Database Object
   private _databaseName: string = "./my_db"; // <-- Database == folder location

   constructor() {
       this._db = new PouchDB(this._databaseName);
   }
}

From NodeJS:

#!/usr/bin/env node

'use strict'

let PouchDB = require ('pouchdb');
let databaseName = "./my_db"; // <-- same as in angular
let db = new PouchDb ( databaseName ); // <-- should be the same database
Community
  • 1
  • 1
flamusdiu
  • 1,722
  • 2
  • 14
  • 31
  • can you post some sample code you've tried? pouchdb is designed to run in browser and should work OOTB. Regardless of angular or angular cli. – Ahmed Musallam Apr 02 '17 at 18:19
  • @AhmedMusallam Yeah, I looked at it. I uses IndexDB like linvodb3. Let me replace the linvodb3 code that I've been using and I'll post something tonight. – flamusdiu Apr 02 '17 at 20:39
  • @AhmedMusallam added some example code. I will test this tonight after work. – flamusdiu Apr 03 '17 at 12:32
  • Actually, that won't work; PouchDB stores just like linvodb3 -- using IndexDB in the browser. At least until I can figure out how to use LevelDB adapter properly, but that seems to be usable from NodeJS. Or, I would have to run a PouchDB server locally... which I was avoiding. – flamusdiu Apr 03 '17 at 20:32
  • 1
    I'm not exactly sure what you are after. pouchdb uses indexedDB under the hood (as the docs suggest). there should be no problem with using it with angular. if you want to sync the db with a remote one, you can start a CouchDb instance in your local and sync to it. Please try creating an angular cli project on github and try using pouchdb, if you get stuck, let me know. – Ahmed Musallam Apr 03 '17 at 20:57
  • lI cannot use CouchDb (this is for an offline data store for usage for a bunch of information for my work which I may not have access to web nor can I post company information online either. However, at the same time, I need to inject my docs through NodeJS CLI. This is two fold: 1) to pre-package the db with the app and 2) so I can give the app to other peers at my company to use. – flamusdiu Apr 03 '17 at 21:08
  • Oh, btw, I did get angular-cli and pouchdb working which was not even that hard. =) I do like the sync between local and servers. – flamusdiu Apr 03 '17 at 21:09
  • So If I understand correctly, you want to ship a database with your application. Ie you want to persist some data on it, your peers can copy the app as-is then run it and that data will still be persisted. if that's correct then you should checkout: https://github.com/pouchdb/pouchdb-server You can build a small script that'll bring up the pouchdb-server before running the angular app. all changes to the pouchdb-server are persisted to the filesystem in-place. – Ahmed Musallam Apr 03 '17 at 21:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139811/discussion-between-ahmed-musallam-and-flamusdiu). – Ahmed Musallam Apr 03 '17 at 21:59

1 Answers1

2

I am using NodeJS/Electron/Express-PouchDB to get this to work. I also spawned express through the usage of cluster to ensure express was started with electron and both get tore down together.

For NodeJS/Electron side:

    const PouchDB = require('pouchdb');
    const express = require('express');
    const expressPouchDB = require('express-pouchdb');

    const app = express();

    PouchDB.defaults({prefix: './db/'});

    app.use('/',expressPouchDB(PouchDB, {
          overrideMode: {
            include: ['routes/fauxton']
          }
        })
    );

    resolve(app.listen(3000));

For Angular2/4:

import * as PouchDB from 'pouchdb';
import * as pouchdbFind from 'pouchdb-find';

@Injectable()
export class PouchdbService {
    private _localdb: string = "http://localhost:3000/main" ;
    private _pouchDb: any;

  constructor() {
    this._pouchDb = new PouchDB(this._localdb);
    PouchDB.plugin(pouchdbFind);

 ...

This gives me access the PouchDB service.

flamusdiu
  • 1,722
  • 2
  • 14
  • 31