Skip to content

Commit 36ff42b

Browse files
authored
feat: Update to Parse Server 6 syntax (#427)
1 parent 2d072a6 commit 36ff42b

File tree

7 files changed

+53
-76
lines changed

7 files changed

+53
-76
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
node-version: [12.x]
15+
node-version: [16.x]
1616
name: ${{ matrix.name }}
1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v3
1919
- name: Use Node.js ${{ matrix.node-version }}
20-
uses: actions/setup-node@v1
20+
uses: actions/setup-node@v3
2121
with:
2222
node-version: ${{ matrix.node-version }}
2323
- name: Install Dependancies

cloud/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// It is best practise to organize your cloud functions group into their own file. You can then import them in your main.js.
2-
require('./functions.js');
2+
await import('./functions.js');

index.js

+19-27
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
// Example express application adding the parse-server module to expose Parse
22
// compatible API routes.
33

4-
const express = require('express');
5-
const ParseServer = require('parse-server').ParseServer;
6-
const path = require('path');
7-
const args = process.argv || [];
8-
const test = args.some(arg => arg.includes('jasmine'));
9-
10-
const databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
11-
12-
if (!databaseUri) {
13-
console.log('DATABASE_URI not specified, falling back to localhost.');
14-
}
15-
const config = {
16-
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
4+
import express from 'express';
5+
import { ParseServer } from 'parse-server';
6+
import path from 'path';
7+
const __dirname = path.resolve();
8+
import http from 'http';
9+
10+
export const config = {
11+
databaseURI:
12+
process.env.DATABASE_URI || process.env.MONGODB_URI || 'mongodb://localhost:27017/dev',
1713
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
1814
appId: process.env.APP_ID || 'myAppId',
1915
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
@@ -26,16 +22,17 @@ const config = {
2622
// If you wish you require them, you can set them as options in the initialization above:
2723
// javascriptKey, restAPIKey, dotNetKey, clientKey
2824

29-
const app = express();
25+
export const app = express();
3026

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

3430
// Serve the Parse API on the /parse URL prefix
35-
const mountPath = process.env.PARSE_MOUNT || '/parse';
36-
if (!test) {
37-
const api = new ParseServer(config);
38-
app.use(mountPath, api);
31+
if (!process.env.TESTING) {
32+
const mountPath = process.env.PARSE_MOUNT || '/parse';
33+
const server = new ParseServer(config);
34+
await server.start();
35+
app.use(mountPath, server.app);
3936
}
4037

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

52-
const port = process.env.PORT || 1337;
53-
if (!test) {
54-
const httpServer = require('http').createServer(app);
49+
if (!process.env.TESTING) {
50+
const port = process.env.PORT || 1337;
51+
const httpServer = http.createServer(app);
5552
httpServer.listen(port, function () {
5653
console.log('parse-server-example running on port ' + port + '.');
5754
});
5855
// This will enable the Live Query real-time server
59-
ParseServer.createLiveQueryServer(httpServer);
56+
await ParseServer.createLiveQueryServer(httpServer);
6057
}
61-
62-
module.exports = {
63-
app,
64-
config,
65-
};

package.json

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@
99
"license": "MIT",
1010
"main": "index.js",
1111
"scripts": {
12-
"coverage": "nyc jasmine",
12+
"coverage": "TESTING=true nyc jasmine",
1313
"lint": "eslint --cache ./cloud && eslint --cache index.js && eslint --cache ./spec",
1414
"lint-fix": "eslint --cache --fix ./cloud && eslint --cache --fix index.js && eslint --cache --fix ./spec",
1515
"prettier": "prettier --write '{cloud,spec}/{**/*,*}.js' 'index.js'",
1616
"start": "node index.js",
17-
"test": "mongodb-runner start && jasmine",
17+
"test": "mongodb-runner start && TESTING=true jasmine",
1818
"watch": "nodemon index.js"
1919
},
2020
"dependencies": {
21-
"express": "4.18.1",
22-
"parse": "3.4.2",
23-
"parse-server": "5.2.1"
21+
"axios": "1.3.5",
22+
"express": "4.18.2",
23+
"parse": "4.0.1",
24+
"parse-server": "6.0.0"
2425
},
26+
"type": "module",
2527
"devDependencies": {
26-
"@babel/eslint-parser": "7.17.0",
27-
"eslint": "8.15.0",
28-
"jasmine": "4.1.0",
29-
"mongodb-runner": "4.9.0",
30-
"nodemon": "2.0.16",
28+
"@babel/eslint-parser": "7.21.3",
29+
"eslint": "8.38.0",
30+
"jasmine": "4.6.0",
31+
"mongodb-runner": "4.10.0",
32+
"nodemon": "2.0.22",
3133
"nyc": "15.1.0",
32-
"prettier": "2.6.2"
34+
"prettier": "2.8.7"
3335
},
3436
"engines": {
3537
"node": ">=12.22.10 <19"

spec/Tests.spec.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
describe('Parse Server example', () => {
23
Parse.User.enableUnsafeCurrentUser();
34
it('call function', async () => {
@@ -20,17 +21,13 @@ describe('Parse Server example', () => {
2021
}
2122
});
2223
it('coverage for /', async () => {
23-
const { text, headers } = await Parse.Cloud.httpRequest({
24-
url: 'http://localhost:30001/',
25-
});
24+
const { data, headers } = await axios.get('http://localhost:30001/');
2625
expect(headers['content-type']).toContain('text/html');
27-
expect(text).toBe('I dream of being a website. Please star the parse-server repo on GitHub!');
26+
expect(data).toBe('I dream of being a website. Please star the parse-server repo on GitHub!');
2827
});
2928
it('coverage for /test', async () => {
30-
const { text, headers } = await Parse.Cloud.httpRequest({
31-
url: 'http://localhost:30001/test',
32-
});
29+
const { data, headers } = await axios.get('http://localhost:30001/test');
3330
expect(headers['content-type']).toContain('text/html');
34-
expect(text).toContain('<title>Parse Server Example</title>');
31+
expect(data).toContain('<title>Parse Server Example</title>');
3532
});
3633
});

spec/helper.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
const Parse = require('parse/node');
2-
Parse.initialize('test');
3-
Parse.serverURL = 'http://localhost:30001/test';
4-
Parse.masterKey = 'test';
5-
const { startParseServer, stopParseServer, dropDB } = require('./utils/test-runner.js');
1+
import { startParseServer, stopParseServer, dropDB } from './utils/test-runner.js';
62
beforeAll(async () => {
73
await startParseServer();
84
}, 100 * 60 * 2);

spec/utils/test-runner.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
const http = require('http');
2-
const { ParseServer } = require('parse-server');
3-
const { config, app } = require('../../index.js');
4-
const Config = require('../../node_modules/parse-server/lib/Config');
1+
import http from 'http';
2+
import { ParseServer } from 'parse-server';
3+
import { app, config } from '../../index.js';
54

6-
let parseServerState = {};
7-
const dropDB = async () => {
5+
export const dropDB = async () => {
86
await Parse.User.logOut();
9-
const app = Config.get('test');
10-
return await app.database.deleteEverything(true);
7+
return await Parse.Server.database.deleteEverything(true);
118
};
9+
let parseServerState = {};
1210

1311
/**
1412
* Starts the ParseServer instance
1513
* @param {Object} parseServerOptions Used for creating the `ParseServer`
1614
* @return {Promise} Runner state
1715
*/
18-
async function startParseServer() {
16+
export async function startParseServer() {
1917
delete config.databaseAdapter;
2018
const parseServerOptions = Object.assign(config, {
2119
databaseURI: 'mongodb://localhost:27017/parse-test',
@@ -29,13 +27,13 @@ async function startParseServer() {
2927
silent: true,
3028
});
3129
const parseServer = new ParseServer(parseServerOptions);
32-
app.use(parseServerOptions.mountPath, parseServer);
30+
await parseServer.start();
31+
app.use(parseServerOptions.mountPath, parseServer.app);
3332
const httpServer = http.createServer(app);
3433
await new Promise(resolve => httpServer.listen(parseServerOptions.port, resolve));
3534
Object.assign(parseServerState, {
3635
parseServer,
3736
httpServer,
38-
expressApp: app,
3937
parseServerOptions,
4038
});
4139
return parseServerOptions;
@@ -45,15 +43,7 @@ async function startParseServer() {
4543
* Stops the ParseServer instance
4644
* @return {Promise}
4745
*/
48-
async function stopParseServer() {
49-
const { httpServer } = parseServerState;
50-
await new Promise(resolve => httpServer.close(resolve));
46+
export async function stopParseServer() {
47+
await new Promise(resolve => parseServerState.httpServer.close(resolve));
5148
parseServerState = {};
5249
}
53-
54-
module.exports = {
55-
dropDB,
56-
startParseServer,
57-
stopParseServer,
58-
parseServerState,
59-
};

0 commit comments

Comments
 (0)