Suppose you’re interested in implementing a new feature in Infisical’s backend, let’s call it “feature-x.” Here are the general steps you should follow.
In order to run schema migrations you need to expose your database connection string. Create a .env.migration
file to set the database connection URI for migration scripts, or alternatively, export the DB_CONNECTION_URI
environment variable.
If your feature involves a change in the database, you need to first address this by generating the necessary database schemas.
TableName
enum in /src/db/schemas/models.ts
to include the new table name.npm run migration:new
and give it a relevant name, such as feature-x
./src/db/migrations/<timestamp>_<feature-x>.ts
.up
and down
functions to create or alter Postgres fields on migration up and to revert these changes on migration down, ensuring idempotency as outlined here.While typically you would need to manually write TS types for Knex type-sense, we have automated this process:
npm run migration:latest
to apply all database changes.npm run generate:schema
to automatically generate types and schemas using zod in the /src/db/schemas
folder.schema/index
and include the new tables in /src/@types/knex.d.ts
to enable type-sensing in Knex.js.Once the database changes are in place, it’s time to create the APIs for feature-x
:
npm run generate:component
.feature-x
. This will create a feature-x
folder in /src/services
containing three files.
feature-x-dal
: The Database Access Layer functions.feature-x-service
: The service layer where all the business logic is handled.feature-x-type
: The types used by feature-x
.For reusable shared functions, set up a file named feature-x-fns
.
Use the custom Infisical function ormify
in src/lib/knex
for simple database operations within the DAL.
Server-related logic is handled in /src/server
. To connect the service layer to the server layer, we use Fastify plugins for dependency injection:
fastify.d.ts
file under the service
namespace of a FastifyServerInstance type./src/server/routes/index.ts
, instantiate the required dependencies for feature-x
, such as the DAL and service layers, and then pass them to fastify.register("service,{...dependencies})
.server.services.<registered service name>.<function>
.npm generate:component
.src/server/routes/v<version-number>/<router component name>
v1/index.ts
.Suppose you’re interested in implementing a new feature in Infisical’s backend, let’s call it “feature-x.” Here are the general steps you should follow.
In order to run schema migrations you need to expose your database connection string. Create a .env.migration
file to set the database connection URI for migration scripts, or alternatively, export the DB_CONNECTION_URI
environment variable.
If your feature involves a change in the database, you need to first address this by generating the necessary database schemas.
TableName
enum in /src/db/schemas/models.ts
to include the new table name.npm run migration:new
and give it a relevant name, such as feature-x
./src/db/migrations/<timestamp>_<feature-x>.ts
.up
and down
functions to create or alter Postgres fields on migration up and to revert these changes on migration down, ensuring idempotency as outlined here.While typically you would need to manually write TS types for Knex type-sense, we have automated this process:
npm run migration:latest
to apply all database changes.npm run generate:schema
to automatically generate types and schemas using zod in the /src/db/schemas
folder.schema/index
and include the new tables in /src/@types/knex.d.ts
to enable type-sensing in Knex.js.Once the database changes are in place, it’s time to create the APIs for feature-x
:
npm run generate:component
.feature-x
. This will create a feature-x
folder in /src/services
containing three files.
feature-x-dal
: The Database Access Layer functions.feature-x-service
: The service layer where all the business logic is handled.feature-x-type
: The types used by feature-x
.For reusable shared functions, set up a file named feature-x-fns
.
Use the custom Infisical function ormify
in src/lib/knex
for simple database operations within the DAL.
Server-related logic is handled in /src/server
. To connect the service layer to the server layer, we use Fastify plugins for dependency injection:
fastify.d.ts
file under the service
namespace of a FastifyServerInstance type./src/server/routes/index.ts
, instantiate the required dependencies for feature-x
, such as the DAL and service layers, and then pass them to fastify.register("service,{...dependencies})
.server.services.<registered service name>.<function>
.npm generate:component
.src/server/routes/v<version-number>/<router component name>
v1/index.ts
.