totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

How to change the path of NOSQL Embedded? #741

Closed aalfiann closed 4 years ago

aalfiann commented 4 years ago

Hello peter,

I create my own library and this library is using NoSQL Embedded.

When I want to unit test with mocha, I can't set the path of NOSQL()

script_test.js I've tried

require('total.js');
var app = require('../src/app.js');

var nosql = NOSQL(__dirname+"fixtures/data.nosql");  // not working
var nosql = NOSQL.load(__dirname+"fixtures/data.nosql"); // not working
var nosql = NOSQL(F.path.root("../../test/fixtures/data.nosql")); // not working

My structure library

molda commented 4 years ago

Have you tried console.log(__dirname+"fixtures/data.nosql") ? __dirname returns a path NOT ENDING with / See docs https://nodejs.org/docs/latest/api/modules.html#modules_dirname

aalfiann commented 4 years ago

sorry I've typo but it still not working,

var nosql = NOSQL(__dirname+"/fixtures/data.nosql");

I try to console.log(nosql), it shows like this

Database {
  readonly: false,
  filename:
   '/home/aziz/Work/nodejs/totaljs-query-builder/node_modules/mocha/bin/databases/home/aziz/Work/nodejs/totaljs-query-builder/test/fixtures/data.nosql.nosql',
aalfiann commented 4 years ago

I also tried like this

var nosql = NOSQL("../../../test/fixtures/data");

it shows like this

Database {
  readonly: false,
  filename:
   '/home/aziz/Work/nodejs/totaljs-query-builder/node_modules/mocha/bin/databases/../../../test/fixtures/data.nosql',
molda commented 4 years ago

NOSQL() doesn't support loading from absolute path, it must be relative to the ./databases folder. Try this:

    var db = require('total.js/nosql');
    var nosql = db.load('data', __dirname+"/fixtures/data.nosql");
aalfiann commented 4 years ago

Now I got this work...

Thank you

aalfiann commented 4 years ago

Sorry, I've reopen this issues because this way still not working for join.

I've tried

builder.join("data2",DB.load("data2",  __dirname+"/fixtures/data2")).on("id","id");

response ok, there is no error but no data join.

Thank you.

molda commented 4 years ago

Is that also a typo __dirname+"/fixtures/data2" <- .nosql missing

Try this:

var db = require('total.js/nosql');
var nosql = db.load('data', __dirname+"/fixtures/data.nosql");
var nosql2 = db.load('data2', __dirname+"/fixtures/data2.nosql");
...
    builder.join("data2",nosql2).on("id","id");
aalfiann commented 4 years ago

It's no typo, if I add .nosql in the last string, it's not work,

then i've tried like you said above, still not working.

var nosql = db.load('data', __dirname+"/fixtures/data.nosql"); // not working
var nosql2 = db.load('data2', __dirname+"/fixtures/data2.nosql"); // not working
var nosql = db.load('data', __dirname+"/fixtures/data"); // working
var nosql2 = db.load('data2', __dirname+"/fixtures/data2"); // join not working but there is no error

It hard for me to debug because no error log appearing.

I've think maybe I just got wrong how to join it or maybe there is mistake in my library, but when I tried to put in database directory, everything working fine, so from here I can confirm there is no issue in my library.

petersirka commented 4 years ago

Hi, I have updated a bit NoSQL embedded, but you need to install Total.js beta: $ npm install total.js@beta:

var a = NOSQL('~absolute/path/to/users.nosql');
var b = NOSQL('~absolute/path/to/address.nosql');

a.find().join('address', b).on('userid', 'id').callback(console.log);

I don't recommend to change path because NoSQL embedded can create temporary files in some specific cases.

aalfiann commented 4 years ago

Thank you @petersirka , I have tried the beta version, but join still not working with absolute path..

test/query.js

const assert = require('assert');
require('total.js');

describe('new beta version test', function() {

    it('single test', function(){
        var a = NOSQL(__dirname+'/fixtures/data1.nosql');
        a.find().callback(console.log);
    });

    it('join test', function(){
        var a = NOSQL(__dirname+'/fixtures/data1.nosql');
        var b = NOSQL(__dirname+'/fixtures/data2.nosql');

        a.find().join('address', b).on('user_id', 'id').callback(console.log);
    });

});

npm test

test/fixtures/data1.nosql

{"user_id":1,"name":"budi","age":10}
{"user_id":5,"name":"wawan","age":20}
{"user_id":3,"name":"tono","age":30}

test/fixtures/data2.nosql

{"id":1,"address":"bandung","email":"a@b.com"}
{"id":2,"address":"jakarta","email":"c@d.com"}
{"id":3,"address":"solo","email":"e@f.com"}
{"id":4,"address":"solo, balapan","email":"g@h.com"}
{"id":5,"address":"surabaya","email":"i@j.com"}
petersirka commented 4 years ago

Of course because you need to add ~ before the path.

Look detailed:

NOSQL('~absolute/path/to/users.nosql');
// ---> ~absolute/path/
aalfiann commented 4 years ago

Sorry I didn't look closely..

But now I got this work..

Thank You, @petersirka

I will wait the update for this..