
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
knect-mongo
Advanced tools
Mongodb connection and model helper.
Below example utilizes async/await you can also use promises or callbacks if you wish, all supported.
$ npm install knect-mongo s-
OR
$ yarn add knect-mongo
After install import or require knect-mongo.
import { KnectMongo } from 'knect-mongo';
/**
* Create singleton instance.
*/
const knect = new KnectMongo();
(async function init() {
const dbOptions = {
// your options here.
};
await knect.connect('mongodb://localhost:27017/mydb', dbOptions);
export interface IBase {
_id?: LikeObjectId;
created?: number;
modified?: number;
}
export interface IPost extends IBase {
title: string;
body: string;
user: LikeObjectId;
}
export interface IUser extends IBase {
firstName: string;
lastName: string;
posts: (string | number | IPost | Model<any>)[];
}
export const UserSchema: ISchema<IUser> = {
joins: {
posts: { collection: 'post' }
}
};
export const PostSchema: ISchema<IPost> = {
joins: {
user: { collection: 'user' }
}
};
const User = knect.model('user', UserSchema);
const Post = knect.model('post', PostSchema);
try {
// Create a new user from model.
const user = new User({ firstName: 'Milton', lastName: 'Waddams' });
// Save our new model.
await user.save();
}
catch(err) {
if (err) throw err;
}
})();
You can find all static methods for a model in the source document.ts file.
Additional boilerplate from above left out for clarity.
async function getUser(_id) {
// Create the Model
const User = knect.model('user', { your_schema_here });
// Using await get the user data.
const userData = await User.findOne({ _id });
}
In the above example we create the model but what if a model already exists? It's important to note if using Typescript we need to pass back in the schema for the model. This is because we cannot store the design time type in our map. Simply pass in our defined schema as show above then import and reuse it here.
// file containing all our defined schemas NOT SHOWN ABOVE.
// this is NOT required if NOT using Typescript.
import { IUserSchema } from './schemas';
async function getUser(_id) {
// Get the Model
// NOTE: if NOT using Typescript "<IUserSchema>" is NOT required.
const User = knect.model<IUserSchema>('user');
// Using await get the user data.
const userData = await User.findOne({ _id });
}
See https://blujedis.github.io/knect-mongo/
See CHANGE.md
See LICENSE.md
FAQs
Mongodb connection and model helper.
We found that knect-mongo demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.