ffc-database is an npm module developed by DEFRA, providing a structured utility layer for database interactions within FFC services. It is built upon the Sequelize ORM, abstracting common setup and model loading patterns. The module simplifies connecting to SQL databases (e.g., PostgreSQL, as shown in examples) by taking a configuration object, dynamically loading Sequelize models from a specified filesystem path, and exposing the connected Sequelize instance and loaded models. The current stable version is 1.0.24. While specific release cadence is not provided, its versioning and association with government services suggest a focus on stability and compatibility within its ecosystem. Its key differentiator lies in its opinionated, service-centric wrapper around Sequelize, streamlining database access for specific internal FFC applications.
npm install ffc-databaseVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate the `ffc-database` module, connect to a PostgreSQL database using environment variables for sensitive data, dynamically load Sequelize models, execute a simple raw SQL query, and gracefully close the database connection.
For ESM projects, consider using dynamic `import()` or ensuring your project's `package.json` correctly handles module types. For Sequelize specifically, ensure compatibility by checking how it's imported in ESM contexts.
Verify that `config.modelPath` is an absolute or correct relative path to your model directory. Ensure each model file exports a function that accepts `sequelize` and `DataTypes` as arguments and returns the defined model.
Implement connection closing in application shutdown hooks (e.g., `process.on('SIGTERM', ...)`) or after a block of operations where the connection is no longer needed. Ensure a new `Base` instance is created if `connect()` is to be called again.Verify the database server is running and accessible. Double-check `host`, `port`, `username`, `password`, and `database` in your `config` object. Ensure no firewall rules are blocking the connection.
Install the required database dialect package: `npm install --save pg` for PostgreSQL, `npm install --save mysql2` for MySQL, etc.
Confirm that `config.modelPath` points to the correct directory. Check the model file (`yourmodelname.js`) for syntax errors, ensure it exports a function, and that `sequelize.define` is correctly used within it.