MongoDB
FoalTS provides two ways to interact with a MongoDB database in your application: Mongoose and TypeORM.
#
Usage with Mongoose#
Generating a new project with MongooseWhen creating an application with the --mongodb
flag, the CLI generates a new project with mongoose
and @foal/mongoose
installed. The User
model is defined using this ODM as well as the create-user
script.
#
Generating a modelYou cannot create entities in a Mongoose project, as it is specific to TypeORM. Instead, you can use this command to generate a new model:
#
ConfigurationThe URI of the MongoDB database can be passed through:
- the config file
config/default.json
with themongodb.uri
key, - or with the environment variable
MONGODB_URI
.
Example (config/default.json
):
#
AuthenticationMongoDBStore
#
The If you use sessions with @TokenRequired
or @TokenOptional
, you must use the MongoDBStore
from @foal/mongodb
.
fetchUser
function#
The Example with JSON Web Tokens:
#
Usage with TypeORM#
Configurationormconfig.js
config/default.json
#
AuthenticationMongoDBStore
#
The If you use sessions with @TokenRequired
or @TokenOptional
, you must use the MongoDBStore
from @foal/mongodb
. The TypeORMStore does not work with noSQL databases.
fetchMongoDBUser
function#
The user.entity.ts
Example with JSON Web Tokens:
#
LimitationsWhen using MongoDB, there are some features that are not available:
- the
foal g rest-api <name>
command, - and the Groups & Permissions system.