Skip to content

Commit

Permalink
Merge pull request #67 from ExWeiv/dev
Browse files Browse the repository at this point in the history
v4 is Ready
  • Loading branch information
loeiks authored May 21, 2024
2 parents 020ec3a + c135fb4 commit 9b85bc1
Show file tree
Hide file tree
Showing 180 changed files with 2,170 additions and 1,372 deletions.
23 changes: 20 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# CHANGELOG of weiv-data
# CHANGELOG of WeivData

In this file you can find what's changed in each version. (Versions with -dev, -test tags can contain BUG/s and errors don't use them in production)

---

### 4.1.0

- Documentation is updated with new informations and better TS support.
- TS internal system is changed and now auto-complete works better.
- Validation system added for almost all functions, with some security checks.
- NPM packages versions updated.
- Error messages changed a bit.
- Aggregation system is replaced with new one, now order of methods matters and there are more updates about aggregate function.
- Some internal updates to WeivDataFilter.
- New function added to get the current version of the package.
- Query system is replaced with new one, now it's better and a bit faster.
- All ids are returned as string from now. Any objectid is converted to string just before return.
- onlyOwner flag added for some functions to enable permission of `Member Author` just like in WixData.
- mongodb version updated to 6.6.2
- BUG fixes and general performance improvements.
- v4 is first skeleton version of WeivData.

### 3.0.4

- BUG Fixes
Expand Down Expand Up @@ -180,7 +197,7 @@ In this file you can find what's changed in each version. (Versions with -dev, -
### 1.5.3

- TS Updates
- We have started working for a Discord Server and documentation page built with weiv-data.
- We have started working for a Discord Server and documentation page built with WeivData.

### 1.5.2

Expand All @@ -194,7 +211,7 @@ In this file you can find what's changed in each version. (Versions with -dev, -

### 1.5.0

- All data hooks are available to use and now data hooks are implemented into weiv-data.
- All data hooks are available to use and now data hooks are implemented into WeivData.
- onFailure hook is still not available.
- We have started to test data hooks if they are working as expected.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# weiv-data Node.js APIs
# WeivData Node.js APIs

<br>

Expand Down
352 changes: 138 additions & 214 deletions app/DOCS.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# weiv-data Node.js APIs
# WeivData Node.js APIs

[![Build for Velo by Wix](https://img.shields.io/badge/Built%20for-Velo%20by%20Wix-3638f4)](https://wix.com/velo)
[![🚀 GitHub](https://github.com/ExWeiv/weiv-data/actions/workflows/publish.yml/badge.svg?branch=main)](https://github.com/ExWeiv/weiv-data/actions/workflows/publish.yml)

The official [weiv-data APIs](https://www.npmjs.com/package/@exweiv/weiv-data) for Node.js to build web applications using MongoDB inside Wix with Velo. Only limited by MongoDB and your pocket. Not limited by wix-data APIs since you won't use them.
The official [WeivData APIs](https://www.npmjs.com/package/@exweiv/weiv-data) for Node.js to build web applications using MongoDB inside Wix with Velo. Only limited by MongoDB and your pocket. Not limited by wix-data APIs since you won't use them.

### When and Why to Use?

Expand Down
9 changes: 8 additions & 1 deletion app/lib/Aggregate/aggregate_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.AggregateResult = void 0;
const lodash_1 = require("lodash");
const validator_1 = require("../Helpers/validator");
const mongodb_1 = require("mongodb");
const connection_helpers_1 = require("../Helpers/connection_helpers");
const item_helpers_1 = require("../Helpers/item_helpers");
class Aggregate {
constructor(collectionId) {
this._pipeline = new Array();
Expand Down Expand Up @@ -209,7 +211,12 @@ class AggregateResult extends Aggregate {
}
};
return {
items,
items: items.map((item) => {
if (mongodb_1.ObjectId.isValid(item._id)) {
item._id = (0, item_helpers_1.convertObjectId)(item._id);
}
return item;
}),
length,
hasNext,
next,
Expand Down
5 changes: 2 additions & 3 deletions app/lib/Collections/createCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ exports.createCollection = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const name_helpers_1 = require("../Helpers/name_helpers");
const validator_1 = require("../Helpers/validator");
async function createCollection(collectionId, options, createOptions) {
async function createCollection(collectionId, suppressAuth, createOptions) {
try {
const { safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ collectionId, collectionOptions: createOptions, options }, ["collectionId"], "createCollection");
const { suppressAuth } = safeOptions || {};
const { safeCollectionOptions } = await (0, validator_1.validateParams)({ collectionId, collectionOptions: createOptions, suppressAuth }, ["collectionId"], "createCollection");
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
await database.createCollection(collectionName, safeCollectionOptions);
Expand Down
5 changes: 2 additions & 3 deletions app/lib/Collections/deleteCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ exports.deleteCollection = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const name_helpers_1 = require("../Helpers/name_helpers");
const validator_1 = require("../Helpers/validator");
async function deleteCollection(collectionId, options, deleteOptions) {
async function deleteCollection(collectionId, suppressAuth, deleteOptions) {
try {
const { safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ collectionId, collectionOptions: deleteOptions, options }, ["collectionId"], "deleteCollection");
const { suppressAuth } = safeOptions || {};
const { safeCollectionOptions } = await (0, validator_1.validateParams)({ collectionId, collectionOptions: deleteOptions, suppressAuth }, ["collectionId"], "deleteCollection");
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
return await database.dropCollection(collectionName, safeCollectionOptions);
Expand Down
5 changes: 2 additions & 3 deletions app/lib/Collections/listCollections.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.listCollections = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const validator_1 = require("../Helpers/validator");
async function listCollections(databaseName, options, filter, listOptions) {
async function listCollections(databaseName, suppressAuth, filter, listOptions) {
try {
const { safeCollectionFilter, safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ databaseName, options, collectionFilter: filter, collectionOptions: listOptions }, ["databaseName"], "listCollections");
const { suppressAuth } = safeOptions || {};
const { safeCollectionFilter, safeCollectionOptions } = await (0, validator_1.validateParams)({ databaseName, suppressAuth, collectionFilter: filter, collectionOptions: listOptions }, ["databaseName"], "listCollections");
const { database } = await (0, connection_helpers_1.connectionHandler)(`${databaseName}/`, suppressAuth);
return await database.listCollections(safeCollectionFilter, safeCollectionOptions).toArray();
}
Expand Down
5 changes: 2 additions & 3 deletions app/lib/Collections/renameCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ exports.renameCollection = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const name_helpers_1 = require("../Helpers/name_helpers");
const validator_1 = require("../Helpers/validator");
async function renameCollection(collectionId, newCollectionName, options, renameOptions) {
async function renameCollection(collectionId, newCollectionName, suppressAuth, renameOptions) {
try {
const { safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ collectionId, newCollectionName, options, collectionOptions: renameOptions }, ["collectionId", "newCollectionName"], "renameCollection");
const { suppressAuth } = safeOptions || {};
const { safeCollectionOptions } = await (0, validator_1.validateParams)({ collectionId, newCollectionName, suppressAuth, collectionOptions: renameOptions }, ["collectionId", "newCollectionName"], "renameCollection");
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
await database.renameCollection(collectionName, newCollectionName, safeCollectionOptions);
Expand Down
90 changes: 77 additions & 13 deletions app/lib/Filter/data_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.WeivDataFilter = void 0;
const lodash_1 = require("lodash");
const item_helpers_1 = require("../Helpers/item_helpers");
const validator_1 = require("../Helpers/validator");
class WeivDataFilter {
constructor() {
this.filters = {};
}
and(query) {
if (!query) {
throw new Error(`WeivData - query parameter must be valid to work with and method!`);
}
if (!this.filters["$and"]) {
this.filters["$and"] = [];
}
this.filters["$and"].push(query.filters);
this.filters["$and"].push((0, validator_1.copyOwnPropsOnly)(query.filters));
return this;
}
between(propertyName, rangeStart, rangeEnd) {
if (!propertyName || typeof propertyName !== "string" || !rangeStart || !rangeEnd) {
throw new Error(`WeivData - propertyName, rangeStart and rangeEnd must have valid values to work with between method!`);
}
if (!this.memoizedBetween) {
this.memoizedBetween = (0, lodash_1.memoize)((propertyName, rangeStart, rangeEnd) => {
return this.addFilter(propertyName, {
Expand All @@ -27,6 +34,9 @@ class WeivDataFilter {
return this;
}
contains(propertyName, string) {
if (!propertyName || !string || typeof (propertyName || string) !== "string") {
throw new Error(`WeivData - propertyName and string parameter must be valid to work with contains method!`);
}
if (!this.memoizedContains) {
this.memoizedContains = (0, lodash_1.memoize)((propertyName, string) => {
return this.addFilter(propertyName, {
Expand All @@ -39,6 +49,9 @@ class WeivDataFilter {
return this;
}
endsWith(propertyName, string) {
if (!propertyName || !string || typeof (propertyName || string) !== "string") {
throw new Error(`WeivData - propertyName and string parameter must be valid to work with endsWith method!`);
}
if (!this.memoizedEndsWith) {
this.memoizedEndsWith = (0, lodash_1.memoize)((propertyName, string) => {
return this.addFilter(propertyName, {
Expand All @@ -51,6 +64,9 @@ class WeivDataFilter {
return this;
}
eq(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with eq method!`);
}
if (!this.memoizedEq) {
this.memoizedEq = (0, lodash_1.memoize)((propertyName, value) => {
if (propertyName === "_id") {
Expand All @@ -67,107 +83,143 @@ class WeivDataFilter {
return this;
}
ge(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with ge method!`);
}
if (!this.memoizedGe) {
this.memoizedGe = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $gte: value, });
return this.addFilter(propertyName, { $gte: value });
});
}
this.memoizedGe(propertyName, value);
return this;
}
gt(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with gt method!`);
}
if (!this.memoizedGt) {
this.memoizedGt = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $gt: value, });
return this.addFilter(propertyName, { $gt: value });
});
}
this.memoizedGt(propertyName, value);
return this;
}
hasAll(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with hasAll method!`);
}
if (!Array.isArray(value)) {
value = [value];
}
if (!this.memoizedHasAll) {
this.memoizedHasAll = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $all: value, });
return this.addFilter(propertyName, { $all: value });
});
}
this.memoizedHasAll(propertyName, value);
return this;
}
hasSome(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with hasSome method!`);
}
if (!Array.isArray(value)) {
value = [value];
}
if (!this.memoizedHasSome) {
this.memoizedHasSome = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $in: value, });
return this.addFilter(propertyName, { $in: value });
});
}
this.memoizedHasSome(propertyName, value);
return this;
}
isEmpty(propertyName) {
if (!propertyName || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName parameter must be valid to work with isEmpty method!`);
}
if (!this.memoizedIsEmpty) {
this.memoizedIsEmpty = (0, lodash_1.memoize)((propertyName) => {
return this.addFilter(propertyName, { $exists: false, });
return this.addFilter(propertyName, { $exists: false });
});
}
this.memoizedIsEmpty(propertyName);
return this;
}
isNotEmpty(propertyName) {
if (!propertyName || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName parameter must be valid to work with isNotEmpty method!`);
}
if (!this.memoizedIsNotEmpty) {
this.memoizedIsNotEmpty = (0, lodash_1.memoize)((propertyName) => {
return this.addFilter(propertyName, { $exists: true, });
return this.addFilter(propertyName, { $exists: true });
});
}
this.memoizedIsNotEmpty(propertyName);
return this;
}
le(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with le method!`);
}
if (!this.memoizedLe) {
this.memoizedLe = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $lte: value, });
return this.addFilter(propertyName, { $lte: value });
});
}
this.memoizedLe(propertyName, value);
return this;
}
lt(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with lt method!`);
}
if (!this.memoizedLt) {
this.memoizedLt = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $lt: value, });
return this.addFilter(propertyName, { $lt: value });
});
}
this.memoizedLt(propertyName, value);
return this;
}
ne(propertyName, value) {
if (!propertyName || !value || typeof propertyName !== "string") {
throw new Error(`WeivData - propertyName and value parameter must be valid to work with ne method!`);
}
if (!this.memoizedNe) {
this.memoizedNe = (0, lodash_1.memoize)((propertyName, value) => {
return this.addFilter(propertyName, { $ne: value, });
return this.addFilter(propertyName, { $ne: value });
});
}
this.memoizedNe(propertyName, value);
return this;
}
not(query) {
if (!query) {
throw new Error(`WeivData - query parameter must be valid to work with not method!`);
}
if (!this.filters["$nor"]) {
this.filters["$nor"] = [];
}
this.filters["$nor"].push(query.filters);
this.filters["$nor"].push((0, validator_1.copyOwnPropsOnly)(query.filters));
return this;
}
or(query) {
if (!query) {
throw new Error(`WeivData - query parameter must be valid to work with or method!`);
}
if (!this.filters["$or"]) {
this.filters["$or"] = [];
}
this.filters["$or"].push(query.filters);
this.filters["$or"].push((0, validator_1.copyOwnPropsOnly)(query.filters));
return this;
}
startsWith(propertyName, string) {
if (!propertyName || !string || typeof (propertyName || string) !== "string") {
throw new Error(`WeivData - propertyName and string parameter must be valid to work with startsWith method!`);
}
if (!this.memoizedStartsWith) {
this.memoizedStartsWith = (0, lodash_1.memoize)((propertyName, string) => {
return this.addFilter(propertyName, {
Expand All @@ -180,16 +232,28 @@ class WeivDataFilter {
return this;
}
addFilter(propertyName, newFilter) {
this.sanitizeFilters(newFilter);
this.filters[propertyName] = {
...this.filters[propertyName],
...newFilter
};
return this.filters;
}
sanitizeFilters(filters) {
for (const key of Object.getOwnPropertyNames(filters)) {
if (key === "__proto__" || key === "constructor" || key === "prototype") {
throw new Error(`Invalid filter key: ${key}`);
}
if (typeof filters[key] === 'object' && filters[key] !== null) {
this.sanitizeFilters(filters[key]);
}
}
}
get _filters() {
const copyFilters = (0, validator_1.copyOwnPropsOnly)(this.filters);
return {
$match: {
...this.filters
...copyFilters
}
};
}
Expand Down
Loading

0 comments on commit 9b85bc1

Please sign in to comment.