A NestJS package that provides seamless integration with FaunaDB, allowing you to interact with FaunaDB effortlessly within your NestJS applications.
npm install nestjs-fauna
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.
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
).
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 {}
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.
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.
This project is licensed under the MIT License. Feel free to use and modify the code as you see fit.