Thank you for the solution. and sorry for the late reply.
I was trying to fix my code after seeing the link you posted.
I’m embarrassed to say this, but I haven’t solved the problem yet.
and this is my code…
server
|_models
| |
| |_index.js
| |_user.js
|
|_index.js
index.js
require("dotenv").config();
const express = require("express");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const app = express();
const indexRouter = require("./routes");
const port = process.env.SERVER_PORT || 80;
const { createAgent } = require('@forestadmin/agent');
const { createSequelizeDataSource } = require('@forestadmin/datasource-sequelize');
const sequelizeInstance = require('./models');
createAgent({
authSecret: process.env.FOREST_AUTH_SECRET,
envSecret: process.env.FOREST_ENV_SECRET,
isProduction: process.env.NODE_ENV === 'production',
})
.addDataSource(createSequelizeDataSource(sequelizeInstance))
.mountOnExpress(app)
.start()
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
cors({
origin: ["..."],
method: ["GET", "POST", "PATCH", "PUT", "DELETE"],
credentials: true,
SameSite: "None",
}),
);
app.use("/", indexRouter);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
module.exports = app;
server/models/index.js
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js');
const db = {};
let sequelize = new Sequelize({
host: config.host,
username: config.username,
password: config.password,
port: config.port,
database: config.database,
dialect: 'mysql'
})
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
server/models/user.js
require("dotenv").config();
const { createAgent } = require('@forestadmin/agent');
const { createSequelizeDataSource } = require('@forestadmin/datasource-sequelize');
"use strict"
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User extends Model {
static associate(models) {
this.hasOne(models.Purchase, { foreignKey: "userId", sourceKey: "id" });
this.hasOne(models.UserInfo, { foreignKey: "userId", sourceKey: "id" });
}
}
User.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
identification: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
},
{ sequelize, modelName: 'User',}
);
// Create agent and import collections from sequelize
const agent = createAgent({
authSecret: process.env.FOREST_AUTH_SECRET,
envSecret: process.env.FOREST_ENV_SECRET,
isProduction: process.env.NODE_ENV === 'production',
}).addDataSource(createSequelizeDataSource(sequelize));
return User;
};
I tried to fix the model file after seeing link you sent me, but I don’t understand it well.
should I make agent in model file and main application file both?
and does the agent in the model file just have to declare?
I would appreciate it if you could give me a solution to the insufficient questions. Or is there any example file to refer to?