linq2js / linq2fire

Supports special operators: IN, NOT IN, OR, !=, startsWith (^=), array-contains (has) and many more
25 stars 1 forks source link

startAfter #11

Open 0xNoSignal opened 6 years ago

0xNoSignal commented 6 years ago

Hi,

I know you have pagination with "next()", but is it possible also to use operations such as startAfter ?

linq2js commented 6 years ago

I wll add that soon, please give some use cases for testing

eljass commented 5 years ago

Any timetable for this? Or how can I use next() without firing it right after the first get()? I have a table which has pagination buttons. And after user clicks the button the function having next() will fire up but right now it only returns the first page again.

linq2js commented 5 years ago

please show me your code and sample data

eljass commented 5 years ago

Here is my query:

import { db } from '@/firebase'
import { lowerCase, orderBy, last } from 'lodash'
import linq from 'linq2fire'

const setData = heading => docs => {
  console.log(heading)
  const data = []
  docs.forEach(doc => {
    const event = doc.data()
    data.push(event)
  })
  return orderBy(data, ['created.seconds'], ['asc'])
}

export const queryData = async ({
  state,
  rootState,
  firstPage: Boolean,
  nextPage: Boolean,
  prevPage: Boolean
}) => {
  try {
    const searchQuery = rootState.search.query
    const whereQuery = {
      'created >=': searchQuery.start,
      'created <=': searchQuery.end,
      time: searchQuery.time,
      ...(searchQuery.cafeIds.length
        ? { cafeId: searchQuery.cafeIds }
        : undefined),
      ...(searchQuery.textSearch
        ? { 'keywords has': lowerCase(searchQuery.textSearch) }
        : undefined)
    }
    console.log(whereQuery)
    const $events = linq(db).from('events')
    const query = $events
      .limit(state.tableParams.perPage)
      .orderBy({
        created: 'asc'
      })
      .where(whereQuery)

    if (firstPage) {
      const latest = query.get()
      console.log(latest)
      return await latest.then(setData('First Page')).catch(error => {
        console.error(error)
        return []
      })
    }
    if (nextPage) {
      return await query
        .startAfter(last(state.data).created.toDate(), last(state.data).eventId)
        .get()
        // OR useof next() ?
        // .next()
        .then(setData('Next Page'))
        .catch(error => {
          console.error(error)
          return []
        })
    }
    return await query
      // previous page's first item is stored when user paginated to the previous page
      .startAt(state.firstItem.created.toDate(), state.firstItem.eventId)
      .get()
      .then(setData('Previous Page'))
      .catch(error => {
        console.error(error)
        return []
      })
  } catch (error) {
    console.error(error)
    return []
  }
}

And here is sample of my data:

{
    cafeId: "1",
    created: DATE_OBJECT,
    eventId: 234,
    keywords: ["product_name1"],
    payment: { amount: NUMBER, type: "PAYMENT_TYPE" },
    productCount: 1,
    products: {
      lydwb5ryce9jbty85briw: {
        id: 281,
        price: NUMBER,
        product: "PRODUCT_NAME1",
        vat: NUMBER
      }
    },
    time: "HOUR",
    total: NUMBER
  },
  {
    cafeId: "3",
    created: DATE_OBJECT,
    eventId: 2,
    keywords: ["product_name1"],
    payment: { amount: NUMBER, type: "PAYMENT_TYPE" },
    productCount: 1,
    products: {
      jtdnvzopnma137u4cwvyq8: {
        id: 16,
        price: NUMBER,
        product: "PRODUCT_NAME1",
        vat: NUMBER
      }
    },
    time: "HOUR",
    total: NUMBER
  },
  {
    cafeId: "2",
    created: DATE_OBJECT,
    eventId: 3,
    keywords: ["product_name1"],
    payment: { amount: NUMBER, type: "PAYMENT_TYPE" },
    productCount: 1,
    products: {
      l0llwuw4kwdqaz6ysj5to: {
        id: 125,
        price: NUMBER,
        product: "PRODUCT_NAME1",
        vat: NUMBER
      }
    },
    time: "HOUR",
    total: NUMBER
  },
  {
    cafeId: "1",
    created: DATE_OBJECT,
    eventId: 235,
    keywords: ["product_name1", "product_name2", "product_name3"],
    payment: { amount: NUMBER, type: "PAYMENT_TYPE" },
    productCount: 3,
    products: {
      "97t3em8v3rkxk4ev3ptk": {
        id: 187,
        price: NUMBER,
        product: "PRODUCT_NAME1",
        vat: NUMBER
      },
      tl9krdyijxfay6caw1ptg: {
        id: 610,
        price: NUMBER,
        product: "PRODUCT_NAME2",
        vat: NUMBER
      },
      vt1paw66vomfc24q0143b: {
        id: 606,
        price: NUMBER,
        product: "PRODUCT_NAME3",
        vat: NUMBER
      }
    },
    time: "HOUR",
    total: NUMBER
  }
...MUCH MORE DOCUMENTS