@rym-lib/query-module
TypeScript icon, indicating that this package has built-in type declarations

0.0.17 • Public • Published

query-module

Installation

npm i @rym-lib/query-module
npm i @rym-lib/query-module-driver-sequelize

Usage

import { QueryDriverSequelize } from '@rym-lib/query-module-driver-sequelize'

import { driver } from '~/my-query-driver'

type Data = {
  id: string
  name: string
  displayName: string
  email: string
  thumbnailUrl: string
}

const userQuery = defineQuery<Data, QueryDriverSequelize>(driver, {
  source: (builder) =>
    builder
      .column('id')
      .column('profile.name', 'name')
      .column('profile.display_name', 'displayName')
      .column('email')
      .column('profile.thumbnail_url', 'thumbnailUrl')
      .from('users', 'user')
      .leftJoin('user_profiles', 'profile', 'user.id = profile.user_id'),
  rules: {
    id: 'user.id',
    name: 'profile.name',
    displayName: 'profile.display_name',
    email: 'user.email',
  },
})

// filter by any column value.
// can operate eq, ne, contains, gt, gte, lt, lte, in
const userList = await userQuery.many({
  filter: {
    displayName: {
      contains: 'abc',
    },
  },
})
console.log(userList.items)

// lookup single row or null
const user = await userQuery.one({
  filter: {
    id: {
      eq: '12345',
    },
  },
})
console.log(user)

Docs

Methods

.many(params = {})

filter by params, and return matching rows array or empty array.

const rows = query.many()

.one(params = {})

finding a record by params, when missing result, return null.

const row = query.one({
  filter: {
    id: {
      eq: 'id_1234',
    },
  },
})

if (row === null) {
  throw new Error('row not found')
}

Finding parameters

The implementation of these parameters is left to the driver.

filter

Filter data by defined property names.

const result = await query.many({
  filter: {},
})

Support operators are

  • eq
  • ne
  • contains
  • not_contains
  • lte
  • lt
  • gte
  • gt
  • in

orderBy

Specify sort order.

const result = await query.many({
  orderBy: ['created_at:desc', 'name:asc'],
})

take

Specify take rows count.

const result = await query.many({
  take: 10,
})

if (rows.length <= 10) {
  // true
}

skip

Specify thr first item to be retrieved

const result = await query.many({
  skip: 10,
})

Drivers

Middleware

Readme

Keywords

none

Package Sidebar

Install

npm i @rym-lib/query-module

Weekly Downloads

70

Version

0.0.17

License

MIT

Unpacked Size

20.3 kB

Total Files

5

Last publish

Collaborators

  • mizuki_r