Skip to content

Commit

Permalink
Merge pull request #97 from ExWeiv/dev
Browse files Browse the repository at this point in the history
Important BUG Fix and Default DB Name Option for `config.js`
  • Loading branch information
loeiks authored Jul 30, 2024
2 parents 1f451d2 + 4de49f8 commit 48fdd40
Show file tree
Hide file tree
Showing 129 changed files with 428 additions and 397 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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

- New config option `defaultIdType` is now available for setting the default returned id type globally. This option can be overwritten by the options of each function.
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
18 changes: 11 additions & 7 deletions app/lib/Config/weiv_data_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ const weivDataConfigs = __importStar(require("../../../../../../../../../user-co
const error_manager_1 = require("../Errors/error_manager");
const lodash_1 = require("lodash");
var __weivDatasavedConfigs__ = {
defaultIdType: "String"
defaultIdType: "String",
defaultDatabaseName: "ExWeiv"
};
const memoizedConfig = (0, lodash_1.memoize)(() => {
const configs = weivDataConfigs["config"];
if (configs && Object.keys(__weivDatasavedConfigs__).length === 0) {
const userConfig = configs();
__weivDatasavedConfigs__ = { ...__weivDatasavedConfigs__, ...userConfig };
}
return __weivDatasavedConfigs__;
});
function getWeivDataConfigs() {
try {
const configs = weivDataConfigs["config"];
if (configs && Object.keys(__weivDatasavedConfigs__).length === 0) {
const userConfig = configs();
__weivDatasavedConfigs__ = { ...__weivDatasavedConfigs__, ...userConfig };
}
return __weivDatasavedConfigs__;
return memoizedConfig();
}
catch (err) {
(0, error_manager_1.kaptanLogar)("00021", `while getting configs of WeivData library, ${err}`);
Expand Down
11 changes: 5 additions & 6 deletions app/lib/Filter/data_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const lodash_1 = require("lodash");
const validator_1 = require("../Helpers/validator");
const error_manager_1 = require("../Errors/error_manager");
const id_converters_1 = require("../Functions/id_converters");
const weiv_data_config_1 = require("../Config/weiv_data_config");
class WeivDataFilter {
constructor() {
this.filters = {};
Expand All @@ -20,7 +19,7 @@ class WeivDataFilter {
this.filters["$and"].push((0, validator_1.copyOwnPropsOnly)(query.filters));
return this;
}
between(propertyName, rangeStart, rangeEnd, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
between(propertyName, rangeStart, rangeEnd, convertIds) {
if (!propertyName || typeof propertyName !== "string" || !rangeStart || !rangeEnd) {
(0, error_manager_1.kaptanLogar)("00020", `propertyName, rangeStart and rangeEnd must have valid values to work with between method!`);
}
Expand Down Expand Up @@ -71,7 +70,7 @@ class WeivDataFilter {
this.memoizedEndsWith(propertyName, string);
return this;
}
eq(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
eq(propertyName, value, convertIds) {
if (!propertyName || value === undefined || typeof propertyName !== "string") {
(0, error_manager_1.kaptanLogar)("00020", `propertyName and value parameter must be valid to work with eq method!`);
}
Expand Down Expand Up @@ -114,7 +113,7 @@ class WeivDataFilter {
this.memoizedGt(propertyName, value);
return this;
}
hasAll(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
hasAll(propertyName, value, convertIds) {
if (!propertyName || !value || typeof propertyName !== "string") {
(0, error_manager_1.kaptanLogar)("00020", `propertyName and value parameter must be valid to work with hasAll method!`);
}
Expand Down Expand Up @@ -142,7 +141,7 @@ class WeivDataFilter {
this.memoizedHasAll(propertyName, value);
return this;
}
hasSome(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
hasSome(propertyName, value, convertIds) {
if (!propertyName || !value || typeof propertyName !== "string") {
(0, error_manager_1.kaptanLogar)("00020", `propertyName and value parameter must be valid to work with hasSome method!`);
}
Expand Down Expand Up @@ -218,7 +217,7 @@ class WeivDataFilter {
this.memoizedLt(propertyName, value);
return this;
}
ne(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
ne(propertyName, value, convertIds) {
if (!propertyName || value === undefined || typeof propertyName !== "string") {
(0, error_manager_1.kaptanLogar)("00020", `propertyName and value parameter must be valid to work with ne method!`);
}
Expand Down
4 changes: 3 additions & 1 deletion app/lib/Helpers/connection_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const customConnectionOptions = __importStar(require("../../../../../../../../..
const automatic_connection_provider_1 = require("../Connection/automatic_connection_provider");
const name_helpers_1 = require("./name_helpers");
const error_manager_1 = require("../Errors/error_manager");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function connectionHandler(collectionId, suppressAuth = false) {
try {
if (!collectionId || typeof collectionId !== "string") {
Expand All @@ -42,7 +43,8 @@ async function connectionHandler(collectionId, suppressAuth = false) {
db = pool.db(dbName);
}
else {
db = pool.db("ExWeiv");
const { defaultDatabaseName } = (0, weiv_data_config_1.getWeivDataConfigs)();
db = pool.db(defaultDatabaseName || "ExWeiv");
}
return { memberId, database: db, collection: db.collection(collectionName) };
}
Expand Down
4 changes: 3 additions & 1 deletion app/lib/Helpers/name_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.splitCollectionId = void 0;
const lodash_1 = require("lodash");
const error_manager_1 = require("../Errors/error_manager");
const weiv_data_config_1 = require("../Config/weiv_data_config");
exports.splitCollectionId = (0, lodash_1.memoize)(splitCollectionIdMain);
function splitCollectionIdMain(collectionId) {
if (!collectionId || typeof collectionId !== "string") {
(0, error_manager_1.kaptanLogar)("00007");
}
const [dbName, collectionName] = collectionId.split('/');
const { defaultDatabaseName } = (0, weiv_data_config_1.getWeivDataConfigs)();
if (!dbName || !collectionName) {
return { dbName: "ExWeiv", collectionName: dbName };
return { dbName: defaultDatabaseName || "ExWeiv", collectionName: dbName };
}
return { dbName, collectionName };
}
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": "4.9.0",
"version": "4.9.1",
"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
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
11 changes: 5 additions & 6 deletions app/src/Filter/data_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { copyOwnPropsOnly } from '../Helpers/validator';
import { kaptanLogar } from '../Errors/error_manager';
import type { Document } from 'mongodb/mongodb';
import { convertIdToObjectId } from '../Functions/id_converters';
import { getConvertIdsValue } from '../Config/weiv_data_config';

export class WeivDataFilter {
private readonly filters: { [key: string]: any } = {};
Expand All @@ -22,7 +21,7 @@ export class WeivDataFilter {
}

private memoizedBetween!: Function;
between(propertyName: string, rangeStart: any, rangeEnd: any, convertIds: boolean = getConvertIdsValue()): WeivDataFilter {
between(propertyName: string, rangeStart: any, rangeEnd: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || typeof propertyName !== "string" || !rangeStart || !rangeEnd) {
kaptanLogar("00020", `propertyName, rangeStart and rangeEnd must have valid values to work with between method!`);
}
Expand Down Expand Up @@ -86,7 +85,7 @@ export class WeivDataFilter {
}

private memoizedEq!: Function;
eq(propertyName: string, value: any, convertIds: boolean = getConvertIdsValue()): WeivDataFilter {
eq(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || value === undefined || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with eq method!`);
}
Expand Down Expand Up @@ -142,7 +141,7 @@ export class WeivDataFilter {
}

private memoizedHasAll!: Function;
hasAll(propertyName: string, value: any, convertIds: boolean = getConvertIdsValue()): WeivDataFilter {
hasAll(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || !value || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with hasAll method!`);
}
Expand Down Expand Up @@ -173,7 +172,7 @@ export class WeivDataFilter {
}

private memoizedHasSome!: Function;
hasSome(propertyName: string, value: any, convertIds: boolean = getConvertIdsValue()): WeivDataFilter {
hasSome(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || !value || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with hasSome method!`);
}
Expand Down Expand Up @@ -268,7 +267,7 @@ export class WeivDataFilter {
}

private memoizedNe!: Function;
ne(propertyName: string, value: any, convertIds: boolean = getConvertIdsValue()): WeivDataFilter {
ne(propertyName: string, value: any, convertIds?: boolean): WeivDataFilter {
if (!propertyName || value === undefined || typeof propertyName !== "string") {
kaptanLogar("00020", `propertyName and value parameter must be valid to work with ne method!`);
}
Expand Down
5 changes: 4 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 @@ -23,10 +24,12 @@ export async function connectionHandler(collectionId: CollectionID, suppressAuth
const { dbName, collectionName } = splitCollectionId(collectionId);
const { pool, memberId } = await useClient(suppressAuth);

// If no dbname provided, default db will be used which is ExWeiv
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
Loading

0 comments on commit 48fdd40

Please sign in to comment.