Skip to content

Commit 579c406

Browse files
📝 Add docstrings to dev
Docstrings generation was requested by @ymc9. * #406 (comment) The following files were modified: * `packages/cli/src/actions/action-utils.ts` * `packages/cli/src/actions/db.ts` * `packages/cli/src/actions/migrate.ts` * `packages/cli/src/utils/exec-utils.ts` * `packages/testtools/src/client.ts` * `tests/e2e/scripts/generate.ts`
1 parent ed931da commit 579c406

File tree

6 files changed

+107
-9
lines changed

6 files changed

+107
-9
lines changed

packages/cli/src/actions/action-utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import fs from 'node:fs';
66
import path from 'node:path';
77
import { CliError } from '../cli-error';
88

9+
/**
10+
* Resolve and return the filesystem path to the ZenStack schema file to use.
11+
*
12+
* If `file` is provided, it is validated and returned. Otherwise the function looks for a schema configured in the nearest package.json `zenstack.schema`; if that path is a directory, it looks for `schema.zmodel` inside it. If no package.json configuration is found, the function checks the default locations `./zenstack/schema.zmodel` and `./schema.zmodel`.
13+
*
14+
* @param file - Optional explicit path to a schema file or directory to use
15+
* @returns The resolved path to the schema file to load
16+
* @throws {CliError} If the resolved schema file (or expected schema inside a configured directory) does not exist
17+
*/
918
export function getSchemaFile(file?: string) {
1019
if (file) {
1120
if (!fs.existsSync(file)) {
@@ -123,4 +132,4 @@ function findUp<Multiple extends boolean = false>(
123132
return (multiple && result.length > 0 ? result : undefined) as FindUpResult<Multiple>;
124133
}
125134
return findUp(names, up, multiple, result);
126-
}
135+
}

packages/cli/src/actions/db.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ export async function run(command: string, options: Options) {
1919
}
2020
}
2121

22+
/**
23+
* Pushes the Prisma schema to the database using a temporary schema file and removes the temporary file when finished.
24+
*
25+
* Generates a temporary Prisma schema from the provided base schema, runs `prisma db push` with optional flags, and ensures the temporary file is deleted regardless of success or failure. Subprocess errors from the Prisma CLI are handled internally.
26+
*
27+
* @param options - Configuration for the push:
28+
* - `schema`: path to the base Prisma schema to use when generating the temporary schema (optional).
29+
* - `acceptDataLoss`: include `--accept-data-loss` when pushing to the database (optional).
30+
* - `forceReset`: include `--force-reset` when pushing to the database (optional).
31+
*/
2232
async function runPush(options: Options) {
2333
// generate a temp prisma schema file
2434
const schemaFile = getSchemaFile(options.schema);
@@ -44,4 +54,4 @@ async function runPush(options: Options) {
4454
fs.unlinkSync(prismaSchemaFile);
4555
}
4656
}
47-
}
57+
}

packages/cli/src/actions/migrate.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ type ResolveOptions = CommonOptions & {
2828
};
2929

3030
/**
31-
* CLI action for migration-related commands
31+
* Run a migration-related CLI command using a temporary Prisma schema and ensure the temporary schema file is removed.
32+
*
33+
* @param command - The migration command to run: 'dev', 'reset', 'deploy', 'status', or 'resolve'
34+
* @param options - Common options that may include `schema` (path to a Prisma schema) and `migrations` (path to the migrations directory); additional command-specific options are accepted for certain commands
3235
*/
3336
export async function run(command: string, options: CommonOptions) {
3437
const schemaFile = getSchemaFile(options.schema);
@@ -64,6 +67,14 @@ export async function run(command: string, options: CommonOptions) {
6467
}
6568
}
6669

70+
/**
71+
* Run Prisma Migrate in development mode using the given schema and options.
72+
*
73+
* Invokes the CLI command equivalent to `migrate dev` with `--schema` and `--skip-generate`, and adds `--name` and/or `--create-only` when provided in `options`.
74+
*
75+
* @param prismaSchemaFile - Path to the Prisma schema file to use for the migrate command.
76+
* @param options - Options controlling migrate behavior (may include `name` and `createOnly`).
77+
*/
6778
function runDev(prismaSchemaFile: string, options: DevOptions) {
6879
try {
6980
const cmd = [
@@ -79,6 +90,12 @@ function runDev(prismaSchemaFile: string, options: DevOptions) {
7990
}
8091
}
8192

93+
/**
94+
* Runs `prisma migrate reset` against the provided Prisma schema file.
95+
*
96+
* @param prismaSchemaFile - Path to the Prisma schema file to target
97+
* @param options - Reset options; if `options.force` is `true`, the reset proceeds without interactive confirmation
98+
*/
8299
function runReset(prismaSchemaFile: string, options: ResetOptions) {
83100
try {
84101
const cmd = [
@@ -93,6 +110,11 @@ function runReset(prismaSchemaFile: string, options: ResetOptions) {
93110
}
94111
}
95112

113+
/**
114+
* Executes a Prisma Migrate deploy using the specified Prisma schema file.
115+
*
116+
* @param prismaSchemaFile - Path to the Prisma schema file to use for the deploy command
117+
*/
96118
function runDeploy(prismaSchemaFile: string, _options: DeployOptions) {
97119
try {
98120
const cmd = ['migrate deploy', ` --schema "${prismaSchemaFile}"`].join('');
@@ -102,6 +124,13 @@ function runDeploy(prismaSchemaFile: string, _options: DeployOptions) {
102124
}
103125
}
104126

127+
/**
128+
* Show the current status of database migrations for the given Prisma schema.
129+
*
130+
* Runs the `migrate status` command against the provided schema file. Subprocess failures are handled by the module's subprocess error handler.
131+
*
132+
* @param prismaSchemaFile - Path to the Prisma schema file to use for the status check
133+
*/
105134
function runStatus(prismaSchemaFile: string, _options: StatusOptions) {
106135
try {
107136
execPrisma(`migrate status --schema "${prismaSchemaFile}"`);
@@ -110,6 +139,13 @@ function runStatus(prismaSchemaFile: string, _options: StatusOptions) {
110139
}
111140
}
112141

142+
/**
143+
* Resolve migration status for specified migration names against a Prisma schema.
144+
*
145+
* @param prismaSchemaFile - Path to the Prisma schema file to use for the migrate command
146+
* @param options - Resolve options; include `applied` to mark a migration as applied and/or `rolledBack` to mark a migration as rolled back
147+
* @throws CliError - If neither `applied` nor `rolledBack` is provided on `options`
148+
*/
113149
function runResolve(prismaSchemaFile: string, options: ResolveOptions) {
114150
if (!options.applied && !options.rolledBack) {
115151
throw new CliError('Either --applied or --rolled-back option must be provided');
@@ -134,4 +170,4 @@ function handleSubProcessError(err: unknown) {
134170
} else {
135171
process.exit(1);
136172
}
137-
}
173+
}

packages/cli/src/utils/exec-utils.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export function execSync(cmd: string, options?: Omit<ExecSyncOptions, 'env'> & {
1616
}
1717

1818
/**
19-
* Utility for running package commands through npx/bunx
19+
* Run a package-manager command using `bunx` when running on Bun, otherwise `npx`.
20+
*
21+
* @param cmd - The package command and its arguments (e.g. `"install foo"` or `"run build"`).
22+
* @param options - Additional child_process.execSync options; `env` may be provided to override or extend the environment.
2023
*/
2124
export function execPackage(
2225
cmd: string,
@@ -27,7 +30,12 @@ export function execPackage(
2730
}
2831

2932
/**
30-
* Utility for running prisma commands
33+
* Execute the Prisma CLI with the provided command-line arguments.
34+
*
35+
* Resolves the installed Prisma binary and runs it via `node` with the given `args`.
36+
*
37+
* @param args - Command-line arguments to pass to the Prisma CLI (e.g., `"migrate deploy"`).
38+
* @param options - Options forwarded to the underlying `execSync`. If `options.env` is provided, its entries are merged with `process.env`.
3139
*/
3240
export function execPrisma(args: string, options?: Omit<ExecSyncOptions, 'env'> & { env?: Record<string, string> }) {
3341
let prismaPath: string;
@@ -39,4 +47,4 @@ export function execPrisma(args: string, options?: Omit<ExecSyncOptions, 'env'>
3947
prismaPath = require.resolve('prisma/build/index.js');
4048
}
4149
execSync(`node ${prismaPath} ${args}`, options);
42-
}
50+
}

packages/testtools/src/client.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ export async function createTestClient<Schema extends SchemaDef>(
9292
schema: string,
9393
options?: CreateTestClientOptions<Schema>,
9494
): Promise<any>;
95+
/**
96+
* Creates and configures a test ZenStackClient using the provided schema and options.
97+
*
98+
* This function prepares a working directory and datasource, optionally creates or resets
99+
* the test database (SQLite file or PostgreSQL database), may write schema files, copy
100+
* additional files into the work directory, and applies any provided client plugins.
101+
*
102+
* @param schema - A schema definition object or a ZModel schema string. When a string is provided,
103+
* a TypeScript schema is generated into a temporary work directory.
104+
* @param options - Configuration for test client creation. Important options:
105+
* - provider: 'sqlite' | 'postgresql' (selects the database provider)
106+
* - schemaFile: path to an existing .zmodel file to copy/adjust into the workDir
107+
* - dbName: explicit database name (auto-generated when omitted)
108+
* - usePrismaPush: when true, runs `prisma db push` using a generated Prisma schema
109+
* - extraSourceFiles: additional source files to include when generating a TS schema
110+
* - workDir: directory to use for generated files and database (created if omitted)
111+
* - debug: when true, enables test logging and prints the work directory
112+
* - dbFile: path to an existing SQLite database file to copy into the workDir (sqlite only)
113+
* - dataSourceExtensions: list of datasource extensions to include in generated schema
114+
* - copyFiles: array of { globPattern, destination } entries whose matched files are copied
115+
* (matched relative to the current test file) into the workDir
116+
* - plugins: array of PolicyPlugin or other client plugins to apply to the returned client
117+
* @returns The configured ZenStackClient instance for the prepared test schema and datasource.
118+
*/
95119
export async function createTestClient<Schema extends SchemaDef>(
96120
schema: Schema | string,
97121
options?: CreateTestClientOptions<Schema>,
@@ -288,4 +312,4 @@ function getTestDbName(provider: string) {
288312
.substring(0, 30) +
289313
digest.slice(0, 6)
290314
);
291-
}
315+
}

tests/e2e/scripts/generate.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { fileURLToPath } from 'node:url';
77

88
const dir = path.dirname(fileURLToPath(import.meta.url));
99

10+
/**
11+
* Discovers project .zmodel schema files and generates TypeScript schemas for each.
12+
*
13+
* Searches the repository's ORM and app schema locations for `.zmodel` files, logs each file being processed, and invokes `generate` for every discovered file.
14+
*/
1015
async function main() {
1116
const zmodelFiles = [
1217
...glob.sync(path.resolve(dir, '../orm/schemas/**/*.zmodel')),
@@ -18,6 +23,12 @@ async function main() {
1823
}
1924
}
2025

26+
/**
27+
* Generates TypeScript schema files from a .zmodel file and writes them to the schema's directory.
28+
*
29+
* @param schemaPath - Filesystem path to the source `.zmodel` schema file
30+
* @throws Error if the schema document fails to load; the error message includes the schema path and load errors
31+
*/
2132
async function generate(schemaPath: string) {
2233
const generator = new TsSchemaGenerator();
2334
const outDir = path.dirname(schemaPath);
@@ -35,4 +46,4 @@ async function generate(schemaPath: string) {
3546
await generator.generate(result.model as Model, { outDir });
3647
}
3748

38-
main();
49+
main();

0 commit comments

Comments
 (0)