Skip to content

Commit

Permalink
Merge branch 'feat/index-sortable-fields' of github.com:payloadcms/pa…
Browse files Browse the repository at this point in the history
…yload
  • Loading branch information
jmikrut committed Oct 4, 2021
2 parents 0a5a607 + ad09782 commit bba1f1f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions demo/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default buildConfig({
defaultLocale: 'en',
fallback: true,
},
// indexSortableFields: true,
hooks: {
afterError: (err) => {
console.error('global error config handler', err);
Expand Down
1 change: 1 addition & 0 deletions docs/configuration/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Payload is a *config-based*, code-first CMS and application framework. The Paylo
| `csrf` | A whitelist array of URLs to allow Payload cookies to be accepted from as a form of CSRF protection. [More](/docs/authentication/overview#csrf-protection) |
| `defaultDepth` | If a user does not specify `depth` while requesting a resource, this depth will be used. [More](/docs/getting-started/concepts#depth) |
| `maxDepth` | The maximum allowed depth to be permitted application-wide. This setting helps prevent against malicious queries. Defaults to `10`. |
| `indexSortableFields`| Automatically create database an index on every sortable type of top-level field. |
| `upload` | Base Payload upload configuration. [More](/docs/upload/overview#payload-wide-upload-options). |
| `routes` | Control the routing structure that Payload binds itself to. Specify `admin`, `api`, `graphQL`, and `graphQLPlayground`. |
| `email` | Base email settings to allow Payload to generate email such as Forgot Password requests and other requirements. [More](/docs/email/overview#configuration) |
Expand Down
2 changes: 2 additions & 0 deletions docs/production/deployment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ If you are using a [persistent filesystem-based cloud host](#persistent-vs-ephem
Alternatively, you can rely on a third-party MongoDB host such as [MongoDB Atlas](https://www.mongodb.com/). With Atlas or a similar cloud provider, you can trust them to take care of your database's availability, security, redundancy, and backups.
When using Azure Cosmos, MongoDB requires an index on any field being sorted on in a query. To make this easier, see the [indexSortableFields](/docs/configuration/overview) configuration option.
## File storage
If you are using Payload to [manage file uploads](/docs/upload/overview), you need to consider where your uploaded files will be permanently stored. If you do not use Payload for file uploads, then this section does not impact your app whatsoever.
Expand Down
1 change: 1 addition & 0 deletions src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default joi.object({
}),
local: joi.boolean(),
upload: joi.object(),
indexSortableFields: joi.boolean(),
rateLimit: joi.object()
.keys({
window: joi.number(),
Expand Down
3 changes: 2 additions & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type Config = {
},
defaultDepth?: number;
maxDepth?: number;
indexSortableFields?: boolean;
rateLimit?: {
window?: number;
max?: number;
Expand All @@ -143,7 +144,7 @@ export type Config = {
hooks?: {
afterError?: AfterErrorHook;
};
plugins?: Plugin[]
plugins?: Plugin[];
};

export type SanitizedConfig = Omit<DeepRequired<Config>, 'collections' | 'globals'> & {
Expand Down
2 changes: 1 addition & 1 deletion src/globals/buildModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const buildModel = (config: SanitizedConfig): mongoose.PaginateModel<any> | null
const Globals = mongoose.model('globals', globalsSchema);

Object.values(config.globals).forEach((globalConfig) => {
const globalSchema = buildSchema(config, globalConfig.fields, {});
const globalSchema = buildSchema(config, globalConfig.fields, { global: true });
Globals.discriminator(globalConfig.slug, globalSchema);
});

Expand Down
7 changes: 7 additions & 0 deletions src/mongoose/buildSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import { Schema, SchemaDefinition, SchemaOptions } from 'mongoose';
import { SanitizedConfig } from '../config/types';
import { ArrayField, Block, BlockField, Field, GroupField, RadioField, RelationshipField, RowField, SelectField, UploadField } from '../fields/config/types';
import sortableFieldTypes from '../fields/sortableFieldTypes';

type BuildSchemaOptions = {
options?: SchemaOptions
allowIDField?: boolean
disableRequired?: boolean
global?: boolean
}

type FieldSchemaGenerator = (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions) => SchemaDefinition;
Expand Down Expand Up @@ -89,10 +91,15 @@ const buildSchema = (config: SanitizedConfig, configFields: Field[], buildSchema
if (fieldSchema) {
fields = fieldSchema(field, fields, config, buildSchemaOptions);
}

// geospatial field index must be created after the schema is created
if (fieldIndexMap[field.type]) {
indexFields.push(...fieldIndexMap[field.type](field, config));
}

if (config.indexSortableFields && !buildSchemaOptions.global && !field.index && !field.hidden && sortableFieldTypes.indexOf(field.type) > -1) {
indexFields.push({ [field.name]: 1 });
}
});

const schema = new Schema(fields, options);
Expand Down

0 comments on commit bba1f1f

Please sign in to comment.