Database Exist
Check if a database exists in the system.
When to use:
- Before creating a database to avoid conflicts
- Conditional database operations
- Database validation in applications
Definition
await zkdb
.db(databaseName: string)
.exist(): Promise<Result<boolean>>;
Parameters
- None
Returns
Promise<boolean>: Returnstrueif the database exists,falseotherwise.
Example
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",
});
// Check if database exists before creating
if (!(await zkdb.db('zkdb_test').exist()).unwrapOr(false)) {
console.log('Database does not exist, creating...');
await zkdb.db('zkdb_test').create({ merkleHeight: 20 });
} else {
console.log('Database already exists');
}
// Check multiple databases
const databases = ['zkdb_test', 'zkdb_test2', 'zkdb_test3'];
for (const dbName of databases) {
const exists = (await zkdb.db(dbName).exist()).unwrapOr(false);
console.log(`Database ${dbName}: ${exists ? 'exists' : 'not found'}`);
}
Result:
Database does not exist, creating...
Database zkdb_test: exists
Database zkdb_test2: not found
Database zkdb_test3: exists