
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
nestjs-appwrite
Advanced tools
Easier Appwrite integration for your NestJS application.
Easier Appwrite integration for your NestJS application.
This is a core library developed by Coffee IT which offers easier integration with Appwrite when using the NestJS framework.
The library offers a few functionalities:
The library will create your database collections and configures the attribute and indexes based on your schema decorators.
Setup the AppwriteModule inside your main AppModule and pass the required options. Based on the useEnvVarsForSecrets boolean the library will either use env variables for secrets or otherwise AWS parameter store. The library expects the following secrets:
JWT_SECRET=Secret that can be used to verify JWT tokens, only required when using AccessTokenGuard
APPWRITE_API_KEY=The Appwrite Api key
The names of the secrets need to be uppercase when using env variables and lowercase when using parameter store.
@Module({
imports: [
AppwriteModule.forRoot({
endpoint: Config.APPWRITE_ENDPOINT,
projectId: Config.APPWRITE_PROJECT_ID,
databaseId: Config.APPWRITE_DATABASE_ID,
awsRegion: AWS_REGION,
useEnvVarsForSecrets: Config.USE_ENV_VARS,
}),
],
})
export class AppModule {}
You can create Appwrite database schemas based on decorators. For example:
import { ADMIN_TEAM_ID, EXPERIENCES_COLLECTION_ID, EXPERIENCES_COLLECTION_NAME } from '../../constants';
import { ExperienceType } from '../domain/experience-type.enum';
import { Permission, Role } from 'node-appwrite';
import { EnumProp, Index, Schema, StringProp } from 'appwrite-core';
import { Document } from 'appwrite-core';
import { ApiProperty, IntersectionType } from '@nestjs/swagger';
@Schema({
collectionId: EXPERIENCES_COLLECTION_ID,
collectionName: EXPERIENCES_COLLECTION_NAME,
permissions: [
Permission.read(Role.any()),
Permission.write(Role.team(ADMIN_TEAM_ID))
],
})
export class Experience {
@ApiProperty()
@StringProp()
title: string;
@ApiProperty()
@StringProp({ size: 30 })
@Index({ type: 'unique', orders: 'DESC' })
coverImageId: string;
@ApiProperty()
@StringProp({ size: 1000 })
content: string;
@ApiProperty({ enum: ExperienceType })
@EnumProp({ enum: ExperienceType })
type: ExperienceType;
}
export class ExperienceModel extends IntersectionType(Experience, Document) {}
Within the module that needs the schema you will have to import the schema. For example:
@Module({
imports: [
AppwriteModule.forFeatureAsync([
{ class: Experience },
]),
],
providers: [],
controllers: [ExperienceController]
})
export class ExperienceModule {}
You're now ready to use the schema within your controllers and services. The schema will be wrapped inside a AppwriteRepository class which offers many useful database operations. Example usage within a controller:
@Controller()
export class ExperienceController {
constructor(
@Inject(Experience.name) private readonly experienceRepo: AppwriteRepository<Experience>,
) {}
@Post('experiences')
@UseGuards(AccessTokenGuard)
@ApiOkResponse({ type: ExperienceModel })
public async createExperience(@AppClient() client: ClientWithToken, @Body() dto: CreateExperienceDto): Promise<ExperienceModel> {
return this.experienceRepo.asAppClient(client).createDocument(dto);
}
}
The ClientWithToken object is available here because it gets added to the request by the AccessTokenGuard. You then have to use asAppClient() to make use of the permissions of the client.
That's it. When you start your service the database will be configured and you can create a document with the properties of the schema.
This library exposes a few services that are globally available.
You can simply import the AccessTokenGuard and use it in your controller
@UseGuards(AccessTokenGuard)
You can inject an instance of the appwrite server client like so:
@Controller()
export class AppController implements OnApplicationBootstrap {
constructor(
@Inject(CLIENT_PROVIDER_NAME) private readonly rootClient: Client,
) {
}
public async onApplicationBootstrap(): Promise<void> {
await this.setupTeams();
}
private async setupTeams(): Promise<void> {
const teams = new Teams(this.rootClient);
try {
await teams.get(ADMIN_TEAM_ID);
} catch (err: any) {
if (err.code === HttpStatus.NOT_FOUND) {
await teams.create(ADMIN_TEAM_ID, ADMIN_TEAM_NAME);
this.logger.debug(`Created team with id ${ADMIN_TEAM_ID} and name ${ADMIN_TEAM_NAME}`);
return;
}
this.logger.error(err);
}
}
}
As you can see you can use the client to do any setup work yourself.
Let's say you want to make use of a secret that might be configured inside parameter store on a server or as a env variable locally.
You can then make use of SecretStoreService. Simply import the SecretStoreService inside your class like so:
@Injectable()
export class FirebaseService implements OnApplicationBootstrap {
private readonly logger = new Logger();
constructor(
private readonly storeService: SecretStoreService
) { }
public async onApplicationBootstrap(): Promise<void> {
await this.init();
}
private async init() {
let firebaseServiceAccount = await this.storeService.getSecretString(FIREBASE_SERVICE_ACCOUNT_SECRET);
if (!firebaseServiceAccount) {
this.logger.error(`Secret ${FIREBASE_SERVICE_ACCOUNT_SECRET} is undefined, unable to use Firebase`);
return;
}
admin.initializeApp({
credential: admin.credential.cert(firebaseServiceAccount)
});
}
}
In this example we get the Firebase service account secret from the SecretStoreService to setup firebase.
FAQs
Easier Appwrite integration for your NestJS application.
The npm package nestjs-appwrite receives a total of 143 weekly downloads. As such, nestjs-appwrite popularity was classified as not popular.
We found that nestjs-appwrite demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.