rivet-gg / opengb

Open Game Backend: Open-source modular backend for all games and tools. Works with Godot, Unity, Unreal Engine, and HTML5.
https://opengb.dev
Apache License 2.0
103 stars 6 forks source link

Cannot create module without defined DB model, Prisma throws error #426

Open jog1t opened 2 months ago

jog1t commented 2 months ago

@kacper said:

Can't create a module without a defined db model. Prisma throws an error.

Steps to reproduce:

  1. Init new OpenGB
  2. Adds some modules
  3. Run opengb create script my_module
  4. Run opengb create script my_module test
  5. Run opengb dev

You should see the following error:

error: Failed to run: prisma generate

  stdout: Prisma schema loaded from schema.prisma
  Prisma schema loaded from schema.prisma
  stderr: Error: 
You don't have any models defined in your schema.prisma, so nothing will be generated.
You can define a model like this:

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

More information in our documentation:
https://pris.ly/d/prisma-schema
  Error: 
  You don't have any models defined in your schema.prisma, so nothing will be generated.
  You can define a model like this:

  model User {
    id    Int     @id @default(autoincrement())
    email String  @unique
    name  String?
  }

  More information in our documentation:
  https://pris.ly/d/prisma-schema

Failed. Found 1 error.
linear[bot] commented 2 months ago

OGBE-164 Cannot create module without defined DB model, Prisma throws error

greptile-apps[bot] commented 2 months ago

To resolve the issue of creating a module without a defined DB model, follow these steps:

  1. Modify the schema.prisma file to include a default model if none are defined. This can be done in the runPrismaCommand function in /src/migrate/prisma.ts.
// Append generator config
const schemaPath = resolve(dbDir, "schema.prisma");
let schema = await Deno.readTextFile(schemaPath);

// Check if schema contains any model definitions
if (!/model\s+\w+\s+\{/.test(schema)) {
  schema += dedent`
    model DefaultModel {
      id    Int     @id @default(autoincrement())
      name  String?
    }
  `;
}

schema += dedent`
  // Generated by Open Game Backend
  generator client {
    provider = "prisma-client-js"
    output = "${dbDir}/client"
    previewFeatures = ["driverAdapters"]
  }
`;
await Deno.writeTextFile(schemaPath, schema);

This will ensure that a default model is added to the schema.prisma file if no models are defined, preventing Prisma from throwing an error.

References

/src/runtime/runtime.ts /tests/basic/modules/foo/db/schema.prisma /src/migrate/prisma.ts /src/migrate/generate.ts /src/cli/commands/db.ts

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/rivet-gg/opengb/main) · [Edit Issue Bot Settings](https://app.greptile.com/apps/github)