Skip to content

Dependency Caching

ItzNotABug edited this page May 3, 2024 · 1 revision

Manage your dependencies effectively by avoiding passing them around -

// Inject a repository in your `index.js`
const repository = new AppwriteRepository();
appExpress.inject(repository);

// Retrieve in some other file like `./routes/auth.js`
import AppwriteRepository from './data/repository.js';

appExpress.get('/user/auth/:userId', async (request, response) => {
  const { userId } = request.params;
  const repository = request.retrieve(AppwriteRepository);
  const result = await repository.performAuth(userId);
});

If you have multiple variables of the same class types, make sure to use an identifier:

// Inject repositories
appExpress.inject(sourceRepository, 'source');
appExpress.inject(destinationRepository, 'destination');

// Retrieve
appExpress.post('/migration', async (request, response) => {
  const { sourceKey, destinationKey } = request.body;
   
  // perform some checks with the API Keys
  const source = request.retrieve(Repository, 'source');
  const destination = request.retrieve(Repository, 'destination');

  // perform a migration
  const migration = new Migration(source, destination);
  const result = await migration.mirrorDatabases();

  return response.json({ result })
});

Clone this wiki locally