Skip to content

Commit c9b119a

Browse files
authored
feat: Add TypeScript support (#595)
1 parent f9d0f17 commit c9b119a

25 files changed

+1687
-285
lines changed

.dockerignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
.nyc_output
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
28+
node_modules
29+
30+
# Emacs
31+
*~
32+
.eslintcache
33+
34+
# build folder
35+
dist
36+
37+
# Docker files
38+
Docker*

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
APP_ID=myAppId
2+
MASTER_KEY=myMasterKey
3+
MONGODB_URI=mongodb://localhost:27017/parse
4+
PORT=1337
5+
SERVER_URL=http://localhost:1337

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ node_modules
3030
# Emacs
3131
*~
3232
.eslintcache
33+
34+
# build folder
35+
dist
36+
37+
# env files
38+
.env*
39+
!.env.example

Dockerfile

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
FROM node:latest
1+
# Builder stage
2+
FROM node:20.19.1-alpine AS builder
23

3-
RUN mkdir parse
4+
WORKDIR /usr/src/parse
5+
6+
COPY package*.json .
47

5-
ADD . /parse
6-
WORKDIR /parse
78
RUN npm install
89

9-
ENV APP_ID setYourAppId
10-
ENV MASTER_KEY setYourMasterKey
11-
ENV DATABASE_URI setMongoDBURI
10+
COPY . .
1211

13-
# Optional (default : 'parse/cloud/main.js')
14-
# ENV CLOUD_CODE_MAIN cloudCodePath
12+
RUN npm run build
1513

16-
# Optional (default : '/parse')
17-
# ENV PARSE_MOUNT mountPath
14+
# latest supported node version when this Dockerfile was written
15+
FROM node:20.19.1-alpine
1816

19-
EXPOSE 1337
17+
WORKDIR /usr/src/parse
2018

21-
# Uncomment if you want to access cloud code outside of your container
22-
# A main.js file must be present, if not Parse will not start
19+
# Copy only the required files from the builder stage
20+
COPY --from=builder /usr/src/parse/node_modules ./node_modules
21+
COPY --from=builder /usr/src/parse/dist ./
22+
COPY --from=builder /usr/src/parse/public ./public
2323

24-
# VOLUME /parse/cloud
24+
VOLUME ["/usr/src/parse/cloud", "/usr/src/parse/logs"]
25+
26+
EXPOSE 1337
2527

26-
CMD [ "npm", "start" ]
28+
CMD ["node", "index.js"]

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The [Parse Server guide](https://docs.parseplatform.org/parse-server/guide/) is
2323
---
2424

2525
- [Local Development](#local-development)
26+
- [Docker Deployment](#docker-deployment)
2627
- [Helpful Scripts](#helpful-scripts)
2728
- [Remote Deployment](#remote-deployment)
2829
- [Heroku](#heroku)
@@ -50,19 +51,48 @@ The [Parse Server guide](https://docs.parseplatform.org/parse-server/guide/) is
5051
7. By default the API route will use `/parse` as a base. You can change this by setting the environment variable `PARSE_MOUNT`, for example in the CLI run run `export PARSE_MOUNT=/app` to set the path to `app`.
5152
8. Your Parse Server is not running and is connected to your local database named `dev` in which the data is stored that you manage via Parse Server.
5253

54+
## Docker Deployment
55+
56+
You can also run Parse Server using Docker:
57+
58+
1. Create a `.env` file with your configuration variables. For example:
59+
```env
60+
APP_ID=myAppId
61+
MASTER_KEY=myMasterKey
62+
DATABASE_URI=mongodb://localhost:27017/parse
63+
PORT=1337
64+
PARSE_MOUNT=/parse
65+
```
66+
67+
2. Run Docker with the following command, mounting volumes as needed:
68+
```bash
69+
docker build -t parse-server .
70+
docker run -p 1337:1337 --env-file .env \
71+
-v $(pwd)/logs:/usr/src/parse/logs \
72+
-v $(pwd)/cloud:/usr/src/parse/cloud \
73+
parse-server
74+
```
75+
76+
This allows you to:
77+
- Use an environment file for configuration
78+
- Mount the logs directory to persist logs outside the container
79+
- Mount the cloud directory to access your Cloud Code files from the container
80+
81+
You can customize the mounted volumes based on your needs, such as mounting config files or other directories that require persistence or runtime modifications.
82+
5383
## Helpful Scripts
5484
These scripts can help you to develop your app for Parse Server:
5585

5686
* `npm run watch` will start your Parse Server and restart if you make any changes.
57-
* `npm run lint` will check the linting of your cloud code, tests and `index.js`, as defined in `.eslintrc.json`.
58-
* `npm run lint-fix` will attempt fix the linting of your cloud code, tests and `index.js`.
59-
* `npm run prettier` will help improve the formatting and layout of your cloud code, tests and `index.js`, as defined in `.prettierrc`.
87+
* `npm run lint` will check the linting of your cloud code, tests and `index.ts`, as defined in `.eslintrc.json`.
88+
* `npm run lint-fix` will attempt fix the linting of your cloud code, tests and `index.ts`.
89+
* `npm run prettier` will help improve the formatting and layout of your cloud code, tests and `index.ts`, as defined in `.prettierrc`.
6090
* `npm test` will run all tests
6191
* `npm run coverage` will run tests and check coverage. Output is available in the `/coverage` folder.
6292

6393
## Configuration
6494

65-
Configuration is located in `config.js`.
95+
Configuration is located in `config.ts`.
6696

6797

6898
# Remote Deployment
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
Parse.Cloud.define('hello', req => {
2+
// @ts-expect-error req.log exists, but it was not added to types/parse
23
req.log.info(req);
34
return 'Hi';
45
});
56

67
Parse.Cloud.define('helloAsyncFunction', async req => {
78
await new Promise(resolve => setTimeout(resolve, 1000));
9+
// @ts-expect-error req.log exists, but it was not added to types/parse
810
req.log.info(req);
911
return 'Hi async';
1012
});
1113

1214
Parse.Cloud.beforeSave('TestObject', () => {
13-
throw new Parse.Error(9001, 'Saving test objects is not available.');
15+
throw new Parse.Error(Parse.Error.OTHER_CAUSE, 'Saving test objects is not available.');
1416
});
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +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-
await Promise.all([
3-
import('./functions.js')
4-
]);
2+
await Promise.all([import('./functions.js')]);

cloud/schema.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

cloud/schema.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const schemaDefinitions = [
2+
{
3+
className: 'TestObject',
4+
fields: {
5+
beforeSave: { type: 'Boolean', defaultValue: false },
6+
additionalData: { type: 'String' },
7+
},
8+
classLevelPermissions: {
9+
find: { '*': true },
10+
count: { '*': true },
11+
get: { '*': true },
12+
update: { '*': true },
13+
create: { '*': true },
14+
delete: { '*': true },
15+
},
16+
},
17+
];

config.js renamed to config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { schemaDefinitions } from "./cloud/schema.js";
1+
import { schemaDefinitions } from './cloud/schema.js';
2+
23
export const config = {
3-
databaseURI: process.env.DATABASE_URI || process.env.MONGODB_URI || 'mongodb://localhost:27017/dev',
4-
cloud: process.env.CLOUD_CODE_MAIN || './cloud/main.js',
4+
databaseURI:
5+
process.env.DATABASE_URI || process.env.MONGODB_URI || 'mongodb://localhost:27017/dev',
6+
cloud: () => import('./cloud/main.js'),
57
appId: process.env.APP_ID || 'myAppId',
68
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
79
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed

eslint.config.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

eslint.config.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import js from '@eslint/js';
2+
import tseslint from 'typescript-eslint';
3+
import globals from 'globals';
4+
import path from 'node:path';
5+
6+
const __dirname = path.resolve();
7+
8+
export default tseslint.config(
9+
js.configs.recommended,
10+
...tseslint.configs.recommended,
11+
{
12+
files: ['**/*.ts'],
13+
languageOptions: {
14+
ecmaVersion: 2022,
15+
sourceType: 'module',
16+
parser: tseslint.parser,
17+
parserOptions: {
18+
project: ['./tsconfig.json', './spec/tsconfig.json'],
19+
tsconfigRootDir: __dirname,
20+
},
21+
globals: {
22+
...globals.node,
23+
Parse: 'readonly',
24+
},
25+
},
26+
rules: {
27+
'@typescript-eslint/explicit-function-return-type': 'off',
28+
'@typescript-eslint/no-explicit-any': 'warn',
29+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
30+
indent: ['error', 2, { SwitchCase: 1 }],
31+
'linebreak-style': ['error', 'unix'],
32+
'no-trailing-spaces': 'error',
33+
'eol-last': 'error',
34+
'space-in-parens': ['error', 'never'],
35+
'no-multiple-empty-lines': 'warn',
36+
'prefer-const': 'error',
37+
'space-infix-ops': 'error',
38+
'no-useless-escape': 'off',
39+
'require-atomic-updates': 'off',
40+
'no-var': 'warn',
41+
'no-await-in-loop': 'warn',
42+
},
43+
},
44+
{
45+
ignores: ['dist/**/*', 'logs/**/*', 'public/**/*', 'release.config.js'],
46+
}
47+
);

index.js renamed to index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ParseServer } from 'parse-server';
66
import path from 'path';
77
import http from 'http';
88
import { config } from './config.js';
9+
910
const __dirname = path.resolve();
1011
const app = express();
1112

nodemon.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"ignore": [
3+
"dist"
4+
],
5+
"ext": "json,ts,mjs,cjs,js",
6+
"execMap": {
7+
"ts": "tsx"
8+
}
9+
}

0 commit comments

Comments
 (0)