Skip to main content

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.

Metadata

Release files for firebase-rtdb-pagination 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for firebase-rtdb-pagination 1.0.1
File Size Uploaded Provenance Actions
firebase_rtdb_pagination-1.0.1.tar.gz 4.1 kB Without provenance Details for firebase_rtdb_pagination-1.0.1.tar.gz

Release files / firebase_rtdb_pagination-1.0.1.tar.gz

Download URL firebase_rtdb_pagination-1.0.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
625a751d6537da3f62c80fcfa3ebb1228e45d90a98ce16483abd73a2c2d212da
BLAKE2b-256 checksum
How to use checksums
97c588f8882c6256e6ce3a91929207c1a96b1dcec3936ff2900820c346386c48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.11.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.9

Security for firebase-rtdb-pagination 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page