Skip to content

Commit c5f5a55

Browse files
Merge branch 'main' into vars_like_python
2 parents 51b3e75 + be27736 commit c5f5a55

File tree

4 files changed

+10641
-17436
lines changed

4 files changed

+10641
-17436
lines changed

docs/api/woqlclient.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,29 @@ name the databases list will be set to empty
338338
client.organization("admin")
339339
```
340340
341+
## hasDatabase
342+
##### woqlClient.hasDatabase([orgName], [dbName]) ⇒ <code>Promise</code>
343+
Checks if a database exists
344+
345+
Returns true if a DB exists and false if it doesn't. Other results
346+
throw an exception.
347+
348+
349+
| Param | Type | Description |
350+
| --- | --- | --- |
351+
| [orgName] | <code>string</code> | the organization id to set the context to |
352+
| [dbName] | <code>string</code> | the db name to set the context to |
353+
354+
**Example**
355+
```javascript
356+
async function executeIfDatabaseExists(f){
357+
const hasDB = await client.hasDatabase("admin", "testdb")
358+
if (hasDB) {
359+
f()
360+
}
361+
}
362+
```
363+
341364
## getDatabases
342365
##### woqlClient.getDatabases() ⇒ <code>Promise</code>
343366
Gets the organization's databases list.

lib/woqlClient.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,37 @@ WOQLClient.prototype.organization = function (orgId) {
142142
return this.connectionConfig.organization();
143143
};
144144

145+
/**
146+
* Checks if a database exists
147+
*
148+
* Returns true if a DB exists and false if it doesn't. Other results
149+
* throw an exception.
150+
* @param {string} [orgName] the organization id to set the context to
151+
* @param {string} [dbName] the db name to set the context to
152+
* @returns {Promise}
153+
* @example
154+
* async function executeIfDatabaseExists(f){
155+
* const hasDB = await client.hasDatabase("admin", "testdb")
156+
* if (hasDB) {
157+
* f()
158+
* }
159+
* }
160+
*/
161+
WOQLClient.prototype.hasDatabase = async function (orgName, dbName) {
162+
const dbCheckUrl = `${this.connectionConfig.apiURL()}db/${orgName}/${dbName}`;
163+
return new Promise((resolve, reject) => {
164+
this.dispatch(CONST.HEAD, dbCheckUrl).then((req) => {
165+
resolve(true);
166+
}).catch((err) => {
167+
if (err.status === 404) {
168+
resolve(false);
169+
} else {
170+
reject(err);
171+
}
172+
});
173+
});
174+
};
175+
145176
/**
146177
* Gets the organization's databases list.
147178
*

0 commit comments

Comments
 (0)