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 promise that resolves to an array of collections.
await zkdb.db(databaseName: string).collectionList(): Promise<string[]>;
Parameters
- None
Returns
The collectionList
method returns a Promise
that resolves to an array of string
. Each string represents the name of a collection within the specified database. This list provides insights into the database's structure and can help in planning further operations or analyses.
Example
Here is an example demonstrating how to retrieve the list of collections from a specified database:
import { ZkDatabase } from 'zkdb';
const zkdb = await ZkDatabase.connect({
userName: "chiro-user",
privateKey: "EKFTciRxyxshZjimay9sktsn7v5PvmC5zPq7q4JnitHUytxUVnFP",
environment: "node",
// This URL is for test environment
url: "https://api.zkdatabase.org/graphql",
});
await zkdb.auth.signIn();
await zkdb.db('zkdb_test').collectionList()
await zkdb.auth.signOut();
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.