Skip to content

Commit

Permalink
Merge pull request #51 from ExWeiv/dev
Browse files Browse the repository at this point in the history
BUG Fixes for New Collection Manager APIs
  • Loading branch information
loeiks authored Mar 13, 2024
2 parents 70cfa48 + 3d07b47 commit ac8ee33
Show file tree
Hide file tree
Showing 74 changed files with 239 additions and 199 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ In this file you can find what's changed in each version. (Versions with -dev, -

---

### 3.0.2

- BUG Fixes

### 3.0.1

- New functions to `create`, `delete`, `rename` and `list` collections from a selected database. Now you can manage collections in databases with these APIs.
Expand Down
4 changes: 2 additions & 2 deletions app/lib/Collections/createCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ async function createCollection(collectionId, options, createOptions) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId`);
}
const { suppressAuth } = options || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth, true);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
return await database.createCollection(collectionName, createOptions);
await database.createCollection(collectionName, createOptions);
}
catch (err) {
throw Error(`WeivData - Error when creating a new collection in a database, details: ${err}`);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/Collections/deleteCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function deleteCollection(collectionId, options, deleteOptions) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId`);
}
const { suppressAuth } = options || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth, true);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
return await database.dropCollection(collectionName, deleteOptions);
}
Expand Down
4 changes: 2 additions & 2 deletions app/lib/Collections/listCollections.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ async function listCollections(databaseName, options, filter, listOptions) {
throw Error(`WeivData - One or more required param is undefined - Required Params: databaseName`);
}
const { suppressAuth } = options || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(`${databaseName}/`, suppressAuth);
return await database.listCollections(filter, listOptions);
const { database } = await (0, connection_helpers_1.connectionHandler)(`${databaseName}/`, suppressAuth, true);
return await database.listCollections(filter, listOptions).toArray();
}
catch (err) {
throw Error(`WeivData - Error when listing all collections in a database, details: ${err}`);
Expand Down
4 changes: 2 additions & 2 deletions app/lib/Collections/renameCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ async function renameCollection(collectionId, newCollectionName, options, rename
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId, newCollectionName`);
}
const { suppressAuth } = options || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth, true);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
return await database.renameCollection(collectionName, newCollectionName, renameOptions);
await database.renameCollection(collectionName, newCollectionName, renameOptions);
}
catch (err) {
throw Error(`WeivData - Error when renaming a collection, details: ${err}`);
Expand Down
4 changes: 2 additions & 2 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exweiv/weiv-data",
"version": "3.0.1",
"version": "3.0.2",
"description": "Custom API Library for Wix sites to connect MongoDB. Designed to easily switch from wix-data APIs.",
"main": "./lib/index.js",
"files": [
Expand Down
19 changes: 13 additions & 6 deletions app/src/Collections/createCollection.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { connectionHandler } from '../Helpers/connection_helpers';
import { splitCollectionId } from '../Helpers/name_helpers';
import { CollectionID, WeivDataOptions } from '../Helpers/collection';
import type { CreateCollectionOptions, Collection } from 'mongodb';
import type { CreateCollectionOptions } from 'mongodb';

/**
* Creates a new collection inside of a selected database. (User must have createCollection permission inside MongoDB dashboard, you can also use suppressAuth with options).
*
* @param collectionId CollectionID (<database>/<collection>).
* @example
* ```js
* import { createCollection } from '@exweiv/weiv-data';
*
* createCollection('Database/NewCollectionName', { suppressAuth: true });
* ```
*
* @param collectionId CollectionID (< database >/< collection >).
* @param options An object containing options to use when processing this operation.
* @param createOptions Native options of MongoDB driver when creating a collection. [Checkout here.](https://mongodb.github.io/node-mongodb-native/6.5/interfaces/CreateCollectionOptions.html)
* @returns {Promise<Collection>} Fulfilled - The Collection cursor of native MongoDB driver.
* @returns {Promise<void>} void
*/
export async function createCollection(collectionId: CollectionID, options?: WeivDataOptions, createOptions?: CreateCollectionOptions): Promise<Collection> {
export async function createCollection(collectionId: CollectionID, options?: WeivDataOptions, createOptions?: CreateCollectionOptions): Promise<void> {
try {
if (!collectionId) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId`);
}

const { suppressAuth } = options || {};
const { database } = await connectionHandler<true>(collectionId, suppressAuth);
const { database } = await connectionHandler<true>(collectionId, suppressAuth, true);
const { collectionName } = splitCollectionId(collectionId);
return await database.createCollection(collectionName, createOptions);
await database.createCollection(collectionName, createOptions);
} catch (err) {
throw Error(`WeivData - Error when creating a new collection in a database, details: ${err}`);
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/Collections/deleteCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import type { DropCollectionOptions } from 'mongodb';
/**
* Deletes a collection inside of a selected database. (User must have dropCollection permission inside MongoDB dashboard, you can also use suppressAuth with options).
*
* @param collectionId CollectionID (<database>/<collection>).
* @example
* ```js
* import { deleteCollection } from '@exweiv/weiv-data';
*
* deleteCollection('Database/ExistingCollectionName', { suppressAuth: true });
* ```
*
* @param collectionId CollectionID (< database >/< collection >).
* @param options An object containing options to use when processing this operation.
* @param deleteOptions Native options of MongoDB driver when deleting a collection. [Checkout here.](https://mongodb.github.io/node-mongodb-native/6.5/interfaces/DropCollectionOptions.html)
* @returns {Promise<boolean>} Fulfilled - True if succeed.
Expand All @@ -18,7 +25,7 @@ export async function deleteCollection(collectionId: CollectionID, options?: Wei
}

const { suppressAuth } = options || {};
const { database } = await connectionHandler<true>(collectionId, suppressAuth);
const { database } = await connectionHandler<true>(collectionId, suppressAuth, true);
const { collectionName } = splitCollectionId(collectionId);
return await database.dropCollection(collectionName, deleteOptions);
} catch (err) {
Expand Down
17 changes: 12 additions & 5 deletions app/src/Collections/listCollections.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { connectionHandler } from '../Helpers/connection_helpers';
import { WeivDataOptions } from '../Helpers/collection';
import type { ListCollectionsCursor, Document, ListCollectionsOptions } from 'mongodb';
import type { Document, ListCollectionsOptions, CollectionInfo } from 'mongodb';

/**
* Lists collections inside of a selected database. (User must have listCollections permission inside MongoDB dashboard, you can also use suppressAuth with options).
*
* @example
* ```js
* import { listCollections } from '@exweiv/weiv-data';
*
* listCollections('DatabaseName', { suppressAuth: true });
* ```
*
* @param databaseName Database name that you want to get the collections of.
* @param options An object containing options to use when processing this operation.
* @param filter MongoDB native filtering options. [Read more in native docs.](https://mongodb.github.io/node-mongodb-native/6.5/classes/Db.html#listCollections)
* @param listOptions MongoDB native listCollections options. [Read more in native docs.](https://mongodb.github.io/node-mongodb-native/6.5/classes/Db.html#listCollections)
* @returns {Promise<ListCollectionsCursor>} Fulfilled - Native ListCollectionsCursor of MongoDB driver.
* @returns {Promise<CollectionInfo>} Fulfilled - Array of `[CollectionInfo](https://mongodb.github.io/node-mongodb-native/6.3/interfaces/CollectionInfo.html)`
*/
export async function listCollections(databaseName: string, options?: WeivDataOptions, filter?: Document, listOptions?: ListCollectionsOptions): Promise<ListCollectionsCursor> {
export async function listCollections(databaseName: string, options?: WeivDataOptions, filter?: Document, listOptions?: ListCollectionsOptions): Promise<CollectionInfo[]> {
try {
if (!databaseName) {
throw Error(`WeivData - One or more required param is undefined - Required Params: databaseName`);
}

const { suppressAuth } = options || {};
const { database } = await connectionHandler<true>(`${databaseName}/`, suppressAuth);
return await database.listCollections(filter, listOptions);
const { database } = await connectionHandler<true>(`${databaseName}/`, suppressAuth, true);
return await database.listCollections(filter, listOptions).toArray();
} catch (err) {
throw Error(`WeivData - Error when listing all collections in a database, details: ${err}`);
}
Expand Down
19 changes: 13 additions & 6 deletions app/src/Collections/renameCollection.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { connectionHandler } from '../Helpers/connection_helpers';
import { splitCollectionId } from '../Helpers/name_helpers';
import { CollectionID, WeivDataOptions } from '../Helpers/collection';
import type { Collection, RenameOptions } from 'mongodb';
import type { RenameOptions } from 'mongodb';

/**
* Renames a collection inside of a selected database. (User must have renameCollection permission inside MongoDB dashboard, you can also use suppressAuth with options).
*
* @param collectionId CollectionID (<database>/<collection>).
* @example
* ```js
* import { renameCollection } from '@exweiv/weiv-data';
*
* renameCollection('Database/ExistingCollectionName', 'NewCollectionName', { suppressAuth: true });
* ```
*
* @param collectionId CollectionID (< database >/< collection >).
* @param newCollectionName New name of collection.
* @param options An object containing options to use when processing this operation.
* @param renameOptions Native options of MongoDB driver when renaming a collection. [Checkout here.](https://mongodb.github.io/node-mongodb-native/6.5/classes/Db.html#renameCollection)
* @returns {Promise<Collection>} Fulfilled - The Collection cursor of native MongoDB driver.
* @returns {Promise<void>} void
*/
export async function renameCollection(collectionId: CollectionID, newCollectionName: string, options?: WeivDataOptions, renameOptions?: RenameOptions): Promise<Collection> {
export async function renameCollection(collectionId: CollectionID, newCollectionName: string, options?: WeivDataOptions, renameOptions?: RenameOptions): Promise<void> {
try {
if (!collectionId || !newCollectionName) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId, newCollectionName`);
}

const { suppressAuth } = options || {};
const { database } = await connectionHandler<true>(collectionId, suppressAuth);
const { database } = await connectionHandler<true>(collectionId, suppressAuth, true);
const { collectionName } = splitCollectionId(collectionId);
return await database.renameCollection(collectionName, newCollectionName, renameOptions);
await database.renameCollection(collectionName, newCollectionName, renameOptions);
} catch (err) {
throw Error(`WeivData - Error when renaming a collection, details: ${err}`);
}
Expand Down
Loading

0 comments on commit ac8ee33

Please sign in to comment.