This shows a solution to the to the coding exercise given by VARA EDTECH and how to use endpoints to perform CRUD operation. REST API with TypeScript using Express and Prisma Client. The solution uses a MySQL database with some initial dummy data which you can find at ./prisma/seed.ts
.
Clone this repository:
git clone https://github.com/Marrwan/vara_edtech.git
Install npm dependencies:
cd vara
npm install
Create a .env
file in the root of the project and add the following variables:
DATABASE_URL="mysql://root:password@localhost:3306/vara"
NODE_ENV="development"
PORT=4000
Or you can copy it from the .env.example
file.
Run the following command to create your SQLite database file. This also creates the Customer
and Address
tables that are defined in prisma/schema.prisma
:
npx prisma migrate dev --name init
Run the following command to seed the database with some initial data:
npx ts-node prisma/seed.ts
The seed file in prisma/seed.ts
will be executed and your database will be populated with the sample data.
npm run dev
The server is now running on http://localhost:4000
. You can now run the API requests, e.g. http://localhost:4000/api/customers
.
You can access the REST API of the server using the following endpoints:
-
/api/customers
- Fetch all customers -
/api/customers/:id
- fetch a customer by its id
-
/api/address
- Fetch all addresses -
/api/address/:id
- fetch an address by its id
/api/customers
- Create a new customer- Body:
name - string
(required) : The name of the customeremail - string
(required) : The email address of the customerphone - string
(required) : The phone number of the customer
- Body:
/api/address
- Create a new address- Body:
street - string
(required) : The street of the addresscity - string
(required) : The city of the addressstate - string
(required) : The state of the addresszip - string
(required) : The zip code of the addresscustomerId - string
(required) : The id of the customer that the address belongs to
- Body:
/api/customers/:id
- Update a customer- Body:
name - string
(optional) : The name of the customeremail - string
(optional) : The email address of the customerphone - string
(optional) : The phone number of the customer
- Body:
/api/address/:id
- Update an address- Body:
street - string
(optional) : The street of the addresscity - string
(optional) : The city of the addressstate - string
(optional) : The state of the addresszip - string
(optional) : The zip code of the addresscustomerId - string
(optional) : The id of the customer that the address belongs to
- Body:
/api/customers/:id
- Delete a customer
/api/address/:id
- Delete an address
If you want to try this example with another database than MySQL, you can adjust the the database connection in prisma/schema.prisma
by reconfiguring the datasource
block.
Learn more about the different connection configurations in the docs.
Expand for an overview of example configurations with different databases
For PostgreSQL, the connection URL has the following structure:
datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}
Here is an example connection string with a local PostgreSQL database:
datasource db {
provider = "postgresql"
url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"
}
For MySQL, the connection URL has the following structure:
datasource db {
provider = "mysql"
url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"
}
Here is an example connection string with a local MySQL database:
datasource db {
provider = "mysql"
url = "mysql://janedoe:mypassword@localhost:3306/notesapi"
}
Here is an example connection string with a local Microsoft SQL Server database:
datasource db {
provider = "sqlserver"
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}
Here is an example connection string with a local MongoDB database:
datasource db {
provider = "mongodb"
url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
}
Because MongoDB is currently in Preview, you need to specify the previewFeatures
on your generator
block:
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongodb"]
}