Skip to content

Commit

Permalink
Merge branch 'feature/OptionalSchema' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
golflima committed Feb 9, 2018
2 parents 96d27ef + eacc64c commit 5966233
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 29 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ![Virtua SA logo](https://www.virtua.ch/favicon.png) VPX - Virtua Parse eXtender :: Changelog

## 1.0.1

* Add `VpxLoggedEntities` configuration to force *VPX* to monitor only given entities
* *VPX* is now able to run Parse Server intances older than 2.7.2

## 1.0.0 - First public release

*VPX* supports for now :

* Entity operation logging (`created`, `updated`, `deleted`) in `VpxOperationEntityLog`
* Custom cloud code execution (stored in `VpxCustomCloudCode`, called with `vpx-exec-ccc`)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
```
3. Restart your Parse server.

Note : *VPX* requires at least `[email protected]` to work properly, but it has been made compatible in a degraded mode for older versions.

## Usage

*VPX* will monitor every modification made on user Parse entities and log them in the class `VpxEntityOperationLog`:
Expand All @@ -37,6 +39,7 @@ These parameters can be created with Parse Dashboard in `Config > Create a param
| Parameter | Type | Default value | Description
| :-------------------- | :------- | :---------------------------------- | :-------------------------------------------
| `VpxCustomCloudCode` | `String` | `null` | Your custom cloud code to run on Parse server
| `VpxLoggedEntities` | `Array` | `null` | Names of entities to monitor, use `null` to let *VPX* discover new entities
| `VpxLoggedOperations` | `Array` | `["created", "updated", "deleted"]` | Operation types allowed to be logged

## Jobs
Expand Down
78 changes: 52 additions & 26 deletions cloud/virtua-parse-extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
//var Parse = require('parse/node');

function VirtuaParseExtended() {
/**
* Operation on these entities are logged by VPX.
* @type string[]
*/
var currentLoggedOperationEntities = [];

/**
* Custom script that can be evaluated.
* @private
Expand All @@ -37,9 +43,11 @@ function VirtuaParseExtended() {

/**
* Operation on these entities will be logged by VPX.
* An empty array means no entities will be monitored,
* a null value means all entities will be monitored.
* @type string[]
*/
var loggedOperationEntities = [];
var loggedOperationEntities = null;

/**
* Initialize VPX.
Expand All @@ -48,15 +56,19 @@ function VirtuaParseExtended() {
this.init = function() {
console.info('[VPX] Initializing ...');
// Create the VpxEntityOperationLog collection if needed
var schema = new Parse.Schema('VpxEntityOperationLog');
schema.get().catch(function(error) {
const schema = new Parse.Schema('VpxEntityOperationLog');
schema.addString('targetClass')
.addString('targetId')
.addString('operation');
schema.save();
console.info('[VPX] VpxEntityOperationLog schema created.');
});
if (typeof Parse.Schema === "function") {
var schema = new Parse.Schema('VpxEntityOperationLog');
schema.get().catch(function(error) {
const schema = new Parse.Schema('VpxEntityOperationLog');
schema.addString('targetClass')
.addString('targetId')
.addString('operation');
schema.save();
console.info('[VPX] VpxEntityOperationLog schema created.');
});
} else {
console.warn('[VPX] VpxEntityOperationLog schema cannot be created, please upgrade to [email protected] !');
}
// Register jobs
Parse.Cloud.job('vpx-reload', reloadJob);
Parse.Cloud.job('vpx-exec-ccc', execCustomCloudCodeJob);
Expand Down Expand Up @@ -114,33 +126,47 @@ function VirtuaParseExtended() {
});
}

/**
* Register triggers on user Parse entities.
* @argument object entity
*/
monitorEntityOperation = function(className) {
// Skip Parse Core entities, the log itself and already monitored entities
if (!(className.startsWith('_') || className == 'VpxEntityOperationLog' || currentLoggedOperationEntities.includes(className))) {
console.info('[VPX] Processing : ' + className + ' ...');
Parse.Cloud.afterSave(className, afterSaveTrigger);
Parse.Cloud.afterDelete(className, afterDeleteTrigger);
currentLoggedOperationEntities.push(className);
console.info('[VPX] Registred entity for operation logging: ' + className);
}
}

/**
* Loads configuration of Virtua Parse eXtended.
* @returns void
*/
load = function() {
console.log('[VPX] Already monitored entities: ' + loggedOperationEntities);
// Register the triggers for every entities managed by Parse
Parse.Schema.all().then(function(result) {
result.forEach(function(entity) {
var className = entity.className;
// Skip Parse Core entities, the log itself and already monitored entities
if (!(className.startsWith('_') || className == 'VpxEntityOperationLog' || loggedOperationEntities.includes(className))) {
console.info('[VPX] Processing : ' + className + ' ...');
Parse.Cloud.afterSave(className, afterSaveTrigger);
Parse.Cloud.afterDelete(className, afterDeleteTrigger);
loggedOperationEntities.push(className);
console.info('[VPX] Registred entity for operation logging: ' + className);
}
});
console.log('[VPX] Monitored entities: ' + loggedOperationEntities);
});
// Load configuration
Parse.Config.get().then(function(config) {
customCloudCode = config.get('VpxCustomCloudCode') || customCloudCode;
loggedOperationEntities = config.get('VpxLoggedEntities') || loggedOperationEntities;
loggedOperationTypes = config.get('VpxLoggedOperations') || loggedOperationTypes;
console.info('[VPX] VpxCustomCloudCode: ' + customCloudCode);
console.info('[VPX] VpxLoggedEntities: ' + loggedOperationEntities);
console.info('[VPX] VpxLoggedOperations: ' + loggedOperationTypes);
console.info('[VPX] Already monitored entities: ' + currentLoggedOperationEntities);
// Register the triggers for every entities managed by Parse
if (loggedOperationEntities === null && typeof Parse.Schema === "function") {
console.info('[VPX] Discovering new entities to monitor ...');
Parse.Schema.all().then(function(result) {
result.map(x => x.className).forEach(monitorEntityOperation);
console.info('[VPX] Monitored entities: ' + currentLoggedOperationEntities);
});
} else {
console.info('[VPX] Using value of VpxLoggedEntities to register new entities to monitor ...');
loggedOperationEntities.forEach(monitorEntityOperation);
console.info('[VPX] Monitored entities: ' + currentLoggedOperationEntities);
}
});
}

Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mongo:
image: mongo
image: mongo:3.6
labels:
- "com.dnsdock.alias=mongo.vpx.docker"

parse-server:
image: parseplatform/parse-server
image: parseplatform/parse-server:2.7.2
environment:
- PARSE_SERVER_APPLICATION_ID=VPX-Test
- PARSE_SERVER_CLOUD=/parse-server/cloud/main.js
Expand All @@ -22,7 +22,7 @@ parse-server:
- ./logs:/parse-server/logs

parse-dashboard:
image: parseplatform/parse-dashboard
image: parseplatform/parse-dashboard:1.1.2
environment:
- PARSE_DASHBOARD_ALLOW_INSECURE_HTTP=1
- PARSE_DASHBOARD_APP_ID=VPX-Test
Expand Down

0 comments on commit 5966233

Please sign in to comment.