Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find with 'OR' condition does not work with typeorm. #259

Open
gionaico opened this issue Apr 28, 2021 · 2 comments
Open

find with 'OR' condition does not work with typeorm. #259

gionaico opened this issue Apr 28, 2021 · 2 comments

Comments

@gionaico
Copy link

I have a problem when I use find action with the "query" param, I would like to use the ''OR" condition but currently just is possible "AND" condition.

                   find: {
			cache: {
				keys: ["populate", "fields", "limit", "offset", "sort", "search", "searchFields", "query"]
			},
			params: {
				populate: [
					{ type: "string", optional: true },
					{ type: "array", optional: true, items: "string" },
				],
				fields: [
					{ type: "string", optional: true },
					{ type: "array", optional: true, items: "string" },
				],
				limit: { type: "number", integer: true, min: 0, optional: true, convert: true },
				offset: { type: "number", integer: true, min: 0, optional: true, convert: true },
				sort: { type: "string", optional: true },
				search: { type: "string", optional: true },
				searchFields: [
					{ type: "string", optional: true },
					{ type: "array", optional: true, items: "string" },
				],
				query: [
					{ type: "object", optional: true },
					{ type: "string", optional: true },
				],
			},
			handler(ctx) {				
				let params = this.sanitizeParams(ctx, ctx.params);
				return this._find(ctx, params);
			}
		}

This problem would be resolved if add one type more on "query" param and this looks like this.

find: {
	cache: {
		...
	},
	params: {
		....
		query: [
                        ...
			{ type: "array", optional: true}
		],
	},
	handler(ctx) {
		....
	}
}
@gionaico gionaico changed the title find with 'OR' condition. find with 'OR' condition does not work with typeorm. Apr 28, 2021
@icebob
Copy link
Member

icebob commented Apr 28, 2021

Why can array solve the problem? If it's not implemented in the service and adapters.
By the way, if the query is a String, it wil be parsed to JSON object which is passed to the adapter.

@gionaico
Copy link
Author

In my case my service looks like this.

const DbService = require("moleculer-db");

const SVC: ServiceSchema = {
  name: svc1,
  model: model1,
  adapter: ADAPTER("table", [...models]),
  mixins: [DbService],
  actions:{
      ....
  }
}

TypeOrm allow make queries like this:

// Querying with OR operator:
userRepository.find({
  where: [
    { firstName: "Timber", lastName: "Saw" },
    { firstName: "Stan", lastName: "Lee" }
  ]
});

So when I call my action like this:

     ctx.call(`svc1.find`, { 
         searchFields, 
         query 
   });

The thing is that when I exec the code the first place where it goes is the mixin "moleculer-db" and if this does not have a "query" param with a type array, moleculer throw an params validation error.

Maybe it is a bad implementation but I have could resolve the problem with this.

find: {
	cache: {
		...
	},
	params: {
		....
		query: [
                        ...
			{ type: "array", optional: true}
		],
	},
	handler(ctx) {
		....
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants