TypeScript MongoDB compatibility quickstart
This quickstart will get you up and running with an application that demonstrates Tigris MongoDB compatibility.
You can view the source for the TypeScript MongoDB compatibility quickstart app on GitHub.
Prerequisites
- Node.js LTS or higher
- A Tigris Cloud account
Get the code
Using create-tigris-app
The quickest way to get up and running with the TypeScript MongoDB compatibility
quickstart is by using the
create-tigris-app
CLI:
- Head to the Tigris Web console
- Create a new project
- Run the MongoDB Driver
create-tigris-app
command shown in the TypeScript tab of the Getting started section for the project
This will download the Git repo, install the application dependencies, and
create a .env
with the required envionment variables.
Manually
Clone the repo, navigate into the directory, and install the application dependencies:
git clone https://github.com/tigrisdata-community/tigris-mongodb-typescript-example.git
cd tigris-mongodb-typescript-example
npm i
Create a project in Tigris, create an application key, and copy the Client ID
and Client Secret values for use in a .env
file. Then:
- Create a
.env
file in the root of the app with the same properties asenv.example
- Update the values with your connection string, database name, and collection name
Install the app dependencies:
npm i
Run the app
Start the app as follows:
npm start
Create a new Game:
curl --location --request POST 'http://localhost:8080/games' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Fable Anniversary",
"price": 4.99,
"category": "Video Game"
}'
List Games:
curl --location --request GET 'http://localhost:8080/games'
Get a single Game:
curl --location --request GET 'http://localhost:8080/games/{_id}'
Delete a Game:
curl --location --request DELETE 'http://localhost:8080/games/{_id}'
Getting to know the code
The below diagram shows the overall architecture of the diagram and the following sections will explain the code:
Games Router
The src/routes/games.router.ts
class uses Express's Router functionality. It
defines the endpoints available to clients for the Create, Read, Update and
Delete (CRUD) operations and then communicates with the database via the
MongoDB NodeJS Driver.
Database Service
The src/services/database.service.ts
class implements the connection the
database. This is also where schema validation is applied to the collection at
database level.
Models
games.ts
defines the model (properties and data types expected) in a Game
document and is used throughout the codebase.
Explore the data
You can explore the data created by the quickstart application in the Tigris Cloud Console.