This is a GraphQL API powered by Fastify designed to provide data on trending pools, user management, and favorite token functionality. Below, you'll find all the details you need to get started with installation, configuration, usage, and further development of this service.
This backend is a GraphQL API built with Fastify, providing:
- Trending Pools: Retrieves data on trending pools or tokens.
- User Management: Allows users to be created and managed within the database.
- Favorite Token Functionality: Users can add or remove tokens from their favorites.
Key files to look at:
app.js: Main entry point that sets up the Fastify server, integrates Mercurius for GraphQL, and configures routes/resolvers.package.json: Lists all dependencies and scripts.database_migration.sql: Contains SQL statements to create tables, indices, and manage database schema.Dockerfile: Provides instructions for building a Docker image.
Ensure you have the following installed before proceeding:
- Node.js (the required version is specified in
package.json) - npm (usually bundled with Node.js)
- PostgreSQL database (version 12+ recommended)
-
Clone the repository:
git clone https://github.com/your-username/orbit-track.git
-
Navigate to the backend directory:
cd orbit-track/backend -
Install dependencies:
npm install
-
Environment Configuration:
- This service requires a PostgreSQL connection URL. Set the
DATABASE_URLenvironment variable to your Postgres connection string, for example:export DATABASE_URL="postgresql://postgres@localhost:5432/postgres"
- If
DATABASE_URLis not provided, a default connection string defined inapp.jswill be used:const dbUrl = process.env.DATABASE_URL || 'postgresql://postgres@localhost:5432/postgres';
- You may need to append
?sslmode=disableto the connection URL to disable SSL verification:postgresql://postgres@localhost:5432/postgres?sslmode=disable
- You may need to append
- This service requires a PostgreSQL connection URL. Set the
-
Create the Database:
- Make sure your PostgreSQL server is running.
- Create a new database (e.g.,
orbit_track_db) if it doesn't already exist.
-
Run the Migration Script:
- Execute the contents of
database_migration.sqlin your PostgreSQL database:-- Example usage in psql: \i path/to/database_migration.sql
- This script creates the following:
- users table (with a
dropstatement that removes any existing table—use caution in production). - favorite_tokens table (also includes a
dropstatement). - Indexes for performance.
- users table (with a
- Execute the contents of
Important: The
DROP TABLEstatements can remove existing data. Use with caution in production environments.
-
Production Mode:
npm start
The server listens on port 4000 by default.
-
Development Mode (with Hot Reloading):
npm run dev
This starts the server with automatic restarts on file changes, which is ideal for local development.
- Run All Tests:
npm test - Test Structure:
- Tests are located in the
testdirectory. - We use Node.js's built-in test runner for testing.
- Tests are located in the
- Mocks:
- Database Mocks:
test/utils/mockPgClient.jsmocks PostgreSQL interactions. - External Service Mocks:
test/utils/mockFetch.jsmocks external HTTP requests.
- Database Mocks:
This setup ensures tests do not rely on a live database or external services, allowing for consistent and reliable test runs.
This service exposes a GraphQL endpoint at:
http://localhost:4000/graphql
-
trendingPools
Returns a list of trending pools/tokens.query { trendingPools { id symbol volume } }
-
favoritesByUser
Fetches a user's favorite tokens.query($userId: ID!) { favoritesByUser(userId: $userId) { tokenId symbol } }
-
getMultipleTokens
Retrieves multiple token details by IDs or symbols.query($symbols: [String!]!) { getMultipleTokens(symbols: $symbols) { symbol price } }
-
createUserWithPublicKey
Creates a user record with the provided public key.mutation($publicKey: String!) { createUserWithPublicKey(publicKey: $publicKey) { id publicKey } }
-
addFavoriteToken
Adds a token to the user's favorites.mutation($userId: ID!, $tokenId: String!) { addFavoriteToken(userId: $userId, tokenId: $tokenId) { userId tokenId } }
-
removeFavoriteToken
Removes a token from the user's favorites.mutation($userId: ID!, $tokenId: String!) { removeFavoriteToken(userId: $userId, tokenId: $tokenId) { success } }
For more details on the schema, see the GraphQL schema defined in app.js.
-
Build the Docker Image:
docker build -t orbit-track-backend .- See
Dockerfilefor build instructions. - The base image is
node:18-alpineto keep the image lightweight and secure. - A non-root user is added to enhance security within the container.
- See
-
Run the Container:
docker run -p 4000:4000 --env DATABASE_URL="postgresql://user:password@host:port/dbname" orbit-track-backend- Map port 4000 (internal) to a port on your host (e.g.,
-p 4000:4000). - Pass in
DATABASE_URLvia--envif needed.
- Map port 4000 (internal) to a port on your host (e.g.,
-
.dockerignore:
- The .dockerignore file excludes unnecessary files from the Docker build context (e.g.,
node_modules, local logs, etc.).
- The .dockerignore file excludes unnecessary files from the Docker build context (e.g.,
Here’s a simplified overview of how this backend works:
- Fastify Server: Set up in
app.js. - Mercurius Integration: Fastify plugin for GraphQL, handling queries/mutations.
- PostgreSQL Database: Connected via
@fastify/postgres, configured to useDATABASE_URLor a default local connection. - Request Flow:
- Client → GraphQL Query/Mutation → Mercurius → Resolvers
- Resolvers may fetch data from:
- Database (via
@fastify/postgresand direct SQL queries). - External APIs (if needed).
- Database (via
- Response is returned to the client in GraphQL format.
- Performance:
- Indexes defined in
database_migration.sqlhelp with fast lookups of user favorites, tokens, etc. - Fastify’s low overhead ensures quick request handling.
- Indexes defined in