https://github.com/haplifeman/mongo-adonis
mongo-lucid is a MongoDB adapter for AdonisJS 6's Lucid ORM, enabling seamless integration with the latest MongoDB versions.
https://github.com/haplifeman/mongo-adonis
adonis adonis-framework adonisjs lucid mongo mongodb nodejs orm typescript
Last synced: about 2 months ago
JSON representation
mongo-lucid is a MongoDB adapter for AdonisJS 6's Lucid ORM, enabling seamless integration with the latest MongoDB versions.
- Host: GitHub
- URL: https://github.com/haplifeman/mongo-adonis
- Owner: HapLifeMan
- License: mit
- Created: 2025-03-16T15:41:29.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-19T17:15:11.000Z (about 1 year ago)
- Last Synced: 2025-03-19T17:15:16.547Z (about 1 year ago)
- Topics: adonis, adonis-framework, adonisjs, lucid, mongo, mongodb, nodejs, orm, typescript
- Language: TypeScript
- Homepage:
- Size: 93.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![npm-image]][npm-url] [![license-image]][license-url] [![adonisjs-image]][adonisjs-url] [![mongodb-image]][mongodb-url] [![nodejs-image]][nodejs-url] ![typescript-image]
# Adonis x Lucid x Mongo
> [!WARNING]
> π§ This package is in active development and may contain breaking changes. While it contains 100+ tests to ensure reliability, I recommend using it in production with caution. The documentation may not be up-to-date and can have inconsistent information. Thanks for your understanding!
> Leverage the power of objects in Node.js with MongoDB's object-oriented database and AdonisJS 6's Lucid ORM. Built specifically for AdonisJS 6, `mongo-adonis` supports latest MongoDB versions.
## β¨ Features
- π **Seamless MongoDB Integration**: Use MongoDB with the familiar Lucid ORM API
- π― **TypeScript First**: Full type safety and autocompletion
- π **Active Record Pattern**: Intuitive model-based database operations
- π **Rich Relationships**: Support for HasOne, HasMany, BelongsTo relationships
- π¨ **Decorator Support**: Clean and declarative model definitions
- π **Powerful Query Builder**: Fluent API for complex queries
- π§ **Direct MongoDB Access**: Use MongoDB's native API directly with `db.collection.find({})`
- π **Built-in Auth Support**: Works seamlessly with AdonisJS 6 auth
- π **Advanced Serialization**: Fine-grained control over JSON representation
- π¦ **Zero Configuration**: Get started with minimal setup
## πΊοΈ Roadmap
- [ ] Create boilerplate
- [ ] Improve query builder
- [ ] Many-to-Many relationships
- [ ] Paginate methods
- [ ] ? Migrations
- [ ] ? Factories
- [ ] ? Seeds
## π Quick Start
### 1. Install the Package
```bash
bun install mongo-adonis
```
### 2. Configure the Provider
```bash
node ace configure mongo-adonis
```
### 3. Fill Environment Variables
```env
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_USER=
DB_PASSWORD=
DB_DATABASE=mongo-adonis
DB_AUTH_SOURCE=admin
```
## π Usage Examples
### Basic Model Definition
```ts
import { MongoModel, column, ObjectId } from 'mongo-adonis'
export class Post extends MongoModel {
@column({ isPrimary: true })
declare _id: ObjectId
@column()
declare name: string
@column()
declare description: string
@column()
declare content: string
}
```
### CRUD Operations
```ts
// Create
const user = await User.create({
name: 'John Doe',
email: 'john@example.com',
password: 'secret'
})
// Read
const user = await User.find('60a1e2c3d4e5f6a7b8c9d0e1')
const users = await User.all()
// Update
user.name = 'Jane Doe'
await user.save()
// Delete
await user.delete()
```
### Advanced Queries
```ts
// Complex queries with the fluent API
const activeUsers = await User.query()
.where('status', 'active')
.where('age', '>=', 18)
.orderBy('name', 'asc')
.limit(10)
.offset(0)
.all()
```
### Relationships
```ts
import { column, hasMany, belongsTo, ObjectId, MongoModel } from 'mongo-adonis'
export class User extends MongoModel {
@column({ isPrimary: true })
declare _id: ObjectId
@column()
declare name: string
@hasMany(() => Post)
declare posts: HasMany
}
export class Post extends MongoModel {
@column({ isPrimary: true })
declare _id: ObjectId
@column()
declare title: string
@belongsTo(() => User)
declare user: BelongsTo
}
```
### Working with Relationships
```ts
// Create a user with posts
const user = await User.create({
name: 'John Doe',
email: 'john@example.com'
})
// Create posts for the user
await user.posts.create({
title: 'First Post',
content: 'Hello World!'
})
await user.posts.createMany([
{
title: 'Second Post',
content: 'Another post',
},
{
title: 'Third Post',
content: 'Another one',
}
])
// Load user with their posts
const userWithPosts = await User.query().where('_id', user._id).first()
// Get all posts for a user
const userPosts = await user.posts.all()
// Find a post and load its user
const post = await Post.query().where('_id', postId).first()
// Get the user of a post
const postUser = await post.user.exec()
// Update a post's user
const newUser = await User.find(newUserId)
await post.user.associate(newUser)
// Remove a post from a user
await post.user.dissociate()
```
## π Authentication
Works seamlessly with AdonisJS 6 auth, update the `app/models/user.ts` file:
```ts
import { DateTime } from 'luxon'
import hash from '@adonisjs/core/services/hash'
import { compose } from '@adonisjs/core/helpers'
import { MongoModel, column, withAuthFinder, ObjectId } from 'mongo-adonis'
const AuthFinder = withAuthFinder(() => hash.use('scrypt'), {
uids: ['email'],
passwordColumnName: 'password',
})
export default class User extends compose(MongoModel, AuthFinder) {
@column({ isPrimary: true })
declare _id: ObjectId
@column()
declare email: string
@column({ serialize: false })
declare password: string
@column.dateTime({ autoCreate: true })
declare createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime
}
```
## π€ Contributing
Please see our [Contributing Guide](CONTRIBUTING.md) for details.
## π License
MIT License - see the [LICENSE.md](LICENSE.md) file for details.
---
[npm-image]: https://img.shields.io/npm/v/mongo-adonis.svg
[npm-url]: https://npmjs.org/package/mongo-adonis
[license-image]: https://img.shields.io/npm/l/mongo-adonis
[license-url]: LICENSE.md
[adonisjs-image]: https://img.shields.io/badge/AdonisJS-6-5A45FF
[adonisjs-url]: https://adonisjs.com
[mongodb-image]: https://img.shields.io/badge/MongoDB-4.0--8.0-47A248
[mongodb-url]: https://www.mongodb.com/docs/drivers/node/current/compatibility/
[nodejs-image]: https://img.shields.io/badge/Node.js-16--22-339933
[nodejs-url]: https://www.mongodb.com/docs/drivers/node/current/compatibility/
[typescript-image]: https://img.shields.io/badge/TypeScript-3178C6