nestjs-fauna
v1.1.0
Published
The ultimate Fauna Db module for Nestjs
Downloads
10
Maintainers
Readme
NestJS Fauna
A NestJS package that provides seamless integration with FaunaDB, allowing you to interact with FaunaDB effortlessly within your NestJS applications.
Table of Contents
🚀 Installation
npm install nestjs-fauna
🤖 Getting Started
Before you start using this package, you need to have a FaunaDB account and a database set up. If you haven't already done this, head over to the FaunaDB site and follow their instructions to get started.
Once you have your FaunaDB credentials, you can configure the package in your NestJS application.
Configuration
In your NestJS application, create a .env
file in the root directory and add your FaunaDB secret key:
FAUNADB_SECRET_KEY = your_faunadb_secret_key_here
Make sure to load the .env
file using a package like dotenv
in your application's main entry file (e.g., main.ts
).
Module Registration
Register the FaunaDB module in your NestJS application. Import the FaunaModule
and pass in the configuration options:
import { Module } from '@nestjs/common'
import { FaunaModule } from 'nestjs-fauna'
@Module({
imports: [
FaunaModule.forRoot({
secret: process.env.FAUNADB_SECRET_KEY,
// Other options if needed
}),
],
})
export class AppModule {}
🏀 Usage
With the FaunaDB module configured, you can now inject the FaunaService
into your services or controllers to interact with the FaunaDB client.
import { Injectable } from '@nestjs/common'
import { fql } from 'nestjs-fauna'
@Injectable()
export class MyService {
constructor(private readonly faunaService: FaunaService) {}
async getDocumentById(id: string): Promise<any> {
// ensure you have the collection, or create a new one
// then execute it
const collectionQuery = fql`Collection.create({name: 'users'})`
await this.faunaService.query(collectionQuery)
// get the document from database
const firstDocumentQuery = fql`users.all().first()`
return this.faunaService.query(firstDocumentQuery)
// result will be
// something like this
{
id: "366172354849538082",
coll: users,
ts: Time("2023-05-30T17:33:40.220Z"),
// your data
}
}
// Add more methods to interact with FaunaDB as needed
}
In the example above, we use the FaunaService.query
method to execute a query against the FaunaDB client. You can leverage FaunaDB's powerful query language to perform various operations on your collections and documents.
For more information on the available query functions and how to use them, refer to the FaunaDB documentation.
✅ Features
- Seamless integration with NestJS applications.
- Easy-to-use API for interacting with FaunaDB.
- Utilizes FaunaDB's powerful query language for flexible database operations.
- Configurable and supports multiple environments using environment variables.
- Update using new version of FQL (version 10)
🩷 Contributing
We welcome contributions from the community to improve this package. If you find any issues or have suggestions for enhancements, please open an issue or submit a pull request.
To contribute please follow the instruction on Contributing
Please ensure your code follows the established coding standards and includes appropriate unit tests.
🪪 License
This project is licensed under the MIT License. Feel free to use and modify the code as you see fit.