feat: builds revisions models
This commit is contained in:
@@ -11,6 +11,10 @@ const RichText: CollectionConfig = {
|
||||
access: {
|
||||
read: () => true,
|
||||
},
|
||||
revisions: {
|
||||
max: 5,
|
||||
retainDeleted: false,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'defaultRichText',
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
import { Collection } from './config/types';
|
||||
|
||||
const bindCollectionMiddleware = (collection: string) => (req: Request & { collection: string }, res: Response, next: NextFunction) => {
|
||||
const bindCollectionMiddleware = (collection: Collection) => (req: Request & { collection: Collection }, res: Response, next: NextFunction): void => {
|
||||
req.collection = collection;
|
||||
next();
|
||||
};
|
||||
|
||||
@@ -65,6 +65,10 @@ const sanitizeCollection = (config: Config, collection: CollectionConfig): Sanit
|
||||
sanitized.slug = toKebabCase(sanitized.slug);
|
||||
sanitized.labels = sanitized.labels || formatLabels(sanitized.slug);
|
||||
|
||||
if (sanitized.revisions) {
|
||||
if (sanitized.revisions === true) sanitized.revisions = {};
|
||||
}
|
||||
|
||||
if (sanitized.upload) {
|
||||
if (sanitized.upload === true) sanitized.upload = {};
|
||||
|
||||
|
||||
@@ -77,6 +77,13 @@ const collectionSchema = joi.object().keys({
|
||||
}),
|
||||
joi.boolean(),
|
||||
),
|
||||
revisions: joi.alternatives().try(
|
||||
joi.object({
|
||||
max: joi.number(),
|
||||
retainDeleted: joi.boolean(),
|
||||
}),
|
||||
joi.boolean(),
|
||||
),
|
||||
upload: joi.alternatives().try(
|
||||
joi.object({
|
||||
staticURL: joi.string(),
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Field } from '../../fields/config/types';
|
||||
import { PayloadRequest } from '../../express/types';
|
||||
import { IncomingAuthType, Auth } from '../../auth/types';
|
||||
import { IncomingUploadType, Upload } from '../../uploads/types';
|
||||
import { IncomingRevisionsType } from '../../revisions/types';
|
||||
|
||||
export interface CollectionModel extends PaginateModel<any>, PassportLocalModel<any> {
|
||||
buildQuery: (query: unknown, locale?: string) => Record<string, unknown>
|
||||
@@ -138,6 +139,7 @@ export type CollectionConfig = {
|
||||
};
|
||||
auth?: IncomingAuthType | boolean;
|
||||
upload?: IncomingUploadType | boolean;
|
||||
revisions?: IncomingRevisionsType | boolean;
|
||||
timestamps?: boolean
|
||||
};
|
||||
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import mongoose from 'mongoose';
|
||||
import paginate from 'mongoose-paginate-v2';
|
||||
import express from 'express';
|
||||
import passport from 'passport';
|
||||
import passportLocalMongoose from 'passport-local-mongoose';
|
||||
import Passport from 'passport-local';
|
||||
import { UpdateQuery } from 'mongodb';
|
||||
import { buildRevisionFields } from '../revisions/buildFields';
|
||||
import buildQueryPlugin from '../mongoose/buildQuery';
|
||||
import apiKeyStrategy from '../auth/strategies/apiKey';
|
||||
import buildSchema from './buildSchema';
|
||||
import buildCollectionSchema from './buildSchema';
|
||||
import buildSchema from '../mongoose/buildSchema';
|
||||
import bindCollectionMiddleware from './bindCollection';
|
||||
import { SanitizedCollectionConfig } from './config/types';
|
||||
import { SanitizedConfig } from '../config/types';
|
||||
import { CollectionModel, SanitizedCollectionConfig } from './config/types';
|
||||
import { Payload } from '../index';
|
||||
import { getCollectionRevisionsName } from '../revisions/createCollectionName';
|
||||
|
||||
const LocalStrategy = Passport.Strategy;
|
||||
|
||||
@@ -17,7 +21,7 @@ export default function registerCollections(ctx: Payload): void {
|
||||
ctx.config.collections = ctx.config.collections.map((collection: SanitizedCollectionConfig) => {
|
||||
const formattedCollection = collection;
|
||||
|
||||
const schema = buildSchema(formattedCollection, ctx.config as SanitizedConfig);
|
||||
const schema = buildCollectionSchema(formattedCollection, ctx.config);
|
||||
|
||||
if (collection.auth) {
|
||||
schema.plugin(passportLocalMongoose, {
|
||||
@@ -62,8 +66,28 @@ export default function registerCollections(ctx: Payload): void {
|
||||
}
|
||||
}
|
||||
|
||||
if (collection.revisions) {
|
||||
const revisionModelName = getCollectionRevisionsName(collection);
|
||||
|
||||
const revisionSchema = buildSchema(
|
||||
ctx.config,
|
||||
buildRevisionFields(collection),
|
||||
{
|
||||
options: {
|
||||
timestamps: true,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
revisionSchema.plugin(paginate, { useEstimatedCount: true })
|
||||
.plugin(buildQueryPlugin);
|
||||
|
||||
ctx.revisions[collection.slug] = mongoose.model(revisionModelName, revisionSchema) as CollectionModel;
|
||||
}
|
||||
|
||||
|
||||
ctx.collections[formattedCollection.slug] = {
|
||||
Model: mongoose.model(formattedCollection.slug, schema),
|
||||
Model: mongoose.model(formattedCollection.slug, schema) as CollectionModel,
|
||||
config: formattedCollection,
|
||||
};
|
||||
|
||||
|
||||
10
src/index.ts
10
src/index.ts
@@ -7,7 +7,7 @@ import {
|
||||
InitOptions,
|
||||
} from './config/types';
|
||||
import {
|
||||
Collection, PaginatedDocs,
|
||||
Collection, CollectionModel, PaginatedDocs,
|
||||
} from './collections/config/types';
|
||||
import Logger from './utilities/logger';
|
||||
import bindOperations from './init/bindOperations';
|
||||
@@ -52,7 +52,13 @@ require('isomorphic-fetch');
|
||||
export class Payload {
|
||||
config: SanitizedConfig;
|
||||
|
||||
collections: Collection[] = [];
|
||||
collections: {
|
||||
[slug: string]: Collection;
|
||||
} = {}
|
||||
|
||||
revisions: {
|
||||
[slug: string]: CollectionModel;
|
||||
} = {}
|
||||
|
||||
graphQL: {
|
||||
resolvers: GraphQLResolvers
|
||||
|
||||
16
src/revisions/buildFields.ts
Normal file
16
src/revisions/buildFields.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Field } from '../fields/config/types';
|
||||
import { SanitizedCollectionConfig } from '../collections/config/types';
|
||||
|
||||
export const buildRevisionFields = (collection: SanitizedCollectionConfig): Field[] => [
|
||||
{
|
||||
name: 'parent',
|
||||
type: 'relationship',
|
||||
index: true,
|
||||
relationTo: collection.slug,
|
||||
},
|
||||
{
|
||||
name: 'revision',
|
||||
type: 'group',
|
||||
fields: collection.fields,
|
||||
},
|
||||
];
|
||||
3
src/revisions/createCollectionName.ts
Normal file
3
src/revisions/createCollectionName.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { SanitizedCollectionConfig } from '../collections/config/types';
|
||||
|
||||
export const getCollectionRevisionsName = (collection: SanitizedCollectionConfig): string => `_${collection.slug}_revisions`;
|
||||
4
src/revisions/types.ts
Normal file
4
src/revisions/types.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export type IncomingRevisionsType = {
|
||||
max?: number
|
||||
retainDeleted?: boolean
|
||||
}
|
||||
Reference in New Issue
Block a user