Skip to content

Commit

Permalink
chore: working on mongodb integration for jms #447
Browse files Browse the repository at this point in the history
  • Loading branch information
svendjanis committed Dec 2, 2022
1 parent de96245 commit 217e92b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {
collection,
Expand Down Expand Up @@ -32,11 +33,20 @@ import {environment} from '../../src/environments/environment';
export class MongoDatabaseService extends DbService {
constructor(
private firestore: Firestore,
private functions: Functions
private functions: Functions,
private http: HttpClient
) {
super();
}

removeDocument(moduleId, id) {
return this.http.delete<any>(`/api/document/${moduleId}/${id}`);
}

setDocument(moduleId, id, data) {
return this.http.post<any>(`/api/document/${moduleId}`, data);
}

url(url: string) {

if (environment.origin) {
Expand All @@ -61,22 +71,8 @@ export class MongoDatabaseService extends DbService {
source?,
collectionGroup?
) {
const sources = {
server: getDocsFromServer,
cache: getDocsFromCache
};
const method = source ? sources[source] : getDocs;

return from(
method(
this.collection(moduleId, pageSize, sort, cursor, filters, collectionGroup)
)
)
.pipe(
map((res: any) =>
res.docs
)
);
console.log(moduleId);
return this.http.get<any>(`/api/documents/${moduleId}`);
}

getStateChanges(
Expand Down Expand Up @@ -209,31 +205,31 @@ export class MongoDatabaseService extends DbService {
);
}

setDocument(moduleId, documentId, data, options) {
return from(
setDoc(
doc(
this.firestore,
moduleId,
documentId
),
data,
options || {}
)
);
}
// setDocument(moduleId, documentId, data, options) {
// return from(
// setDoc(
// doc(
// this.firestore,
// moduleId,
// documentId
// ),
// data,
// options || {}
// )
// );
// }

removeDocument(moduleId, documentId) {
return from(
deleteDoc(
doc(
this.firestore,
moduleId,
documentId
)
)
);
}
// removeDocument(moduleId, documentId) {
// return from(
// deleteDoc(
// doc(
// this.firestore,
// moduleId,
// documentId
// )
// )
// );
// }

createUserAccount(email: string, password: string) {
return this.callFunction('cms-createUser', {email, password}) as any;
Expand Down
18 changes: 15 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as express from "express";
import {MongoClient, ObjectId} from 'mongodb';

const app = express();
const port = 5200;
const port = 4200;

app.use(express.json());

Expand All @@ -20,6 +20,18 @@ app.get('/api/document/:moduleId/:documentId', (req, res) => {
.catch();
});

app.get('/api/documents/:moduleId', (req, res) => {
async function exec() {
const cursor = db.collection(req.params.moduleId).find({});
const data = await cursor.toArray();
console.log(data);
return data
}

exec().then((data) => res.json(data)).catch();

});

app.post('/api/document/:moduleId', (req, res) => {
async function exec() {
return db.collection(req.params.moduleId).insertOne(req.body)
Expand All @@ -40,12 +52,12 @@ app.put('/api/document/:moduleId', (req, res) => {
.catch();
});

app.delete('/api/document/:moduleId', (req, res) => {
app.delete('/api/document/:moduleId/:documentId', (req, res) => {
return db.collection(req.params.moduleId).deleteOne({_id: new ObjectId(req.params.documentId)})
.then((data) => res.json(data))
.catch();
});

app.listen(port, () => {
console.log(`[server]: Server is running at https://localhost:${port}`);
console.log(`[server]: Server is running at http://localhost:${port}`);
});

0 comments on commit 217e92b

Please sign in to comment.