adonis-queries

1.0.9 • Public • Published

Adonis Queries

Query builder lucid adonis. this package is inspired by laravel query builder

This package adds a query builder for use in the adonis framework. These query builders include:

  • when
  • whereDate
  • whereBetween and whereNotBetween
  • whereBy and orWhereBy
  • whereHasBy and orWhereHasBy
  • exists and doesntExist
  • value

How To Use

Installation

npm i adonis-queries --save

Make trait Query

adonis make:trait Query

Register adonis queries on trait Query

const Queries = require('adonis-queries')

class Query {
  register (Model) {
    Queries(Model)
  }
}

module.exports = Query

Use to Model

class User extends Model {
  static boot () {
    super.boot()
    this.addTrait('Query')
  }
}

Example:

Query when

Before

if (request.input('search')) {
  model.where('name', request.input('search'))
}

After

model.when(request.input('search'), query => {
  query.where('name', request.input('search'))
})

Query whereDate

Before

if (request.input('date')) {
  model.whereRaw(`DATE(created_at) = ?`, [request.input('date')])
}

After

# by default operator is '=', but use custom other operator '<', '>', '<=', '<>', '>='
model.whereDate('created_at', request.input('date'), operator)

Query whereDateBetween

Before

if (request.input('start_date') && request.input('end_date')) {
  model.whereRaw(`DATE(created_at) BETWEEN ? AND ?`, [request.input('start_date'), request.input('end_date')])
}

After

model.whereDateBetween('created_at', [request.input('start_date'), request.input('end_date')])
model.whereDateNotBetween('created_at', [request.input('start_date'), request.input('end_date')])

Query whereBy and orWhereBy

Before

if (request.input('search')) {
  model.where('name', request.input('search'))
}

After

# by default operator is '=', but use custom other operator '<', '>', '<=', '<>', '>=', 'like or ilike'
model.whereBy('name', request.input('search'), operator)
 .orWhereBy('username', request.input('search'), operator)

query whereHasBy and orWhereHasBy

Before

if (request.input('search')) {
  model.whereHas('users', query => {
    query.where('name', request.input('search'))
  })
}

After

# by default operator is '=', but use custom other operator '<', '>', '<=', '<>', '>=', 'like or ilike'
model.whereHasBy('users', 'name', request.input('search'), operator)
 .orWhereHasBy('users', 'username', request.input('search'), operator)

Addition

Exists and DoesntExist

# return boolean true or false
model.exists()
model.doesntExist()

Value

# return single field
model.value('email')

License

Copyright (c) 2021

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i adonis-queries

Weekly Downloads

1

Version

1.0.9

License

MIT

Unpacked Size

7.42 kB

Total Files

3

Last publish

Collaborators

  • ayocodingit