Postgres
Overview
Postgres settlement layer is a settlement layer that is using following tech under the hood that we curated for you to be as fast as possible:
- PostgreSQL (duh!)
- oRPC (for communication between the settlement layer and the app)
- drizzle-orm (for database operations)
- better-auth (for authentication)
Development
DB Schema
Pg settlement layer uses drizzle-orm to interact with the database. When you want to store some data in a db you have to create a table schema first.
You can do this either in db-schema.ts file or create a new module with a db schema file that will be reexported from db-schema.ts:
import { pgTable, varchar, integer } from 'drizzle-orm/pg-core';
export const countersTable = pgTable('counters', {
userId: varchar({ length: 255 }).notNull().primaryKey(),
value: integer().notNull(),
});
export const globalCounterTable = pgTable('global_counter', {
value: integer().notNull().default(0),
});Want to learn more?
Checkout the drizzle docs to see how excatly build the db schema.
Context Provider
_fundset/settlement-layer/pg/index.tsx file is a context provider component so if you need anything that will need to wrap the app with this specifc SL, you can add it there:
const PgSettlementLayerProvider = ({ children }: React.PropsWithChildren) => {
return <>{children}</>;
};
export default PgSettlementLayerProvider;Migrations
drizzle folder contains all the migrations for the db. You can use drizzle-kit to generate the migrations and run them against a database.
pnpm drizzle-kit generate
pnpm drizzle-kit migrateoRPC
_fundset/settlement-layer/pg/orpc folder contains all the orpc files that are required for it to work. Check out the oRPC docs to learn more about it.
You should be particularly interested in router.ts file, where you can add your own modules:
import { counterModule } from '../../modules/counter/pg/orpc';
export const router = {
...counterModule,
};Modules
Each module that supports postgres, will have a pg directory inside it.
Check out the docs on how to build your own modules.
Payload CMS Plugin
_fundset/settlement-layer/pg/plugin folder contains the payload CMS plugin files. If you want you can extend it to suit your needs. Check out the payload docs to learn more about it.
Deployment
This is a step by step guide on how to deploy an app with a pg settlement layer to Vercel.
- Publish your fundset app's code to github
- Create a postgres database instance e.g. with Neon on Vercel
- set the database connection string in the
DATABASE_URIenvironment variable in your .env file - in the
packages/webfolder, create Payload migrations withpnpm payload migrate:create - run Payload migrations with
pnpm payload migrate - run
pnpm drizzle-kit generateto generate drizzle migrations - run
pnpm drizzle-kit migrateto migrate the database - Create a new Vercel project out of it and add the following environment variables to your Vercel project:
DATABASE_URI- your postgres database connection stringPAYLOAD_SECRET- a random stringBETTER_AUTH_SECRET- a random stringNEXT_PUBLIC_APP_URL- URL of your fundset app (e.g.https://<your-app-name>.vercel.app)
- Deploy your Vercel project.
- Go to
/adminpage and create a new admin user and log in. - In the admin dashboard, go to
Fundset Settlement LayerGlobal and add a settlement layer config
Your app is now ready to use!