chore: normalize payload collections and globals outside db adapter
This commit is contained in:
@@ -11,7 +11,12 @@ import { Auth, IncomingAuthType, User } from '../../auth/types';
|
|||||||
import { IncomingUploadType, Upload } from '../../uploads/types';
|
import { IncomingUploadType, Upload } from '../../uploads/types';
|
||||||
import { IncomingCollectionVersions, SanitizedCollectionVersions } from '../../versions/types';
|
import { IncomingCollectionVersions, SanitizedCollectionVersions } from '../../versions/types';
|
||||||
import { BuildQueryArgs } from '../../mongoose/queries/buildQuery';
|
import { BuildQueryArgs } from '../../mongoose/queries/buildQuery';
|
||||||
import { CustomPreviewButtonProps, CustomPublishButtonProps, CustomSaveButtonProps, CustomSaveDraftButtonProps } from '../../admin/components/elements/types';
|
import {
|
||||||
|
CustomPreviewButtonProps,
|
||||||
|
CustomPublishButtonProps,
|
||||||
|
CustomSaveButtonProps,
|
||||||
|
CustomSaveDraftButtonProps,
|
||||||
|
} from '../../admin/components/elements/types';
|
||||||
import type { Props as ListProps } from '../../admin/components/views/collections/List/types';
|
import type { Props as ListProps } from '../../admin/components/views/collections/List/types';
|
||||||
import type { Props as EditProps } from '../../admin/components/views/collections/Edit/types';
|
import type { Props as EditProps } from '../../admin/components/views/collections/Edit/types';
|
||||||
|
|
||||||
@@ -354,7 +359,7 @@ export interface SanitizedCollectionConfig extends Omit<DeepRequired<CollectionC
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type Collection = {
|
export type Collection = {
|
||||||
Model: CollectionModel;
|
Model?: CollectionModel;
|
||||||
config: SanitizedCollectionConfig;
|
config: SanitizedCollectionConfig;
|
||||||
graphQL?: {
|
graphQL?: {
|
||||||
type: GraphQLObjectType
|
type: GraphQLObjectType
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ import { PayloadRequest } from '../../express/types';
|
|||||||
import { Access, Endpoint, EntityDescription, GeneratePreviewURL } from '../../config/types';
|
import { Access, Endpoint, EntityDescription, GeneratePreviewURL } from '../../config/types';
|
||||||
import { Field } from '../../fields/config/types';
|
import { Field } from '../../fields/config/types';
|
||||||
import { IncomingGlobalVersions, SanitizedGlobalVersions } from '../../versions/types';
|
import { IncomingGlobalVersions, SanitizedGlobalVersions } from '../../versions/types';
|
||||||
import { CustomSaveButtonProps, CustomSaveDraftButtonProps, CustomPublishButtonProps, CustomPreviewButtonProps } from '../../admin/components/elements/types';
|
import {
|
||||||
|
CustomPreviewButtonProps,
|
||||||
|
CustomPublishButtonProps,
|
||||||
|
CustomSaveButtonProps,
|
||||||
|
CustomSaveDraftButtonProps,
|
||||||
|
} from '../../admin/components/elements/types';
|
||||||
|
|
||||||
export type TypeWithID = {
|
export type TypeWithID = {
|
||||||
id: string | number
|
id: string | number
|
||||||
@@ -143,7 +148,7 @@ export interface SanitizedGlobalConfig extends Omit<DeepRequired<GlobalConfig>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type Globals = {
|
export type Globals = {
|
||||||
Model: GlobalModel
|
Model?: GlobalModel
|
||||||
config: SanitizedGlobalConfig[]
|
config: SanitizedGlobalConfig[]
|
||||||
graphQL?: {
|
graphQL?: {
|
||||||
[slug: string]: {
|
[slug: string]: {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import { GraphQLNonNull, GraphQLBoolean, GraphQLInt, GraphQLString } from 'graphql';
|
import { GraphQLBoolean, GraphQLInt, GraphQLNonNull, GraphQLString } from 'graphql';
|
||||||
import { singular } from 'pluralize';
|
import { singular } from 'pluralize';
|
||||||
import formatName from '../../graphql/utilities/formatName';
|
import formatName from '../../graphql/utilities/formatName';
|
||||||
import { buildVersionGlobalFields } from '../../versions/buildGlobalFields';
|
import { buildVersionGlobalFields } from '../../versions/buildGlobalFields';
|
||||||
@@ -20,7 +20,6 @@ import { buildPolicyType } from '../../graphql/schema/buildPoliciesType';
|
|||||||
import { docAccessResolver } from './resolvers/docAccess';
|
import { docAccessResolver } from './resolvers/docAccess';
|
||||||
|
|
||||||
function initGlobalsGraphQL(payload: Payload): void {
|
function initGlobalsGraphQL(payload: Payload): void {
|
||||||
if (payload.config.globals) {
|
|
||||||
Object.keys(payload.globals.config).forEach((slug) => {
|
Object.keys(payload.globals.config).forEach((slug) => {
|
||||||
const global: SanitizedGlobalConfig = payload.globals.config[slug];
|
const global: SanitizedGlobalConfig = payload.globals.config[slug];
|
||||||
const {
|
const {
|
||||||
@@ -152,6 +151,5 @@ function initGlobalsGraphQL(payload: Payload): void {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export default initGlobalsGraphQL;
|
export default initGlobalsGraphQL;
|
||||||
|
|||||||
@@ -41,8 +41,12 @@ export async function init(
|
|||||||
if (collection.indexes) {
|
if (collection.indexes) {
|
||||||
collection.indexes.forEach((index) => {
|
collection.indexes.forEach((index) => {
|
||||||
// prefix 'version.' to each field in the index
|
// prefix 'version.' to each field in the index
|
||||||
const versionIndex = { fields: {}, options: index.options };
|
const versionIndex = {
|
||||||
Object.entries(index.fields).forEach(([key, value]) => {
|
fields: {},
|
||||||
|
options: index.options,
|
||||||
|
};
|
||||||
|
Object.entries(index.fields)
|
||||||
|
.forEach(([key, value]) => {
|
||||||
versionIndex.fields[`version.${key}`] = value;
|
versionIndex.fields[`version.${key}`] = value;
|
||||||
});
|
});
|
||||||
versionSchema.index(versionIndex.fields, versionIndex.options);
|
versionSchema.index(versionIndex.fields, versionIndex.options);
|
||||||
@@ -50,7 +54,10 @@ export async function init(
|
|||||||
}
|
}
|
||||||
|
|
||||||
versionSchema.plugin(paginate, { useEstimatedCount: true })
|
versionSchema.plugin(paginate, { useEstimatedCount: true })
|
||||||
.plugin(getBuildQueryPlugin({ collectionSlug: collection.slug, versionsFields: versionCollectionFields }));
|
.plugin(getBuildQueryPlugin({
|
||||||
|
collectionSlug: collection.slug,
|
||||||
|
versionsFields: versionCollectionFields,
|
||||||
|
}));
|
||||||
|
|
||||||
if (collection.versions?.drafts) {
|
if (collection.versions?.drafts) {
|
||||||
versionSchema.plugin(mongooseAggregatePaginate);
|
versionSchema.plugin(mongooseAggregatePaginate);
|
||||||
@@ -70,14 +77,10 @@ export async function init(
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
if (payload.config.globals) {
|
|
||||||
const model = buildGlobalModel(payload.config);
|
const model = buildGlobalModel(payload.config);
|
||||||
this.globals = model;
|
this.globals = model;
|
||||||
|
|
||||||
payload.globals = {
|
payload.globals.Model = model;
|
||||||
Model: model,
|
|
||||||
config: payload.config.globals,
|
|
||||||
};
|
|
||||||
|
|
||||||
payload.config.globals.forEach((global) => {
|
payload.config.globals.forEach((global) => {
|
||||||
if (global.versions) {
|
if (global.versions) {
|
||||||
@@ -109,4 +112,3 @@ export async function init(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -194,6 +194,15 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
|||||||
this.config = await loadConfig(this.logger);
|
this.config = await loadConfig(this.logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.globals = {
|
||||||
|
config: this.config.globals,
|
||||||
|
};
|
||||||
|
this.config.collections.forEach((collection) => {
|
||||||
|
this.collections[collection.slug] = {
|
||||||
|
config: collection,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
// THIS BLOCK IS TEMPORARY UNTIL 2.0.0
|
// THIS BLOCK IS TEMPORARY UNTIL 2.0.0
|
||||||
// We automatically add the Mongoose adapter
|
// We automatically add the Mongoose adapter
|
||||||
// if there is no defined database adapter
|
// if there is no defined database adapter
|
||||||
@@ -209,7 +218,9 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.db = this.config.db;
|
this.db = this.config.db;
|
||||||
|
if (this.db?.connect) {
|
||||||
this.mongoMemoryServer = await this.db.connect({ payload: this, config: this.config });
|
this.mongoMemoryServer = await this.db.connect({ payload: this, config: this.config });
|
||||||
|
}
|
||||||
|
|
||||||
// Configure email service
|
// Configure email service
|
||||||
const emailOptions = options.email ? { ...(options.email) } : this.config.email;
|
const emailOptions = options.email ? { ...(options.email) } : this.config.email;
|
||||||
@@ -221,12 +232,13 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
|||||||
this.email = buildEmail(this.emailOptions, this.logger);
|
this.email = buildEmail(this.emailOptions, this.logger);
|
||||||
this.sendEmail = sendEmail.bind(this);
|
this.sendEmail = sendEmail.bind(this);
|
||||||
|
|
||||||
await this.db.init({ payload: this, config: this.config });
|
|
||||||
|
|
||||||
if (!this.config.graphQL.disable) {
|
if (!this.config.graphQL.disable) {
|
||||||
registerGraphQLSchema(this);
|
registerGraphQLSchema(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.db.init) {
|
||||||
|
await this.db?.init({ payload: this, config: this.config });
|
||||||
|
}
|
||||||
this.preferences = { Model: PreferencesModel };
|
this.preferences = { Model: PreferencesModel };
|
||||||
|
|
||||||
serverInitTelemetry(this);
|
serverInitTelemetry(this);
|
||||||
|
|||||||
@@ -861,7 +861,7 @@ type User {
|
|||||||
id: String
|
id: String
|
||||||
updatedAt: DateTime
|
updatedAt: DateTime
|
||||||
createdAt: DateTime
|
createdAt: DateTime
|
||||||
email: EmailAddress
|
email: EmailAddress!
|
||||||
resetPasswordToken: String
|
resetPasswordToken: String
|
||||||
resetPasswordExpiration: DateTime
|
resetPasswordExpiration: DateTime
|
||||||
salt: String
|
salt: String
|
||||||
@@ -929,7 +929,6 @@ input User_email_operator {
|
|||||||
in: [EmailAddress]
|
in: [EmailAddress]
|
||||||
not_in: [EmailAddress]
|
not_in: [EmailAddress]
|
||||||
all: [EmailAddress]
|
all: [EmailAddress]
|
||||||
exists: Boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
input User_id_operator {
|
input User_id_operator {
|
||||||
@@ -1855,7 +1854,7 @@ input mutationCollection2Update_NestedGroup_MetaInput {
|
|||||||
input mutationUserInput {
|
input mutationUserInput {
|
||||||
updatedAt: String
|
updatedAt: String
|
||||||
createdAt: String
|
createdAt: String
|
||||||
email: String
|
email: String!
|
||||||
resetPasswordToken: String
|
resetPasswordToken: String
|
||||||
resetPasswordExpiration: String
|
resetPasswordExpiration: String
|
||||||
salt: String
|
salt: String
|
||||||
|
|||||||
Reference in New Issue
Block a user