Ver código fonte

feat: Add getData interface

Owen Diffey 1 ano atrás
pai
commit
e7cde971d8

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

@@ -110,7 +110,7 @@ export default class DataModule extends BaseModule {
 	 */
 	private async loadModel<ModelName extends keyof Models>(
 		modelName: ModelName
-	) {
+	): Promise<Models[ModelName]> {
 		if (!this.mongoConnection) throw new Error("Mongo is not available");
 
 		const { schema }: { schema: Schemas[ModelName] } = await import(

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

@@ -1,4 +1,4 @@
-import { Schema, SchemaTypes, Types } from "mongoose";
+import { Model, Schema, SchemaTypes, Types } from "mongoose";
 
 export interface AbcSchema {
 	name: string;
@@ -11,7 +11,9 @@ export interface AbcSchema {
 	aNumber: number;
 }
 
-export const schema = new Schema<AbcSchema>({
+export type AbcModel = Model<AbcSchema>;
+
+export const schema = new Schema<AbcSchema, AbcModel>({
 	name: {
 		type: SchemaTypes.String,
 		required: true
@@ -34,3 +36,5 @@ export const schema = new Schema<AbcSchema>({
 	},
 	aNumber: { type: SchemaTypes.Number, required: true }
 });
+
+export type AbcSchemaType = typeof schema;

+ 8 - 6
backend/src/schemas/news.ts

@@ -6,6 +6,7 @@ import {
 	SchemaTypes,
 	Types
 } from "mongoose";
+import { GetData } from "./plugins/getData";
 
 export enum NewsStatus {
 	DRAFT = "draft",
@@ -48,12 +49,11 @@ export interface NewsQueryHelpers {
 	>;
 }
 
-export const schema = new Schema<
-	NewsSchema,
-	Model<NewsSchema, NewsQueryHelpers>,
-	{},
-	NewsQueryHelpers
->(
+export interface NewsModel
+	extends Model<NewsSchema, NewsQueryHelpers>,
+		GetData {}
+
+export const schema = new Schema<NewsSchema, NewsModel, {}, NewsQueryHelpers>(
 	{
 		title: {
 			type: SchemaTypes.String,
@@ -94,3 +94,5 @@ export const schema = new Schema<
 		pluginTags: ["useGetDataPlugin"]
 	}
 );
+
+export type NewsSchemaType = typeof schema;

+ 23 - 14
backend/src/schemas/plugins/getData.ts

@@ -27,26 +27,35 @@ export interface GetDataSchemaOptions extends SchemaOptions {
 	};
 }
 
+export interface GetData {
+	getData(payload: {
+		page: number;
+		pageSize: number;
+		properties: string[];
+		sort: Record<string, "ascending" | "descending">;
+		queries: {
+			data: any;
+			filter: {
+				property: string;
+			};
+			filterType: FilterType;
+		}[];
+		operator: "and" | "or" | "nor";
+	}): Promise<{
+		data: any[];
+		count: number;
+	}>;
+}
+
 export default function getDataPlugin(
 	schema: Schema,
 	options: GetDataSchemaOptions
 ) {
 	schema.static(
 		"getData",
-		async function getData(payload: {
-			page: number;
-			pageSize: number;
-			properties: string[];
-			sort: Record<string, "ascending" | "descending">;
-			queries: {
-				data: any;
-				filter: {
-					property: string;
-				};
-				filterType: FilterType;
-			}[];
-			operator: "and" | "or" | "nor";
-		}) {
+		async function getData(
+			payload: Parameters<GetData["getData"]>[0]
+		): ReturnType<GetData["getData"]> {
 			const { page, pageSize, properties, sort, queries, operator } =
 				payload;
 

+ 7 - 2
backend/src/schemas/station.ts

@@ -1,4 +1,5 @@
-import { Schema, SchemaTypes, Types } from "mongoose";
+import { Model, Schema, SchemaTypes, Types } from "mongoose";
+import { GetData } from "./plugins/getData";
 
 export enum StationType {
 	OFFICIAL = "official",
@@ -60,7 +61,9 @@ export interface StationSchema {
 	};
 }
 
-export const schema = new Schema<StationSchema>(
+export interface StationModel extends Model<StationSchema>, GetData {}
+
+export const schema = new Schema<StationSchema, StationModel>(
 	{
 		type: {
 			type: SchemaTypes.String,
@@ -171,3 +174,5 @@ export const schema = new Schema<StationSchema>(
 	},
 	{ pluginTags: ["useGetDataPlugin"] }
 );
+
+export type StationSchemaType = typeof schema;

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

@@ -1,21 +1,15 @@
-import { Model, Schema } from "mongoose";
-import { AbcSchema } from "../schemas/abc";
-import { NewsQueryHelpers, NewsSchema } from "../schemas/news";
-import { StationSchema } from "../schemas/station";
+import { AbcModel, AbcSchemaType } from "../schemas/abc";
+import { NewsModel, NewsSchemaType } from "../schemas/news";
+import { StationModel, StationSchemaType } from "../schemas/station";
 
 export type Schemas = {
-	abc: Schema<AbcSchema>;
-	news: Schema<
-		NewsSchema,
-		Model<NewsSchema, NewsQueryHelpers>,
-		{},
-		NewsQueryHelpers
-	>;
-	station: Schema<StationSchema>;
+	abc: AbcSchemaType;
+	news: NewsSchemaType;
+	station: StationSchemaType;
 };
 
 export type Models = {
-	abc: Model<AbcSchema>;
-	news: Model<NewsSchema, NewsQueryHelpers>;
-	station: Model<StationSchema>;
+	abc: AbcModel;
+	news: NewsModel;
+	station: StationModel;
 };