From 36ff42b41c1298c36de5cfba89172345eba42894 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 14 Apr 2023 20:42:22 +1000 Subject: [PATCH] feat: Update to Parse Server 6 syntax (#427) --- .github/workflows/ci.yml | 6 ++--- cloud/main.js | 2 +- index.js | 46 ++++++++++++++++----------------------- package.json | 24 ++++++++++---------- spec/Tests.spec.js | 13 +++++------ spec/helper.js | 6 +---- spec/utils/test-runner.js | 32 ++++++++++----------------- 7 files changed, 53 insertions(+), 76 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a3d4ae74b..9d8f921e29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/cloud/main.js b/cloud/main.js index 7c8d64a859..6d2f55bff5 100644 --- a/cloud/main.js +++ b/cloud/main.js @@ -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'); diff --git a/index.js b/index.js index e720980bf0..2baf852c93 100644 --- a/index.js +++ b/index.js @@ -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! @@ -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 @@ -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, -}; diff --git a/package.json b/package.json index edcc175b17..c563070fb4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/spec/Tests.spec.js b/spec/Tests.spec.js index 8893f067f8..dcd01f33ec 100644 --- a/spec/Tests.spec.js +++ b/spec/Tests.spec.js @@ -1,3 +1,4 @@ +import axios from 'axios'; describe('Parse Server example', () => { Parse.User.enableUnsafeCurrentUser(); it('call function', async () => { @@ -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('Parse Server Example'); + expect(data).toContain('Parse Server Example'); }); }); diff --git a/spec/helper.js b/spec/helper.js index a68b934dfb..a127c9e39e 100644 --- a/spec/helper.js +++ b/spec/helper.js @@ -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); diff --git a/spec/utils/test-runner.js b/spec/utils/test-runner.js index dd6eec343f..bab4a77b49 100644 --- a/spec/utils/test-runner.js +++ b/spec/utils/test-runner.js @@ -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', @@ -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; @@ -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, -};