Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve an issue where the prisma.schema location is semi-hardcoded #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 25 additions & 19 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

// Import utility functions
import {
checkIfMigrationsFolderExists,
checkIfPrismaSchemaExists,
getMigrationsFolder,
getPrismaSchema,
formatSchema,
initPrisma,
installStudio,
Expand All @@ -26,7 +26,7 @@
import { ensureDependencyInstalled } from "nypm";

// Module configuration interface
interface ModuleOptions extends Prisma.PrismaClientOptions {

Check failure on line 29 in src/module.ts

View workflow job for this annotation

GitHub Actions / Experimental Dev Release

Namespace '"/home/runner/work/nuxt-prisma/nuxt-prisma/node_modules/.prisma/client/default".Prisma' has no exported member 'PrismaClientOptions'.
writeToSchema: boolean;
formatSchema: boolean;
runMigration: boolean;
Expand All @@ -50,7 +50,7 @@

// Default configuration options for the module
defaults: {
datasources: {

Check failure on line 53 in src/module.ts

View workflow job for this annotation

GitHub Actions / Experimental Dev Release

Object literal may only specify known properties, and 'datasources' does not exist in type 'ModuleOptions | ((nuxt: Nuxt) => ModuleOptions)'.
db: {
url: process.env.DATABASE_URL, // Security: Ensure DATABASE_URL is correctly set and secure
},
Expand Down Expand Up @@ -79,10 +79,6 @@
const skipAllPrompts =
options.skipPrompts || npmLifecycleEvent === "dev:build";

const PRISMA_SCHEMA_CMD = options.prismaSchemaPath
? ["--schema", options.prismaSchemaPath]
: [];

/**
* Helper function to prepare the module configuration
*/
Expand Down Expand Up @@ -116,8 +112,8 @@
nuxt.options.runtimeConfig.public.prisma = defu(
nuxt.options.runtimeConfig.public.prisma || {},
{
log: options.log,

Check failure on line 115 in src/module.ts

View workflow job for this annotation

GitHub Actions / Experimental Dev Release

Property 'log' does not exist on type 'ModuleOptions'.
errorFormat: options.errorFormat,

Check failure on line 116 in src/module.ts

View workflow job for this annotation

GitHub Actions / Experimental Dev Release

Property 'errorFormat' does not exist on type 'ModuleOptions'.
},
);

Expand All @@ -136,14 +132,26 @@
? resolveProject(options.prismaRoot) // Combines paths safely
: PROJECT_PATH;

// Check if the Prisma schema exists
const resolvedPrismaSchema = getPrismaSchema([
resolveProject(LAYER_PATH, "schema.prisma"),
resolveProject(LAYER_PATH, "prisma", "schema.prisma"),
resolveProject(LAYER_PATH, "prisma", "schema"),
]);

const PRISMA_SCHEMA_CMD = [
"--schema",
options.prismaSchemaPath || resolvedPrismaSchema || "",
];

// Ensure Prisma CLI is installed if required
if (options.installCLI) {
log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.action);

try {
await ensureDependencyInstalled("prisma", {
cwd: PROJECT_PATH,
dev: true
dev: true,
});
log(PREDEFINED_LOG_MESSAGES.installPrismaCLI.success);
} catch (error) {
Expand All @@ -152,25 +160,20 @@
await generatePrismaClient(
PROJECT_PATH,
PRISMA_SCHEMA_CMD,
options.log?.includes("error"),

Check failure on line 163 in src/module.ts

View workflow job for this annotation

GitHub Actions / Experimental Dev Release

Property 'log' does not exist on type 'ModuleOptions'.
);
}

// Check if Prisma schema exists
const prismaSchemaExists = checkIfPrismaSchemaExists([
resolveProject(LAYER_PATH, "prisma", "schema.prisma"),
resolveProject(LAYER_PATH, "prisma", "schema"),
]);

/**
* Handle Prisma migrations workflow
*/
const prismaMigrateWorkflow = async () => {
const migrationFolderExists = checkIfMigrationsFolderExists(
const migrationFolder = getMigrationsFolder([
resolveProject(LAYER_PATH, "migrations"),
resolveProject(LAYER_PATH, "prisma", "migrations"),
);
]);

if (migrationFolderExists || !options.runMigration) {
if (migrationFolder || !options.runMigration) {
log(PREDEFINED_LOG_MESSAGES.skipMigrations);
return;
}
Expand Down Expand Up @@ -206,7 +209,10 @@
rootDir: PROJECT_PATH,
provider: "sqlite",
});
await writeToSchema(`${LAYER_PATH}/prisma/schema.prisma`);

if (resolvedPrismaSchema) {
await writeToSchema(resolvedPrismaSchema);
}
};

/**
Expand Down Expand Up @@ -240,7 +246,7 @@
};

// Execute workflows sequentially
if (!prismaSchemaExists) {
if (!resolvedPrismaSchema) {
await prismaInitWorkflow();
}
await prismaMigrateWorkflow();
Expand All @@ -249,13 +255,13 @@
if (options.generateClient) {
if (options.installClient) {
await ensureDependencyInstalled("@prisma/client", {
cwd: PROJECT_PATH
cwd: PROJECT_PATH,
});
}
await generatePrismaClient(
PROJECT_PATH,
PRISMA_SCHEMA_CMD,
options.log?.includes("error"),

Check failure on line 264 in src/module.ts

View workflow job for this annotation

GitHub Actions / Experimental Dev Release

Property 'log' does not exist on type 'ModuleOptions'.
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/package-utils/log-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export const PREDEFINED_LOG_MESSAGES = {
success: `Successfully installed Prisma CLI.`,
error: `Failed to install Prisma CLI.`,
},
checkIfPrismaSchemaExists: {
getPrismaSchema: {
yes: "Prisma schema file exists.",
no: "Prisma schema file does not exist.",
},
initPrisma: {
action: "Initializing Prisma project...\n",
error: "Failed to initialize Prisma project.",
},
checkIfMigrationsFolderExists: {
getMigrationsFolder: {
success: "Database migrations folder exists.",
error: "Database migrations folder does not exist.",
},
Expand Down
28 changes: 14 additions & 14 deletions src/package-utils/setup-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ export type PrismaInitOptions = {
rootDir: string;
};

export function checkIfPrismaSchemaExists(paths: string[]) {
const exists = paths.reduce((prev, current) => {
return existsSync(current) || prev;
}, false);

if (exists) {
logSuccess(PREDEFINED_LOG_MESSAGES.checkIfPrismaSchemaExists.yes);
return true;
export function getPrismaSchema(paths: string[]): string | false {
for (const path of paths) {
if (existsSync(path)) {
logSuccess(PREDEFINED_LOG_MESSAGES.getPrismaSchema.yes);
return path;
}
}

logError(PREDEFINED_LOG_MESSAGES.checkIfPrismaSchemaExists.no);
logError(PREDEFINED_LOG_MESSAGES.getPrismaSchema.no);
return false;
}

Expand Down Expand Up @@ -106,13 +104,15 @@ export async function initPrisma({
}
}

export function checkIfMigrationsFolderExists(path: string) {
if (existsSync(path)) {
logSuccess(PREDEFINED_LOG_MESSAGES.checkIfMigrationsFolderExists.success);
return true;
export function getMigrationsFolder(paths: string[]): string | false {
for (const path of paths) {
if (existsSync(path)) {
logSuccess(PREDEFINED_LOG_MESSAGES.getMigrationsFolder.success);
return path;
}
}

logError(PREDEFINED_LOG_MESSAGES.checkIfMigrationsFolderExists.error);
logError(PREDEFINED_LOG_MESSAGES.getMigrationsFolder.error);
return false;
}

Expand Down
Loading