Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9,853 changes: 2,551 additions & 7,302 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

417 changes: 417 additions & 0 deletions postgres/insforge-test/README.md

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions postgres/insforge-test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
babelConfig: false,
tsconfig: 'tsconfig.json',
},
],
},
transformIgnorePatterns: [`/node_modules/*`],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: ['dist/*']
};
63 changes: 63 additions & 0 deletions postgres/insforge-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "insforge-test",
"version": "0.1.0",
"author": "Interweb <developers@interweb.io>",
"description": "insforge-test offers isolated, role-aware, and rollback-friendly PostgreSQL environments for integration tests with InsForge defaults baked in",
"main": "index.js",
"module": "esm/index.js",
"types": "index.d.ts",
"homepage": "https://github.com/constructive-io/constructive",
"license": "MIT",
"publishConfig": {
"access": "public",
"directory": "dist"
},
"repository": {
"type": "git",
"url": "https://github.com/constructive-io/constructive"
},
"bugs": {
"url": "https://github.com/constructive-io/constructive/issues"
},
"keywords": [
"insforge",
"postgres",
"postgresql",
"testing",
"integration-tests",
"database-testing",
"pg",
"rls",
"role-based-access",
"test-database",
"test-runner",
"jest",
"mocha",
"seed",
"fixtures",
"transactions",
"rollback",
"node-postgres",
"pg-pool",
"pg-client"
],
"scripts": {
"clean": "makage clean",
"copy": "makage assets",
"prepack": "npm run build",
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
"build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
"lint": "eslint . --fix",
"test": "jest --passWithNoTests",
"test:watch": "jest --watch"
},
"dependencies": {
"@pgpmjs/types": "workspace:^",
"deepmerge": "^4.3.1",
"pg-env": "workspace:^",
"pgsql-test": "workspace:^"
},
"devDependencies": {
"makage": "^0.3.0"
}
}
78 changes: 78 additions & 0 deletions postgres/insforge-test/src/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import deepmerge from 'deepmerge';
import { getPgEnvVars, PgConfig } from 'pg-env';
import {
getConnections as getPgConnections,
type GetConnectionOpts,
type GetConnectionResult
} from 'pgsql-test';
import type { PgTestConnectionOptions } from '@pgpmjs/types';

/**
* InsForge default connection options
*/
const INSFORGE_DEFAULTS: Partial<PgTestConnectionOptions> = {
roles: {
anonymous: 'anon',
authenticated: 'authenticated',
administrator: 'project_admin',
default: 'anon',
},
connections: {
app: {
user: 'postgres',
password: 'postgres',
}
}
};

/**
* InsForge default PostgreSQL config
*/
const INSFORGE_PG_DEFAULTS: Partial<PgConfig> = {
port: 5432,
user: 'postgres',
password: 'postgres',
};

/**
* Get connections with InsForge defaults applied.
* Uses deepmerge for proper nested config merging.
*
* Precedence (later wins):
* 1. InsForge defaults
* 2. Environment variables (PGUSER/PGPASSWORD)
* 3. User-provided options
*/
export const getConnections = async (
cn: GetConnectionOpts = {},
seedAdapters?: Parameters<typeof getPgConnections>[1]
): Promise<GetConnectionResult> => {
// Get environment variables (only includes defined keys)
const pgEnvVars = getPgEnvVars();

// Build env overrides - pgEnvVars already only has defined keys
// Mirror user/password to connections.app for the app connection
const envOverrides: Partial<GetConnectionOpts> = {
pg: pgEnvVars,
db: {
connections: {
app: {
...(pgEnvVars.user && { user: pgEnvVars.user }),
...(pgEnvVars.password && { password: pgEnvVars.password }),
}
}
}
};

// Merge: InsForge defaults -> env vars -> user overrides
const mergedOpts = deepmerge.all([
{ pg: INSFORGE_PG_DEFAULTS, db: INSFORGE_DEFAULTS },
envOverrides,
cn,
]) as GetConnectionOpts;

return getPgConnections(mergedOpts, seedAdapters);
};

// Re-export types
export type { GetConnectionOpts, GetConnectionResult };
30 changes: 30 additions & 0 deletions postgres/insforge-test/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PgTestClient } from 'pgsql-test';

/**
* Helper function to insert a new user into auth.users
* @param client - The PgTestClient to use (pg or db)
* @param email - The user's email address
* @param id - Optional user ID (UUID). If not provided, a random UUID will be generated.
* @returns The inserted user object with id and email
*/
export async function insertUser(
client: PgTestClient,
email: string,
id?: string
): Promise<{ id: string; email: string }> {
if (id) {
return await client.one(
`INSERT INTO auth.users (id, email)
VALUES ($1, $2)
RETURNING id, email`,
[id, email]
);
} else {
return await client.one(
`INSERT INTO auth.users (id, email)
VALUES (gen_random_uuid(), $1)
RETURNING id, email`,
[email]
);
}
}
12 changes: 12 additions & 0 deletions postgres/insforge-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Re-export everything from pgsql-test
export * from 'pgsql-test';

// Re-export everything from helpers.ts
export * from './helpers';

// Export InsForge-specific getConnections with defaults baked in
export { getConnections } from './connect';
export type { GetConnectionOpts, GetConnectionResult } from './connect';

// Re-export snapshot utility
export { snapshot } from 'pgsql-test';
1 change: 1 addition & 0 deletions postgres/insforge-test/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'pgsql-test/utils';
9 changes: 9 additions & 0 deletions postgres/insforge-test/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/esm",
"module": "es2022",
"rootDir": "src/",
"declaration": false
}
}
9 changes: 9 additions & 0 deletions postgres/insforge-test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src/"
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
}
Loading