feathersjsv4
nodejs
sequelize
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
const { COLUMN } = require('../utils/constant');
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const node = sequelizeClient.define('node', {
[COLUMN.ID]: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
parent_id: {
type: DataTypes.STRING,
allowNull: true,
}
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
// eslint-disable-next-line no-unused-vars
node.associate = function (models) {
node.belongsTo(models.node, {
as: 'parent',
foreignKey: 'parent_id',
});
node.hasMany(models.node, {
as: 'children',
foreignKey: 'parent_id',
});
};
return node;
};
const sequelizeClient = app.get('sequelizeClient');
const Node = sequelizeClient.models.node;
const node = await Node.findByPk('819f60c1-af16-4bce-9b74-61b0ce1f841f');
const parentNode = await node.getParent();
console.log(parentNode);
const childNode = await node.getChildren();
console.log(childNode);
const nodes = await Node.findAll({
include: [
{ model: Node, as: 'parent', attributes: ['id', 'parent_id'] }, // 包含父節點
{ model: Node, as: 'children', attributes: ['id', 'parent_id'] }, // 包含子節點
],
raw: true,
nest: true,
});
console.log(nodes);
ref: https://zhuanlan.zhihu.com/p/667236864
Feb 9, 2024categories: website網頁設計tags: web hexo
Dec 21, 2023Research tags: graphic IGL Keyword Design and Layout Map Optimal Image Shape VISUALIZATION
Jun 21, 2023台股入門 tags: 股票 新手 [toc] 流程 委託 → 撮合 → 交割 委託
Aug 17, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up