chore: merge
This commit is contained in:
@@ -4,6 +4,7 @@ import executeAccess from '../../auth/executeAccess';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { Collection, PaginatedDocs } from '../config/types';
|
||||
import { hasWhereAccessResult } from '../../auth/types';
|
||||
import flattenWhereConstraints from '../../utilities/flattenWhereConstraints';
|
||||
|
||||
export type Arguments = {
|
||||
collection: Collection
|
||||
@@ -55,6 +56,7 @@ async function find(incomingArgs: Arguments): Promise<PaginatedDocs> {
|
||||
// /////////////////////////////////////
|
||||
|
||||
const queryToBuild: { where?: Where} = {};
|
||||
let useEstimatedCount = false;
|
||||
|
||||
if (where) {
|
||||
let and = [];
|
||||
@@ -68,6 +70,10 @@ async function find(incomingArgs: Arguments): Promise<PaginatedDocs> {
|
||||
...and,
|
||||
],
|
||||
};
|
||||
|
||||
const constraints = flattenWhereConstraints(queryToBuild);
|
||||
|
||||
useEstimatedCount = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'));
|
||||
}
|
||||
|
||||
if (!overrideAccess) {
|
||||
@@ -110,7 +116,7 @@ async function find(incomingArgs: Arguments): Promise<PaginatedDocs> {
|
||||
sort,
|
||||
lean: true,
|
||||
leanWithId: true,
|
||||
useEstimatedCount: true,
|
||||
useEstimatedCount,
|
||||
};
|
||||
|
||||
const paginatedDocs = await Model.paginate(query, optionsToExecute);
|
||||
|
||||
@@ -53,6 +53,20 @@ const formatBaseSchema = (field: Field, buildSchemaOptions: BuildSchemaOptions)
|
||||
index: field.index || field.unique || false,
|
||||
});
|
||||
|
||||
const localizeSchema = (field: Field, schema, locales) => {
|
||||
if (field.localized) {
|
||||
return {
|
||||
type: locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: schema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
index: schema.index,
|
||||
};
|
||||
}
|
||||
return schema;
|
||||
};
|
||||
|
||||
const buildSchema = (config: SanitizedConfig, configFields: Field[], buildSchemaOptions: BuildSchemaOptions = {}): Schema => {
|
||||
const { allowIDField, options } = buildSchemaOptions;
|
||||
let fields = {};
|
||||
@@ -103,128 +117,50 @@ const fieldIndexMap = {
|
||||
const fieldToSchemaMap = {
|
||||
number: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Number };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
text: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
email: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
textarea: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
richText: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Schema.Types.Mixed };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
code: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
point: (field: Field, fields: SchemaDefinition, config: SanitizedConfig): SchemaDefinition => {
|
||||
@@ -241,23 +177,10 @@ const fieldToSchemaMap = {
|
||||
default: field.defaultValue || undefined,
|
||||
},
|
||||
};
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
radio: (field: RadioField, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
@@ -269,65 +192,26 @@ const fieldToSchemaMap = {
|
||||
return option;
|
||||
}),
|
||||
};
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
checkbox: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Boolean };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
date: (field: Field, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Date };
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
upload: (field: UploadField, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
@@ -337,23 +221,9 @@ const fieldToSchemaMap = {
|
||||
ref: field.relationTo,
|
||||
};
|
||||
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
relationship: (field: RelationshipField, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions) => {
|
||||
@@ -434,23 +304,9 @@ const fieldToSchemaMap = {
|
||||
})],
|
||||
};
|
||||
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
group: (field: GroupField, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
@@ -471,23 +327,9 @@ const fieldToSchemaMap = {
|
||||
}),
|
||||
};
|
||||
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: localizeSchema(field, baseSchema, config.localization.locales),
|
||||
};
|
||||
},
|
||||
select: (field: SelectField, fields: SchemaDefinition, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): SchemaDefinition => {
|
||||
@@ -499,26 +341,11 @@ const fieldToSchemaMap = {
|
||||
return option;
|
||||
}),
|
||||
};
|
||||
|
||||
let schemaToReturn;
|
||||
|
||||
if (field.localized) {
|
||||
schemaToReturn = {
|
||||
type: config.localization.locales.reduce((localeSchema, locale) => ({
|
||||
...localeSchema,
|
||||
[locale]: baseSchema,
|
||||
}), {}),
|
||||
localized: true,
|
||||
};
|
||||
} else {
|
||||
schemaToReturn = baseSchema;
|
||||
}
|
||||
|
||||
if (field.hasMany) schemaToReturn = [schemaToReturn];
|
||||
const schemaToReturn = localizeSchema(field, baseSchema, config.localization.locales);
|
||||
|
||||
return {
|
||||
...fields,
|
||||
[field.name]: schemaToReturn,
|
||||
[field.name]: field.hasMany ? [schemaToReturn] : schemaToReturn,
|
||||
};
|
||||
},
|
||||
blocks: (field: BlockField, fields: SchemaDefinition, config: SanitizedConfig): SchemaDefinition => {
|
||||
|
||||
18
src/utilities/flattenWhereConstraints.ts
Normal file
18
src/utilities/flattenWhereConstraints.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { WhereField } from '../types';
|
||||
|
||||
const flattenWhereConstraints = (query): WhereField[] => {
|
||||
if (!query.where && !query.and && !query.or) {
|
||||
return Object.keys(query).map((key) => query[key]);
|
||||
}
|
||||
if (query.where) {
|
||||
const whereResult = flattenWhereConstraints(query.where);
|
||||
return Object.keys(whereResult).map((key) => whereResult[key]);
|
||||
}
|
||||
const nested = [...query.or || [], ...query.and || []];
|
||||
if (nested.length > 0) {
|
||||
return nested.flatMap((nest) => flattenWhereConstraints(nest));
|
||||
}
|
||||
return query;
|
||||
};
|
||||
|
||||
export default flattenWhereConstraints;
|
||||
Reference in New Issue
Block a user