【问题标题】:Seed my database, or insert data programatically in ApostropheCMS播种我的数据库,或在 ApostropheCMS 中以编程方式插入数据
【发布时间】:2018-03-09 20:33:25
【问题描述】:

我想用大量博客文章为我的数据库做一个非常快速和肮脏的“种子”......我很高兴使用 Mongo 游标等,但似乎找不到有效的生命周期方法将这段代码“插入”到……这一切都是为了证明概念,所以它不需要完美!

有人能指出我正确的方向吗?我似乎无法在construct 方法中访问req,所以不能在那里self.insert...

【问题讨论】:

  • 为什么投反对票?这是关于撇号的可靠问题,其他人可能会重视答案。
  • @TomBoutell 我想知道否决票是否是因为该主题不是问题的形式?我没有足够的精力来编辑这个,但也许你有?

标签: apostrophe-cms


【解决方案1】:

查看apostrophe-tasks 模块。这使您可以轻松地在自己的模块中添加命令行任务,并轻松获得具有完全管理员权限的req 对象来执行此类工作。

您的模块可以从construct 内部调用self.apos.tasks.add

self.apos.tasks.add(self.__meta.name, 'insert-stuff', function(apos, argv, callback) {
  var req = self.apos.tasks.getReq();
  return self.find(req, { cool: true }).toArray().then(function(err, pieces) {
    if (err) {
      return callback(err);
    }
    // etc., do things with `pieces`, then invoke callback(null);
  });
};

【讨论】:

    【解决方案2】:

    根据@Tom Boutell 的回复,这是我的最终工作代码...

        construct: function(self, options) {
          self.apos.tasks.add(self.__meta.name, 'insert-blog-articles', function(apos, argv, callback) {
            console.info(`Running ${self.__meta.name}:insert-blog-articles`)
    
            if(!argv.create) throw new Error('Please pass a number of articles to create using --create=n')
    
            const req = self.apos.tasks.getReq()
            const numberToCreate = Array.from(Array(argv.create).keys())
    
            numberToCreate.forEach(item => {
              let blogPost = self.newInstance()
    
              blogPost = Object.assign({}, blogPost, {
                title: 'Post about cats!',
                image: 'https://www.vetbabble.com/wp-content/uploads/2016/11/hiding-cat.jpg',
                published: true,
                testData: true
              })
    
              self.insert(req, blogPost)
                .then(result => { console.info('Inserted doc!', result) })
              })
            })
    
            self.apos.tasks.add(self.__meta.name, 'show-hide-articles', function(apos, argv, callback) {
              console.info('Running task show-hide-articles', argv)
    
              const set = argv.show ? { published: true } : { published: false }
    
              self.apos.docs.db.update(
                { type: 'apostrophe-blog', testData: true },
                { $set: set },
                { multi: true }
              )
              .then(result => {
                argv.show && console.info('Docs updated, now showing posts')
                !argv.show && console.info('Docs updated, now hiding posts')
              })
              .catch(error => { console.warn('Error updating docs', error) })
            })
        }

    这(粗略地)给了我两个任务:

    node app apostrophe-blog:insert-blog-articles --create=100 --> 为我创建 100 篇博客文章

    node app apostrophe-blog:show-hide-articles --show --> 将这些文章的published 标志设置为真或假,具体取决于参数

    【讨论】:

    • 感谢分享代码。如果你使用 bluebird promises,你可以让这段代码在完成后正确退出:
    猜你喜欢
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多