Browse Source

feat: Add documentVersion schema plugin

Owen Diffey 1 year ago
parent
commit
9463279190

+ 4 - 1
backend/src/modules/DataModule.ts

@@ -11,7 +11,9 @@ import path from "path";
 import JobContext from "../JobContext";
 import BaseModule, { ModuleStatus } from "../BaseModule";
 import { UniqueMethods } from "../types/Modules";
-import { Models, Schemas } from "../types/Models";
+import { Models } from "../types/Models";
+import { Schemas } from "../types/Schemas";
+import documentVersionPlugin from "../schemas/plugins/documentVersion";
 import getDataPlugin from "../schemas/plugins/getData";
 import Migration from "../Migration";
 
@@ -55,6 +57,7 @@ export default class DataModule extends BaseModule {
 
 		mongoose.SchemaTypes.String.set("trim", true);
 
+		this.mongoConnection.plugin(documentVersionPlugin);
 		this.mongoConnection.plugin(getDataPlugin, {
 			tags: ["useGetDataPlugin"]
 		});

+ 2 - 1
backend/src/schemas/abc.ts

@@ -1,6 +1,7 @@
 import { Model, Schema, SchemaTypes, Types } from "mongoose";
+import { BaseSchema } from "../types/Schemas";
 
-export interface AbcSchema {
+export interface AbcSchema extends BaseSchema {
 	name: string;
 	autofill?: {
 		enabled?: boolean;

+ 4 - 1
backend/src/schemas/news.ts

@@ -7,6 +7,7 @@ import {
 	Types
 } from "mongoose";
 import { GetData } from "./plugins/getData";
+import { BaseSchema, TimestampsSchema } from "../types/Schemas";
 
 export enum NewsStatus {
 	DRAFT = "draft",
@@ -14,7 +15,7 @@ export enum NewsStatus {
 	ARCHIVED = "archived"
 }
 
-export interface NewsSchema {
+export interface NewsSchema extends BaseSchema, TimestampsSchema {
 	title: string;
 	markdown: string;
 	status: NewsStatus;
@@ -80,6 +81,8 @@ export const schema = new Schema<NewsSchema, NewsModel, {}, NewsQueryHelpers>(
 		}
 	},
 	{
+		// @ts-ignore
+		documentVersion: 3,
 		timestamps: true,
 		query: {
 			published() {

+ 19 - 0
backend/src/schemas/plugins/documentVersion.ts

@@ -0,0 +1,19 @@
+import { Schema, SchemaOptions, SchemaTypes } from "mongoose";
+
+export interface DocumentVersionSchemaOptions extends SchemaOptions {
+	documentVersion: number;
+}
+
+export interface DocumentVersion {
+	documentVersion: number;
+}
+
+export default function documentVersionPlugin(schema: Schema) {
+	schema.add({
+		documentVersion: {
+			type: SchemaTypes.Number,
+			default: schema.options?.documentVersion ?? 1,
+			required: true
+		}
+	});
+}

+ 2 - 5
backend/src/schemas/plugins/getData.ts

@@ -47,10 +47,7 @@ export interface GetData {
 	}>;
 }
 
-export default function getDataPlugin(
-	schema: Schema,
-	options: GetDataSchemaOptions
-) {
+export default function getDataPlugin(schema: Schema) {
 	schema.static(
 		"getData",
 		async function getData(
@@ -64,7 +61,7 @@ export default function getDataPlugin(
 				specialFilters,
 				specialProperties,
 				specialQueries
-			} = options.getData ?? {};
+			} = schema.options?.getData ?? {};
 
 			const pipeline: PipelineStage[] = [];
 

+ 5 - 1
backend/src/schemas/station.ts

@@ -1,5 +1,6 @@
 import { Model, Schema, SchemaTypes, Types } from "mongoose";
 import { GetData } from "./plugins/getData";
+import { BaseSchema, TimestampsSchema } from "../types/Schemas";
 
 export enum StationType {
 	OFFICIAL = "official",
@@ -30,7 +31,7 @@ export enum StationAutofillMode {
 	SEQUENTIAL = "sequential"
 }
 
-export interface StationSchema {
+export interface StationSchema extends BaseSchema, TimestampsSchema {
 	type: StationType;
 	name: string;
 	displayName: string;
@@ -173,6 +174,9 @@ export const schema = new Schema<StationSchema, StationModel>(
 		}
 	},
 	{
+		// @ts-ignore
+		documentVersion: 10,
+		timestamps: true,
 		pluginTags: ["useGetDataPlugin"],
 		// @ts-ignore
 		getData: {

+ 3 - 9
backend/src/types/Models.ts

@@ -1,12 +1,6 @@
-import { AbcModel, AbcSchemaType } from "../schemas/abc";
-import { NewsModel, NewsSchemaType } from "../schemas/news";
-import { StationModel, StationSchemaType } from "../schemas/station";
-
-export type Schemas = {
-	abc: AbcSchemaType;
-	news: NewsSchemaType;
-	station: StationSchemaType;
-};
+import { AbcModel } from "../schemas/abc";
+import { NewsModel } from "../schemas/news";
+import { StationModel } from "../schemas/station";
 
 export type Models = {
 	abc: AbcModel;

+ 18 - 0
backend/src/types/Schemas.ts

@@ -0,0 +1,18 @@
+import { DocumentVersion } from "../schemas/plugins/documentVersion";
+import { AbcSchemaType } from "../schemas/abc";
+import { NewsSchemaType } from "../schemas/news";
+import { StationSchemaType } from "../schemas/station";
+
+// eslint-disable-next-line
+export interface BaseSchema extends DocumentVersion {}
+
+export interface TimestampsSchema {
+	createdAt: number;
+	updatedAt: number;
+}
+
+export type Schemas = {
+	abc: AbcSchemaType;
+	news: NewsSchemaType;
+	station: StationSchemaType;
+};