init repo
commit
2c746bdd88
@ -0,0 +1,5 @@
|
||||
ENVIRONMENT = "dev"
|
||||
CASSANDRA_DC = datacenter1
|
||||
CASSANDRA_HOST = 0.0.0.0
|
||||
CASSANDRA_USER = cassandra
|
||||
CASSANDRA_PW = cassandra
|
@ -0,0 +1,25 @@
|
||||
module.exports = {
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
project: 'tsconfig.json',
|
||||
tsconfigRootDir : __dirname,
|
||||
sourceType: 'module',
|
||||
},
|
||||
plugins: ['@typescript-eslint/eslint-plugin'],
|
||||
extends: [
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
'plugin:prettier/recommended',
|
||||
],
|
||||
root: true,
|
||||
env: {
|
||||
node: true,
|
||||
jest: true,
|
||||
},
|
||||
ignorePatterns: ['.eslintrc.js'],
|
||||
rules: {
|
||||
'@typescript-eslint/interface-name-prefix': 'off',
|
||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
},
|
||||
};
|
@ -0,0 +1,37 @@
|
||||
# compiled output
|
||||
/dist
|
||||
/node_modules
|
||||
|
||||
.env
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
pnpm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
|
||||
# Tests
|
||||
/coverage
|
||||
/.nyc_output
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
FROM node:18-alpine AS builder
|
||||
|
||||
WORKDIR "/app"
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npm ci
|
||||
|
||||
RUN npm run build
|
||||
|
||||
RUN npm prune --production
|
||||
|
||||
FROM node:18-alpine AS prod
|
||||
|
||||
WORKDIR "/app"
|
||||
|
||||
COPY --from=builder /app/package.json ./package.json
|
||||
COPY --from=builder /app/package-lock.json ./package-lock.json
|
||||
COPY --from=builder /app/dist ./dist
|
||||
COPY --from=builder /app/node_modules ./node_modules
|
||||
COPY --from=builder /app/sf-class2-root.crt ./sf-class2-root.crt
|
||||
COPY --from=builder /app/.env /app/dist/.env
|
||||
|
||||
ENV ENVIRONMENT production
|
||||
ENV CASSANDRA_DC datacenter1
|
||||
ENV CASSANDRA_HOST 0.0.0.0
|
||||
ENV CASSANDRA_USER cassandra
|
||||
ENV CASSANDRA_PW cassandra
|
||||
|
||||
CMD ["sh", "-c", "ENVIRONMENT=${ENVIRONMENT} CASSANDRA_DC=${CASSANDRA_DC} CASSANDRA_HOST=${CASSANDRA_HOST} CASSANDRA_USER=${CASSANDRA_USER} CASSANDRA_PW=${CASSANDRA_PW} npm run start:prod --host 0.0.0.0"]
|
||||
|
||||
EXPOSE 3000
|
@ -0,0 +1,18 @@
|
||||
# Nest Js Simple User API with Cassandra
|
||||
|
||||
This is a simple nest js user api with 2 endpoints - get all users and create a user, using cassandra as the underlying database.
|
||||
|
||||
## Development
|
||||
|
||||
- Copy `.env.example` to `.env`
|
||||
- Make sure cassandra is running locally/via docker and replace the values in the env file accordingly
|
||||
- `npm i`
|
||||
- `npm run start:dev`
|
||||
|
||||
## Production (with AWS Keyspaces)
|
||||
|
||||
- Copy `.env.example` to `.env`
|
||||
- Ensure `ENVIRONMENT = "production"` if using AWS Keyspaces and replace the other values with AWS endpoints
|
||||
- `npm i`
|
||||
- `npm run build`
|
||||
- `npm run start:prod`
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src"
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,75 @@
|
||||
{
|
||||
"name": "quriverse-test",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"prebuild": "rimraf dist",
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^9.0.0",
|
||||
"@nestjs/config": "^2.2.0",
|
||||
"@nestjs/core": "^9.0.0",
|
||||
"@nestjs/platform-express": "^9.0.0",
|
||||
"bcrypt": "^5.0.1",
|
||||
"cassandra-driver": "^4.6.4",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"rxjs": "^7.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^9.0.0",
|
||||
"@nestjs/schematics": "^9.0.0",
|
||||
"@nestjs/testing": "^9.0.0",
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/jest": "28.1.8",
|
||||
"@types/node": "^16.0.0",
|
||||
"@types/supertest": "^2.0.11",
|
||||
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
||||
"@typescript-eslint/parser": "^5.0.0",
|
||||
"eslint": "^8.0.1",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "28.1.3",
|
||||
"prettier": "^2.3.2",
|
||||
"source-map-support": "^0.5.20",
|
||||
"supertest": "^6.1.3",
|
||||
"ts-jest": "28.0.8",
|
||||
"ts-loader": "^9.2.3",
|
||||
"ts-node": "^10.0.0",
|
||||
"tsconfig-paths": "4.1.0",
|
||||
"typescript": "^4.7.4"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEDzCCAvegAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswCQYDVQQGEwJVUzEl
|
||||
MCMGA1UEChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMp
|
||||
U3RhcmZpZWxkIENsYXNzIDIgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDQw
|
||||
NjI5MTczOTE2WhcNMzQwNjI5MTczOTE2WjBoMQswCQYDVQQGEwJVUzElMCMGA1UE
|
||||
ChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMpU3RhcmZp
|
||||
ZWxkIENsYXNzIDIgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwggEgMA0GCSqGSIb3
|
||||
DQEBAQUAA4IBDQAwggEIAoIBAQC3Msj+6XGmBIWtDBFk385N78gDGIc/oav7PKaf
|
||||
8MOh2tTYbitTkPskpD6E8J7oX+zlJ0T1KKY/e97gKvDIr1MvnsoFAZMej2YcOadN
|
||||
+lq2cwQlZut3f+dZxkqZJRRU6ybH838Z1TBwj6+wRir/resp7defqgSHo9T5iaU0
|
||||
X9tDkYI22WY8sbi5gv2cOj4QyDvvBmVmepsZGD3/cVE8MC5fvj13c7JdBmzDI1aa
|
||||
K4UmkhynArPkPw2vCHmCuDY96pzTNbO8acr1zJ3o/WSNF4Azbl5KXZnJHoe0nRrA
|
||||
1W4TNSNe35tfPe/W93bC6j67eA0cQmdrBNj41tpvi/JEoAGrAgEDo4HFMIHCMB0G
|
||||
A1UdDgQWBBS/X7fRzt0fhvRbVazc1xDCDqmI5zCBkgYDVR0jBIGKMIGHgBS/X7fR
|
||||
zt0fhvRbVazc1xDCDqmI56FspGowaDELMAkGA1UEBhMCVVMxJTAjBgNVBAoTHFN0
|
||||
YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xMjAwBgNVBAsTKVN0YXJmaWVsZCBD
|
||||
bGFzcyAyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5ggEAMAwGA1UdEwQFMAMBAf8w
|
||||
DQYJKoZIhvcNAQEFBQADggEBAAWdP4id0ckaVaGsafPzWdqbAYcaT1epoXkJKtv3
|
||||
L7IezMdeatiDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+nLMPUxA2IGvd56D
|
||||
eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl
|
||||
xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp
|
||||
VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY
|
||||
WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q=
|
||||
-----END CERTIFICATE-----
|
@ -0,0 +1,18 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { DbModule } from './db/db.module';
|
||||
import { UserModule } from './user/user.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
}),
|
||||
DbModule,
|
||||
UserModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [],
|
||||
})
|
||||
|
||||
export class AppModule {}
|
@ -0,0 +1,11 @@
|
||||
import { DbService } from './db.service';
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [],
|
||||
providers: [DbService],
|
||||
exports: [DbService],
|
||||
})
|
||||
|
||||
export class DbModule {}
|
@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Client, mapping, auth, types } from 'cassandra-driver';
|
||||
import * as fs from 'fs';
|
||||
|
||||
@Injectable()
|
||||
export class DbService {
|
||||
client: Client;
|
||||
mapping: mapping.Mapper;
|
||||
|
||||
constructor(private configService: ConfigService) {}
|
||||
|
||||
private createClient() {
|
||||
console.log(process.env);
|
||||
this.client = new Client({
|
||||
contactPoints: [process.env.CASSANDRA_HOST],
|
||||
keyspace: 'quriverse',
|
||||
localDataCenter: process.env.CASSANDRA_DC,
|
||||
authProvider: new auth.PlainTextAuthProvider(process.env.CASSANDRA_USER, process.env.CASSANDRA_PW),
|
||||
sslOptions: {
|
||||
ca: [
|
||||
fs.readFileSync('sf-class2-root.crt', 'utf-8')
|
||||
],
|
||||
host: 'cassandra.ap-south-1.amazonaws.com',
|
||||
rejectUnauthorized: true,
|
||||
},
|
||||
protocolOptions: {
|
||||
port: process.env.ENVIRONMENT === "production" ? 9142 : 9042,
|
||||
},
|
||||
queryOptions: {
|
||||
consistency: process.env.ENVIRONMENT === "production" ? types.consistencies.localQuorum : types.consistencies.localOne,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
createMapper(mappingOptions: mapping.MappingOptions) {
|
||||
if (this.client == undefined) {
|
||||
this.createClient();
|
||||
}
|
||||
|
||||
return new mapping.Mapper(this.client, mappingOptions);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
@ -0,0 +1,24 @@
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { Controller, Get, Post, Body, BadRequestException } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { User } from './user.model';
|
||||
|
||||
@Controller()
|
||||
export class UserController {
|
||||
|
||||
constructor(private userService: UserService) {}
|
||||
|
||||
@Get('users')
|
||||
async get() {
|
||||
return this.userService.get();
|
||||
}
|
||||
|
||||
@Post('users')
|
||||
async create(@Body() user: User) {
|
||||
user.password = bcrypt.hashSync(user.password, 10);
|
||||
return this.userService.create(user)
|
||||
.catch(err => {
|
||||
throw new BadRequestException();
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
export class User {
|
||||
id: number = 0;
|
||||
firstname: string = "";
|
||||
lastname: string = "";
|
||||
email: string = "";
|
||||
password: string = "";
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
import { UserRepository } from './user.repository';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DbModule } from '../db/db.module';
|
||||
|
||||
@Module({
|
||||
imports: [DbModule],
|
||||
controllers: [UserController],
|
||||
providers: [
|
||||
UserService,
|
||||
UserRepository,
|
||||
],
|
||||
exports: [
|
||||
UserService,
|
||||
UserRepository,
|
||||
],
|
||||
})
|
||||
|
||||
export class UserModule {}
|
@ -0,0 +1,32 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { mapping } from 'cassandra-driver';
|
||||
import { User } from './user.model';
|
||||
import { DbService } from '../db/db.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserRepository implements OnModuleInit {
|
||||
constructor(private dbService: DbService) {}
|
||||
|
||||
mapper: mapping.ModelMapper<User>;
|
||||
|
||||
onModuleInit() {
|
||||
const mappingOptions: mapping.MappingOptions = {
|
||||
models: {
|
||||
'User': {
|
||||
tables: ['users'],
|
||||
mappings: new mapping.UnderscoreCqlToCamelCaseMappings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.mapper = this.dbService.createMapper(mappingOptions).forModel('User');
|
||||
}
|
||||
|
||||
async get() {
|
||||
return (await this.mapper.findAll()).toArray();
|
||||
}
|
||||
|
||||
async create(user: User) {
|
||||
return (await this.mapper.insert(user)).toArray();
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserRepository } from './user.repository';
|
||||
import { User } from './user.model';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
|
||||
constructor(private userRepository: UserRepository) {}
|
||||
|
||||
async get() {
|
||||
return this.userRepository.get();
|
||||
}
|
||||
|
||||
async create(user: User) {
|
||||
return this.userRepository.create(user);
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"removeComments": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"target": "es2017",
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"baseUrl": "./",
|
||||
"incremental": true,
|
||||
"skipLibCheck": true,
|
||||
"strictNullChecks": false,
|
||||
"noImplicitAny": false,
|
||||
"strictBindCallApply": false,
|
||||
"forceConsistentCasingInFileNames": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue