Socket
Socket
Sign inDemoInstall

xpress-mongo

Package Overview
Dependencies
230
Maintainers
1
Versions
148
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.13.1 to 3.0.0

4

fn/helpers.d.ts

@@ -10,1 +10,5 @@ import escapeRegexp from "escape-string-regexp-node";

export declare function DefaultPaginationData<T>(merge?: Partial<Paginated>): Paginated<T>;
/**
* Do Nothing
*/
export declare function DoNothing(): void;

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultPaginationData = exports.escapeRegexp = void 0;
exports.DoNothing = exports.DefaultPaginationData = exports.escapeRegexp = void 0;
const escape_string_regexp_node_1 = __importDefault(require("escape-string-regexp-node"));

@@ -26,1 +26,8 @@ exports.escapeRegexp = escape_string_regexp_node_1.default;

exports.DefaultPaginationData = DefaultPaginationData;
/**
* Do Nothing
*/
function DoNothing() {
// literally do nothing
}
exports.DoNothing = DoNothing;

7

fn/inbuilt.js

@@ -10,2 +10,3 @@ "use strict";

const index_1 = require("../index");
const helpers_1 = require("./helpers");
/**

@@ -110,4 +111,3 @@ * Get Default value.

if (["watch", "created", "deleted"].includes(event)) {
// noinspection ES6MissingAwait
RunInBackground(() => thisEvent(modelInstance));
RunInBackground(() => thisEvent(modelInstance)).finally(helpers_1.DoNothing);
}

@@ -123,4 +123,3 @@ else {

if (lodash_1.default.has(changes, field)) {
// noinspection ES6MissingAwait
RunInBackground(() => thisEvent[field](modelInstance));
RunInBackground(() => thisEvent[field](modelInstance)).finally(helpers_1.DoNothing);
}

@@ -127,0 +126,0 @@ }

@@ -0,1 +1,6 @@

/**
* Convert array of keys to object of keys and value.
* @param keys
* @param value
*/
export declare const keysToObject: <T extends string | string[] | readonly string[], V>(keys: T, value: V) => T extends readonly string[] ? Record<T[number], V> : Record<string, V>;

@@ -2,0 +7,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.omitIdAndPick = exports.omitIdAnd = exports.pickKeys = exports.omitKeys = exports.keysToObject = void 0;
/**
* Convert array of keys to object of keys and value.
* @param keys
* @param value
*/
const keysToObject = (keys, value) => {

@@ -5,0 +10,0 @@ if (typeof keys === "string")

{
"name": "xpress-mongo",
"version": "2.13.1",
"version": "3.0.0",
"description": "Light Weight ODM for mongoDb NodeJs",

@@ -33,6 +33,6 @@ "main": "index.js",

"dependencies": {
"deep-object-diff": "^1.1.7",
"deep-object-diff": "^1.1.9",
"escape-string-regexp-node": "^1.0.2",
"joi": "^17.6.1",
"mongodb": "4.10.0",
"joi": "^17.8.3",
"mongodb": "5.1.0",
"object-collection": "^3.0.1",

@@ -43,10 +43,11 @@ "uuid": "^9.0.0"

"@types/chance": "^1.1.3",
"@types/node": "^18.7.21",
"@types/uuid": "^8.3.4",
"chance": "^1.1.8",
"@types/node": "^18.15.0",
"@types/uuid": "^9.0.1",
"chance": "^1.1.11",
"japa": "^4.0.0",
"nodemon": "^2.0.20",
"nodemon": "^2.0.21",
"prettier": "^2.8.4",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.3"
"typescript": "^4.9.5"
},

@@ -53,0 +54,0 @@ "engines": {

import { XMongoSchemaBuilder } from "./types/index";
declare const is: XMongoSchemaBuilder;
export = is;
export default is;

@@ -28,2 +28,3 @@ "use strict";

};
Object.defineProperty(exports, "__esModule", { value: true });
const XMongoDataType_1 = __importDefault(require("./XMongoDataType"));

@@ -246,2 +247,2 @@ const mongodb_1 = require("mongodb");

};
module.exports = is;
exports.default = is;
import XMongoDataType from "../XMongoDataType";
import XMongoModel from "../XMongoModel";
import Joi from "joi";
export declare type FunctionReturnsBoolean = (...args: any[]) => boolean;
export declare type FunctionReturnsVoidOrBoolean = (...args: any[]) => boolean | void;
export declare type CastFunctionType = (value: any, key?: string) => any;
export declare type RequiredIf = <T extends XMongoModel>(modelInstance: T) => boolean;
export declare type ValidatorType = FunctionReturnsVoidOrBoolean | {
export type FunctionReturnsBoolean = (...args: any[]) => boolean;
export type FunctionReturnsVoidOrBoolean = (...args: any[]) => boolean | void;
export type CastFunctionType = (value: any, key?: string) => any;
export type RequiredIf = <T extends XMongoModel>(modelInstance: T) => boolean;
export type ValidatorType = FunctionReturnsVoidOrBoolean | {
or?: Array<FunctionReturnsBoolean>;
and?: Array<FunctionReturnsBoolean>;
};
export declare type UseJoi = (joi: Joi.Root) => Joi.Schema;
export declare type SchemaPropertiesType = {
export type UseJoi = (joi: Joi.Root) => Joi.Schema;
export type SchemaPropertiesType = {
name: string;

@@ -26,9 +26,9 @@ default?: any;

};
export declare type StringToAnyObject = Record<string, any>;
declare type InputBuffer = ArrayLike<number>;
export declare type UuidOptions = {
export type StringToAnyObject = Record<string, any>;
type InputBuffer = ArrayLike<number>;
export type UuidOptions = {
name: string | InputBuffer;
namespace: string | InputBuffer;
};
export declare type XMongoSchemaBuilder = {
export type XMongoSchemaBuilder = {
Any(def?: any | (() => any)): XMongoDataType;

@@ -49,8 +49,8 @@ Array(def?: () => Array<any>): XMongoDataType;

};
export declare type XMongoSchema<DataType = any> = Record<keyof Omit<DataType, "_id">, XMongoDataType | Joi.Schema>;
export declare type XMongoSchemaFn = (is: XMongoSchemaBuilder, Joi: Joi.Root) => XMongoSchema;
export declare type XMongoStrictConfig = undefined | boolean | {
export type XMongoSchema<DataType = any> = Record<keyof Omit<DataType, "_id">, XMongoDataType | Joi.Schema>;
export type XMongoSchemaFn = (is: XMongoSchemaBuilder, Joi: Joi.Root) => XMongoSchema;
export type XMongoStrictConfig = undefined | boolean | {
removeNonSchemaFields?: boolean;
};
export declare type FnWithArg<arg = any, result = any> = (c: arg) => result;
export type FnWithArg<arg = any, result = any> = (c: arg) => result;
export {};

@@ -64,2 +64,2 @@ import XMongoModel from "./XMongoModel";

}
export = XMongoClient;
export default XMongoClient;

@@ -5,2 +5,3 @@ "use strict";

};
Object.defineProperty(exports, "__esModule", { value: true });
const XMongoModel_1 = __importDefault(require("./XMongoModel"));

@@ -150,2 +151,2 @@ const XMongoTypedModel_1 = __importDefault(require("./XMongoTypedModel"));

}
module.exports = XMongoClient;
exports.default = XMongoClient;

@@ -83,2 +83,2 @@ import { CastFunctionType, FnWithArg, RequiredIf, SchemaPropertiesType, UseJoi, ValidatorType } from "./types/index";

}
export = XMongoDataType;
export default XMongoDataType;

@@ -5,2 +5,3 @@ "use strict";

};
Object.defineProperty(exports, "__esModule", { value: true });
const joi_1 = __importDefault(require("joi"));

@@ -144,2 +145,2 @@ class XMongoDataType {

}
module.exports = XMongoDataType;
exports.default = XMongoDataType;

@@ -5,7 +5,7 @@ import ObjectCollection from "object-collection";

import { Paginated } from "./types/pagination";
declare type FunctionWithRawArgument = (raw: Collection) => FindCursor | AggregationCursor;
declare type MakeMany<T extends typeof XMongoModel> = {
type FunctionWithRawArgument = (raw: Collection) => FindCursor | AggregationCursor;
type MakeMany<T extends typeof XMongoModel> = {
interceptor?: (d: InstanceType<T>) => InstanceType<T> | false;
};
declare type MakeManyData<T extends typeof XMongoModel> = MakeMany<T> & {
type MakeManyData<T extends typeof XMongoModel> = MakeMany<T> & {
validate?: boolean;

@@ -126,8 +126,13 @@ stopOnError?: boolean;

* Set data in model
* @param key
* @param value
* @param field - Field to set
* @param value - Value to set
* @return {this}
*/
set(key: string | Record<string, any>, value?: any): this;
set<Value = any>(field: string, value: Value): this;
/**
* Set many fields at once
* @param fields - Object of fields to set
*/
setMany<Data extends Record<any, any>>(fields: Data): this;
/**
* Check if field exists in data

@@ -432,2 +437,8 @@ * if value is defined, it checks if the value is the defined value.

/**
* A helper to fetch result as array.
* @param query - a function
* @returns {Promise<[]>}
*/
static toArray<Return = any>(query: FunctionWithRawArgument): Promise<Return[]>;
/**
* Turn query result array provided to model instances.

@@ -455,8 +466,2 @@

/**
* A helper to fetch result as array.
* @param query - a function
* @returns {Promise<[]>}
*/
static toArray(query: FunctionWithRawArgument): Promise<any[]>;
/**
* Register Events for create, update and delete

@@ -563,2 +568,2 @@ * @param event

}
export = XMongoModel;
export default XMongoModel;

@@ -5,2 +5,3 @@ "use strict";

};
Object.defineProperty(exports, "__esModule", { value: true });
const object_collection_1 = __importDefault(require("object-collection"));

@@ -14,2 +15,3 @@ const mongodb_1 = require("mongodb");

const projection_1 = require("../fn/projection");
const helpers_1 = require("../fn/helpers");
/**

@@ -143,15 +145,18 @@ * @class

* Set data in model
* @param key
* @param value
* @param field - Field to set
* @param value - Value to set
* @return {this}
*/
set(key, value) {
if (typeof key === "object" && value === undefined) {
for (const property in key) {
lodash_1.default.set(this.data, property, key[property]);
}
set(field, value) {
lodash_1.default.set(this.data, field, value);
return this;
}
/**
* Set many fields at once
* @param fields - Object of fields to set
*/
setMany(fields) {
for (const property in fields) {
lodash_1.default.set(this.data, property, fields[property]);
}
else if (typeof key === "string") {
lodash_1.default.set(this.data, key, value);
}
return this;

@@ -240,3 +245,3 @@ }

static make(data = {}) {
return new this().set(data);
return new this().setMany(data);
}

@@ -484,3 +489,3 @@ /**

this.$canTalkToDatabase("UPDATE_ERROR");
return this.set(set).save(options);
return this.setMany(set).save(options);
}

@@ -521,2 +526,4 @@ /**

return resolve(false);
// Check if unique schema is defined
// if yes, then check if unique schema is unique
if (this.meta.hasUniqueSchema) {

@@ -527,2 +534,10 @@ await this.$checkUniqueSchema($set);

lodash_1.default.merge(this.original, $set);
// Update data
const res = await this.$static()
.native()
.updateOne(findOneQuery, { $set }, options);
// Resolve
resolve(res);
// Run Watch Event
(0, inbuilt_1.RunOnEvent)("watch", this, changes).finally(helpers_1.DoNothing);
}

@@ -532,15 +547,2 @@ catch (e) {

}
this.$static()
.native()
.updateOne(findOneQuery, { $set }, options, (error, res) => {
if (error) {
return reject(error);
}
else {
// Resolve
resolve(res);
// Run Watch Event
(0, inbuilt_1.RunOnEvent)("watch", this, changes);
}
});
}

@@ -552,16 +554,9 @@ else {

this.$emptyData(this.validate(undefined));
// Check if unique schema is defined
// if yes, then check if unique schema is unique
if (this.meta.hasUniqueSchema) {
await this.$checkUniqueSchema();
}
}
catch (e) {
return reject(e);
}
return this.$static()
.native()
.insertOne(this.data, options, (error, res) => {
if (error)
return reject(error);
const { insertedId } = res;
this.set("_id", insertedId);
const res = await this.$static().native().insertOne(this.data, options);
this.set("_id", res.insertedId);
this.$setOriginal(this.data);

@@ -575,4 +570,7 @@ // set using custom id to false

// Run on created event
(0, inbuilt_1.RunOnEvent)("created", this);
});
(0, inbuilt_1.RunOnEvent)("created", this).finally(helpers_1.DoNothing);
}
catch (e) {
return reject(e);
}
}

@@ -594,3 +592,3 @@ });

*/
unset(keys, options = {}) {
async unset(keys, options = {}) {
this.$canTalkToDatabase("UNSET_ERROR");

@@ -613,15 +611,10 @@ // Throw Error if keys is undefined

// Run MongoDb query and return response in Promise
return new Promise((resolve, reject) => {
return this.$static()
.native()
.updateOne(this.$findOneQuery(), { $unset }, options, (error, res) => {
if (error)
return reject(error);
// Remove keys from current data
for (const key of keys) {
this.toCollection().unset(key);
}
return resolve(res);
});
});
const res = await this.$static()
.native()
.updateOne(this.$findOneQuery(), { $unset }, options);
// Remove keys from current data
for (const key of keys) {
this.toCollection().unset(key);
}
return res;
}

@@ -790,4 +783,3 @@ /**

const result = await this.$static().native().deleteOne(findOneQuery);
(0, inbuilt_1.RunOnEvent)("deleted", this).finally(() => {
});
(0, inbuilt_1.RunOnEvent)("deleted", this).finally(() => { });
return result;

@@ -911,3 +903,3 @@ }

else {
throw Error(`Relationship: (${relationship}) does not exists in model {${this.constructor.name}}`);
throw Error(`Relationship: (${relationship}) does not exist in model {${this.constructor.name}}`);
}

@@ -954,12 +946,5 @@ }

*/
static find(query = {}, options = {}) {
return new Promise((resolve, reject) => {
return this.native()
.find(query, options)
.toArray((error, data) => {
if (error)
return reject(error);
return resolve(data);
});
});
static async find(query = {}, options = {}) {
const data = await this.native().find(query, options).toArray();
return data;
}

@@ -979,3 +964,3 @@ static findRaw(query = {}, options = {}) {

*/
static findOne(query = {}, options = {}, raw = false) {
static async findOne(query = {}, options = {}, raw = false) {
if (typeof options === "boolean") {

@@ -985,18 +970,9 @@ raw = options;

}
return new Promise((resolve, reject) => {
/**
* options as any is used here because mongodb did not make its new
* WithoutProjection type exportable, so we can't make reference to it.
*/
return this.native().findOne(query, options, (error, data) => {
if (error)
return reject(error);
// Return new instance of Model
if (!data)
return resolve(null);
if (raw)
return resolve(data);
return resolve(this.use(data).$updateFindOneQuery(query));
});
});
const data = await this.native().findOne(query, options);
if (!data)
return null;
// if raw is true return the raw data.
if (raw)
return data;
return this.use(data).$updateFindOneQuery(query);
}

@@ -1206,2 +1182,12 @@ /**

/**
* A helper to fetch result as array.
* @param query - a function
* @returns {Promise<[]>}
*/
static toArray(query) {
if (typeof query !== "function")
throw Error(".toArray expects a function as argument");
return query(this.native()).toArray();
}
/**
* Turn query result array provided to model instances.

@@ -1225,36 +1211,14 @@

*/
static fromQuery(query, interceptor = false) {
return new Promise((resolve, reject) => {
return query(this.native()).toArray((error, lists) => {
if (error)
return reject(error);
/**
* Check if interceptor is a function
* if it is we pass list to the function
*
* else we pass it to self (fromArray)
*/
return resolve(typeof interceptor === "function"
? interceptor(lists)
: this.fromArray(lists));
});
});
static async fromQuery(query, interceptor = false) {
const lists = await this.toArray(query);
/**
* Check if interceptor is a function
* if it is we pass list to the function
* else we pass it to self (fromArray)
*/
return typeof interceptor === "function"
? interceptor(lists)
: this.fromArray(lists);
}
/**
* A helper to fetch result as array.
* @param query - a function
* @returns {Promise<[]>}
*/
static toArray(query) {
return new Promise((resolve, reject) => {
if (typeof query !== "function")
return reject(Error(".toArray expects a function as argument"));
query(this.native()).toArray((error, data) => {
if (error)
return reject(error);
return resolve(data);
});
});
}
/**
* Register Events for create, update and delete

@@ -1323,3 +1287,3 @@ * @param event

// Set Normal Data
this.set(data);
this.setMany(data);
return this.$appendData(append);

@@ -1415,2 +1379,3 @@ }

$checkUniqueSchema(data) {
// return a promise that resolves to true if no unique fields are defined
return new Promise(async (resolve, reject) => {

@@ -1421,5 +1386,9 @@ if (!this.meta.hasUniqueSchema)

data = this.data;
// loop through unique fields
for (const field of this.meta.hasUniqueSchema) {
// if the field is not defined in the data, skip it
if (!data[field])
continue;
// get field schema
// if no schema is defined, skip it
const schema = this.schema[field];

@@ -1429,2 +1398,3 @@ if (!schema)

try {
// get the unique query
const uniqueQuery = schema.uniqueQuery || {};

@@ -1437,5 +1407,3 @@ let findQuery = uniqueQuery.query;

}
const findField = await this.$static()
.native()
.findOne(findQuery);
const findField = await this.$static().exists(findQuery);
if (findField)

@@ -1598,2 +1566,2 @@ return reject(Error(`Field: (${field}) expects a unique value!`));

}
module.exports = XMongoModel;
exports.default = XMongoModel;

@@ -6,3 +6,4 @@ import XMongoModel from "./XMongoModel";

data: DT;
set: (key: string | keyof DT | StringToAnyObject | Record<keyof DT | string, any>, value?: any) => this;
set: (key: string | keyof DT, value: any) => this;
setMany: (fields: Record<keyof DT | string, any>) => this;
get: <T = unknown>(key: keyof DT | string, $default?: any) => T;

@@ -20,10 +21,15 @@ has: (key: keyof DT | string, value: any) => boolean;

* SetTyped is same as `set` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param key
* @param value
*/
setTyped(key: keyof DT | Record<keyof DT, any>, value?: any): this;
setTyped<Value = any>(key: keyof DT, value: Value): this;
/**
* setManyTyped is same as `setMany` but will throw error at compile time if key used does not exist in schema.
* @param fields
*/
setManyTyped(fields: Record<keyof DT, any>): this;
/**
* hasTyped is same as `has` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param key

@@ -35,3 +41,3 @@ * @param value

* getTyped is same as `get` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param key

@@ -43,3 +49,3 @@ * @param $default

* updateTyped is same as `update` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param set

@@ -50,2 +56,2 @@ * @param options

}
export = XMongoTypedModel;
export default XMongoTypedModel;

@@ -5,2 +5,3 @@ "use strict";

};
Object.defineProperty(exports, "__esModule", { value: true });
const XMongoModel_1 = __importDefault(require("./XMongoModel"));

@@ -17,3 +18,3 @@ class XMongoTypedModel extends XMongoModel_1.default {

* SetTyped is same as `set` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param key

@@ -26,4 +27,11 @@ * @param value

/**
* setManyTyped is same as `setMany` but will throw error at compile time if key used does not exist in schema.
* @param fields
*/
setManyTyped(fields) {
return this.setMany(fields);
}
/**
* hasTyped is same as `has` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param key

@@ -37,3 +45,3 @@ * @param value

* getTyped is same as `get` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param key

@@ -47,3 +55,3 @@ * @param $default

* updateTyped is same as `update` but will throw error at compile time
* if key used does not exists in schema.
* if key used does not exist in schema.
* @param set

@@ -56,2 +64,2 @@ * @param options

}
module.exports = XMongoTypedModel;
exports.default = XMongoTypedModel;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc