{"id":29132087,"url":"https://github.com/defra/ai-legacy-backend","last_synced_at":"2026-04-24T22:32:07.584Z","repository":{"id":300991185,"uuid":"1004951519","full_name":"DEFRA/ai-legacy-backend","owner":"DEFRA","description":"Git repository for service ai-legacy-backend","archived":false,"fork":false,"pushed_at":"2025-06-24T15:20:09.000Z","size":383,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-24T16:30:38.208Z","etag":null,"topics":["backend","cdp","node","service"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DEFRA.png","metadata":{"files":{"readme":"README.md","changelog":"changelog/db.changelog-1.0.xml","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-19T12:32:49.000Z","updated_at":"2025-06-24T15:20:11.000Z","dependencies_parsed_at":"2025-06-24T16:32:40.496Z","dependency_job_id":"dc131fcf-4bf8-44ec-8243-53b040ac6f82","html_url":"https://github.com/DEFRA/ai-legacy-backend","commit_stats":null,"previous_names":["defra/ai-legacy-backend"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/DEFRA/ai-legacy-backend","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fai-legacy-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fai-legacy-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fai-legacy-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fai-legacy-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DEFRA","download_url":"https://codeload.github.com/DEFRA/ai-legacy-backend/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEFRA%2Fai-legacy-backend/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262720721,"owners_count":23353456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend","cdp","node","service"],"created_at":"2025-06-30T06:12:19.943Z","updated_at":"2026-04-24T22:32:07.578Z","avatar_url":"https://github.com/DEFRA.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AI Legacy Backend\n\nCore delivery platform Node.js Backend Template.\n\n## Table of Contents\n\n- [Requirements](#requirements)\n  - [Node.js](#nodejs)\n- [Local Development](#local-development)\n  - [Setup](#setup)\n  - [Database Setup](#database-setup)\n  - [Development](#development)\n  - [Testing](#testing)\n  - [Production](#production)\n  - [NPM Scripts](#npm-scripts)\n  - [Formatting](#formatting)\n    - [Windows Prettier Issue](#windows-prettier-issue)\n- [Development Helpers](#development-helpers)\n  - [MongoDB Locks](#mongodb-locks)\n- [Docker](#docker)\n  - [Development Image](#development-image)\n  - [Production Image](#production-image)\n  - [Docker Compose](#docker-compose)\n  - [Complete Setup with Database Seeding](#complete-setup-with-database-seeding)\n- [Database Migrations](#database-migrations)\n- [License](#license)\n  - [About the License](#about-the-license)\n\n## Requirements\n\n### Node.js\n\nPlease install [Node.js](http://nodejs.org/) `\u003e= v22` and [npm](https://nodejs.org/) `\u003e= v9`. You will find it\neasier to use the Node Version Manager [nvm](https://github.com/creationix/nvm)\n\nTo use the correct version of Node.js for this application, via nvm:\n\n```bash\ncd ai-legacy-backend\nnvm use\n```\n\n## Local Development\n\n### Setup\n\nInstall application dependencies:\n\n```bash\nnpm install\n```\n\n### Database Setup\n\nThis application uses PostgreSQL with Liquibase for database migrations. To set up the database:\n\n1. **Start the PostgreSQL service:**\n\n   ```bash\n   docker compose up postgres -d\n   ```\n\n2. **Run database migrations:**\n\n   ```bash\n   docker compose run --rm liquibase update\n   ```\n\n3. **Clear all tables (if needed):**\n\n   ```bash\n   docker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms -c \"\n   DO \\$\\$ DECLARE\n       r RECORD;\n   BEGIN\n       FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP\n           EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' RESTART IDENTITY CASCADE';\n       END LOOP;\n   END \\$\\$;\"\n   ```\n\n4. **Populate the database with reference data:**\n\n   ```bash\n   # Run the seed scripts in order\n   docker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/01_reference_data.sql\n   docker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/02_geographic_administrative.sql\n   docker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/03_premises_farms.sql\n   docker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/04_tb_cases_testing.sql\n   ```\n\n5. **Verify the data was loaded:**\n\n   ```bash\n   # Check TB status reference data\n   docker exec -it ai-legacy-backend-postgres psql -U postgres -d tbcms -c \"SELECT * FROM tb_status_t;\"\n   ```\n\n### Development\n\nTo run the application in `development` mode run:\n\n```bash\nnpm run dev\n```\n\n### Testing\n\nTo test the application run:\n\n```bash\nnpm run test\n```\n\n### Production\n\nTo mimic the application running in `production` mode locally run:\n\n```bash\nnpm start\n```\n\n### NPM Scripts\n\nAll available NPM scripts can be seen in [package.json](./package.json).\nTo view them in your command line run:\n\n```bash\nnpm run\n```\n\n### Formatting\n\n#### Windows Prettier Issue\n\nIf you are having issues with formatting of line breaks on Windows update your global git config by running:\n\n```bash\ngit config --global core.autocrlf false\n```\n\n## Development Helpers\n\n### MongoDB Locks\n\nIf you require a write lock for Mongo you can acquire it via `server.locker` or `request.locker`:\n\n```javascript\nasync function doStuff(server) {\n  const lock = await server.locker.lock('unique-resource-name')\n\n  if (!lock) {\n    // Lock unavailable\n    return\n  }\n\n  try {\n    // do stuff\n  } finally {\n    await lock.free()\n  }\n}\n```\n\nKeep it small and atomic.\n\nYou may use **using** for the lock resource management.\nNote test coverage reports do not like that syntax.\n\n```javascript\nasync function doStuff(server) {\n  await using lock = await server.locker.lock('unique-resource-name')\n\n  if (!lock) {\n    // Lock unavailable\n    return\n  }\n\n  // do stuff\n\n  // lock automatically released\n}\n```\n\nHelper methods are also available in `/src/helpers/mongo-lock.js`.\n\n## Docker\n\n### Development Image\n\n**Build:**\n\n```bash\ndocker build --target development --no-cache --tag ai-legacy-backend:development .\n```\n\n**Run:**\n\n```bash\ndocker run -e PORT=3002 -p 3002:3002 ai-legacy-backend:development\n```\n\n### Production Image\n\n**Build:**\n\n```bash\ndocker build --no-cache --tag ai-legacy-backend .\n```\n\n**Run:**\n\n```bash\ndocker run -e PORT=3002 -p 3002:3002 ai-legacy-backend\n```\n\n### Docker Compose\n\nA local environment with:\n\n- PostgreSQL database\n- MongoDB\n- Redis\n- This service\n- Liquibase for database migrations\n- A commented out frontend example\n\n```bash\ndocker compose up --build -d\n```\n\n#### Complete Setup with Database Seeding\n\nTo set up the complete environment with database schema and reference data:\n\n```bash\n# 1. Start all services\ndocker compose up\n\n# 2. In a separate terminal, run database migrations\ndocker compose run --rm liquibase update\n\n# 3. Seed the database with reference data\ndocker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/01_reference_data.sql\ndocker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/02_geographic_administrative.sql\ndocker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/03_premises_farms.sql\ndocker exec -i ai-legacy-backend-postgres psql -U postgres -d tbcms \u003c database-docs/db-seeds/04_tb_cases_testing.sql\n\n# 4. Test the API\ncurl http://localhost:3002/api/v1/reference/tb-status\n```\n\n### Database Migrations\n\nThis project uses Liquibase for database schema management. The changelog files are located in the `/changelog` directory.\n\n#### Running Migrations in Development\n\n```bash\n# Run migrations against the development database\ndocker compose run --rm liquibase update\n```\n\n#### Running Migrations in Test Environment\n\n```bash\n# Run migrations against the test database\ndocker compose -f compose.yaml -f compose.test.yaml run --rm liquibase update\n```\n\n#### Other Useful Liquibase Commands\n\n```bash\n# Check migration status\ndocker compose run --rm liquibase status\n\n# Rollback last changeset\ndocker compose run --rm liquibase rollback-count 1\n\n# Generate SQL for pending changes (dry run)\ndocker compose run --rm liquibase update-sql\n\n# Validate changelog syntax\ndocker compose run --rm liquibase validate\n```\n\n**Note:** The Liquibase service uses Docker profiles and will only start when explicitly run with `docker compose run`.\n\n## License\n\nTHIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:\n\n\u003chttp://www.nationalarchives.gov.uk/doc/open-government-licence/version/3\u003e\n\nThe following attribution statement MUST be cited in your products and applications when using this information.\n\n\u003e Contains public sector information licensed under the Open Government license v3\n\n### About the License\n\nThe Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable\ninformation providers in the public sector to license the use and re-use of their information under a common open\nlicence.\n\nIt is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefra%2Fai-legacy-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefra%2Fai-legacy-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefra%2Fai-legacy-backend/lists"}