chore: Typescript database models (#2886)

closes #2798
This commit is contained in:
Tom Moor
2022-01-06 18:24:28 -08:00
committed by GitHub
parent d3cbf250e6
commit b20a341f0c
207 changed files with 5624 additions and 5315 deletions

View File

@@ -1,85 +1,105 @@
import { subMilliseconds } from "date-fns";
import { Op } from "sequelize";
import {
BelongsTo,
Column,
Default,
ForeignKey,
Table,
DataType,
} from "sequelize-typescript";
import { USER_PRESENCE_INTERVAL } from "@shared/constants";
import { User } from "@server/models";
import { DataTypes, Op, sequelize } from "../sequelize";
import Document from "./Document";
import User from "./User";
import BaseModel from "./base/BaseModel";
import Fix from "./decorators/Fix";
const View = sequelize.define("view", {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
lastEditingAt: {
type: DataTypes.DATE,
},
count: {
type: DataTypes.INTEGER,
defaultValue: 1,
},
});
@Table({ tableName: "views", modelName: "view" })
@Fix
class View extends BaseModel {
@Column
lastEditingAt: Date | null;
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'models' implicitly has an 'any' type.
View.associate = (models) => {
View.belongsTo(models.Document);
View.belongsTo(models.User);
};
@Default(1)
@Column(DataType.INTEGER)
count: number;
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'where' implicitly has an 'any' type.
View.increment = async (where) => {
const [model, created] = await View.findOrCreate({
where,
});
// associations
if (!created) {
model.count += 1;
model.save();
@BelongsTo(() => User, "userId")
user: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
userId: string;
@BelongsTo(() => Document, "documentId")
document: Document;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string;
static async incrementOrCreate(where: {
userId?: string;
documentId?: string;
collectionId?: string;
}) {
const [model, created] = await this.findOrCreate({
where,
});
if (!created) {
model.count += 1;
model.save();
}
return model;
}
return model;
};
View.findByDocument = async (documentId: string) => {
return View.findAll({
where: {
documentId,
},
order: [["updatedAt", "DESC"]],
include: [
{
model: User,
paranoid: false,
static async findByDocument(documentId: string) {
return this.findAll({
where: {
documentId,
},
],
});
};
View.findRecentlyEditingByDocument = async (documentId: string) => {
return View.findAll({
where: {
documentId,
lastEditingAt: {
[Op.gt]: subMilliseconds(new Date(), USER_PRESENCE_INTERVAL * 2),
},
},
order: [["lastEditingAt", "DESC"]],
});
};
View.touch = async (documentId: string, userId: string, isEditing: boolean) => {
const [view] = await View.findOrCreate({
where: {
userId,
documentId,
},
});
if (isEditing) {
const lastEditingAt = new Date();
view.lastEditingAt = lastEditingAt;
await view.save();
order: [["updatedAt", "DESC"]],
include: [
{
model: User,
paranoid: false,
},
],
});
}
return view;
};
static async findRecentlyEditingByDocument(documentId: string) {
return this.findAll({
where: {
documentId,
lastEditingAt: {
[Op.gt]: subMilliseconds(new Date(), USER_PRESENCE_INTERVAL * 2),
},
},
order: [["lastEditingAt", "DESC"]],
});
}
static async touch(documentId: string, userId: string, isEditing: boolean) {
const [view] = await this.findOrCreate({
where: {
userId,
documentId,
},
});
if (isEditing) {
const lastEditingAt = new Date();
view.lastEditingAt = lastEditingAt;
await view.save();
}
return view;
}
}
export default View;