· 5 years ago · Jul 07, 2020, 01:58 PM
1// Bot.js
2const { DataTypes } = require('sequelize');
3const sequelize = require('../database/connection');
4
5module.exports = sequelize.define("bot", {
6 id: {
7 type: DataTypes.INTEGER,
8 autoIncrement: true,
9 primaryKey: true
10 },
11 name: {
12 type: DataTypes.STRING(45)
13 },
14 isActive: {
15 field: "is_active",
16 type: DataTypes.BOOLEAN,
17 },
18 secretKey: {
19 field: "secret_key",
20 type: DataTypes.STRING(100)
21 },
22 url: {
23 type: DataTypes.STRING(45)
24 },
25 token: {
26 type: DataTypes.STRING(45)
27 }
28});
29
30//SmoochApp.js
31const { DataTypes } = require('sequelize');
32const sequelize = require('../database/connection');
33
34module.exports = sequelize.define("smooch_app", {
35 id: {
36 type: DataTypes.INTEGER,
37 autoIncrement: true,
38 primaryKey: true
39 },
40 botId: {
41 field: "bot_id",
42 type: DataTypes.INTEGER
43 },
44 appId: {
45 field: "app_id",
46 type: DataTypes.STRING,
47 allowNull: false
48 }
49});
50
51//index.js (models)
52
53const SmoochApp = require('./SmoochApp');
54const Bot = require('./Bot');
55
56SmoochApp.belongsTo(Bot, {
57 // as: "Bot",
58 foreignKey: "botId"
59});
60
61Bot.hasMany(SmoochApp, {
62 // as: "SmoochApp",
63 foreignKey: "botId"
64});
65
66module.exports = {
67 SmoochApp,
68 Bot
69}
70
71//query
72const bot = await Bot.findOne({
73 where: {
74 isActive: true
75 },
76 include: [{
77 model: SmoochApp,
78 where: {
79 appId: body.app._id
80 }
81 }]
82 })
83
84//output
85Executing (default): SELECT `bot`.*, `smooch_apps`.`id` AS `smooch_apps.id`, `smooch_apps`.`bot_id` AS `smooch_apps.botId`, `smooch_apps`.`app_id` AS `smooch_apps.appId` FROM (SELECT `bot`.`id`, `bot`.`name`, `bot`.`is_active` AS `isActive`, `bot`.`secret_key` AS `secretKey`, `bot`.`url`, `bot`.`token` FROM `bots` AS `bot` WHERE `bot`.`is_active` = true AND ( SELECT `bot_id` FROM `smooch_apps` AS `smooch_apps` WHERE (`smooch_apps`.`app_id` = 'NjEGyjnZB0s.FXwWAllA1-KBWKjMAAi3HUWWum-WnUwQNEQqKX2Gxv4' AND `smooch_apps`.`bot_id` = `bot`.`id`) LIMIT 1 ) IS NOT NULL LIMIT 1) AS `bot` INNER JOIN `smooch_apps` AS `smooch_apps` ON `bot`.`id` = `smooch_apps`.`bot_id` AND `smooch_apps`.`app_id` = 'NjEGyjnZB0s.FXwWAllA1-KBWKjMAAi3HUWWum-WnUwQNEQqKX2Gxv4';
86(node:5631) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: Unknown column 'bot.id' in 'field list'
87 at Query.formatError (/home/farias/kognita/azure-functions/smoochToBot/node_modules/sequelize/lib/dialects/mysql/query.js:239:16)
88 at Query.run (/home/farias/kognita/azure-functions/smoochToBot/node_modules/sequelize/lib/dialects/mysql/query.js:54:18)
89 at processTicksAndRejections (internal/process/task_queues.js:97:5)
90(node:5631) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
91(node:5631) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.