Answer the question
In order to leave comments, you need to log in
How to get related data?
There is an application where you need to add some functions, there is a line with rate data where the userId field contains the user id and is connected to the users table, and so, you need to get the rate line in such a way that it contains information about the user, so as not to make a separate request, I tried almost everything, so I'll throw off part of the code below, maybe I don't notice something.
Search in db
async getMinesGame():Promise<HistoryMinesGame[]> {
return this.db.MinesGame.findAll({
where :{
winAmount:{[Op.not]: null}
},
include: [{
model: this.db.User,
as: 'userId'
}]
//Перепробовал очень много вариантов не один не сработал
});
}
import { BelongsTo, BelongsToGetAssociationMixin, DataTypes, Model, Sequelize } from 'sequelize';
import { AssociableModelStatic } from './index';
import { User } from './User';
export enum MinesGameStatus {
InGame = 'InGame',
Ended = 'Ended',
}
export interface MinesGame extends Model {
readonly id: string;
readonly userId: string;
readonly betAmount: number;
readonly winAmount: number;
readonly bombsCount: number;
readonly fieldConf: number;
readonly stepsConf: number;
readonly status: MinesGameStatus;
readonly clientSeed: string;
readonly serverSeed: string;
readonly nonce: number;
readonly createdAt: Date;
readonly updatedAt: Date;
getUser: BelongsToGetAssociationMixin<User>;
}
export interface HistoryMinesGame extends MinesGame {
readonly winAmount: number;
}
export type MinesGameStatic = AssociableModelStatic<
MinesGame,
{
User: BelongsTo<MinesGame, User>;
}
>;
export const initMinesGame = (sequelize: Sequelize): MinesGameStatic => {
const MinesGame = sequelize.define('MinesGame', {
id: {
type: DataTypes.INTEGER.UNSIGNED,
primaryKey: true,
autoIncrement: true,
get(this: MinesGame) {
return ((this.getDataValue('id') as unknown) as number).toString();
},
},
userId: {
type: DataTypes.INTEGER.UNSIGNED,
get(this: MinesGame) {
return ((this.getDataValue('userId') as unknown) as number).toString();
},
},
betAmount: {
type: DataTypes.DOUBLE(8, 2),
allowNull: false,
},
winAmount: {
type: DataTypes.DOUBLE(8, 2),
allowNull: true,
},
bombsCount: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
},
fieldConf: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
},
stepsConf: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 0,
},
status: {
type: DataTypes.ENUM(...Object.values(MinesGameStatus)),
allowNull: false,
defaultValue: MinesGameStatus.InGame,
},
clientSeed: {
type: DataTypes.STRING,
allowNull: false,
},
serverSeed: {
type: DataTypes.STRING,
allowNull: false,
},
nonce: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
},
}) as MinesGameStatic;
MinesGame.associate = database => {
MinesGame.User = MinesGame.belongsTo(database.User, { as: 'user'});
};
return MinesGame;
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question