Skip to content

Commit 055aa3f

Browse files
committed
feat: publish challenge Prisma client contract
1 parent df00093 commit 055aa3f

46 files changed

Lines changed: 57713 additions & 59550 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,34 @@ remain compatible. Database access uses Prisma 7 with the PostgreSQL driver
3737
adapter. Domain events continue to be sent through the existing Bus API wrapper;
3838
this service does not connect to Kafka directly.
3939

40-
The API runtime client is Prisma 7. The checked-in
41-
`packages/challenge-prisma-client` artifact remains on Prisma 6 for its existing
42-
downstream consumers, so the root generation command intentionally targets only
43-
the API client. Upgrading that shared artifact requires a coordinated downstream
44-
release.
40+
The API runtime client and the checked-in external client use Prisma 7. The
41+
external client has a stable package wrapper at
42+
`packages/challenge-prisma-client`; generated Prisma files live below its
43+
`generated` directory so regeneration cannot overwrite the public contract.
44+
45+
## External Prisma client
46+
47+
Services that need to aggregate challenge data directly can install the
48+
`packages/challenge-prisma-client` Git subdirectory as
49+
`@topcoder/challenge-api-v6`. The package exports all generated challenge
50+
models, enums, Prisma helpers, and `PrismaClient`, plus this supported factory:
51+
52+
```ts
53+
import { createChallengePrismaClient } from '@topcoder/challenge-api-v6';
54+
55+
const challenges = createChallengePrismaClient(process.env.CHALLENGE_DATABASE_URL, {
56+
log: ['warn', 'error'],
57+
});
58+
```
59+
60+
`createChallengePrismaClient(connectionString, options?)` creates a Prisma 7
61+
PostgreSQL driver adapter, preserves the optional `schema` query parameter in
62+
the connection URL, and returns a disconnected client that connects lazily on
63+
its first query. Call `$disconnect()` during application shutdown. The factory
64+
throws `TypeError` when `connectionString` is empty or not a string; Prisma may
65+
raise its normal configuration and database errors while creating or using the
66+
client. Connection-defining `adapter` and `accelerateUrl` options are owned by
67+
the factory and intentionally excluded from its options type.
4568

4669
## Configuration
4770

@@ -90,7 +113,7 @@ configuration parameters.
90113
Run `nvm use` before pnpm commands. Make sure `DATABASE_URL` is set before any
91114
database operation or application startup.
92115

93-
1. Install dependencies and generate the Prisma client: `pnpm install`
116+
1. Install dependencies and generate both Prisma clients: `pnpm install`
94117
2. Build the API: `pnpm build`
95118
3. Create or update local database tables: `pnpm create-tables`
96119
4. Seed tables: `pnpm seed-tables`
@@ -130,7 +153,8 @@ database operation or application startup.
130153
DATABASE_URL=
131154
```
132155

133-
Then run `pnpm install`. The postinstall hook generates the Prisma 7 client.
156+
Then run `pnpm install`. The postinstall hook generates both the internal and
157+
external Prisma 7 clients.
134158

135159
2. 🚢 Start docker-compose with services which are required to start Topcoder Challenges API locally
136160

@@ -239,6 +263,13 @@ To run unit tests alone
239263
pnpm test
240264
```
241265

266+
To smoke-test the packaged external Prisma factory without connecting to a
267+
database:
268+
269+
```bash
270+
pnpm test:external-client
271+
```
272+
242273
To run unit tests with coverage report
243274

244275
```bash

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
"main": "dist/main.js",
66
"packageManager": "pnpm@11.15.1",
77
"scripts": {
8+
"prebuild": "pnpm run prisma:generate",
89
"build": "nest build",
910
"start": "node dist/main.js",
1011
"start:dev": "nest start --watch",
1112
"start:debug": "nest start --debug --watch",
1213
"start:prod": "node dist/main.js",
1314
"postinstall": "pnpm run prisma:generate",
14-
"prisma:generate": "prisma generate --generator client",
15+
"prisma:generate": "prisma generate",
1516
"lint": "eslint --no-error-on-unmatched-pattern \"*.ts\" \"src/**/*.ts\" \"prisma/**/*.ts\" \"test/**/*.ts\"",
1617
"lint:fix": "pnpm lint --fix",
1718
"format": "prettier --write \"*.ts\" \"src/**/*.ts\" \"prisma/**/*.ts\" \"test/**/*.ts\"",
@@ -21,6 +22,7 @@
2122
"recalculate-winners": "node --require ts-node/register/transpile-only data-migration/src/scripts/recalculateChallengeWinners.js",
2223
"recalculate-winners:csv": "node --require ts-node/register/transpile-only data-migration/src/scripts/recalculateChallengeWinners.js --csv-only",
2324
"test": "NODE_ENV=test mocha --require ts-node/register/transpile-only \"test/unit/**/*.test.{js,ts}\" --exit",
25+
"test:external-client": "node --test packages/challenge-prisma-client/factory.test.js",
2426
"e2e": "NODE_ENV=test mocha --require ts-node/register/transpile-only \"test/e2e/*.test.{js,ts}\" --exit",
2527
"test:cov": "nyc --reporter=html --reporter=text pnpm test",
2628
"e2e:cov": "nyc --reporter=html --reporter=text pnpm e2e",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `@topcoder/challenge-api-v6`
2+
3+
This package is the supported external Prisma client for the Challenge API v6
4+
schema. It re-exports the generated Prisma surface and provides
5+
`createChallengePrismaClient(connectionString, options?)`, which configures the
6+
Prisma 7 PostgreSQL driver adapter and honors the connection URL's optional
7+
`schema` query parameter.
8+
9+
```ts
10+
import { createChallengePrismaClient } from '@topcoder/challenge-api-v6';
11+
12+
const client = createChallengePrismaClient(process.env.CHALLENGE_DATABASE_URL);
13+
const activeCount = await client.challenge.count({
14+
where: { status: 'ACTIVE' },
15+
});
16+
await client.$disconnect();
17+
```
18+
19+
The client connects lazily. Applications own its lifecycle and must disconnect
20+
it during shutdown. An empty or non-string connection URL raises `TypeError`;
21+
Prisma reports its normal configuration and database errors during creation or
22+
query execution.

0 commit comments

Comments
 (0)