Skip to main content
Version: v2

MongoDB (noSQL)

Creating a new project

To generate a new project that uses MongoDB, run the command createapp with the flag --mongodb.

foal createapp my-app --mongodb

Configuration

npm install mongodb
database:
type: mongodb
url: mongodb://localhost:27017/test-foo-bar

Defining Entities and Columns

More documentation here: https://github.com/typeorm/typeorm/blob/master/docs/mongodb.md.

The definition of entities and columns is the same as in relational databases, except that the ID type must be an ObjectID and the column decorator must be @ObjectIdColumn.

import { Entity, ObjectID, ObjectIdColumn, Column } from 'typeorm';

@Entity()
export class User {

@ObjectIdColumn()
id: ObjectID;

@Column()
firstName: string;

@Column()
lastName: string;

}

Example

import { getMongoRepository } from 'typeorm';

const user = await getMongoRepository(User).findOne('xxxx');

Authentication

The fetchMongoDBUser function

user.entity.ts

import { Entity, ObjectID, ObjectIdColumn } from 'typeorm';

@Entity()
export class User {

@ObjectIdColumn()
id: ObjectID;

}

Example with JSON Web Tokens:

import { JWTRequired } from '@foal/jwt';
import { fetchMongoDBUser } from '@foal/typeorm';

import { User } from '../entities';

@JWTRequired({ user: fetchMongoDBUser(User) })
class MyController {}

The MongoDBStore

npm install @foal/mongodb

If you use sessions with @UseSessions, you must use the MongoDBStore from @foal/mongodb. The TypeORMStore does not work with noSQL databases.

Limitations

When using MongoDB, there are some features that are not available:

  • the foal g rest-api <name> command,
  • and the Groups & Permissions system.