Skip to content

Commit

Permalink
feat: Update to Parse Server 6 syntax (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy authored Apr 14, 2023
1 parent 2d072a6 commit 36ff42b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 76 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
node-version: [16.x]
name: ${{ matrix.name }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependancies
Expand Down
2 changes: 1 addition & 1 deletion cloud/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// It is best practise to organize your cloud functions group into their own file. You can then import them in your main.js.
require('./functions.js');
await import('./functions.js');
46 changes: 19 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// Example express application adding the parse-server module to expose Parse
// compatible API routes.

const express = require('express');
const ParseServer = require('parse-server').ParseServer;
const path = require('path');
const args = process.argv || [];
const test = args.some(arg => arg.includes('jasmine'));

const databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
const config = {
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
import express from 'express';
import { ParseServer } from 'parse-server';
import path from 'path';
const __dirname = path.resolve();
import http from 'http';

export const config = {
databaseURI:
process.env.DATABASE_URI || process.env.MONGODB_URI || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
Expand All @@ -26,16 +22,17 @@ const config = {
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

const app = express();
export const app = express();

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
const mountPath = process.env.PARSE_MOUNT || '/parse';
if (!test) {
const api = new ParseServer(config);
app.use(mountPath, api);
if (!process.env.TESTING) {
const mountPath = process.env.PARSE_MOUNT || '/parse';
const server = new ParseServer(config);
await server.start();
app.use(mountPath, server.app);
}

// Parse Server plays nicely with the rest of your web routes
Expand All @@ -49,17 +46,12 @@ app.get('/test', function (req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});

const port = process.env.PORT || 1337;
if (!test) {
const httpServer = require('http').createServer(app);
if (!process.env.TESTING) {
const port = process.env.PORT || 1337;
const httpServer = http.createServer(app);
httpServer.listen(port, function () {
console.log('parse-server-example running on port ' + port + '.');
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
await ParseServer.createLiveQueryServer(httpServer);
}

module.exports = {
app,
config,
};
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@
"license": "MIT",
"main": "index.js",
"scripts": {
"coverage": "nyc jasmine",
"coverage": "TESTING=true nyc jasmine",
"lint": "eslint --cache ./cloud && eslint --cache index.js && eslint --cache ./spec",
"lint-fix": "eslint --cache --fix ./cloud && eslint --cache --fix index.js && eslint --cache --fix ./spec",
"prettier": "prettier --write '{cloud,spec}/{**/*,*}.js' 'index.js'",
"start": "node index.js",
"test": "mongodb-runner start && jasmine",
"test": "mongodb-runner start && TESTING=true jasmine",
"watch": "nodemon index.js"
},
"dependencies": {
"express": "4.18.1",
"parse": "3.4.2",
"parse-server": "5.2.1"
"axios": "1.3.5",
"express": "4.18.2",
"parse": "4.0.1",
"parse-server": "6.0.0"
},
"type": "module",
"devDependencies": {
"@babel/eslint-parser": "7.17.0",
"eslint": "8.15.0",
"jasmine": "4.1.0",
"mongodb-runner": "4.9.0",
"nodemon": "2.0.16",
"@babel/eslint-parser": "7.21.3",
"eslint": "8.38.0",
"jasmine": "4.6.0",
"mongodb-runner": "4.10.0",
"nodemon": "2.0.22",
"nyc": "15.1.0",
"prettier": "2.6.2"
"prettier": "2.8.7"
},
"engines": {
"node": ">=12.22.10 <19"
Expand Down
13 changes: 5 additions & 8 deletions spec/Tests.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios';
describe('Parse Server example', () => {
Parse.User.enableUnsafeCurrentUser();
it('call function', async () => {
Expand All @@ -20,17 +21,13 @@ describe('Parse Server example', () => {
}
});
it('coverage for /', async () => {
const { text, headers } = await Parse.Cloud.httpRequest({
url: 'http://localhost:30001/',
});
const { data, headers } = await axios.get('http://localhost:30001/');
expect(headers['content-type']).toContain('text/html');
expect(text).toBe('I dream of being a website. Please star the parse-server repo on GitHub!');
expect(data).toBe('I dream of being a website. Please star the parse-server repo on GitHub!');
});
it('coverage for /test', async () => {
const { text, headers } = await Parse.Cloud.httpRequest({
url: 'http://localhost:30001/test',
});
const { data, headers } = await axios.get('http://localhost:30001/test');
expect(headers['content-type']).toContain('text/html');
expect(text).toContain('<title>Parse Server Example</title>');
expect(data).toContain('<title>Parse Server Example</title>');
});
});
6 changes: 1 addition & 5 deletions spec/helper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const Parse = require('parse/node');
Parse.initialize('test');
Parse.serverURL = 'http://localhost:30001/test';
Parse.masterKey = 'test';
const { startParseServer, stopParseServer, dropDB } = require('./utils/test-runner.js');
import { startParseServer, stopParseServer, dropDB } from './utils/test-runner.js';
beforeAll(async () => {
await startParseServer();
}, 100 * 60 * 2);
Expand Down
32 changes: 11 additions & 21 deletions spec/utils/test-runner.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const http = require('http');
const { ParseServer } = require('parse-server');
const { config, app } = require('../../index.js');
const Config = require('../../node_modules/parse-server/lib/Config');
import http from 'http';
import { ParseServer } from 'parse-server';
import { app, config } from '../../index.js';

let parseServerState = {};
const dropDB = async () => {
export const dropDB = async () => {
await Parse.User.logOut();
const app = Config.get('test');
return await app.database.deleteEverything(true);
return await Parse.Server.database.deleteEverything(true);
};
let parseServerState = {};

/**
* Starts the ParseServer instance
* @param {Object} parseServerOptions Used for creating the `ParseServer`
* @return {Promise} Runner state
*/
async function startParseServer() {
export async function startParseServer() {
delete config.databaseAdapter;
const parseServerOptions = Object.assign(config, {
databaseURI: 'mongodb://localhost:27017/parse-test',
Expand All @@ -29,13 +27,13 @@ async function startParseServer() {
silent: true,
});
const parseServer = new ParseServer(parseServerOptions);
app.use(parseServerOptions.mountPath, parseServer);
await parseServer.start();
app.use(parseServerOptions.mountPath, parseServer.app);
const httpServer = http.createServer(app);
await new Promise(resolve => httpServer.listen(parseServerOptions.port, resolve));
Object.assign(parseServerState, {
parseServer,
httpServer,
expressApp: app,
parseServerOptions,
});
return parseServerOptions;
Expand All @@ -45,15 +43,7 @@ async function startParseServer() {
* Stops the ParseServer instance
* @return {Promise}
*/
async function stopParseServer() {
const { httpServer } = parseServerState;
await new Promise(resolve => httpServer.close(resolve));
export async function stopParseServer() {
await new Promise(resolve => parseServerState.httpServer.close(resolve));
parseServerState = {};
}

module.exports = {
dropDB,
startParseServer,
stopParseServer,
parseServerState,
};

0 comments on commit 36ff42b

Please sign in to comment.