add mongoose orm to the stack builder (#191)

This commit is contained in:
José Alberto Gómez García
2025-05-02 15:23:04 +02:00
committed by GitHub
parent 946f3eb421
commit 437cf9a45a
15 changed files with 524 additions and 273 deletions

View File

@@ -40,6 +40,8 @@ export const dependencyVersionMap = {
"@prisma/client": "^6.7.0",
prisma: "^6.7.0",
mongoose: "^8.14.0",
"vite-plugin-pwa": "^0.21.2",
"@vite-pwa/assets-generator": "^0.2.6",

View File

@@ -62,6 +62,12 @@ export async function setupDatabase(config: ProjectConfig): Promise<void> {
projectDir: serverDir,
});
}
} else if (orm === "mongoose") {
await addPackageDependency({
dependencies: ["mongoose"],
devDependencies: [],
projectDir: serverDir,
});
}
if (database === "sqlite" && dbSetup === "turso") {

View File

@@ -367,6 +367,11 @@ export async function setupAuthTemplate(
PKG_ROOT,
`templates/auth/server/db/prisma/${db}`,
);
} else if (orm === "mongoose") {
authDbSrc = path.join(
PKG_ROOT,
`templates/auth/server/db/mongoose/${db}`,
)
}
if (authDbSrc && (await fs.pathExists(authDbSrc))) {
await processAndCopyFiles("**/*", authDbSrc, serverAppDir, context);

View File

@@ -60,7 +60,7 @@ async function main() {
.option("orm", {
type: "string",
describe: "ORM type",
choices: ["drizzle", "prisma", "none"],
choices: ["drizzle", "prisma", "mongoose", "none"],
})
.option("auth", {
type: "boolean",
@@ -446,78 +446,82 @@ function processAndValidateFlags(
config.dbSetup = "none";
}
if (effectiveDatabase === "mongodb" && effectiveOrm === "drizzle") {
if (effectiveDatabase === "mongodb" && effectiveOrm === "drizzle") {
consola.fatal(
"Drizzle ORM is not compatible with MongoDB. Please use --orm prisma or --orm mongoose.",
);
process.exit(1);
}
if (
effectiveOrm === "mongoose" &&
effectiveDatabase &&
effectiveDatabase !== "mongodb"
) {
consola.fatal(
`Mongoose ORM requires MongoDB. Cannot use --orm mongoose with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
if (config.dbSetup && config.dbSetup !== "none") {
const dbSetup = config.dbSetup;
if (!effectiveDatabase || effectiveDatabase === "none") {
consola.fatal(
"MongoDB is only available with Prisma. Cannot use --database mongodb with --orm drizzle",
`Database setup '--db-setup ${dbSetup}' requires a database. Cannot use when database is 'none'.`,
);
process.exit(1);
}
if (config.dbSetup && config.dbSetup !== "none") {
const dbSetup = config.dbSetup;
if (dbSetup === "turso") {
if (effectiveDatabase && effectiveDatabase !== "sqlite") {
consola.fatal(
`Turso setup requires SQLite. Cannot use --db-setup turso with --database ${effectiveDatabase}`,
);
process.exit(1);
}
if (effectiveOrm === "prisma") {
consola.fatal(
"Turso setup is not compatible with Prisma. Cannot use --db-setup turso with --orm prisma",
);
process.exit(1);
}
config.database = "sqlite";
config.orm = "drizzle";
} else if (dbSetup === "prisma-postgres") {
if (effectiveDatabase && effectiveDatabase !== "postgres") {
consola.fatal(
`Prisma PostgreSQL setup requires PostgreSQL. Cannot use --db-setup prisma-postgres with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
if (
effectiveOrm &&
effectiveOrm !== "prisma" &&
effectiveOrm !== "none"
) {
consola.fatal(
`Prisma PostgreSQL setup requires Prisma ORM. Cannot use --db-setup prisma-postgres with --orm ${effectiveOrm}.`,
);
process.exit(1);
}
config.database = "postgres";
config.orm = "prisma";
} else if (dbSetup === "mongodb-atlas") {
if (effectiveDatabase && effectiveDatabase !== "mongodb") {
consola.fatal(
`MongoDB Atlas setup requires MongoDB. Cannot use --db-setup mongodb-atlas with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
if (
effectiveOrm &&
effectiveOrm !== "prisma" &&
effectiveOrm !== "none"
) {
consola.fatal(
`MongoDB Atlas setup requires Prisma ORM. Cannot use --db-setup mongodb-atlas with --orm ${effectiveOrm}.`,
);
process.exit(1);
}
config.database = "mongodb";
config.orm = "prisma";
} else if (dbSetup === "neon") {
if (effectiveDatabase && effectiveDatabase !== "postgres") {
consola.fatal(
`Neon PostgreSQL setup requires PostgreSQL. Cannot use --db-setup neon with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
config.database = "postgres";
if (dbSetup === "turso") {
if (effectiveDatabase && effectiveDatabase !== "sqlite") {
consola.fatal(
`Turso setup requires SQLite. Cannot use --db-setup turso with --database ${effectiveDatabase}`,
);
process.exit(1);
}
if (effectiveOrm !== "drizzle") {
consola.fatal(
`Turso setup requires Drizzle ORM. Cannot use --db-setup turso with --orm ${effectiveOrm ?? "none"}.`,
);
process.exit(1);
}
} else if (dbSetup === "prisma-postgres") {
if (effectiveDatabase !== "postgres") {
consola.fatal(
`Prisma PostgreSQL setup requires PostgreSQL. Cannot use --db-setup prisma-postgres with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
if (effectiveOrm !== "prisma") {
consola.fatal(
`Prisma PostgreSQL setup requires Prisma ORM. Cannot use --db-setup prisma-postgres with --orm ${effectiveOrm}.`,
);
process.exit(1);
}
} else if (dbSetup === "mongodb-atlas") {
if (effectiveDatabase !== "mongodb") {
consola.fatal(
`MongoDB Atlas setup requires MongoDB. Cannot use --db-setup mongodb-atlas with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
if (effectiveOrm !== "prisma" && effectiveOrm !== "mongoose") {
consola.fatal(
`MongoDB Atlas setup requires Prisma or Mongoose ORM. Cannot use --db-setup mongodb-atlas with --orm ${effectiveOrm}.`,
);
process.exit(1);
}
} else if (dbSetup === "neon") {
if (effectiveDatabase !== "postgres") {
consola.fatal(
`Neon PostgreSQL setup requires PostgreSQL. Cannot use --db-setup neon with --database ${effectiveDatabase}.`,
);
process.exit(1);
}
}
}
const includesNuxt = effectiveFrontend?.includes("nuxt");
const includesSvelte = effectiveFrontend?.includes("svelte");
@@ -576,13 +580,13 @@ function processAndValidateFlags(
process.exit(1);
}
if (config.addons.includes("husky") && !config.addons.includes("biome")) {
consola.warn(
"Husky addon is recommended to be used with Biome for lint-staged configuration.",
);
}
config.addons = [...new Set(config.addons)];
if (config.addons.includes("husky") && !config.addons.includes("biome")) {
consola.warn(
"Husky addon is recommended to be used with Biome for lint-staged configuration.",
);
}
config.addons = [...new Set(config.addons)];
}
const onlyNativeFrontend =
effectiveFrontend &&

View File

@@ -3,6 +3,24 @@ import pc from "picocolors";
import { DEFAULT_CONFIG } from "../constants";
import type { ProjectBackend, ProjectDatabase, ProjectOrm } from "../types";
const ormOptions = {
prisma: {
value: "prisma" as const,
label: "Prisma",
hint: "Powerful, feature-rich ORM",
},
mongoose: {
value: "mongoose" as const,
label: "Mongoose",
hint: "Elegant object modeling tool",
},
drizzle: {
value: "drizzle" as const,
label: "Drizzle",
hint: "Lightweight and performant TypeScript ORM",
},
};
export async function getORMChoice(
orm: ProjectOrm | undefined,
hasDatabase: boolean,
@@ -16,26 +34,16 @@ export async function getORMChoice(
if (!hasDatabase) return "none";
if (orm !== undefined) return orm;
if (database === "mongodb") {
log.info("Only Prisma is supported with MongoDB.");
return "prisma";
}
const options = [
...(database === "mongodb"
? [ormOptions.prisma, ormOptions.mongoose]
: [ormOptions.drizzle, ormOptions.prisma]),
];
const response = await select<ProjectOrm>({
message: "Select ORM",
options: [
{
value: "drizzle",
label: "Drizzle",
hint: "lightweight and performant TypeScript ORM",
},
{
value: "prisma",
label: "Prisma",
hint: "Powerful, feature-rich ORM",
},
],
initialValue: DEFAULT_CONFIG.orm,
options,
initialValue: database === "mongodb" ? "prisma" : DEFAULT_CONFIG.orm,
});
if (isCancel(response)) {

View File

@@ -4,7 +4,7 @@ export type ProjectDatabase =
| "mongodb"
| "mysql"
| "none";
export type ProjectOrm = "drizzle" | "prisma" | "none";
export type ProjectOrm = "drizzle" | "prisma" | "mongoose" | "none";
export type ProjectPackageManager = "npm" | "pnpm" | "bun";
export type ProjectAddons =
| "pwa"