Added Crud Rest API for guest players, unknown players in saved games are automatically added as guests. Games are now as such saved correctly. More decoupling from models. Updated express for better error handling.

This commit is contained in:
Daniel Ledda
2020-05-25 22:57:36 +02:00
parent 3a7e7de3d4
commit 4ccbdf599e
15 changed files with 483 additions and 140 deletions

41
src/models/utils.ts Normal file
View File

@@ -0,0 +1,41 @@
export class GenericModelError extends Error {
constructor(message: string) {
super(message);
this.name = "GenericModelError";
}
}
export class ModelParameterError extends GenericModelError {
constructor(message: string) {
super(message);
this.name = "ModelParameterError";
}
}
type CallbackWrapper = <T>(query: () => T) => Promise<any>;
export const tryQuery: CallbackWrapper = async (cb) => {
try {
return cb();
}
catch (err) {
throw new GenericModelError(err);
}
};
export const globalSchemaOptions = {
toObject: {
transform: function (doc: any, ret: any) {
ret.id = ret._id;
delete ret._id;
delete ret.__v;
}
},
toJSON: {
transform: function (doc: any, ret: any) {
ret.id = ret._id;
delete ret._id;
delete ret.__v;
},
}
};