@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
17
Maintainers
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 yourUser
that only aUser
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].