add db schema

This commit is contained in:
mbecker20
2022-03-14 00:14:37 -07:00
parent 37c2f6a203
commit 1bd5e8675a
16 changed files with 168 additions and 28 deletions
+22
View File
@@ -13,5 +13,27 @@
"",
"export default ${1:$TM_FILENAME_BASE};"
]
},
"schema": {
"scope": "typescript",
"prefix": "schema",
"body": [
"import { FastifyInstance } from \"fastify\";",
"import fp from \"fastify-plugin\";",
"import { Schema } from \"mongoose\";",
"",
"const ${2:$TM_FILENAME_BASE} = fp((app: FastifyInstance, _: {}, done: () => void) => {",
"\tconst schema = new Schema({",
"\t\t${0}",
"\t});",
"\t",
"\tapp.decorate(\"${2:$TM_FILENAME_BASE}\", app.mongoose.model(\"${1}\", schema));",
"\t",
"\tdone();",
"});",
"",
"export default ${2:$TM_FILENAME_BASE};"
]
}
}
-22
View File
@@ -1,22 +0,0 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import mongoose, { Model } from "mongoose";
import { MONGO_URL } from "../config";
import { User } from "@monitor/types";
declare module "fastify" {
interface FastifyInstance {
mongoose: typeof mongoose;
users: Model<User>;
}
}
const db = fp((app: FastifyInstance, _: {}, done: () => void) => {
mongoose.connect(MONGO_URL);
app.decorate("mongoose", mongoose);
done();
});
export default db;
+3 -3
View File
@@ -1,9 +1,9 @@
import fastify from "fastify";
import fastifyCors from "fastify-cors";
import auth from "./auth";
import auth from "./util/auth";
import { LOG, PORT } from "./config";
import db from "./db";
import ws from "./ws";
import db from "./util/db";
import ws from "./util/ws";
const app = fastify({ logger: LOG })
.register(fastifyCors)
@@ -2,7 +2,7 @@ import axios from "axios";
import { FastifyInstance } from "fastify";
import fastifyOauth2, { OAuth2Namespace } from "fastify-oauth2";
import fp from "fastify-plugin";
import { HOST, SECRETS, TOKEN_EXPIRES_IN } from "../config";
import { HOST, SECRETS, TOKEN_EXPIRES_IN } from "../../config";
declare module "fastify" {
interface FastifyInstance {
@@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify";
import { FastifyInstance } from "fastify";
import fastifyJwt from "fastify-jwt";
import fp from "fastify-plugin";
import { SECRETS } from "../config";
import { SECRETS } from "../../config";
declare module "fastify" {
interface FastifyInstance {
@@ -2,7 +2,7 @@ import { User } from "@monitor/types";
import { compare, hash } from "bcrypt";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import fp from "fastify-plugin";
import { PASSWORD_SALT_ROUNDS, TOKEN_EXPIRES_IN } from "../config";
import { PASSWORD_SALT_ROUNDS, TOKEN_EXPIRES_IN } from "../../config";
const local = fp((app: FastifyInstance, _: {}, done: () => void) => {
app.post(
+25
View File
@@ -0,0 +1,25 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import { Schema } from "mongoose";
const builds = fp((app: FastifyInstance, _: {}, done: () => void) => {
const schema = new Schema({
name: { type: String, unique: true, index: true },
/* repo related */
repo: String,
branch: String,
accessToken: String, // to gain access to private repos
/* build related */
buildPath: String, // build folder relative to repo root
dockerfilePath: String, // relative to buildPath
pullName: { type: String, index: true }, // derived on build creation
imageName: String, // derived on build creation
owner: { type: String, index: true }, // userID / username
});
app.decorate("builds", app.mongoose.model("Build", schema));
done();
});
export default builds;
+34
View File
@@ -0,0 +1,34 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import { Schema } from "mongoose";
import { Conversion, EnvironmentVar, Volume } from "./misc";
const deployments = fp((app: FastifyInstance, _: {}, done: () => void) => {
const schema = new Schema({
name: { type: String, unique: true, index: true },
owner: { type: String, index: true },
serverID: { type: String, index: true },
buildID: { type: String, index: true }, // if deploying a monitor build
/* to create docker run command */
image: String, // used if deploying an external image (from docker hub)
latest: Boolean, // if custom image, use this to add :latest
ports: [Conversion],
volumes: [Volume],
environment: [EnvironmentVar],
network: String,
restart: String,
postImage: String, // interpolated into run command after the image String
containerUser: String, // after -u in the run command
/* to manage repo for static frontend, mounted as a volume */
repo: String,
accessToken: String,
pullName: { type: String, index: true }, // for auto pull of repo
containerMount: String, // the file path to mount repo on inside the container
});
app.decorate("deployments", app.mongoose.model("Deployment", schema));
done();
});
export default deployments;
+35
View File
@@ -0,0 +1,35 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import mongoose, { Model } from "mongoose";
import { MONGO_URL } from "../../config";
import { Build, Deployment, Update, User } from "@monitor/types";
import users from "./users";
import updates from "./updates";
import deployments from "./deployments";
import builds from "./builds";
declare module "fastify" {
interface FastifyInstance {
mongoose: typeof mongoose;
users: Model<User>;
deployments: Model<Deployment>;
builds: Model<Build>;
updates: Model<Update>;
}
}
const db = fp((app: FastifyInstance, _: {}, done: () => void) => {
mongoose.connect(MONGO_URL);
app.decorate("mongoose", mongoose);
app
.register(users)
.register(deployments)
.register(builds)
.register(updates);
done();
});
export default db;
+21
View File
@@ -0,0 +1,21 @@
import { Schema } from "mongoose";
export const Conversion = new Schema({
local: String,
container: String,
});
export const Volume = new Schema({
variable: String,
value: String,
});
export const EnvironmentVar = new Schema({
variable: String,
value: String,
});
export const Log = new Schema({
stdout: String,
stderr: String,
});
+25
View File
@@ -0,0 +1,25 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import { Schema } from "mongoose";
import { Log } from "./misc";
const updates = fp((app: FastifyInstance, _: {}, done: () => void) => {
const schema = new Schema({
buildID: { type: String, index: true },
deploymentID: { type: String, index: true },
serverID: { type: String, index: true },
operation: { type: String, index: true },
command: String,
log: Log,
timestamp: Number,
note: String,
isError: Boolean,
operator: { type: String, index: true }, // the userID or username
});
app.decorate("updates", app.mongoose.model("Update", schema));
done();
});
export default updates;
View File