Skip to content

Commit

Permalink
Merge pull request #96 from ExWeiv/dev
Browse files Browse the repository at this point in the history
Globally Defined ID Types (new config options)
  • Loading branch information
loeiks authored Jul 30, 2024
2 parents ca8bdac + 2a9f3a9 commit 1f451d2
Show file tree
Hide file tree
Showing 169 changed files with 554 additions and 450 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ In this file you can find what's changed in each version. (Versions with -dev, -

---

### 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.
- Small security updates.
- Documentation updates.

### 4.8.0

- Better type support added for many functions with optional type parameters. Now optionally you can use TS generics to generate dynamically generated types for many functions but not for all.
Expand Down
14 changes: 14 additions & 0 deletions app/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Contents:
- Security in WeivData and MongoDB
- About Data Hooks in WeivData
- Cold Start of WeivData
- Setting Up Configuration in WeivData
- Help

---
Expand Down Expand Up @@ -176,6 +177,19 @@ backend/

9. You should be good to go!

## Setting Up Configuration in WeivData

In WeivData you can configure some settings via `config.js` file. All you need to do is export an object and name it as `config`, as an example:

```js
export const config = {
defaultIdType: "String",
syncDatabase: "MyDBName"
};
```

That config will be used by the library to customize or setup some setting/features in the library for you.

## Security in WeivData and MongoDB

There are not very much to do with security side when you use MongoDB Atlas since it's already managed by MongoDB they will handle the most of the security features. But you should be careful about these steps:
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Aggregate/aggregate_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const validator_1 = require("../Helpers/validator");
const connection_helpers_1 = require("../Helpers/connection_helpers");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const error_manager_1 = require("../Errors/error_manager");
const weiv_data_config_1 = require("../Config/weiv_data_config");
class Aggregate {
constructor(collectionId) {
this._pipeline = new Array();
Expand Down Expand Up @@ -194,7 +195,7 @@ class Aggregate {
class AggregateResult extends Aggregate {
async run(options) {
try {
const { readConcern, suppressAuth, convertIds } = options || {};
const { readConcern, suppressAuth, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...(0, validator_1.copyOwnPropsOnly)(options) };
await this._handleConnection_(suppressAuth);
const pipeline = [...this._pipeline];
this._pageSize = this._limitNumber || 50;
Expand Down
13 changes: 12 additions & 1 deletion app/lib/Config/weiv_data_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConvertIdsValue = void 0;
exports.getWeivDataConfigs = getWeivDataConfigs;
const weivDataConfigs = __importStar(require("../../../../../../../../../user-code/backend/WeivData/config"));
const error_manager_1 = require("../Errors/error_manager");
var __weivDatasavedConfigs__ = {};
const lodash_1 = require("lodash");
var __weivDatasavedConfigs__ = {
defaultIdType: "String"
};
function getWeivDataConfigs() {
try {
const configs = weivDataConfigs["config"];
Expand All @@ -40,3 +44,10 @@ function getWeivDataConfigs() {
(0, error_manager_1.kaptanLogar)("00021", `while getting configs of WeivData library, ${err}`);
}
}
const memoizedCheckIdType = (0, lodash_1.memoize)(() => {
return getWeivDataConfigs().defaultIdType === "String" ? true : false;
});
const getConvertIdsValue = () => {
return memoizedCheckIdType();
};
exports.getConvertIdsValue = getConvertIdsValue;
11 changes: 6 additions & 5 deletions app/lib/Filter/data_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 @@ -19,7 +20,7 @@ class WeivDataFilter {
this.filters["$and"].push((0, validator_1.copyOwnPropsOnly)(query.filters));
return this;
}
between(propertyName, rangeStart, rangeEnd, convertIds) {
between(propertyName, rangeStart, rangeEnd, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
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 @@ -70,7 +71,7 @@ class WeivDataFilter {
this.memoizedEndsWith(propertyName, string);
return this;
}
eq(propertyName, value, convertIds) {
eq(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
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 @@ -113,7 +114,7 @@ class WeivDataFilter {
this.memoizedGt(propertyName, value);
return this;
}
hasAll(propertyName, value, convertIds) {
hasAll(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
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 @@ -141,7 +142,7 @@ class WeivDataFilter {
this.memoizedHasAll(propertyName, value);
return this;
}
hasSome(propertyName, value, convertIds) {
hasSome(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
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 @@ -217,7 +218,7 @@ class WeivDataFilter {
this.memoizedLt(propertyName, value);
return this;
}
ne(propertyName, value, convertIds) {
ne(propertyName, value, convertIds = (0, weiv_data_config_1.getConvertIdsValue)()) {
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
3 changes: 2 additions & 1 deletion app/lib/Functions/Helpers/findOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const hook_manager_1 = require("../../Hooks/hook_manager");
const validator_1 = require("../../Helpers/validator");
const error_manager_1 = require("../../Errors/error_manager");
const internal_id_converter_1 = require("../../Helpers/internal_id_converter");
const weiv_data_config_1 = require("../../Config/weiv_data_config");
async function findOne(collectionId, propertyName, value, options) {
try {
const { safeValue, safeOptions } = await (0, validator_1.validateParams)({ collectionId, propertyName, value, options }, ["collectionId", "propertyName", "value"], "findOne");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let editedFilter = { propertyName, value: safeValue };
if (suppressHooks != true) {
const modifiedFilter = await (0, hook_manager_1.runDataHook)(collectionId, "beforeFindOne", [{ propertyName, value: safeValue }, context]).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/Helpers/getAndRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const member_id_helpers_1 = require("../../Helpers/member_id_helpers");
const error_manager_1 = require("../../Errors/error_manager");
const internal_id_converter_1 = require("../../Helpers/internal_id_converter");
const id_converters_1 = require("../id_converters");
const weiv_data_config_1 = require("../../Config/weiv_data_config");
async function getAndRemove(collectionId, itemId, options) {
try {
const { safeItemId, safeOptions } = await (0, validator_1.validateParams)({ collectionId, itemId, options }, ["collectionId", "itemId"], "getAndRemove");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let editedItemId = safeItemId;
if (suppressHooks != true) {
const modifiedItemId = await (0, hook_manager_1.runDataHook)(collectionId, "beforeGetAndRemove", [safeItemId, context]).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/Helpers/getAndReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const validator_1 = require("../../Helpers/validator");
const member_id_helpers_1 = require("../../Helpers/member_id_helpers");
const error_manager_1 = require("../../Errors/error_manager");
const internal_id_converter_1 = require("../../Helpers/internal_id_converter");
const weiv_data_config_1 = require("../../Config/weiv_data_config");
async function getAndReplace(collectionId, itemId, value, options) {
try {
const { safeItemId, safeValue, safeOptions } = await (0, validator_1.validateParams)({ collectionId, itemId, value, options }, ["collectionId", "itemId", "value"], "getAndReplace");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let editedItem = safeValue;
if (suppressHooks != true) {
const modifiedItem = await (0, hook_manager_1.runDataHook)(collectionId, "beforeGetAndReplace", [safeValue, context]).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/Helpers/getAndUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const validator_1 = require("../../Helpers/validator");
const member_id_helpers_1 = require("../../Helpers/member_id_helpers");
const error_manager_1 = require("../../Errors/error_manager");
const internal_id_converter_1 = require("../../Helpers/internal_id_converter");
const weiv_data_config_1 = require("../../Config/weiv_data_config");
async function getAndUpdate(collectionId, itemId, value, options) {
try {
const { safeItemId, safeValue, safeOptions } = await (0, validator_1.validateParams)({ collectionId, itemId, value, options }, ["collectionId", "itemId", "value"], "getAndUpdate");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let editedItem = safeValue;
if (suppressHooks != true) {
const modifiedItem = await (0, hook_manager_1.runDataHook)(collectionId, "beforeGetAndUpdate", [safeValue, context]).catch((err) => {
Expand Down
4 changes: 3 additions & 1 deletion app/lib/Functions/QueryReferenced/query_referenced_result.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const query_referenced_helpers_1 = require("../../Helpers/query_referenced_helpe
const connection_helpers_1 = require("../../Helpers/connection_helpers");
const error_manager_1 = require("../../Errors/error_manager");
const internal_id_converter_1 = require("../../Helpers/internal_id_converter");
const validator_1 = require("../../Helpers/validator");
const weiv_data_config_1 = require("../../Config/weiv_data_config");
class QueryReferencedResult {
constructor(collectionId, targetCollectionId, itemId, propertyName, queryOptions, options) {
this.currentPage = 0;
Expand All @@ -22,7 +24,7 @@ class QueryReferencedResult {
}
async getResult() {
try {
const { suppressAuth, readConcern, convertIds } = this.options;
const { suppressAuth, readConcern, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...(0, validator_1.copyOwnPropsOnly)(this.options) };
await this._handleConnection_(suppressAuth);
const pipelineOptions = this.__getPipelineOptions__();
const pipeline = (0, query_referenced_helpers_1.getPipeline)(this.itemId, this.targetCollectionId, this.propertyName, pipelineOptions);
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/bulkInsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const hook_helpers_1 = require("../Helpers/hook_helpers");
const validator_1 = require("../Helpers/validator");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function bulkInsert(collectionId, items, options) {
try {
const { safeItems, safeOptions } = await (0, validator_1.validateParams)({ collectionId, items, options }, ["collectionId", "items"], "bulkInsert");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, enableVisitorId, readConcern, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, enableVisitorId, readConcern, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let ownerId = await (0, member_id_helpers_1.getOwnerId)(enableVisitorId);
let editedItems = safeItems.map(async (item) => {
item._updatedDate = new Date();
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/bulkRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const member_id_helpers_1 = require("../Helpers/member_id_helpers");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const id_converters_1 = require("./id_converters");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function bulkRemove(collectionId, itemIds, options) {
try {
const { safeItemIds, safeOptions } = await (0, validator_1.validateParams)({ collectionId, itemIds, options }, ["collectionId", "itemIds"], "bulkRemove");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let currentMemberId;
if (onlyOwner) {
currentMemberId = await (0, member_id_helpers_1.getOwnerId)();
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/bulkSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const validator_1 = require("../Helpers/validator");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const id_converters_1 = require("./id_converters");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function bulkSave(collectionId, items, options) {
try {
const { safeItems, safeOptions } = await (0, validator_1.validateParams)({ collectionId, items, options }, ["collectionId", "items"], "bulkSave");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, enableVisitorId, readConcern, onlyOwner, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, enableVisitorId, readConcern, onlyOwner, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
const currentMemberId = await (0, member_id_helpers_1.getOwnerId)(enableVisitorId);
let ownerId = await (0, member_id_helpers_1.getOwnerId)(enableVisitorId);
let editedItems = safeItems.map(async (item) => {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/bulkUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const member_id_helpers_1 = require("../Helpers/member_id_helpers");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const id_converters_1 = require("./id_converters");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function bulkUpdate(collectionId, items, options) {
try {
const { safeItems, safeOptions } = await (0, validator_1.validateParams)({ collectionId, items, options }, ["collectionId", "items"], "bulkUpdate");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, onlyOwner, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
const currentMemberId = await (0, member_id_helpers_1.getOwnerId)();
let editedItems = safeItems.map(async (item) => {
if (suppressHooks != true) {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const validator_1 = require("../Helpers/validator");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const id_converters_1 = require("./id_converters");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function get(collectionId, itemId, options) {
try {
const { safeOptions, safeItemId } = await (0, validator_1.validateParams)({ collectionId, itemId, options }, ["collectionId", "itemId"], "get");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let editedItemId;
if (suppressHooks != true) {
editedItemId = await (0, hook_manager_1.runDataHook)(collectionId, "beforeGet", [safeItemId, context]).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/increment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const validator_1 = require("../Helpers/validator");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const id_converters_1 = require("./id_converters");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function increment(collectionId, itemId, propertyName, value, options) {
try {
const { safeOptions } = await (0, validator_1.validateParams)({ collectionId, itemId, propertyName, value, options }, ["collectionId", "itemId", "propertyName", "value"], "increment");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, readConcern, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let editedModify = { propertyName, value };
if (suppressHooks != true) {
const modifiedParams = await (0, hook_manager_1.runDataHook)(collectionId, "beforeIncrement", [{ propertyName, value }, context]).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/Functions/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const hook_helpers_1 = require("../Helpers/hook_helpers");
const validator_1 = require("../Helpers/validator");
const error_manager_1 = require("../Errors/error_manager");
const internal_id_converter_1 = require("../Helpers/internal_id_converter");
const weiv_data_config_1 = require("../Config/weiv_data_config");
async function insert(collectionId, item, options) {
try {
const { safeItem, safeOptions } = await (0, validator_1.validateParams)({ collectionId, item, options }, ["collectionId", "item"], "insert");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, enableVisitorId, readConcern, convertIds } = safeOptions || {};
const { suppressAuth, suppressHooks, enableVisitorId, readConcern, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
const defaultValues = {
_updatedDate: new Date(),
_createdDate: new Date(),
Expand Down
Loading

0 comments on commit 1f451d2

Please sign in to comment.