Moleculer DBService中的严格集合/表名称设置

时间:2019-05-20 14:01:31

标签: moleculer

我正在设计Moleculer JS微服务,它扩展了Moleculer DB并且可以扩展SQL(例如Sequelize)和noSQL(例如MongoDB)适配器。

这是book服务架构

const uuid = require('uuid/v4')
const Sequelize = require('sequelize')
const DBService = require('moleculer-db')

const pkg = require('../package.json')
const adapter = require('../db/sql.adapter')

module.exports = {
  name: 'book',
  version: pkg.version,
  mixins: [DBService],
  settings: {
    $noVersionPrefix: true,
    idField: 'id',
  },
  adapter,
  model: {
    name: 'book',
    define: {
      id: { type: Sequelize.UUID, primaryKey: true, allowNull: false, defaultValue: uuid },
      name: { type: Sequelize.STRING, allowNull: false },
    },
  },
}

尽管将在启动book表/集合的Molecularr服务上创建模型名称为books(无论是SQL还是noSQL都无关紧要)。没关系。

但是,如果我要创建另一个将保留已删除项目的服务,则将分别创建books_archivebook_archive-books_archivesbook_archives。 / p>

documentation包含

freezeTableName: true,
tableName: 'booksArchive',

配置属性,我尝试将其放在模式的settings块中和model块中,以及在适配器构造属性中(请参见下文),但是没有运气-仍然创建了collection(table)仅包含一个额外的s字符。有什么解决办法吗?

const adapter = new SQLAdapter(config.get('database.pg.uri'), {
    dialect: 'postgres',
    logging: false,
    freezeTableName: true,
    tableName: 'booksArchive',
})

1 个答案:

答案 0 :(得分:0)

基于序列化文档,应在“模型”选项中进行设置。试试吧:

module.exports = {
  name: 'book',
  version: pkg.version,
  mixins: [DBService],
  settings: {
    $noVersionPrefix: true,
    idField: 'id',
  },
  adapter,
  model: {
    name: 'book',
    define: {
      id: { type: Sequelize.UUID, primaryKey: true, allowNull: false, defaultValue: uuid },
      name: { type: Sequelize.STRING, allowNull: false },
    },
    options: {
      freezeTableName: true,
      tableName: 'booksArchive',
    }
  },
}