crud-generator-haskell is a tool for generating CRUD (Create, Read, Update, Delete) APIs for a given data model. It is designed to simplify the process of creating a RESTful API by automating the repetitive tasks involved in generating a CRUD API.
To use crud-generator, you must define data model using a JSON template, like this:
{
"name": "user",
"prefix": "api",
"entity": "User",
"fields": [
{
"fieldName": "name",
"fieldType": "text"
},
{
"fieldName": "surname",
"fieldType": "text"
}
]
}
you can define as many models as you project need.
To use crud-generator with stack, follow these steps:
-
First, ensure that you have Stack installed on your system.
-
If you dont have sqlite3 installed, you can download and install.
-
Open a terminal and navigate to the root directory of your project.
-
Start the stack ghci REPL by running the following command:
stack ghci
In the REPL, run the migrateModel function, passing in the connection string for your database, default is db.sqlite3:
migrateModel connectionString
This will create the necessary tables in database to support the generated API.
-
Exit the REPL by typing :quit.
-
Finally, start the server by running the following command:
After running these commands, exit from stack ghci and run the following command to start the server on port 8080:
stack run
Or if you prefer, open two terminals side by side, in the first terminal. Run the following command to continuously monitor the source files and trigger a rebuild whenever any file changes:
stack build --file-watch
So, after this, the project rebuilt whenever a file is modified.
In the second terminal. Run the following command to start the server on port 8080. Where api is defined in package.yaml
stack exec api
curl --location --request GET 'localhost:8080/api/user'
And this should return a list of user.