Adonis.js搜索查询
我正在尝试使用AdonisJs找到一种执行搜索查询的方法,但我找不到任何方法来使用Lucid ORM ...
我目前正在使用它,但显然不是制作搜索查询的正确方法:
let posts = yield Post.query().with('category').where({
title: request.input('term')
}).forPage(1, 10).fetch()
如何使用adonis.js框架直接执行postgres SQL查询?
SELECT id FROM posts WHERE content LIKE '%searchterm%' OR WHERE tags LIKE '%searchterm%' OR WHERE title LIKE '%searchterm%'
回答如下:
找到了使用Database.schema.raw(execute queries here)
在Adonis中直接执行SQL查询的解决方案,所以:
const postQuery = yield Database.schema.raw('SELECT * FROM posts');
const posts = postQuery.rows
console.log(posts);
编辑1 使用Lucid ORM执行搜索查询:
const term = request.input('term');
yield Database.select('*').from('posts').where('title', 'LIKE', '%'+term+'%')
console.log(posts);
编辑2 更好的原始查询:
yield Database.select('*').from('posts').whereRaw('title @@ :term OR description @@ :term', {term: '%'+term+'%'})
Adonis.js搜索查询
我正在尝试使用AdonisJs找到一种执行搜索查询的方法,但我找不到任何方法来使用Lucid ORM ...
我目前正在使用它,但显然不是制作搜索查询的正确方法:
let posts = yield Post.query().with('category').where({
title: request.input('term')
}).forPage(1, 10).fetch()
如何使用adonis.js框架直接执行postgres SQL查询?
SELECT id FROM posts WHERE content LIKE '%searchterm%' OR WHERE tags LIKE '%searchterm%' OR WHERE title LIKE '%searchterm%'
回答如下:
找到了使用Database.schema.raw(execute queries here)
在Adonis中直接执行SQL查询的解决方案,所以:
const postQuery = yield Database.schema.raw('SELECT * FROM posts');
const posts = postQuery.rows
console.log(posts);
编辑1 使用Lucid ORM执行搜索查询:
const term = request.input('term');
yield Database.select('*').from('posts').where('title', 'LIKE', '%'+term+'%')
console.log(posts);
编辑2 更好的原始查询:
yield Database.select('*').from('posts').whereRaw('title @@ :term OR description @@ :term', {term: '%'+term+'%'})