Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions angular-app/angular.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"cli": {
"analytics": false
},
"version": 1,
"newProjectRoot": "projects",
"projects": {
Expand Down
8 changes: 5 additions & 3 deletions angular-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"scripts": {
"postinstall": "cd ../backend/ && npm install",
"ng": "ng",
"prestart": "warp project select && cd ../backend/ && npm run ts-build",
"start:ts-watch": "cd ../backend/ && npm run ts-watch",
"start:client": "ng serve",
"start:server": "warp dev ../backend/",
"start": "run-p start:*",
"ts-build": "cd ../backend/ && npm run ts-build",
"build:client": "ng build",
"build:server": "warp build ../backend/",
"build": "run-s build:server build:client",
"predeploy": "warp project select",
"deploy": "warp deploy ./ ../backend/",
"test": "ng test",
"lint": "ng lint",
Expand All @@ -26,7 +28,7 @@
"@angular/platform-browser": "~11.2.11",
"@angular/platform-browser-dynamic": "~11.2.11",
"@angular/router": "~11.2.11",
"@warpjs/engine": "^4.0.9",
"@warpjs/engine": "^4.0.11",
"rxjs": "~6.6.0",
"tslib": "^2.3.0",
"zone.js": "~0.11.3"
Expand All @@ -51,6 +53,6 @@
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.1.5",
"warp": "^4.0.9"
"warp": "^4.0.11"
}
}
12 changes: 6 additions & 6 deletions angular-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';

import Backend from 'backend';
const { hello, fetchMovies } = new Backend() as any;

const backend = new Backend();

@Component({
selector: 'app-root',
Expand All @@ -14,13 +14,13 @@ export class AppComponent implements OnInit {
movies = {};

ngOnInit() {
hello().then((msg: string) => {
backend.hello().then((msg: string) => {
this.message = msg;
});

// fetchMovies from mongodb
fetchMovies('Star Trek').then(
(data: any) => (this.movies = JSON.stringify(data, null, 2))
);
backend.fetchMovies('Star Trek').then((data) => {
this.movies = JSON.stringify(data, null, 2);
});
}
}
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# dependencies
node_modules/
dist/
.warp


Expand Down
9 changes: 8 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"main": "src/index.js",
"license": "MIT",
"scripts": {
"ts-watch": "tsc --watch",
"ts-build": "tsc"
},
"dependencies": {
"mongodb": "^3.6.6"
},
"devDependencies": {
"typescript": "^4.3.5"
}
}
6 changes: 5 additions & 1 deletion backend/index.js → backend/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { fetchMovies } = require('./src/mongodb');
const { fetchMovies } = require("./mongodb");

/**
*
* @returns {Promise<string>} return a hello from your backend module
*/
const hello = () => {
return `Hello from ScaleDynamics Platform, MongoDB, Angular and Node.js ${process.version} !`;
};
Expand Down
24 changes: 19 additions & 5 deletions backend/src/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
const { MongoClient } = require('mongodb');
const { MongoClient } = require("mongodb");

/**
* A movie
* @typedef {Object} Movie
* @property {string} title - Title of the movie
* @property {string} year - Year of release of the movie
* @property {string} plot - Plot of the movie
* @property {string} poster - Link to the poster of the movie
*/

// connection URI
const URI = 'mongodb+srv://test:test@movies-scqxj.gcp.mongodb.net/';
const URI = "mongodb+srv://test:test@movies-scqxj.gcp.mongodb.net/";

// create & connect a new MongoDB client
const connection = MongoClient(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).connect();

/**
*
* @param {string} search the name of the movies you seek
* @returns {Promise<Movie[]>} return an array of movies
*/
const fetchMovies = async (search) => {
// await database connection
const client = await connection.catch((error) => {
throw new Error(`Database connection failed (${error.message})`);
});
// request database
return client
.db('sample_mflix')
.collection('movies')
.find({ poster: { $exists: true }, title: { $regex: search, $options: 'i' } })
.db("sample_mflix")
.collection("movies")
.find({ poster: { $exists: true }, title: { $regex: search, $options: "i" } })
.project({ _id: 0, title: 1, year: 1, plot: 1, poster: 1 })
.sort({ year: -1 })
.limit(50)
Expand Down
11 changes: 11 additions & 0 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist",
"moduleResolution": "node"
},
"include": ["src/**/*.js"]
}
10 changes: 7 additions & 3 deletions backend/warp.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module.exports = {
output: {
format: 'node-module',
projectPath: '../angular-app',
name: 'backend',
format: "node-module",
projectPath: "../angular-app",
name: "backend",
},
expose: {
source: "src/index.js",
type: "dist/index.d.ts",
},
};