记录nocobase的部署、搭建及其插件开发技巧

坑1、因为我mysql的版本是5.6,所以用docker的方式部署,不知道搞错了什么。连插件build的时候一直提示yarn install。目前无解,我啥都没搞,一直卡在这好久。解决:在另一个机器搭建了非docker版本,build后以插件的方式安装到docker版本去

坑2、mysql、nodjs、yarn版本一定要和官网对应!!否则很多问题出现找不到,解决非常非常困难。因为版本不同,编译的插件其他版本无法使用。

1、安装:

官网已经详细:https://docs-cn.nocobase.com/welcome/getting-started/installation/create-nocobase-app

2、开发简单插件:

1、先创建简单的插件demo

yarn pm create @my-project/plugin-wxs

yarn  build @my-project/plugin-wxs

注意、最好删了项目根目录的yarn.lock 然后yarn install一次最好。然后重启就会在插件库出现,yarn install 解决了我不能yarn pm add @my-project/plugin-hello 显示的问题

坑:手动删了一个插件,然后数据库没删,它在加载的时候一直报错,无法显示任何界面,花了我半天时间,最后进数据库去看,发现插件表有数据,删掉重启就恢复了

2、简单demo

1、自定义一个api接口,在server目录写

代码:

import { Plugin } from '@nocobase/server';
const axios = require('axios');

export class PluginWxsServer extends Plugin {
  async afterAdd() {}

  async beforeLoad() {}

  async load() {
    // 公开hello表的所有操作
    this.app.acl.allow('hello', '*', 'public');
    
     this.app.resource({
      name: 'wxserver',
      actions: {
        async register(ctx, next) {
         try {
          const response = await axios.get('https://hgks.aizwkj.com/server-api/api/t_8vxfgoykz71:list?filter={%22user_wj_id%22:49}&appends=f_4c7x6l61kqa&except=f_4c7x6l61kqa.f_btzy6xntn88&appends[]=f_4c7x6l61kqa.f_gxstjml35dc');
          console.log('服务端请求结果', response.data);
          
          ctx.body = {
            status: 'success',
            data: response.data
          };
        } catch (error) {
          console.error('API请求错误', error.message);
          
          ctx.body = {
            status: 'error',
            message: error.message,
            details: error.response?.data || {}
          };
          
          ctx.status = error.response?.status || 500;
        }
          next();
        },
      },
    });
    this.app.acl.allow('wxserver', 'register', 'public');
      
  }

  async install() {}

  async afterEnable() {}

  async afterDisable() {}

  async remove() {}
}

export default PluginWxsServer;

这个是开放权限的,不懂问豆包

 this.app.acl.allow('wxserver', 'register', 'public');

访问

192.168.1.126:13001/api/wxserver:register

结果:

记录一下关于数据表的关联:

这样就关联了另一个表的数据了

3、数据库操作

单条查询:通过主键(ID 或 TK)获取单条记录

async function getSingleRecord(ctx) {
  const tableRepo = ctx.db.getRepository('tableName');
  const { id } = ctx.params; // 从URL参数获取ID,如 /api/tableName/:id
  
  // 方法1:使用 get 方法(推荐,性能更好)
  const record = await tableRepo.get(id);
  
  // 方法2:使用 findOne 方法(支持更复杂的筛选条件)
  const record2 = await tableRepo.findOne({
    filter: { id }
  });
  
  ctx.body = record;
}

关联查询:查询主表数据并包含关联表数据

async function queryWithRelations(ctx) {
  const tableRepo = ctx.db.getRepository('tableName');
  
  // 示例:查询订单表(orders)并包含关联的用户(user)和商品(products)数据
  const orders = await tableRepo.find({
    filter: {},
    include: [
      'user', // 关联用户表
      {
        association: 'products', // 关联商品表
        filter: { price: { $gt: 100 } } // 筛选关联商品价格大于100
      }
    ]
  });
  
  ctx.body = orders;
}

demo:读取数据库的token信息,提交给微信小程序url获取文字素材

    // 公开hello表的所有操作
    this.app.acl.allow('hello', '*', 'public');
    
     this.app.resource({
      name: 'wxserver',
      actions: {
        async register(ctx, next) {
         try {
          const system = ctx.db.getRepository('system'); // 替换为实际表名
          // 使用 find 方法查询所有数据
          const records = await system.findOne(); 
          console.error('1 records.wx_token=', records.wx_token); 
          const url='https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='+records.wx_token.access_token
          const response = await axios.post(url,
          {
              type: "news",
              offset: 0,
              count: 20
          });
          console.error('response=', response,"url=",url); 
            // 只返回响应数据,而不是整个响应对象
          ctx.body = response.data;
          
        } catch (error) {
          console.error('API请求错误', error.message);
          
          ctx.body = {
            status: 'error',
            message: error.message,
            details: error.response?.data || {}
          };
          
          ctx.status = error.response?.status || 500;
        }
          next();
        },
      },
    });
    this.app.acl.allow('wxserver', 'register', 'public');
      
  }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_陈陆亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值