moleculer-db icon indicating copy to clipboard operation
moleculer-db copied to clipboard

how do i find a query?

Open xeroxstar opened this issue 5 years ago • 2 comments

I find moleculer-db is very confusing and frustrating, I am not sure but i can't make the database work as it's documented, i have to search for a domain in database but i am not getting the results i need:

this.broker.call('$database.service.get',{host : domain.params}).then(resolve) -> Unhandled rejection ValidationError: Parameters validation error!

tried this: this.adapter.find({ host : domain.params }).then(resolve) - > return all list of hosts in array that looks like this: [{host:'host1.com'},{host:''hotst2.com}] but i need to find one host not all of them.

tried this: this.find({ query: { host: domain.params } }).then(resolve) UnhandledPromiseRejectionWarning: TypeError: this.find is not a function

here is my service:

module.exports = {
    name      : "$database.service",
    adapter  : new db.MemoryAdapter({ filename :  'hosts.db' }),
    actions: {
        init (domain) {
          return new Promise((resolve) => {
            this.adapter.insert(domain.params);
            resolve();
          });
        },
        getDomain(domain){
          return new Promise((resolve) => {
             this.adapter.find({ host : domain.params }).then(resolve);
             //`this.broker.call('$database.service.get',{host : domain.params}).then(resolve) -> Unhandled rejection ValidationError: Parameters validation error!`;
             //this.find({ query: { host: domain.params } }).then(resolve) UnhandledPromiseRejectionWarning: TypeError: this.find is not a function
          });
        }
    }
}

Thank you

xeroxstar avatar May 02 '19 14:05 xeroxstar

You can use any one of the following code to achieve this.

this.adapter.find({ query: {host: domain.params} }).then(...); this.broker.call("$database.service.find", { query: {host: domain.params} }).then(...);

Response object will contain an array of all records that fulfils the query. If your query will result in only one record then you can access it using

this.adapter.find({ query: {host: domain.params} }).then(result => { console.log(result[0]); });

aliazlan4 avatar May 02 '19 18:05 aliazlan4

Thank you, i will try it...

xeroxstar avatar May 03 '19 03:05 xeroxstar