Skip to content

Commit

Permalink
Some BUG Fixes and new default db value for config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
loeiks committed Jul 30, 2024
1 parent 575f9fd commit a7d4d70
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ In this file you can find what's changed in each version. (Versions with -dev, -
### 4.9.1

- BUG fix for query and aggregation filters (weivData.filters).
- New config option: `defaultDatabaseName` enables you to specify default database name.

### 4.9.0

Expand Down
3 changes: 2 additions & 1 deletion app/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ In WeivData you can configure some settings via `config.js` file. All you need t
```js
export const config = {
defaultIdType: "String",
syncDatabase: "MyDBName"
syncDatabase: "MyDBName",
defaultDatabaseName: "ExWeiv"
};
```

Expand Down
23 changes: 14 additions & 9 deletions app/src/Config/weiv_data_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import { kaptanLogar } from '../Errors/error_manager';
import { memoize } from 'lodash';

var __weivDatasavedConfigs__: CustomOptions.WeivDataConfig = {
defaultIdType: "String"
defaultIdType: "String",
defaultDatabaseName: "ExWeiv"
};

export function getWeivDataConfigs(): CustomOptions.WeivDataConfig {
try {
const configs: undefined | (() => CustomOptions.WeivDataConfig) = weivDataConfigs["config"];
const memoizedConfig = memoize(() => {
const configs: undefined | (() => CustomOptions.WeivDataConfig) = weivDataConfigs["config"];

if (configs && Object.keys(__weivDatasavedConfigs__).length === 0) {
const userConfig = configs();
__weivDatasavedConfigs__ = { ...__weivDatasavedConfigs__, ...userConfig };
}
if (configs && Object.keys(__weivDatasavedConfigs__).length === 0) {
const userConfig = configs();
__weivDatasavedConfigs__ = { ...__weivDatasavedConfigs__, ...userConfig };
}

return __weivDatasavedConfigs__;
return __weivDatasavedConfigs__;
});

export function getWeivDataConfigs(): CustomOptions.WeivDataConfig {
try {
return memoizedConfig();
} catch (err) {
kaptanLogar("00021", `while getting configs of WeivData library, ${err}`);
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/Helpers/connection_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Collection, Db, MongoClientOptions } from 'mongodb/mongodb';
import type { CollectionID } from '@exweiv/weiv-data';
import type { Options } from 'node-cache';
import { kaptanLogar } from '../Errors/error_manager';
import { getWeivDataConfigs } from '../Config/weiv_data_config';

export type ConnectionHandlerResult = {
memberId?: string,
Expand All @@ -27,7 +28,8 @@ export async function connectionHandler(collectionId: CollectionID, suppressAuth
if (dbName && typeof dbName === "string") {
db = pool.db(dbName);
} else {
db = pool.db("ExWeiv");
const { defaultDatabaseName } = getWeivDataConfigs();
db = pool.db(defaultDatabaseName || "ExWeiv");
}

return { memberId, database: db, collection: db.collection(collectionName) };
Expand Down
5 changes: 4 additions & 1 deletion app/src/Helpers/name_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { memoize } from 'lodash';
import type { CollectionID } from '@exweiv/weiv-data'
import { kaptanLogar } from '../Errors/error_manager';
import { getWeivDataConfigs } from '../Config/weiv_data_config';

export const splitCollectionId = memoize(splitCollectionIdMain);
function splitCollectionIdMain(collectionId: CollectionID): { dbName: string, collectionName: string } {
Expand All @@ -9,9 +10,11 @@ function splitCollectionIdMain(collectionId: CollectionID): { dbName: string, co
}

const [dbName, collectionName] = collectionId.split('/');
const { defaultDatabaseName } = getWeivDataConfigs();

if (!dbName || !collectionName) {
return { dbName: "ExWeiv", collectionName: dbName };
// When no dbname passed first value is the collection name so default db name is used here.
return { dbName: defaultDatabaseName || "ExWeiv", collectionName: dbName };
}

return { dbName, collectionName };
Expand Down
7 changes: 7 additions & 0 deletions app/weivdata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,13 @@ declare module '@exweiv/weiv-data' {
* There are only two options: "String" or "ObjectID". If you don't pick any of these default will be "String".
*/
defaultIdType?: "String" | "ObjectID"

/**
* @description
*
* You can specify the default database name here, this is optional and can be overwritten by the collectionId you pass to functions. If not defined default value will be ExWeiv.
*/
defaultDatabaseName?: string
}

/**
Expand Down

0 comments on commit a7d4d70

Please sign in to comment.