AdonisJS Lucid MongoDB
NB - WORK IN PROGRESS
🙏 This repository is base on adonis-lucid. This package only work with mongodb.
Breaking update
Version 2.x was published to npm with new name. Make sure you install correct version:
Adonis version | Version | Name |
---|---|---|
3.x.x | 1.x.x | adonis-lucid-mongodb |
4.x.x | 2.x.x | lucid-mongo |
Adonis-lucid-mongodb is a mongo query builder and ODM for Adonis framework. It also has support for database migrations, seeds and factories as Adonis-lucid.
You can learn more about AdonisJS and all of its awesomeness on http://adonisjs.com 🌲
You can see example here adonis-mongodb-boilerplate
Note: If you decided use this package you need replace all setting of adonis-lucid by this package. See install steps for more detail
Usage
The usage of LucidMongo is similar to Lucid
Official Documentation of Lucid here
Query
const users = Userall const users = User const users = User const users = Userconst user = User const users = User const users = User const users = User const users = User // to query geo near you need declare field type as geometry and add 2d or 2dsphere index in migration fileconst images = Image const images = Image
Aggregation
const count = Customer const count = Customer const max = Employee const total = Employee const avg = Employee
Relations
This package support relations like adonis-lucid:
- hasOne
- belongsTo
- hasMany
- hasManyThrough
- belongsToMany
More Documentation of adonis relationships
mongodb has no join query so this package has no query like: has
, whereHas
, doesntHave
, whereDoesntHave
Addition relations
morphMany:
A model can belong to more than one other model, on a single association. For example, you might have a Picture model that belongs to either an Author model or a Reader model
{ return this } { return this } { return this }
embedsOne:
EmbedsOne is used to represent a model that embeds another model, for example, a Customer embeds one billingAddress.
{ return this }
embedsMany:
Use an embedsMany relation to indicate that a model can embed many instances of another model. For example, a Customer can have multiple email addresses and each email address is a complex object that contains label and address.
{ return this }
referMany:
Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s)
{ return this }
Query relationships
const user = User const user = User const user = User const user = User const user = User
Query logging
To show query logs run this command:
- Linux, MacOS
DEBUG=mquery npm run dev
- Windows
setx DEBUG mquery && npm run dev
Migration
Current only support create, drop, rename collection and index
{ this this this this this this}
Field type
Type of
mongodb.ObjectID
The objectId fields will be converted to mongodb.ObjectID before save to db.
static { return '_id' 'categoryId' } //default return ['_id']
The where query conditions will be converted to objectId too
const article = Articleconst articles = Article
Type of
date
static { return 'dob' }
The field declare as date will be converted to moment js object after get from db
const staff = Staffconst yearAgo = staffdob
You can set attribute of model as moment js object, field will be converted to date before save to db
staffdob =
The where query conditions will be converted to date too
const user = User
Date type is UTC timezone
Type of
geometry
static { return 'location' }
When declare field type as geometry the field will be transformed to geoJSON type
const image = Image
Result:
After get from db it will be retransformed to
lat: 1 lng: 1
Get the mongodb drive
You can get the instance of mongodb drive to execute raw query
const Database = const mongo = Database const users = mongo const phone = mongo
More document about mongo drive here
Installation
To setup this package
$ npm i --save adonis-lucid-mongodb
and then register lucid providers inside the your bootstrap/app.js
file.
you should complete replace all setting of adonis-lucid
with adonis-lucid-mongodb
const providers = // ... // 'adonis-lucid/providers/DatabaseProvider', // 'adonis-lucid/providers/LucidMongoProvider', // 'adonis-lucid/providers/FactoryProvider' 'adonis-lucid-mongodb/providers/DatabaseProvider' 'adonis-lucid-mongodb/providers/LucidMongoProvider' 'adonis-lucid-mongodb/providers/FactoryProvider' const aceProviders = // ... // 'adonis-lucid/providers/CommandsProvider', // 'adonis-lucid/providers/MigrationsProvider', // 'adonis-lucid/providers/SchemaProvider', // 'adonis-lucid/providers/SeederProvider', 'adonis-lucid-mongodb/providers/CommandsProvider' 'adonis-lucid-mongodb/providers/MigrationsProvider' 'adonis-lucid-mongodb/providers/SchemaProvider' 'adonis-lucid-mongodb/providers/SeederProvider'
setting up aliases inside bootstrap/app.js
file.
const aliases = Database: 'Adonis/Src/Database' Lucid: 'Adonis/Src/LucidMongo' Schema: 'Adonis/Src/Schema' Migrations: 'Adonis/Src/Migrations' Factory: 'Adonis/Src/Factory'
add config to config/database.js
file
moduleexports = /* |-------------------------------------------------------------------------- | Default Connection |-------------------------------------------------------------------------- | | Connection defines the default connection settings to be used while | interacting with Mongodb databases. | */ connection: Env /*-------------------------------------------------------------------------*/ mongodb: client: 'mongodb' connection: host: Env port: Env user: Env password: Env database: Env auth: source: Env mechanism: Env
Contribution Guidelines
In favor of active development we accept contributions for everyone. You can contribute by submitting a bug, creating pull requests or even improving documentation.