Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
A lightweight typed MongoDb library for TypeScript.
npm i -S mongot
A collection is a class which support CRUD operations for own documents.
# UserCollection.ts
import {Collection, collection} from 'mongot';
import {UserDocument} from './UserDocument';
@collection('users', UserDocument) // bind to a document schema
class UserCollection extends Collection<UserDocument> {
findByEmail(email: string) {
return this.findOne({email});
}
}
Document schemas support following types: string
, boolean
, number
,
date
, Object
(any), SchemaFragment
(also known as subdocument),
array
type may contain any of these type.
Buffer
didn't tested at this time.
Any type can be defined via a function decorator @prop
:
@prop fieldName: string
For arrays you need pass a proto to the function decorator @prop
like as:
@prop(Date) loginDates: SchemaArray<Date> = new SchemaArray();
# UserDocument.ts
import {hook, prop, SchemaDocument} from 'mongot';
@unique({email: -1})
class UserDocument extends SchemaDocument {
@prop firstName: string;
@prop lastName: string;
@prop @req email: string;
@prop registered: Date = new Date(); // default value
@prop updated: Date;
@hook
beforeUpdate() {
this.updated = new Date();
}
get displayName() {
return this.firstName +
(this.lastName ? ' ' + this.lastName : '');
}
}
To connect your collections to MongoDb you should create a repository:
# index.ts
import {Repository} from 'mongot';
const options = {};
const repository = new Repository('mongodb://localhost/test', options);
The Repository
class constructor has same arguments that MongoClient.
Before querying you should get a collection instance via
Repository.get
.
# index.ts
async function main(): void {
const users: UserCollection = repository.get(UserCollection);
const user = await users.findByEmail('username@example.com');
// do something
...
}
Coming soon... maybe
You can if you know how it cook.
MIT
FAQs
MongoT is a modern ODM library for MongoDb.
The npm package mongot receives a total of 16 weekly downloads. As such, mongot popularity was classified as not popular.
We found that mongot 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.