npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@elijahjcobb/mongo

v0.0.4

Published

A package to handle a connection with a MongoDB that is type safe using components similar to React.

Downloads

22

Readme

Mongo

A package to handle a connection with a MongoDB that is type safe using components similar to React.

Examples

Import

Import all modules needed from the @elijahjcobb/mongo package.

import { ECMObject, ECMDatabase, ECMQuery } from "@elijahjcobb/mongo";

Initialize

Everything in this package is promise based. So inside an async function call the static function connect() on the ECMDatabase class.

import { ECMDatabase } from "@elijahjcobb/mongo";
await ECMDatabase.connect("mongodb://localhost:27017", "mongo");

Creating a Prototype

Create a class that extends the abstract class ECMObject. ECMObject requires a generic Props type that conforms to the ECMObjectPropType type. So make a interface that extends ECMObjectPropType and then use that interface as the type for the ECMObject. Make sure to add a public constructor to your new class that calls super(collection: string). That way to make a new object of your class you don't even need to supply its collection.

Example

import { ECMObject } from "@elijahjcobb/mongo";

interface UserProps {
	name: string;
	age: number;
}

class User extends ECMObject<UserProps> {

	public constructor() {

		super("user");

	}

}

In the example above a User class is created. Now you can easily do anything on your user. You can also write functions on your User that only a User would have.

Using a Prototype

Every prototype has a props property that conforms to the type you define. An object will have:

  • id: string
  • updatedAt: number
  • createdAt: number
  • props: T

Props

The props follows the interface you supply when making a class that extends ECMObject. Using the example from above you can access different properties on the User.

let user: User = new User();
user.props.name = "Elijah";
user.props.age = 20;
user.id; // the id of the user
user.updatedAt; // the timestamp the user was last updated
user.createdAt; // the timestamp the user created

await user.update();
await user.create();
await user.delete();
await user.fetch("id-goes-here");
await user.updateProps("name", "age");

Fetching an Object

You can use the ECMQuery to fetch an object. Pass the class of the object and the id and it will return a type safe instance of the class for the given id.

let user: User = await ECMQuery.getForId(User, "the-id-of-user");

Query with ECMQuery

Create Query

To create a query, make a new instance of ECMQuery and pass the class of the object you will be querying and whether all the filters should be queried as AND or OR;

Add Filter

You can add filters to a query with the addFilter(<ECMFilter>) function. A filter instance required a key that is a key of the props type you provide for the class, a filter type, and a value.

Sort

You can sort the query by using the setSort(<ECMSort>) method that takes a ECMSort instance. To create a new instance, provide a key that is a key of the props, and a sort direction.

Setting a Limit

Use the setLimit(<number>) function that takes a number to limit the amount of responses from the query.

Sending Query

You can use the getAll() or getFirst() methods that return promises of either the first object that matches the query, or all objects that match the query.

Example

let query: ECMQuery<User, UserProps> = new ECMQuery(User, ECMConditionType.And);
query.addFilter(new ECMFilter("age", ECMFilterType.GreaterThan, 12));
query.addFilter(new ECMFilter("age", ECMFilterType.LessThan, 40));
query.setSort(new ECMSort("name", ECMSortType.LeastToGreatest));
query.setLimit(1000);
let responses: ECArray<User> = await query.getAll();

responses.forEach((user: User) => {

	 user.print();

});

Documentation

Everything is completely documented. You can view the declaration files or even the source code on GitHub.

Bugs

If you find any bugs please create an issue on GitHub or if you are old fashioned email me at [email protected].