Skip to main content

Collection Management


Retrieving a List of Collections​

The zkDatabase library provides the collectionList method to retrieve a list of all collections within a specified database. This function is particularly useful for administrators and developers looking to understand the database structure, inspect available collections, or perform batch operations across multiple collections.

Definition​

The collectionList function is called on a database object, and it returns a Result that contains an array of collection names.

const collections = (await zkdb.db(databaseName).collectionList()).unwrap();

Parameters​

  • None

Returns​

The collectionList method returns a Result that resolves to an array of string. Each string represents the name of a collection within the specified database.

Example​

Here is an example demonstrating how to retrieve the list of collections from a specified database:

import { ZkDatabase } from 'zkdb';

const zkdb = new ZkDatabase({
apiKey: 'zkdb_536aac02a1b7.c001b6b8f...da3aa4185d8d2ad07f0ae94aT',
// This URL is for test environment
url: "https://serverless.zkdatabase.org/graphql",
});
const dbTest = zkdb.db('zkdb_test');
const collections = (await dbTest.collectionList()).unwrap();
console.log('Collections:', collections);

This snippet, when executed, retrieves all collection names from the zkdb_test database.

Expected Output​

The result of the collectionList method will be an array containing the names of collections or detailed information about each collection, depending on the implementation specifics. For example:

Collections in zkdb_test: [
'users',
'products',
'orders',
// ...other collection names
]

This document provides a clear and structured explanation for retrieving collections using zkDatabase, ensuring users understand the method's functionality, parameters, and expected outcomes.