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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@appt/mongoose

v1.0.35

Published

This a wrap for Mongoose ODM to work into the Appt's ecosystem with advanced database models and schemes.

Downloads

150

Readme

@appt/mongoose

This is a plugin made for Appt that wraps Mongoose ODM to work into the Appt's ecosystem with advanced database models and schemes.

We assume you got here after seeing the Appt's Core session of main concepts. If you don't, we strongly recommend you to step back an take a 5 minutes reading to get used with some key concepts we're going to apply here.

Install

$ npm install @appt/mongoose --save

Resources

The @appt/mongoose plugin export some resources which can be imported as seen below:

import {
   Mongoose,   
   MongooseParse,
   TModel,
   TSchema,
   SchemaTypes,
   SchemaProperties
} from '@appt/mongoose';

Mongoose

At the example below, we have a component that needs to act as a database connector of an application. By default, an Appt component is just a class with a "signature" that it can be injected by other class. As our component here needs to a specific behavior, we need to make use of a Special-Type Extender called TDatabase. This special type makes part of the @appt/core package and it indicates our component should act as a database connector, but it does not know how or which kind of database to connect. That's why we need to use a "driver", which here is "Mongoose". At this point, you only need to provide the uri connection and, if you need a little more configuration, pass into options attribute any param allowed by the mongoose connection.

import { Component, TDatabase } from '@appt/core';
import { Mongoose } from '@appt/mongoose';

const config = {
	uri: 'mongodb://localhost:27017/appt-demo',
	options: {
		debug:  true,
		useNewUrlParser:  true,
		keepAlive: true
	}
}

@Component({
	extend: TDatabase(Mongoose, config.uri, config.options)
})
export class AppDatabase{}

TModel

This Special-Type Extender add the Mongoose Model behavior to our component. That means once imported by another component (or even inside the model), any mongoose/mongo query method can be accessed into the class context. After define the type as a TModel component, the mongoose model expect to has a mongoose schema as first param. You also can add any configuration allowed for a mongoose models by passing them as second param.

import { Component } from '@appt/core';
import { TModel } from '@appt/mongoose';

@Component({
	extend: TModel('AppShema')
})
export class MyModel {
	constructor()
	{
	}

	static getById(_id){
		return this.findOne({ _id: _id });
	}
}

TSchema

The special type to transform a component into a Mongoose Schema. All the configurations accepted by mongoose can be passed through it (TSchema(config)).

import { Component } from '@appt/core';
import { TSchema } from '@appt/mongoose';

@Component({
	extend: TSchema
})
export class AppShema {
	constructor(){
		this.name = {
			type: String,
			trim: true,
			default: "",
		}

		this.email = {
			type: String,
			trim: true,
			default: "",
		}
	}
}

SchemaTypes

This is an Appt interface for Mongoose ODM schema types. It exposes every type available in Mongoose.

import { SchemaTypes } from '@appt/mongoose';
...
	this._id = {
		type: SchemaTypes.ObjectId,
		require: true
	}
...

MongooseParse

Another Appt interface, but in this case, for Mongoose ODM type parsers. It exposes every parser available by Mongoose.

import { MongooseParse } from '@appt/mongoose';
...
	getById(myId){
		return this.findOne({
			_id: MongooseParse.ObjectId(myId)
		})
	}
...

SchemaProperties

Remember the TSchema example above? The implementations of mongoose schemas can has a lot more straightfoward approach using this helper. Let's see it:

import { Component } from '@appt/core';
import { TSchema, SchemaProperties } from '@appt/mongoose';

@Component({
	extend: TSchema,
	inject: SchemaProperties
})
export class AppShema {
	constructor({
		asString,
		asBoolean,
		asObjectId,
		asNumber,
		asDate,
		asEnum
	})
	{
		// Defaults: trim: true, default: ""
		// telephones is a array of strings
		this.telephones = [ asString() ];
		
		// Defaults: default: 0
		this.age = asNumber();

		// Defaults: null
		this.school = asObjectId();

		// Defaults: default: false
		this.active = asBoolean();

		// Defaults: default: Date.now
		this.created_at = asDate();

		// Defaults: null
		// Methods: ofStrings, ofNumbers, ofDates, ofBooleans, ofObjectIds
		this.profile = asEnum({
			default: 'student'
		}).ofStrings([
			'teacher', 
			'student'
		]);
	}
}

Compatibility

We're using ES6 features! Which means you gonna need to compile your code to work with current versions of NodeJs. Thankfully, there's a lot of tools out there doing that, such as babel. You might also want to work with TypeScript. If you do, check the experimental decorators support option to start coding.

That's all folks!

If you have any suggestion or want to contribute somehow, let me know!

License


MIT License

  

Copyright (c) 2017 Rodrigo Brabo

  

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

  

The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.

  

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.