Commit 409bb8ee authored by David Hardy's avatar David Hardy

Converted to node 10 and triggers

parent d067893b
......@@ -17,7 +17,7 @@
"clean": "rimraf ./node_modules && rimraf ./lib"
},
"engines": {
"node": "8"
"node": "10"
},
"main": "lib/src/index.js",
"dependencies": {
......
......@@ -2,9 +2,9 @@ import {Command} from './command';
import {from, OperatorFunction} from 'rxjs';
import {switchMap, tap} from 'rxjs/operators';
import {Logger} from '../environment/logger';
import {DatabaseService} from '../facade/database.service';
import {DatabaseService} from '../environment/database.service';
import {ImageData} from "../../.mono/model/image-data.model";
import {StorageService} from "../facade/storage.service";
import {StorageService} from "../environment/storage.service";
export class ImageDataDeletedCommand implements Command {
......
......@@ -3,9 +3,9 @@ import {ObjectMetadata} from 'firebase-functions/lib/providers/storage';
import {map, switchMap, tap} from 'rxjs/operators';
import {Command} from './command';
import {PlatformEnvironment} from '../environment/platform-environment';
import {DatabaseService} from '../facade/database.service';
import {DatabaseService} from '../environment/database.service';
import Jimp = require('jimp');
import {StorageService} from "../facade/storage.service";
import {StorageService} from "../environment/storage.service";
export class ImageUploadedCommand implements Command {
......
......@@ -2,7 +2,7 @@ import {Command} from './command';
import {OperatorFunction} from 'rxjs';
import {map, switchMap, tap} from 'rxjs/operators';
import {Logger} from '../environment/logger';
import {DatabaseService} from '../facade/database.service';
import {DatabaseService} from '../environment/database.service';
import {VoteContainer} from "../../.mono/model/vote.model";
import {ImageData} from "../../.mono/model/image-data.model";
......
import * as functions from 'firebase-functions';
import {StorageService} from './facade/storage.service';
import {take} from 'rxjs/operators';
import {Command} from './commands/command';
import {EMPTY, from, of} from 'rxjs';
import {ImageDataDeletedCommand} from './commands/image-data-deleted.command';
import {Logger} from './environment/logger';
import {ImageUploadedCommand} from './commands/image-uploaded.command';
import {DatabaseService} from './facade/database.service';
import {PlatformEnvironment} from './environment/platform-environment';
import * as admin from 'firebase-admin';
import {VoteCastedCommand, VoteCastedCommandData} from './commands/vote-casted.command';
import {VoteContainer} from "../.mono/model/vote.model";
import {storageFinalizeTrigger} from "./triggers/storage.trigger";
import {httpEchoTrigger} from "./triggers/https.trigger";
import {firestoreImageDeletedTrigger} from "./triggers/firestore/firestore-images.trigger";
import {firestoreImageVoteWriteTrigger} from "./triggers/firestore/firestore-image-vote.trigger";
admin.initializeApp();
admin.firestore().settings({timestampsInSnapshots: true});
function executeCommand(command: Command): Promise<any> {
return (of(1) as any).pipe(
...command.pipeline,
take(1)
).toPromise();
}
exports.https = {
echo: httpEchoTrigger
};
const cors = require('cors')({origin: true});
export const echo = functions.https.onRequest((request, response) => {
const id = request.params['id'] || 0;
from(admin.firestore().doc(`test/test${id}`).get())
.subscribe(doc => {
const count = (doc.data() || {count: 0}).count + 1;
admin.firestore().doc(`test/test${id}`).set({count});
cors(request, response, () => {
response.status(200).send(`{"message": "Hello from Firebase!", "count":${count}, "id":${id}}`)
})
});
});
exports.storage = {
finalize: storageFinalizeTrigger
};
export const storageFinalizeFunctions = functions.storage.object().onFinalize((object, context) => {
if (ImageUploadedCommand.CAN_EXECUTE(object)) {
return executeCommand(new ImageUploadedCommand(object, new StorageService(), new DatabaseService(), new PlatformEnvironment()));
} else {
console.log(`No command for object ${object.name}, with eventType ${context.eventType}`);
return EMPTY.toPromise();
}
});
export const deleteImages = functions.firestore.document(ImageDataDeletedCommand.PATH).onDelete((snapshot) => {
return executeCommand(new ImageDataDeletedCommand(snapshot.data(), new StorageService(), new DatabaseService(), new Logger()));
});
export const voteAggregate = functions.firestore.document(VoteCastedCommand.PATH).onWrite((snapshot, context) => {
return executeCommand(new VoteCastedCommand(
context.params as VoteCastedCommandData,
snapshot.before.data() as VoteContainer,
snapshot.after.data() as VoteContainer,
new DatabaseService(),
new Logger()
));
});
exports.firestore = {
imageDeleted: firestoreImageDeletedTrigger,
imageVoteWrite: firestoreImageVoteWriteTrigger
};
import * as functions from "firebase-functions";
import {VoteCastedCommand, VoteCastedCommandData} from "../../commands/vote-casted.command";
import {VoteContainer} from "../../../.mono/model/vote.model";
import {DatabaseService} from "../../environment/database.service";
import {Logger} from "../../environment/logger";
import {Command} from "../../commands/command";
import {of} from "rxjs";
import {take} from "rxjs/operators";
function executeCommand(command: Command): Promise<any> {
return (of(1) as any).pipe(
...command.pipeline,
take(1)
).toPromise();
}
export const firestoreImageVoteWriteTrigger = functions
.region('europe-west1')
.firestore.document(VoteCastedCommand.PATH).onWrite((snapshot, context) => {
return executeCommand(new VoteCastedCommand(
context.params as VoteCastedCommandData,
snapshot.before.data() as VoteContainer,
snapshot.after.data() as VoteContainer,
new DatabaseService(),
new Logger()
));
});
import * as functions from "firebase-functions";
import {ImageDataDeletedCommand} from "../../commands/image-data-deleted.command";
import {StorageService} from "../../environment/storage.service";
import {DatabaseService} from "../../environment/database.service";
import {Logger} from "../../environment/logger";
import {Command} from "../../commands/command";
import {of} from "rxjs";
import {take} from "rxjs/operators";
function executeCommand(command: Command): Promise<any> {
return (of(1) as any).pipe(
...command.pipeline,
take(1)
).toPromise();
}
export const firestoreImageDeletedTrigger = functions
.region('europe-west1')
.firestore.document(ImageDataDeletedCommand.PATH).onDelete((snapshot) => {
return executeCommand(new ImageDataDeletedCommand(snapshot.data(), new StorageService(), new DatabaseService(), new Logger()));
});
import * as functions from "firebase-functions";
import {from} from "rxjs";
import * as admin from "firebase-admin";
const cors = require('cors')({origin: true});
export const httpEchoTrigger = functions
.region('europe-west1')
.https.onRequest((request, response) => {
const id = request.params['id'] || 0;
from(admin.firestore().doc(`test/test${id}`).get())
.subscribe(doc => {
const count = (doc.data() || {count: 0}).count + 1;
admin.firestore().doc(`test/test${id}`).set({count});
cors(request, response, () => {
response.status(200).send(`{"message": "Hello from Firebase!", "count":${count}, "id":${id}}`)
})
});
});
import * as functions from "firebase-functions";
import {ImageUploadedCommand} from "../commands/image-uploaded.command";
import {StorageService} from "../environment/storage.service";
import {DatabaseService} from "../environment/database.service";
import {PlatformEnvironment} from "../environment/platform-environment";
import {EMPTY, of} from "rxjs";
import {Command} from "../commands/command";
import {take} from "rxjs/operators";
function executeCommand(command: Command): Promise<any> {
return (of(1) as any).pipe(
...command.pipeline,
take(1)
).toPromise();
}
export const storageFinalizeTrigger = functions
.region('europe-west1')
.runWith({timeoutSeconds: 540, memory: '2GB'})
.storage.object().onFinalize((object, context) => {
if (ImageUploadedCommand.CAN_EXECUTE(object)) {
return executeCommand(new ImageUploadedCommand(object, new StorageService(), new DatabaseService(), new PlatformEnvironment()));
} else {
console.log(`No command for object ${object.name}, with eventType ${context.eventType}`);
return EMPTY.toPromise();
}
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment