From 0432f857d7ee3d992bab41bd7bb1039b6e584a91 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Mon, 23 Aug 2021 21:31:30 -0400 Subject: [PATCH 1/3] chore(refactor):start refactoring types --- src/models/profile.model.ts | 2 +- src/repositories/profile.repository.ts | 6 +++--- src/types/{profiles.ts => digimon.ts} | 20 +++++++++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) rename src/types/{profiles.ts => digimon.ts} (92%) diff --git a/src/models/profile.model.ts b/src/models/profile.model.ts index 27117af..38061cb 100644 --- a/src/models/profile.model.ts +++ b/src/models/profile.model.ts @@ -1,5 +1,5 @@ import mongoose from 'mongoose' -import type { ProfileSchema } from '../types/profiles' +import type { ProfileSchema } from '../types/digimon' const techniqueSchema = new mongoose.Schema({ name: { type: String, required: true }, diff --git a/src/repositories/profile.repository.ts b/src/repositories/profile.repository.ts index b4cb21f..6891aff 100644 --- a/src/repositories/profile.repository.ts +++ b/src/repositories/profile.repository.ts @@ -7,8 +7,8 @@ import type { Attribute, Field, Group, - ProfileSchema -} from '../types/profiles' + DigimonSchema +} from '../types/digimon' class ProfileRepository { async read(req: Request) { @@ -85,7 +85,7 @@ class ProfileRepository { technique: getRequestBody.technique ?? getDocumentFromDB[0].technique, artwork: getRequestBody.artwork ?? getDocumentFromDB[0].artwork, profile: getRequestBody.profile ?? getDocumentFromDB[0].profile - } as ProfileSchema + } as DigimonSchema const updateProfile = await profileModel.updateOne( { diff --git a/src/types/profiles.ts b/src/types/digimon.ts similarity index 92% rename from src/types/profiles.ts rename to src/types/digimon.ts index 50cc998..a811e08 100644 --- a/src/types/profiles.ts +++ b/src/types/digimon.ts @@ -1,16 +1,26 @@ -export interface Profile { +export interface Digimon { name: string level: Level type: Type attribute: Attribute field: Field[] | null group: Group[] | null - technique: [{ name: string; description: string | null }] - artwork: string - profile: string + // TODO: make into a reference + technique: [Technique] + profile: { + artwork: string + sprite: string + description: string + } +} + +export type Technique = { + _id: string + name: string + description: string | null } -export interface ProfileSchema extends Profile { +export interface DigimonSchema extends Digimon { _id: { $oid: string } __v: number timestamps: { From 2272b842cd0fc9a09061840dfaf5329feff10abd Mon Sep 17 00:00:00 2001 From: Ricardo Date: Mon, 23 Aug 2021 21:41:53 -0400 Subject: [PATCH 2/3] chore(refactor): update comments --- src/types/digimon.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/types/digimon.ts b/src/types/digimon.ts index a811e08..00b0d98 100644 --- a/src/types/digimon.ts +++ b/src/types/digimon.ts @@ -5,11 +5,15 @@ export interface Digimon { attribute: Attribute field: Field[] | null group: Group[] | null - // TODO: make into a reference + // refactor into a reference technique: [Technique] profile: { artwork: string - sprite: string + sprite: { + // collect sprites into an array + sprite: string[] + map: string + } description: string } } From b51f9cabfb36585b6cba02456f7ac9bf6ac6f13f Mon Sep 17 00:00:00 2001 From: Ricardo Date: Tue, 24 Aug 2021 18:52:17 -0400 Subject: [PATCH 3/3] chore(refactor): factoring model --- src/models/profile.model.ts | 39 +++++++++++++++++++++++++++++-------- src/types/digimon.ts | 12 +++++++++--- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/models/profile.model.ts b/src/models/profile.model.ts index 38061cb..a0fffba 100644 --- a/src/models/profile.model.ts +++ b/src/models/profile.model.ts @@ -1,12 +1,29 @@ import mongoose from 'mongoose' -import type { ProfileSchema } from '../types/digimon' +const Schema = mongoose.Schema +import type { DigimonSchema, TechniqueSchema } from '../types/digimon' const techniqueSchema = new mongoose.Schema({ name: { type: String, required: true }, - description: { type: String, required: true } + description: { type: String, required: true }, + timestamps: { + createdAt: { + type: Date, + required: true, + default: Date.now() + }, + updatedAt: { + type: Date, + default: null + }, + deletedAt: { + type: Date, + default: null + } + } }) -const profileSchema = new mongoose.Schema({ +const digimonSchema = new mongoose.Schema({ + _id: Schema.Types.ObjectId, name: { type: String, unique: true, @@ -36,7 +53,7 @@ const profileSchema = new mongoose.Schema({ type: Array, required: true }, - technique: [techniqueSchema], + technique: [{ type: Schema.Types.ObjectId, ref: 'technique' }], artwork: { type: String, required: true, @@ -53,7 +70,8 @@ const profileSchema = new mongoose.Schema({ default: Date.now() }, updatedAt: { - type: Date + type: Date, + default: null }, deletedAt: { type: Date, @@ -62,7 +80,12 @@ const profileSchema = new mongoose.Schema({ } }) -export default mongoose.model( - 'profile', - profileSchema +const Digimon = mongoose.model( + 'digimon', + digimonSchema +) + +const Technique = mongoose.model( + 'technique', + techniqueSchema ) diff --git a/src/types/digimon.ts b/src/types/digimon.ts index 00b0d98..3ccf515 100644 --- a/src/types/digimon.ts +++ b/src/types/digimon.ts @@ -5,12 +5,10 @@ export interface Digimon { attribute: Attribute field: Field[] | null group: Group[] | null - // refactor into a reference technique: [Technique] profile: { artwork: string sprite: { - // collect sprites into an array sprite: string[] map: string } @@ -19,7 +17,6 @@ export interface Digimon { } export type Technique = { - _id: string name: string description: string | null } @@ -34,6 +31,15 @@ export interface DigimonSchema extends Digimon { } } +export interface TechniqueSchema extends Technique { + _id: { $oid: string } + timestamps: { + createdAt: string + updatedAt?: string + deletedAt: string + } +} + export type Level = | 'Baby I' | 'Baby II'