@@ -44,9 +44,9 @@ export class NoteHistoryModel extends Model<InferAttributes<NoteHistoryModel>, I
4444export default class NoteHistorySequelizeStorage {
4545 public model : typeof NoteHistoryModel ;
4646
47- public userModel : typeof UserModel | null = null ;
47+ public userModel : typeof UserModel | undefined = undefined ;
4848
49- public noteModel : typeof NoteModel | null = null ;
49+ public noteModel : typeof NoteModel | undefined = undefined ;
5050
5151 private readonly database : Sequelize ;
5252
@@ -96,7 +96,7 @@ export default class NoteHistorySequelizeStorage {
9696
9797 this . model . belongsTo ( this . userModel , {
9898 foreignKey : 'userId' ,
99- as : this . userModel . tableName ,
99+ as : 'user' ,
100100 } ) ;
101101 }
102102
@@ -109,7 +109,7 @@ export default class NoteHistorySequelizeStorage {
109109
110110 this . model . belongsTo ( this . noteModel , {
111111 foreignKey : 'noteId' ,
112- as : this . noteModel . tableName ,
112+ as : 'note' ,
113113 } ) ;
114114 }
115115
@@ -139,10 +139,22 @@ export default class NoteHistorySequelizeStorage {
139139 * @returns array of metadata of the history records
140140 */
141141 public async getNoteHistoryByNoteId ( noteId : NoteHistoryRecord [ 'noteId' ] ) : Promise < NoteHistoryMeta [ ] > {
142- return await this . model . findAll ( {
142+ const historyMeta = await this . model . findAll ( {
143143 where : { noteId } ,
144144 attributes : [ 'id' , 'userId' , 'createdAt' ] ,
145+ include : {
146+ model : this . userModel ,
147+ as : 'user' ,
148+ attributes : [ 'name' , 'photo' ] ,
149+ } ,
145150 } ) ;
151+
152+ /**
153+ * We need this cast because of using sequelize model.include
154+ * Since it returns NoteHistoryModel[], however we included userModel,
155+ * without this cast NoteHistoryModel[] and NoteHistoryMeta[] are incompatible
156+ */
157+ return historyMeta as unknown as NoteHistoryMeta [ ] ;
146158 }
147159
148160 /**
0 commit comments