Skip to content
This repository has been archived by the owner on Nov 16, 2019. It is now read-only.

alterior-mvc/alterior-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Alterior MongoDB Support

npm version Build Status Join the chat at https://gitter.im/alterior-mvc/Lobby

NOTE: This library is intended for use with Alterior 2.x. It is not compatible with Alterior 3.x.

Use this package if you want to connect to MongoDB from your Alterior application.

Accessing MongoDB from Alterior

npm i mongodb @alterior/mongo

Now use mongoProvider to inject an instance of mongodb.Db into your application:

import { AppOptions } from '@alterior/core';
import { mongoProvider } from '@alterior/mongo';
import * as mongodb from 'mongodb';

@AppOptions({
    providers: [mongoProvider(mongodb.Db)]
})
class App { 
}

Now, you can inject mongodb.Db into your controllers:

import { Controller, Get } from '@alterior/core';
import * as mongodb from 'mongodb';

@Controller()
class SampleController {
    constructor(
        private db : mongodb.Db
    ) {
    }
    
    @Get('/stuff')
    public getStuff() {
        return this.db.collection('stuff').find().toArray();
    }
}

You can pass any token into mongoProvider, which can be used to inject multiple database connections if necessary.

Storing Sessions in MongoDB

This package also provides a connector for storing Express sessions in MongoDB using mongo-connect.

import * as mongodb from 'mongodb';
import { mongoProvider, mongoSession } from '@alterior/mongo';

@AppOptions({
	providers: [mongoProvider(mongodb.Db)]
	middleware: [mongoSession(mongodb.Db, SESSION_SECRET)]
})
export class App { }

You can then access and modify session data from your route methods. We recommend you make an interface representing your session data.

interface SessionData {
	displayName : string;
	cartTotal : number;
}

@Controller()
class SampleController {
	@Get('/cart/total') 
	public get(session : SessionData) {
		return session.cartTotal;
	} 
}

About

A simple MongoDB provider for Alterior

Resources

Stars

Watchers

Forks

Packages

No packages published