|
1 | | ---- |
2 | | -title: CLI Database |
3 | | -description: | |
4 | | - How spraxium database scaffolds a complete ORM integration module in seconds. |
5 | | - Choose your ORM and database engine through the interactive wizard, then start writing queries immediately. |
6 | | -order: 5 |
7 | | -category: CLI |
8 | | ---- |
9 | | - |
10 | | -## What it does |
11 | | - |
12 | | -`spraxium database` (alias `spraxium db`) generates a fully wired database module inside your project. It walks you through an interactive wizard where you select an ORM and a database engine, then scaffolds the module files, patches your environment config with the required keys, installs the necessary packages, and optionally registers the module in your *AppModule*. By the end of the wizard your project is ready to run queries with no manual configuration. |
13 | | - |
14 | | -The command accepts an optional ORM argument to skip the first selection step. When you already know what you want, `spraxium database prisma` jumps directly to the database engine prompt. |
15 | | - |
16 | | -## When to use |
17 | | - |
18 | | -Use this command at the beginning of a project or when adding database access to an existing Spraxium application. It is the equivalent of manually installing packages, writing connection boilerplate, and registering the module, all collapsed into a single interactive flow. Run it once per database integration; for schema changes or migrations, use the ORM tooling directly after setup. |
19 | | - |
20 | | -## Supported ORMs |
21 | | - |
22 | | -<DropdownGroup> |
23 | | - <Dropdown title="Prisma, schema-first with auto-generated client"> |
24 | | - Prisma is the best choice when you want a strongly typed query client generated from a declarative schema file. It supports PostgreSQL, MySQL, and SQLite out of the box. After the wizard runs, define your models in `prisma/schema.prisma` and run `npx prisma migrate dev` to apply changes. Prisma is ideal for teams that prefer schema-as-source-of-truth workflows, readable migration history, and full TypeScript type safety from database to application layer. |
25 | | - </Dropdown> |
26 | | - <Dropdown title="Drizzle, lightweight SQL-native TypeScript ORM"> |
27 | | - Drizzle is suited for projects that value small bundle size, direct SQL control, and schema defined in TypeScript rather than a separate file. It supports PostgreSQL, MySQL, and SQLite. The generated *DatabaseService* exposes a `.getDb()` method that returns the Drizzle query builder, ready to use. Choose Drizzle when you want minimal abstraction with full TypeScript inference and prefer defining tables as TypeScript objects over `.prisma` syntax. |
28 | | - </Dropdown> |
29 | | - <Dropdown title="TypeORM, decorator-based with Active Record or Data Mapper"> |
30 | | - TypeORM is the established choice for teams coming from NestJS or Spring backgrounds who are comfortable with class decorators such as *@Entity*, *@Column*, and *@Repository*. It supports PostgreSQL, MySQL, and SQLite. Use TypeORM when your team prefers entity classes as the source of truth and wants a familiar pattern for migrations and relation management. |
31 | | - </Dropdown> |
32 | | - <Dropdown title="Mongoose, document model for MongoDB"> |
33 | | - Mongoose is the right pick when your data is document-oriented and MongoDB is your database. It has a single database target, so the wizard skips the engine selection step. The generated module includes a schema definition and a service that wraps the Mongoose model. Choose Mongoose for applications that need flexible schemas, embedded documents, or MongoDB-specific query operators. |
34 | | - </Dropdown> |
35 | | - <Dropdown title="Mikro-ORM, versatile ORM with Unit of Work pattern"> |
36 | | - Mikro-ORM is a strong choice for advanced use cases that benefit from the Unit of Work pattern and Identity Map. It supports PostgreSQL, MySQL, SQLite, and MongoDB. Mikro-ORM is well suited for complex domain models with deep relation graphs, where controlled change tracking across a request lifecycle matters more than simplicity of setup. |
37 | | - </Dropdown> |
38 | | -</DropdownGroup> |
39 | | - |
40 | | -## What the wizard does |
41 | | - |
42 | | -<Steps> |
43 | | - <Step title="ORM selection"> |
44 | | - If you did not pass an ORM as a CLI argument, the wizard presents an interactive list of the five supported ORMs with a short description for each. Navigate with the arrow keys and press Enter to confirm. |
45 | | - </Step> |
46 | | - <Step title="Database engine selection"> |
47 | | - For ORMs that support multiple engines (Prisma, Drizzle, TypeORM, Mikro-ORM), the wizard asks which engine to target. Mongoose skips this step because it only targets MongoDB. |
48 | | - </Step> |
49 | | - <Step title="Module name"> |
50 | | - The wizard prompts for a module name with `database` as the default. The name is converted to kebab-case for the folder and PascalCase for the class names. A name like `user-db` produces a `UserDbModule` and a `src/modules/user-db/` directory. |
51 | | - </Step> |
52 | | - <Step title="File scaffolding"> |
53 | | - The CLI downloads the template for your ORM and engine combination and writes the module, service, schema, and config files into `src/modules/{name}/`. A root-level config file (such as `drizzle.config.ts` or `prisma/schema.prisma`) is also added if the ORM requires it. |
54 | | - </Step> |
55 | | - <Step title="Environment key injection"> |
56 | | - The required environment variables (`DATABASE_URL`, `DATABASE_PATH`, etc.) are appended to your `app.env.ts` automatically if they are not already present. No manual editing needed. |
57 | | - </Step> |
58 | | - <Step title="Package installation"> |
59 | | - The wizard asks if you want to install packages immediately. Confirming runs the detected package manager to add runtime dependencies (such as the ORM client and database driver) and dev dependencies (CLI tools, type definitions). Post-install commands like `npx prisma generate` are also run automatically. |
60 | | - </Step> |
61 | | - <Step title="AppModule registration"> |
62 | | - If an `app.module.ts` is found in your `src/` directory, the wizard offers to add the generated module to the *imports* array automatically. This connects the module to the DI container so you can inject the *DatabaseService* anywhere in the application. |
63 | | - </Step> |
64 | | -</Steps> |
65 | | - |
66 | | -## Generated file structure |
67 | | - |
68 | | -The exact files vary by ORM but the layout follows the same convention for every combination. |
69 | | - |
70 | | -<FolderStructure> |
71 | | - <Folder name="src/modules/database/"> |
72 | | - <File name="database.module.ts" description="Module class with @Module decorator and provider exports" /> |
73 | | - <File name="database.service.ts" description="Injectable service exposing the ORM client or query builder" /> |
74 | | - <File name="database.schema.ts" description="Schema or entity definition (present for Drizzle, Mongoose, Mikro-ORM)" /> |
75 | | - </Folder> |
76 | | -</FolderStructure> |
77 | | - |
78 | | -<Callout tone="info" title="Root-level config files are also created"> |
79 | | - Some ORMs require a configuration file at the project root. Prisma generates a prisma/ directory with schema.prisma. Drizzle generates drizzle.config.ts. These files are written alongside the module files and should be committed to version control. |
80 | | -</Callout> |
81 | | - |
82 | | -## After the wizard |
83 | | - |
84 | | -Once the command finishes, you can inject *DatabaseService* into any handler, guard, or service within the same module scope. The service is exported from the database module, so importing *DatabaseModule* in any other module gives you access to it through the DI container. |
85 | | - |
86 | | -```typescript filename="src/modules/hello/hello.service.ts" |
87 | | -import { Injectable } from '@spraxium/common'; |
88 | | -import { DatabaseService } from '../database/database.service'; |
89 | | -
|
90 | | -@Injectable() |
91 | | -export class HelloService { |
92 | | - constructor(private readonly db: DatabaseService) {} |
93 | | -
|
94 | | - async getCount(): Promise<number> { |
95 | | - // Use the ORM client returned by the service |
96 | | - return this.db.getDb().select().from(table).then((rows) => rows.length); |
97 | | - } |
98 | | -} |
99 | | -``` |
100 | | - |
101 | | -## Next page |
102 | | - |
103 | | -Continue with [CLI Info](/guide/cli-info). |
| 1 | +--- |
| 2 | +title: CLI Database |
| 3 | +description: | |
| 4 | + How spraxium database scaffolds a complete ORM integration module in seconds. |
| 5 | + Choose your ORM and database engine through the interactive wizard, then start writing queries immediately. |
| 6 | +order: 5 |
| 7 | +category: CLI |
| 8 | +--- |
| 9 | + |
| 10 | +## What it does |
| 11 | + |
| 12 | +`spraxium database` (alias `spraxium db`) generates a fully wired database module inside your project. It walks you through an interactive wizard where you select an ORM and a database engine, then scaffolds the module files, patches your environment config with the required keys, installs the necessary packages, and optionally registers the module in your *AppModule*. By the end of the wizard your project is ready to run queries with no manual configuration. |
| 13 | + |
| 14 | +The command accepts an optional ORM argument to skip the first selection step. When you already know what you want, `spraxium database prisma` jumps directly to the database engine prompt. |
| 15 | + |
| 16 | +## When to use |
| 17 | + |
| 18 | +Use this command at the beginning of a project or when adding database access to an existing Spraxium application. It is the equivalent of manually installing packages, writing connection boilerplate, and registering the module, all collapsed into a single interactive flow. Run it once per database integration; for schema changes or migrations, use the ORM tooling directly after setup. |
| 19 | + |
| 20 | +## Supported ORMs |
| 21 | + |
| 22 | +<DropdownGroup> |
| 23 | + <Dropdown title="Prisma, schema-first with auto-generated client"> |
| 24 | + Prisma is the best choice when you want a strongly typed query client generated from a declarative schema file. It supports PostgreSQL, MySQL, and SQLite out of the box. After the wizard runs, define your models in `prisma/schema.prisma` and run `npx prisma migrate dev` to apply changes. Prisma is ideal for teams that prefer schema-as-source-of-truth workflows, readable migration history, and full TypeScript type safety from database to application layer. |
| 25 | + </Dropdown> |
| 26 | + <Dropdown title="Drizzle, lightweight SQL-native TypeScript ORM"> |
| 27 | + Drizzle is suited for projects that value small bundle size, direct SQL control, and schema defined in TypeScript rather than a separate file. It supports PostgreSQL, MySQL, and SQLite. The generated *DatabaseService* exposes a `.getDb()` method that returns the Drizzle query builder, ready to use. Choose Drizzle when you want minimal abstraction with full TypeScript inference and prefer defining tables as TypeScript objects over `.prisma` syntax. |
| 28 | + </Dropdown> |
| 29 | + <Dropdown title="TypeORM, decorator-based with Active Record or Data Mapper"> |
| 30 | + TypeORM is the established choice for teams coming from NestJS or Spring backgrounds who are comfortable with class decorators such as *@Entity*, *@Column*, and *@Repository*. It supports PostgreSQL, MySQL, and SQLite. Use TypeORM when your team prefers entity classes as the source of truth and wants a familiar pattern for migrations and relation management. |
| 31 | + </Dropdown> |
| 32 | + <Dropdown title="Mongoose, document model for MongoDB"> |
| 33 | + Mongoose is the right pick when your data is document-oriented and MongoDB is your database. It has a single database target, so the wizard skips the engine selection step. The generated module includes a schema definition and a service that wraps the Mongoose model. Choose Mongoose for applications that need flexible schemas, embedded documents, or MongoDB-specific query operators. |
| 34 | + </Dropdown> |
| 35 | + <Dropdown title="Mikro-ORM, versatile ORM with Unit of Work pattern"> |
| 36 | + Mikro-ORM is a strong choice for advanced use cases that benefit from the Unit of Work pattern and Identity Map. It supports PostgreSQL, MySQL, SQLite, and MongoDB. Mikro-ORM is well suited for complex domain models with deep relation graphs, where controlled change tracking across a request lifecycle matters more than simplicity of setup. |
| 37 | + </Dropdown> |
| 38 | +</DropdownGroup> |
| 39 | + |
| 40 | +## What the wizard does |
| 41 | + |
| 42 | +<Steps> |
| 43 | + <Step title="ORM selection"> |
| 44 | + If you did not pass an ORM as a CLI argument, the wizard presents an interactive list of the five supported ORMs with a short description for each. Navigate with the arrow keys and press Enter to confirm. |
| 45 | + </Step> |
| 46 | + <Step title="Database engine selection"> |
| 47 | + For ORMs that support multiple engines (Prisma, Drizzle, TypeORM, Mikro-ORM), the wizard asks which engine to target. Mongoose skips this step because it only targets MongoDB. |
| 48 | + </Step> |
| 49 | + <Step title="Module name"> |
| 50 | + The wizard prompts for a module name with `database` as the default. The name is converted to kebab-case for the folder and PascalCase for the class names. A name like `user-db` produces a `UserDbModule` and a `src/modules/user-db/` directory. |
| 51 | + </Step> |
| 52 | + <Step title="File scaffolding"> |
| 53 | + The CLI downloads the template for your ORM and engine combination and writes the module, service, schema, and config files into `src/modules/{name}/`. A root-level config file (such as `drizzle.config.ts` or `prisma/schema.prisma`) is also added if the ORM requires it. |
| 54 | + </Step> |
| 55 | + <Step title="Environment key injection"> |
| 56 | + The required environment variables (`DATABASE_URL`, `DATABASE_PATH`, etc.) are appended to your `app.env.ts` automatically if they are not already present. No manual editing needed. |
| 57 | + </Step> |
| 58 | + <Step title="Package installation"> |
| 59 | + The wizard asks if you want to install packages immediately. Confirming runs the detected package manager to add runtime dependencies (such as the ORM client and database driver) and dev dependencies (CLI tools, type definitions). Post-install commands like `npx prisma generate` are also run automatically. |
| 60 | + </Step> |
| 61 | + <Step title="AppModule registration"> |
| 62 | + If an `app.module.ts` is found in your `src/` directory, the wizard offers to add the generated module to the *imports* array automatically. This connects the module to the DI container so you can inject the *DatabaseService* anywhere in the application. |
| 63 | + </Step> |
| 64 | +</Steps> |
| 65 | + |
| 66 | +## Generated file structure |
| 67 | + |
| 68 | +The exact files vary by ORM but the layout follows the same convention for every combination. |
| 69 | + |
| 70 | +<FolderStructure> |
| 71 | + <Folder name="src/modules/database/"> |
| 72 | + <File name="database.module.ts" description="Module class with @Module decorator and provider exports" /> |
| 73 | + <File name="database.service.ts" description="Injectable service exposing the ORM client or query builder" /> |
| 74 | + <File name="database.schema.ts" description="Schema or entity definition (present for Drizzle, Mongoose, Mikro-ORM)" /> |
| 75 | + </Folder> |
| 76 | +</FolderStructure> |
| 77 | + |
| 78 | +<Callout tone="info" title="Root-level config files are also created"> |
| 79 | + Some ORMs require a configuration file at the project root. Prisma generates a prisma/ directory with schema.prisma. Drizzle generates drizzle.config.ts. These files are written alongside the module files and should be committed to version control. |
| 80 | +</Callout> |
| 81 | + |
| 82 | +## After the wizard |
| 83 | + |
| 84 | +Once the command finishes, you can inject *DatabaseService* into any handler, guard, or service within the same module scope. The service is exported from the database module, so importing *DatabaseModule* in any other module gives you access to it through the DI container. |
| 85 | + |
| 86 | +```typescript filename="src/modules/hello/hello.service.ts" |
| 87 | +import { Injectable } from '@spraxium/common'; |
| 88 | +import { DatabaseService } from '../database/database.service'; |
| 89 | +
|
| 90 | +@Injectable() |
| 91 | +export class HelloService { |
| 92 | + constructor(private readonly db: DatabaseService) {} |
| 93 | +
|
| 94 | + async getCount(): Promise<number> { |
| 95 | + // Use the ORM client returned by the service |
| 96 | + return this.db |
| 97 | + .getDb() |
| 98 | + .select() |
| 99 | + .from(table) |
| 100 | + .then((rows) => rows.length); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Next page |
| 106 | + |
| 107 | +Continue with [CLI Info](/guide/cli-info). |
0 commit comments