Skip to content

Commit

Permalink
added: nodejs example
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Pasynok committed Nov 5, 2024
1 parent 316e4fe commit be87090
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions website/src/content/docs/how-to-guides/use-with-node-js.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Use with Node.js
sidebar:
order: 8
---

In this guide, we’ll create several containers to manage dependencies and control the initialization order of services in a Node.js application.

## Example

```js
import { createContainer, compose } from '@grlt-hub/app-compose';

// Container for connecting to a database
const dbContainer = createContainer({
id: 'database',
start: async () => {
const dbConnection = await connectToDatabase();
console.log('Database connected');
return { api: { connection: dbConnection } };
},
enable: () => process.env.DB_ENABLE === 'true',
});

// Container for setting up cache
const cacheContainer = createContainer({
id: 'cache',
start: async () => {
const cache = await initializeCache();
console.log('Cache initialized');
return { api: { cache } };
},
enable: () => process.env.CACHE_ENABLE === 'true',
});

// Container for initializing an API service, which depends on the database and cache
const apiContainer = createContainer({
id: 'apiService',
dependsOn: [dbContainer, cacheContainer],
start: async (deps) => {
const api = await initializeApi({ db: deps.database.connection, cache: deps.cache.cache });
console.log('API service started');
return { api: { apiService: api } };
},
enable: () => true,
});

// Start all containers
async function startApp() {
try {
await compose.up([dbContainer, cacheContainer, apiContainer], { debug: true });
console.log('All services are up and running');
} catch (error) {
console.error('Error starting services:', error);
}
}

startApp();
```

## Explanation

- **Database Container**: Connects to the database only if `DB_ENABLE` is set to `true`. The database connection is provided as an API for other containers.
- **Cache Container**: Initializes a cache system if `CACHE_ENABLE` is `true`. This is useful for enabling or disabling caching dynamically.
- **API Service Container**: Depends on both the database and cache containers. It only starts after these dependencies are ready and has access to their APIs.

## Conclusion

This example shows how to use `app-compose` to manage various services in a Node.js application, helping to control their initialization order and dependencies.
2 changes: 1 addition & 1 deletion website/src/content/docs/how-to-guides/use-with.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Use with...
sidebar:
order: 8
order: 9
---

Unfortunately, examples are not available for all libraries.
Expand Down

0 comments on commit be87090

Please sign in to comment.