Skip to content

Commit

Permalink
refactor: add missing schema definitions (parse-community#7917)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumouls authored and mtrezza committed May 1, 2022
1 parent e47b61d commit a169663
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 69 deletions.
36 changes: 19 additions & 17 deletions resources/buildConfigDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@ const nestedOptionTypes = [
'PagesRoute',
'PasswordPolicyOptions',
'SecurityOptions',
'SchemaOptions',
];

/** The prefix of environment variables for nested options. */
const nestedOptionEnvPrefix = {
'AccountLockoutOptions' : 'PARSE_SERVER_ACCOUNT_LOCKOUT_',
'CustomPagesOptions' : 'PARSE_SERVER_CUSTOM_PAGES_',
'AccountLockoutOptions': 'PARSE_SERVER_ACCOUNT_LOCKOUT_',
'CustomPagesOptions': 'PARSE_SERVER_CUSTOM_PAGES_',
'DatabaseOptions': 'PARSE_SERVER_DATABASE_',
'FileUploadOptions' : 'PARSE_SERVER_FILE_UPLOAD_',
'IdempotencyOptions' : 'PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_',
'LiveQueryOptions' : 'PARSE_SERVER_LIVEQUERY_',
'LiveQueryServerOptions' : 'PARSE_LIVE_QUERY_SERVER_',
'PagesCustomUrlsOptions' : 'PARSE_SERVER_PAGES_CUSTOM_URL_',
'PagesOptions' : 'PARSE_SERVER_PAGES_',
'FileUploadOptions': 'PARSE_SERVER_FILE_UPLOAD_',
'IdempotencyOptions': 'PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_',
'LiveQueryOptions': 'PARSE_SERVER_LIVEQUERY_',
'LiveQueryServerOptions': 'PARSE_LIVE_QUERY_SERVER_',
'PagesCustomUrlsOptions': 'PARSE_SERVER_PAGES_CUSTOM_URL_',
'PagesOptions': 'PARSE_SERVER_PAGES_',
'PagesRoute': 'PARSE_SERVER_PAGES_ROUTE_',
'ParseServerOptions' : 'PARSE_SERVER_',
'PasswordPolicyOptions' : 'PARSE_SERVER_PASSWORD_POLICY_',
'ParseServerOptions': 'PARSE_SERVER_',
'PasswordPolicyOptions': 'PARSE_SERVER_PASSWORD_POLICY_',
'SecurityOptions': 'PARSE_SERVER_SECURITY_',
'SchemaOptions': 'PARSE_SERVER_SCHEMA_',
};

function last(array) {
Expand All @@ -50,7 +52,7 @@ const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
function toENV(key) {
let str = '';
let previousIsUpper = false;
for(let i = 0; i < key.length; i++) {
for (let i = 0; i < key.length; i++) {
const char = key[i];
if (letters.indexOf(char) >= 0) {
if (!previousIsUpper) {
Expand Down Expand Up @@ -273,8 +275,8 @@ function inject(t, list) {
return { results, comments };
}

const makeRequire = function(variableName, module, t) {
const decl = t.variableDeclarator(t.identifier(variableName), t.callExpression(t.identifier('require'), [t.stringLiteral(module)]));
const makeRequire = function (variableName, module, t) {
const decl = t.variableDeclarator(t.identifier(variableName), t.callExpression(t.identifier('require'), [t.stringLiteral(module)]));
return t.variableDeclaration('var', [decl])
}
let docs = ``;
Expand All @@ -283,14 +285,14 @@ const plugin = function (babel) {
const moduleExports = t.memberExpression(t.identifier('module'), t.identifier('exports'));
return {
visitor: {
ImportDeclaration: function(path) {
ImportDeclaration: function (path) {
path.remove();
},
Program: function(path) {
Program: function (path) {
// Inject the parser's loader
path.unshiftContainer('body', makeRequire('parsers', './parsers', t));
},
ExportDeclaration: function(path) {
ExportDeclaration: function (path) {
// Export declaration on an interface
if (path.node && path.node.declaration && path.node.declaration.type == 'InterfaceDeclaration') {
const { results, comments } = inject(t, doInterface(path.node.declaration));
Expand All @@ -313,6 +315,6 @@ Do not edit manually, but update Options/index.js
`

const babel = require("@babel/core");
const res = babel.transformFileSync('./src/Options/index.js', { plugins: [ plugin, '@babel/transform-flow-strip-types' ], babelrc: false, auxiliaryCommentBefore, sourceMaps: false });
const res = babel.transformFileSync('./src/Options/index.js', { plugins: [plugin, '@babel/transform-flow-strip-types'], babelrc: false, auxiliaryCommentBefore, sourceMaps: false });
require('fs').writeFileSync('./src/Options/Definitions.js', res.code + '\n');
require('fs').writeFileSync('./src/Options/docs.js', docs);
89 changes: 50 additions & 39 deletions src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,51 @@ Do not edit manually, but update Options/index.js
*/
var parsers = require('./parsers');

module.exports.SchemaOptions = {
afterMigration: {
env: 'PARSE_SERVER_SCHEMA_AFTER_MIGRATION',
help: 'Execute a callback after running schema migrations.',
},
beforeMigration: {
env: 'PARSE_SERVER_SCHEMA_BEFORE_MIGRATION',
help: 'Execute a callback before running schema migrations.',
},
definitions: {
env: 'PARSE_SERVER_SCHEMA_DEFINITIONS',
help:
'Rest representation on Parse.Schema https://docs.parseplatform.org/rest/guide/#adding-a-schema',
required: true,
action: parsers.objectParser,
default: [],
},
deleteExtraFields: {
env: 'PARSE_SERVER_SCHEMA_DELETE_EXTRA_FIELDS',
help:
'Is true if Parse Server should delete any fields not defined in a schema definition. This should only be used during development.',
action: parsers.booleanParser,
default: false,
},
lockSchemas: {
env: 'PARSE_SERVER_SCHEMA_LOCK_SCHEMAS',
help:
'Is true if Parse Server will reject any attempts to modify the schema while the server is running.',
action: parsers.booleanParser,
default: false,
},
recreateModifiedFields: {
env: 'PARSE_SERVER_SCHEMA_RECREATE_MODIFIED_FIELDS',
help:
'Is true if Parse Server should recreate any fields that are different between the current database schema and theschema definition. This should only be used during development.',
action: parsers.booleanParser,
default: false,
},
strict: {
env: 'PARSE_SERVER_SCHEMA_STRICT',
help: 'Is true if Parse Server should exit if schema update fail.',
action: parsers.booleanParser,
default: false,
},
};
module.exports.ParseServerOptions = {
accountLockout: {
env: 'PARSE_SERVER_ACCOUNT_LOCKOUT',
Expand Down Expand Up @@ -385,6 +430,11 @@ module.exports.ParseServerOptions = {
action: parsers.booleanParser,
default: false,
},
schema: {
env: 'PARSE_SERVER_SCHEMA',
help: 'Defined schema',
action: parsers.objectParser,
},
security: {
env: 'PARSE_SERVER_SECURITY',
help: 'The security options to identify and report weak security settings.',
Expand Down Expand Up @@ -464,45 +514,6 @@ module.exports.SecurityOptions = {
default: false,
},
};
module.exports.SchemaOptions = {
definitions: {
help: 'The schema definitions.',
default: [],
},
strict: {
env: 'PARSE_SERVER_SCHEMA_STRICT',
help: 'Is true if Parse Server should exit if schema update fail.',
action: parsers.booleanParser,
default: true,
},
deleteExtraFields: {
env: 'PARSE_SERVER_SCHEMA_DELETE_EXTRA_FIELDS',
help:
'Is true if Parse Server should delete any fields not defined in a schema definition. This should only be used during development.',
action: parsers.booleanParser,
default: false,
},
recreateModifiedFields: {
env: 'PARSE_SERVER_SCHEMA_RECREATE_MODIFIED_FIELDS',
help:
'Is true if Parse Server should recreate any fields that are different between the current database schema and theschema definition. This should only be used during development.',
action: parsers.booleanParser,
default: false,
},
lockSchemas: {
env: 'PARSE_SERVER_SCHEMA_LOCK',
help:
'Is true if Parse Server will reject any attempts to modify the schema while the server is running.',
action: parsers.booleanParser,
default: false,
},
beforeMigration: {
help: 'Execute a callback before running schema migrations.',
},
afterMigration: {
help: 'Execute a callback after running schema migrations.',
},
};
module.exports.PagesOptions = {
customRoutes: {
env: 'PARSE_SERVER_PAGES_CUSTOM_ROUTES',
Expand Down
12 changes: 12 additions & 0 deletions src/Options/docs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @interface SchemaOptions
* @property {Function} afterMigration Execute a callback after running schema migrations.
* @property {Function} beforeMigration Execute a callback before running schema migrations.
* @property {Any} definitions Rest representation on Parse.Schema https://docs.parseplatform.org/rest/guide/#adding-a-schema
* @property {Boolean} deleteExtraFields Is true if Parse Server should delete any fields not defined in a schema definition. This should only be used during development.
* @property {Boolean} lockSchemas Is true if Parse Server will reject any attempts to modify the schema while the server is running.
* @property {Boolean} recreateModifiedFields Is true if Parse Server should recreate any fields that are different between the current database schema and theschema definition. This should only be used during development.
* @property {Boolean} strict Is true if Parse Server should exit if schema update fail.
*/

/**
* @interface ParseServerOptions
* @property {AccountLockoutOptions} accountLockout The account lockout policy for failed login attempts.
Expand Down Expand Up @@ -68,6 +79,7 @@
* @property {String} restAPIKey Key for REST calls
* @property {Boolean} revokeSessionOnPasswordReset When a user changes their password, either through the reset password email or while logged in, all sessions are revoked if this is true. Set to false if you don't want to revoke sessions.
* @property {Boolean} scheduledPush Configuration for push scheduling, defaults to false.
* @property {SchemaOptions} schema Defined schema
* @property {SecurityOptions} security The security options to identify and report weak security settings.
* @property {Function} serverCloseComplete Callback when server has closed
* @property {Function} serverStartComplete Callback when server has started
Expand Down
27 changes: 25 additions & 2 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,28 @@ import { MailAdapter } from '../Adapters/Email/MailAdapter';
import { PubSubAdapter } from '../Adapters/PubSub/PubSubAdapter';
import { WSSAdapter } from '../Adapters/WebSocketServer/WSSAdapter';
import { CheckGroup } from '../Security/CheckGroup';
import type { SchemaOptions } from '../SchemaMigrations/Migrations';

export interface SchemaOptions {
/* Rest representation on Parse.Schema https://docs.parseplatform.org/rest/guide/#adding-a-schema
:DEFAULT: [] */
definitions: any;
/* Is true if Parse Server should exit if schema update fail.
:DEFAULT: false */
strict: ?boolean;
/* Is true if Parse Server should delete any fields not defined in a schema definition. This should only be used during development.
:DEFAULT: false */
deleteExtraFields: ?boolean;
/* Is true if Parse Server should recreate any fields that are different between the current database schema and theschema definition. This should only be used during development.
:DEFAULT: false */
recreateModifiedFields: ?boolean;
/* Is true if Parse Server will reject any attempts to modify the schema while the server is running.
:DEFAULT: false */
lockSchemas: ?boolean;
/* Execute a callback before running schema migrations. */
beforeMigration: ?() => void | Promise<void>;
/* Execute a callback after running schema migrations. */
afterMigration: ?() => void | Promise<void>;
}

type Adapter<T> = string | any | T;
type NumberOrBoolean = number | boolean;
Expand Down Expand Up @@ -246,7 +267,9 @@ export interface ParseServerOptions {
playgroundPath: ?string;
/* Callback when server has started */
serverStartComplete: ?(error: ?Error) => void;
/* Rest representation on Parse.Schema https://docs.parseplatform.org/rest/guide/#adding-a-schema */
/* Defined schema
:ENV: PARSE_SERVER_SCHEMA
*/
schema: ?SchemaOptions;
/* Callback when server has closed */
serverCloseComplete: ?() => void;
Expand Down
21 changes: 10 additions & 11 deletions src/SchemaMigrations/Migrations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// @flow

export interface SchemaOptions {
definitions: JSONSchema[];
strict: ?boolean;
deleteExtraFields: ?boolean;
recreateModifiedFields: ?boolean;
lockSchemas: ?boolean;
beforeMigration: ?() => void | Promise<void>;
afterMigration: ?() => void | Promise<void>;
}

export type FieldValueType =
| 'String'
| 'Boolean'
Expand Down Expand Up @@ -35,17 +45,6 @@ export interface IndexesInterface {
[key: string]: IndexInterface;
}

export interface SchemaOptions {
definitions: JSONSchema[];
strict: ?boolean;
deleteExtraFields: ?boolean;
recreateModifiedFields: ?boolean;
lockSchemas: ?boolean;
/* Callback when server has started and before running schemas migration operations if schemas key provided */
beforeMigration: ?() => void | Promise<void>;
afterMigration: ?() => void | Promise<void>;
}

export type CLPOperation = 'find' | 'count' | 'get' | 'update' | 'create' | 'delete';
// @Typescript 4.1+ // type CLPPermission = 'requiresAuthentication' | '*' | `user:${string}` | `role:${string}`

Expand Down

0 comments on commit a169663

Please sign in to comment.