Skip to content

tigrisdata-archive/tigris-mongodb-typescript-example

 
 

Repository files navigation

Tigris MongoDB compatibility and TypeScript example

Introduction

Welcome to this Tigris MongoDB compatibility and TypeScript example app. This repo aims to give you a working example of how you can use the power of Tigris MongoDB compatibility with TypeScript and Express to create modern web applications.

This repository is a fork of the MongoDB TypesScript example repo with some README updates and a minor configuration change required to use the sample with Tigris.

Prerequisites

Running the example app

Create a project in Tigris, create an application key, and copy the Project name, Client ID, and Client Secret values for use in a .env file. Then:

  1. Create a .env file in the root of the app with the same properties as env.example
  2. Update the values within your connection string, and database name (the name of your Tigris project)

Install the app dependencies:

npm i

Run the app:

npm start

Try out the app

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 diagram below shows the overall application architecture; the following sections will explain the code.

Architecture diagram of the application

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 to the database. This is also where schema validation is applied to the collection.

Models

TypeScript is an optionally statically typed language that allows for taking advantage of object-orientated programming. The games.ts class creates a class that defines the properties and data types expected in our document. We use this model throughout the code to enjoy the benefits of typed objects.

More information

If you want more information about Tigris, the open source alternative to MongoDB Atlas, you can view the Tigris documentation.

Get involved with Tigris by joining the Tigris Discord

About

Welcome to this Tigris MongoDB compatibility and TypeScript example app. This repo aims to give you a working example of how you can use the power of Tigris MongoDB compatibility with TypeScript and Express to create modern web applications.

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • TypeScript 96.5%
  • Shell 3.5%