Skip to main content

Firebase Realtime DB Pagination

Project description

Paginate Firebase RealtimeDB records using cursor pagination.

PyPI version

Overview

The firebase_rtdb_pagination was written to simplify record fetching with pagination.

Usage

import firebase_admin
from firebase_admin import credentials, db
from firebase_rtdb_pagination import FirebaseRTDBPagination


cred = credentials.Certificate(
    "/path/to/service_account.json")
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://PROJECT-ID-default-rtdb.firebaseio.com'
})

Sample Firebase data called inventory:

[
  {
    "name": "Pen",
    "qty": 100
  },
  {
    "name": "Pencil",
    "qty": 10000
  },
  {
    "name": "Notebook",
    "qty": 750
  },
  {
    "name": "Eraser",
    "qty": 0
  },
  {
    "name": "Whiteboard Marker",
    "qty": 10
  }
]

Firebase Rule:

{
    "rules": {
        ...
        "inventory": {
            ".indexOn": ["qty"]
        }
        ...
    }
}

Instantiating pagination class

pagination = FirebaseRTDBPagination(
    firebase_admin_db=db,
    path='/inventory',
    child_key='qty',
    sort_by='desc',
    per_page=3
)

print(pagination.all())
[
  {
    'name': 'Pen',
    'qty': 100
  },
  {
    'name': 'Pencil',
    'qty': 10000
  },
  {
    'name': 'Notebook',
    'qty': 750
  },
  {
    'name': 'Eraser',
    'qty': 0
  },
  {
    'name': 'Whiteboard Marker',
    'qty': 10
  },
  {
    'name': 'Whiteboard',
    'qty': 255
  }
]

# page 1
page1 = pagination.get()
print('page 1: {}'.format(page1))
{
  'data': [
    {
      'name': 'Pencil',
      'qty': 10000
    },
    {
      'name': 'Notebook',
      'qty': 750
    },
    {
      'name': 'Whiteboard',
      'qty': 255
    }
  ],
  'cursor': 255,
  'pages': 2,
  'total': 6
}

# page 2
page2 = pagination.get(page1.get('cursor'))
print('page 2: {}'.format(page2))
{
  'data': [
    {
      'name': 'Pen',
      'qty': 100
    },
    {
      'name': 'Whiteboard Marker',
      'qty': 10
    },
    {
      'name': 'Eraser',
      'qty': 0
    }
  ],
  'cursor': 0,
  'pages': 2,
  'total': 6
}

# page 3
page3 = pagination.get(page2.get('cursor'))
print('page 3: {}'.format(page3))
{
  'data': [],
  'cursor': [],
  'pages': 2,
  'total': 6
}

# page 4
page4 = pagination.get(page3.get('cursor'))
print('page 4: {}'.format(page4))
{
  'data': [],
  'cursor': [],
  'pages': 2,
  'total': 6
}

Getting it

To download firebase_rtdb_pagination, either fork this github repo or simply use Pypi via pip.

$ pip install firebase_rtdb_pagination

License

MIT License

Copyright (c) 2021 Jay Milagroso

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

firebase_rtdb_pagination-1.0.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page