Skip to main content
Version: v2.0

Generating CRUD API

The gen-crud-api command in Godspeed is a powerful tool that automatically generates CRUD (Create, Read, Update, Delete) APIs for your data models.

The framework generates CRUD API using Prisma's database model files and ORM client. It uses Godspeed's Prisma plugin as the ORM and generates CRUD APIs served from http eventsource, which is Express.js by default.

Watch this video to see how CRUD API is generated in Godspeed

Steps to generate CRUD API over REST

Step 1. Create a godspeed project

Create a new project from the CLI and open the created project in vscode

(See How to create)

Step 2. Install the prisma plugin

Add the 'prisma-as-datastore' plugin from the CLI

(See How to add Prisma plugin)

Step 3: Set your Database Connection URL

Database connection URL is required to connect your project to a database and it should be configured within your project’s .env file.

General Format

Each database type has a specific URL format, but most follow the general structure:

protocol://username:password@host:port/database_name

Here, You can check the list of supported databases and Connection URL format for the database you're using.

Add the Database connection URL in .env file as:

DATABASE_URL= "postgresql://postgres:postgres@localhost:5432/yourdb" //for postgres
or
DATABASE_URL= "file:./enter_your_file_name.db" //for SQLite

And then this environment variable is to be provided in the url field of the datasource block in the prisma schema (/src/datasources/_.prisma)

datasource db {
provider = "database_provider_name",
url = env("DATABASE_URL")
}

Step 4. Create Prisma Schema

Now Create a prisma schema file in src/datasources directory

Important Note:
When configuring the Prisma client in your Godspeed project, ensure you add the output field in your Prisma schema's generator block. This field should point to this location src/datasources/prisma-clients/<name_of_your_prisma_file> where the generated Prisma client files will be stored.

generator client {
provider = "prisma-client-js"
output = "./prisma-clients/lms" //just change the name of prisma schema here
previewFeatures = ["metrics"] // to be used in case you want to generate metrics for prisma queries for telemetry.
}

This setup ensures that the generated client is available at the specified path i.e. src/datasources/prisma-clients/

If your schema name is lms.prisma, your file content should look like this.

  datasource db {
provider = "Name of Database Provider" // "mysql", "sqlite", "sqlserver" etc.
url = env("DATABASE_URL") // this is the variable name given to your db_connecion_url in .env
}

generator client {
provider = "prisma-client-js"
output = "./prisma-clients/lms" //in place of lms, give the name of your prisma schema here
previewFeatures = ["metrics"] //if you want to generate metrics for prisma queries for telemetry
}

model User {
id Int @id @default(autoincrement())
pan_number String @unique ///@encrypted
}

4.1 If you already have an existing database, you can introspect it and generate the Prisma model file using prisma db pull. This will generate your .prisma file.

4.2 Copy the generated file to src/datasources folder and rename it as per the name of this datasource that you want to keep. If you don't have an existing database setup with a model, then create a prisma model file from scratch.

Step 5. Generate prisma client and sync your database

Run godspeed prisma prepare. This command will generate the prisma client and will sync the database with prisma schema. The generated client will be stored in src/datasources/prisma-clients/ folder which we specified above in the generator client block.

 $ godspeed prisma prepare

Step 6. Generate CRUD API

The godspeed gen-crud-api command will generate the crud apis based on the sample prisma schema provided at ./src/datasources/<db_name>.prisma

$  godspeed gen-crud-api
       ,_,   ╔════════════════════════════════════╗
(o,o) ║ Welcome to Godspeed ║
({___}) ║ World's First Meta Framework ║
" " ╚════════════════════════════════════╝

> blog-app@1.0.0 gen-crud-api
> npx @godspeedsystems/api-generator

Select datasource / schema to generate CRUD APIs
(x) lms.prisma
( ) For all
( ) Cancel

And your CRUD API will be generated.

Inspect generated events, definitions and functions.

img

Generated events use definitions defined in src/definitions folder, which are in JSON schema format

Generated functions are currently YAML functions

Run and check Swagger spec

You can check the Swagger spec which would have been automatically generated.

#Run the project using godspeed serve command and then open your swagger endpoint in the browser and test your CRUD app from end to end.

localhost:<http_port>/<http_docs_endpoint> which is by default localhost:3000/api-docs