2
0
Эх сурвалжийг харах

refactor: Rename schemas folder and removed old code

Owen Diffey 2 жил өмнө
parent
commit
4e2fd1fbf7

+ 0 - 75
backend/src/Schema.ts

@@ -1,75 +0,0 @@
-import { Attribute } from "./types/Attribute";
-import { Document } from "./types/Document";
-
-export enum Types {
-	String,
-	Number,
-	Date,
-	Boolean,
-	ObjectId,
-	Array,
-	// Map,
-	Schema
-}
-
-export const createAttribute = ({
-	type,
-	required,
-	restricted,
-	item,
-	schema,
-	defaultValue,
-	unique,
-	min,
-	max,
-	enumValues
-}: Partial<Attribute> & { type: Attribute["type"] }) => ({
-	type,
-	required: required ?? true,
-	restricted: restricted ?? false,
-	item,
-	schema,
-	defaultValue,
-	unique: unique ?? false,
-	min,
-	max,
-	enumValues
-});
-
-export default class Schema {
-	private document: Document;
-
-	private timestamps: boolean;
-
-	private version: number;
-
-	public constructor(schema: {
-		document: Document;
-		timestamps?: boolean;
-		version?: number;
-	}) {
-		this.document = {
-			_id: createAttribute({ type: Types.ObjectId }),
-			...schema.document
-		};
-		this.timestamps = schema.timestamps ?? true;
-		this.version = schema.version ?? 1;
-
-		if (this.timestamps) {
-			this.document.createdAt = createAttribute({
-				type: Types.Date
-			});
-			this.document.updatedAt = createAttribute({
-				type: Types.Date
-			});
-		}
-	}
-
-	public getDocument() {
-		return this.document;
-	}
-
-	public getVersion() {
-		return this.version;
-	}
-}

+ 0 - 40
backend/src/collections/abc.ts

@@ -1,40 +0,0 @@
-import Schema, { createAttribute, Types } from "../Schema";
-
-export default new Schema({
-	document: {
-		name: createAttribute({
-			type: Types.String
-		}),
-		autofill: createAttribute({
-			type: Types.Schema,
-			schema: {
-				enabled: createAttribute({
-					type: Types.Boolean,
-					required: false
-				})
-			}
-		}),
-		someNumbers: createAttribute({
-			type: Types.Array,
-			item: {
-				type: Types.Number
-			}
-		}),
-		songs: createAttribute({
-			type: Types.Array,
-			item: {
-				type: Types.Schema,
-				schema: {
-					_id: createAttribute({
-						type: Types.ObjectId
-					})
-				}
-			}
-		}),
-		restrictedName: createAttribute({
-			type: Types.String,
-			restricted: true
-		}),
-		aNumber: createAttribute({ type: Types.Number })
-	}
-});

+ 0 - 132
backend/src/collections/station.ts

@@ -1,132 +0,0 @@
-import Schema, { createAttribute, Types } from "../Schema";
-
-export default new Schema({
-	document: {
-		type: createAttribute({
-			type: Types.String,
-			enumValues: ["official", "community"]
-		}),
-		name: createAttribute({
-			type: Types.String,
-			unique: true,
-			min: 2,
-			max: 16
-		}),
-		displayName: createAttribute({
-			type: Types.String,
-			unique: true,
-			min: 2,
-			max: 32
-		}),
-		description: createAttribute({
-			type: Types.String,
-			min: 2,
-			max: 128
-		}),
-		privacy: createAttribute({
-			type: Types.String,
-			defaultValue: "private",
-			enumValues: ["public", "unlisted", "private"]
-		}),
-		theme: createAttribute({
-			type: Types.String,
-			defaultValue: "blue",
-			enumValues: ["blue", "purple", "teal", "orange", "red"]
-		}),
-		owner: createAttribute({
-			type: Types.ObjectId
-		}),
-		djs: createAttribute({
-			type: Types.Array,
-			item: {
-				type: Types.ObjectId
-			}
-		}),
-		currentSong: createAttribute({
-			type: Types.ObjectId,
-			required: false
-		}),
-		currentSongIndex: createAttribute({
-			type: Types.Number,
-			required: false
-		}),
-		startedAt: createAttribute({
-			type: Types.Date,
-			required: false
-		}),
-		paused: createAttribute({
-			type: Types.Boolean,
-			defaultValue: false
-		}),
-		timePaused: createAttribute({
-			type: Types.Number,
-			defaultValue: 0
-		}),
-		pausedAt: createAttribute({
-			type: Types.Date,
-			required: false
-		}),
-		playlist: createAttribute({
-			type: Types.ObjectId
-		}),
-		queue: createAttribute({
-			type: Types.Array,
-			item: {
-				type: Types.ObjectId
-			}
-		}),
-		blacklist: createAttribute({
-			type: Types.Array,
-			item: {
-				type: Types.ObjectId
-			}
-		}),
-		requests: createAttribute({
-			type: Types.Schema,
-			schema: {
-				enabled: createAttribute({
-					type: Types.Boolean,
-					defaultValue: true
-				}),
-				access: createAttribute({
-					type: Types.String,
-					defaultValue: "owner",
-					enumValues: ["owner", "user"]
-				}),
-				limit: createAttribute({
-					type: Types.Number,
-					defaultValue: 5,
-					min: 1,
-					max: 50
-				})
-			}
-		}),
-		autofill: createAttribute({
-			type: Types.Schema,
-			schema: {
-				enabled: createAttribute({
-					type: Types.Boolean,
-					defaultValue: true
-				}),
-				playlists: createAttribute({
-					type: Types.Array,
-					item: {
-						type: Types.ObjectId
-					}
-				}),
-				limit: createAttribute({
-					type: Types.Number,
-					defaultValue: 30,
-					min: 1,
-					max: 50
-				}),
-				mode: createAttribute({
-					type: Types.String,
-					defaultValue: "random",
-					enumValues: ["random", "sequential"]
-				})
-			}
-		})
-	},
-	version: 9
-});

+ 4 - 6
backend/src/main.ts

@@ -24,8 +24,6 @@ process.on("uncaughtException", err => {
 				: err
 		}
 	});
-
-	console.log(err);
 });
 
 const moduleManager = ModuleManager.getPrimaryInstance();
@@ -47,12 +45,12 @@ global.rs = () => {
 };
 
 setTimeout(async () => {
-	const model = await jobQueue.runJob("data", "getModel", { modelName: "abc" });
-	console.log("Model", model);
-	const abcs = await model.find({});
+	const Model = await jobQueue.runJob("data", "getModel", { name: "abc" });
+	console.log("Model", Model);
+	const abcs = await Model.find({});
 	console.log("Abcs", abcs);
 
-	model.create({
+	Model.create({
 		name: "Test name",
 		someNumbers: [1, 2, 3, 4],
 		songs: [],

+ 15 - 11
backend/src/modules/DataModule.ts

@@ -1,12 +1,12 @@
 import config from "config";
-//import { createClient, RedisClientType } from "redis";
+// import { createClient, RedisClientType } from "redis";
 import mongoose, {
-    MongooseDefaultQueryMiddleware,
-    MongooseDistinctQueryMiddleware,
-    MongooseQueryOrDocumentMiddleware
+	MongooseDefaultQueryMiddleware,
+	MongooseDistinctQueryMiddleware,
+	MongooseQueryOrDocumentMiddleware
 } from "mongoose";
 import JobContext from "../JobContext";
-import BaseModule from "../BaseModule";
+import BaseModule, { ModuleStatus } from "../BaseModule";
 import { UniqueMethods } from "../types/Modules";
 import { Models, Schemas } from "../types/Models";
 
@@ -88,7 +88,7 @@ export default class DataModule extends BaseModule {
 		modelName: ModelName
 	) {
 		const { schema }: { schema: Schemas[ModelName] } = await import(
-			`../models/${modelName.toString()}`
+			`../schemas/${modelName.toString()}`
 		);
 
 		const preMethods: string[] = [
@@ -117,7 +117,7 @@ export default class DataModule extends BaseModule {
 
 		preMethods.forEach(preMethod => {
 			// @ts-ignore
-            schema.pre(preMethods, () => {
+			schema.pre(preMethods, () => {
 				console.log(`Pre-${preMethod}!`);
 			});
 		});
@@ -140,16 +140,20 @@ export default class DataModule extends BaseModule {
 	/**
 	 * getModel - Get model
 	 *
-	 * @param jobContext
-	 * @param payload
 	 * @returns Model
 	 */
 	public async getModel<ModelName extends keyof Models>(
 		jobContext: JobContext,
-		payload: { modelName: ModelName }
+		payload: ModelName | { name: ModelName }
 	) {
 		if (!this.models) throw new Error("Models not loaded");
-		return this.models[payload.modelName];
+
+		if (this.getStatus() !== ModuleStatus.STARTED)
+			throw new Error("Module not started");
+
+		const name = typeof payload === "object" ? payload.name : payload;
+
+		return this.models[name];
 	}
 }
 

+ 13 - 4
backend/src/models/abc.ts → backend/src/schemas/abc.ts

@@ -1,6 +1,17 @@
-import { InferSchemaType, Schema, SchemaTypes } from "mongoose";
+import { Schema, SchemaTypes, Types } from "mongoose";
 
-export const schema = new Schema({
+export interface AbcSchema {
+	name: string;
+	autofill?: {
+		enabled?: boolean;
+	};
+	someNumbers: number[];
+	songs: { _id: Types.ObjectId }[];
+	restrictedName?: string;
+	aNumber: number;
+}
+
+export const schema = new Schema<AbcSchema>({
 	name: {
 		type: SchemaTypes.String,
 		required: true
@@ -23,5 +34,3 @@ export const schema = new Schema({
 	},
 	aNumber: { type: SchemaTypes.Number, required: true }
 });
-
-export type AbcSchema = typeof schema;

+ 0 - 16
backend/src/types/Attribute.ts

@@ -1,16 +0,0 @@
-import { Types } from "../Schema";
-import { AttributeValue } from "./AttributeValue";
-import { Document } from "./Document";
-
-export type Attribute = {
-	type: Types;
-	required: boolean;
-	restricted: boolean;
-	item?: Pick<Attribute, "type" | "item" | "schema">;
-	schema?: Document;
-	defaultValue?: AttributeValue;
-	unique?: boolean;
-	min?: number;
-	max?: number;
-	enumValues?: AttributeValue[];
-};

+ 0 - 3
backend/src/types/AttributeValue.ts

@@ -1,3 +0,0 @@
-import { ObjectId } from "mongodb";
-
-export type AttributeValue = string | number | boolean | Date | ObjectId | null;

+ 0 - 10
backend/src/types/Collections.ts

@@ -1,10 +0,0 @@
-import { Collection } from "mongodb";
-import Schema from "../Schema";
-
-export type Collections = Record<
-	"abc" | "station",
-	{
-		schema: Schema;
-		collection: Collection;
-	}
->;

+ 0 - 3
backend/src/types/Document.ts

@@ -1,3 +0,0 @@
-import { Attribute } from "./Attribute";
-
-export type Document = Record<string, Attribute>;

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

@@ -1,8 +1,8 @@
-import { Model, InferSchemaType } from "mongoose";
-import { AbcSchema } from "../models/abc";
+import { Model, InferSchemaType, Schema } from "mongoose";
+import { AbcSchema } from "../schemas/abc";
 
 export type Schemas = {
-	abc: AbcSchema;
+	abc: Schema<AbcSchema>;
 };
 
 export type Models = Record<