totaljs / framework

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

The right way to using scalar group? #717

Closed aalfiann closed 5 years ago

aalfiann commented 5 years ago

Assume that I want to create a report which is the fields is date_created, user_id, and total using scalar.

data.nosql

{"user_id_channel":"346c681f","channel_name":"Samsung","date_created":"2019-03-19 04:57:38.929","user_id":"afb72eba"}
{"user_id_channel":"9ddead41","channel_name":"LG","date_created":"2019-05-19 04:59:43.038","user_id":"fc220592"}
{"user_id_channel":"5d2aad34","channel_name":"Nokia","date_created":"2019-05-19 04:57:38.929","user_id":"afb72eba"}
{"user_id_channel":"bdab3c0b","channel_name":"LG","date_created":"2019-06-19 04:59:43.038","user_id":"afb72eba"}

Here is my code:

NOSQL('data').find().make(function(builder){
  builder.scalar('group',['user_id']);
  builder.callback(function(err, response, count){
    console.log(response);
  });
});

The result is

{
  "afb72eba": 3,
  "fc220592": 1
}

My expected result is

[
  {
    "date_created": "2019-06-19 04:59:43.038",
    "user_id": "afb72eba",
    "total": 3,
  },
  {
    "date_created": "2019-05-19 04:59:43.038",
    "user_id": "fc220592",
    "total": 1,
  }
]

My question is:

  1. According to the documentation Performs a scalar operation only in join() scope, could you give me example how to create join scope with scalar if there is only one database data.nosql?

  2. How to display the Date field to the result so it will contains more information like my expected result?

  3. I can solve this using Promise and json transform, but for big table I need the efficient or right way. So what is your solutions?

  4. Scalar with multiple group name is not working or I just don't know how to use it? builder.scalar('group',['channel_name','user_id']); but the result is {}

Sorry if the question is too much, because totaljs is great framework and I want to learn more deeply. Thank you

petersirka commented 5 years ago

Wrong thinking... You need to use some relation database like e.g. PostgreSQL. NoSQL embedded is a small file storage for small data. NoSQL embedded is targeted for simple documents, I have extended it about simple joins and scalar operations (simple means really simple).

BTW:

From docs:

database.scalar(type, [field]);

My expected result is ...

group groups data according to the field but it returns grouped fields and count of items in DB. Really I recommend to use PostgreSQL with our DBMS module: https://wiki.totaljs.com/dbms/01-welcome/

aalfiann commented 5 years ago

I did created CRUD but I need some report using NOSQL Embedded. I just playing arround how to convert the response into linechart which is required to group date. Actualy I have the solution already.

The reason Im not using mongodb because is too big for my current project. I will keep going to use NOSQL embedded because this is very fast. I just need more feature if available. :D

Btw, thank you, I will close this issue,