Browse Source

refactor: continue sequalize changes

Generate ObjectID in CreateJob automatically
Fixes for creating minifiedUsers view
Kristian Vos 8 months ago
parent
commit
99d982e8a4

+ 12 - 0
backend/package-lock.json

@@ -12,6 +12,7 @@
 				"axios": "^1.4.0",
 				"bcrypt": "^5.1.0",
 				"body-parser": "^1.20.2",
+				"bson-objectid": "^2.0.4",
 				"config": "^3.3.9",
 				"cookie-parser": "^1.4.6",
 				"cors": "^2.8.5",
@@ -1363,6 +1364,12 @@
 				"node": ">=14.20.1"
 			}
 		},
+		"node_modules/bson-objectid": {
+			"version": "2.0.4",
+			"resolved": "https://registry.npmjs.org/bson-objectid/-/bson-objectid-2.0.4.tgz",
+			"integrity": "sha512-vgnKAUzcDoa+AeyYwXCoHyF2q6u/8H46dxu5JN+4/TZeq/Dlinn0K6GvxsCLb3LHUJl0m/TLiEK31kUwtgocMQ==",
+			"license": "Apache-2.0"
+		},
 		"node_modules/buffer-from": {
 			"version": "1.1.2",
 			"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
@@ -7219,6 +7226,11 @@
 			"resolved": "https://registry.npmjs.org/bson/-/bson-5.3.0.tgz",
 			"integrity": "sha512-ukmCZMneMlaC5ebPHXIkP8YJzNl5DC41N5MAIvKDqLggdao342t4McltoJBQfQya/nHBWAcSsYRqlXPoQkTJag=="
 		},
+		"bson-objectid": {
+			"version": "2.0.4",
+			"resolved": "https://registry.npmjs.org/bson-objectid/-/bson-objectid-2.0.4.tgz",
+			"integrity": "sha512-vgnKAUzcDoa+AeyYwXCoHyF2q6u/8H46dxu5JN+4/TZeq/Dlinn0K6GvxsCLb3LHUJl0m/TLiEK31kUwtgocMQ=="
+		},
 		"buffer-from": {
 			"version": "1.1.2",
 			"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",

+ 1 - 0
backend/package.json

@@ -20,6 +20,7 @@
 		"axios": "^1.4.0",
 		"bcrypt": "^5.1.0",
 		"body-parser": "^1.20.2",
+		"bson-objectid": "^2.0.4",
 		"config": "^3.3.9",
 		"cookie-parser": "^1.4.6",
 		"cors": "^2.8.5",

+ 7 - 2
backend/src/modules/DataModule.ts

@@ -165,9 +165,14 @@ export class DataModule extends BaseModule {
 			setupAssociation();
 		});
 
-		await this._sequelize.sync({ force: true });
+		await this._sequelize.sync();
 
-		// TODO move to a better spot
+		// TODO move to a better spot and improve
+		try {
+			await this._sequelize.query(
+				`DROP TABLE IF EXISTS "minifiedUsers"` 
+			);
+		} catch (err) {}
 		await this._sequelize.query(
 			`CREATE OR REPLACE VIEW "minifiedUsers" AS SELECT _id, username, name, role FROM users`
 		);

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

@@ -1,6 +1,6 @@
 import Joi from "joi";
 import DataModuleJob from "./DataModuleJob";
-
+import ObjectID from "bson-objectid";
 export default abstract class CreateJob extends DataModuleJob {
 	protected static _payloadSchema = Joi.object({
 		query: Joi.object().min(1).required()
@@ -14,6 +14,8 @@ export default abstract class CreateJob extends DataModuleJob {
 		if (Object.hasOwn(model.getAttributes(), "createdBy"))
 			query.createdBy = (await this._context.getUser())._id;
 
+		query[model.primaryKeyAttribute] = ObjectID();
+
 		return model.create(query);
 	}
 }

+ 1 - 2
backend/src/modules/DataModule/models/News.ts

@@ -33,8 +33,7 @@ export const schema = {
 	_id: {
 		type: DataTypes.OBJECTID,
 		allowNull: false,
-		primaryKey: true,
-		defaultValue: () => "66d6d2d2065de4fd650278be" // TODO add ObjectId generator
+		primaryKey: true
 	},
 	title: {
 		type: DataTypes.STRING,

+ 1 - 2
backend/src/modules/DataModule/models/User.ts

@@ -29,8 +29,7 @@ export const schema = {
 	_id: {
 		type: DataTypes.OBJECTID,
 		primaryKey: true,
-		allowNull: false,
-		defaultValue: () => "66d6d2d2065de4fd650278be"
+		allowNull: false
 	},
 	username: {
 		type: DataTypes.STRING,