61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
import mongoose from "mongoose";
|
|
const { Schema } = mongoose;
|
|
|
|
// Define the moonraker connection schema
|
|
const moonrakerSchema = new Schema(
|
|
{
|
|
host: { type: String, required: true },
|
|
port: { type: Number, required: true },
|
|
protocol: { type: String, required: true },
|
|
apiKey: { type: String, default: null, required: false },
|
|
},
|
|
{ _id: false },
|
|
);
|
|
|
|
// Define the alert schema
|
|
const alertSchema = new Schema(
|
|
{
|
|
priority: { type: String, required: true }, // order to show
|
|
type: { type: String, required: true }, // selectFilament, error, info, message,
|
|
},
|
|
{ timestamps: true, _id: false }
|
|
);
|
|
|
|
// Define the main printer schema
|
|
const printerSchema = new Schema(
|
|
{
|
|
name: { type: String, required: true },
|
|
online: { type: Boolean, required: true, default: false },
|
|
state: {
|
|
type: { type: String, required: true, default: "Offline" },
|
|
progress: { required: false, type: Number, default: 0 },
|
|
},
|
|
connectedAt: { type: Date, default: null },
|
|
loadedFilament: {
|
|
type: Schema.Types.ObjectId,
|
|
ref: "Filament",
|
|
default: null,
|
|
},
|
|
moonraker: { type: moonrakerSchema, required: true },
|
|
tags: [{ type: String }],
|
|
firmware: { type: String },
|
|
currentJob: { type: Schema.Types.ObjectId, ref: "PrintJob" },
|
|
currentSubJob: { type: Schema.Types.ObjectId, ref: "PrintSubJob" },
|
|
currentFilamentStock: { type: Schema.Types.ObjectId, ref: "FilamentStock" },
|
|
subJobs: [{ type: Schema.Types.ObjectId, ref: "PrintSubJob" }],
|
|
alerts: [alertSchema],
|
|
},
|
|
{ timestamps: true },
|
|
);
|
|
|
|
// Add virtual id getter
|
|
printerSchema.virtual("id").get(function () {
|
|
return this._id.toHexString();
|
|
});
|
|
|
|
// Configure JSON serialization to include virtuals
|
|
printerSchema.set("toJSON", { virtuals: true });
|
|
|
|
// Create and export the model
|
|
export const printerModel = mongoose.model("Printer", printerSchema);
|