Sailsjs.在Sails-Mongo(MongoDB)上创建(和管理)索引的最佳方法 [英] Sailsjs. Best way to create (and manage) indexes on sails-mongo (mongodb)

查看:138
本文介绍了Sailsjs.在Sails-Mongo(MongoDB)上创建(和管理)索引的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sailsjs 0.12.它也支持models上的index属性 我正在使用npm软件包 Sails-hooks-mongoat 创建反向索引,依此类推. /p>

这不是理想的方法,但是确实有效.现在,他们删除了index属性,并且mongoat目前不安全,尚待更新才能在Sails.js 1.0上运行.

我想知道最好的方法:

  • 在新部署上创建索引.
  • 迁移(确保?)有关部署更新的索引.

解决方案

由于不允许在生产环境中运行"migrate:alter"(即使您尝试运行),因此一种方法是在引导文件中创建这些索引("config/bootstrap.js').

想象一下,您有一个这样的用户模型:

var User = {
  attributes: {
    email     : { type: 'string', unique: true, required: true },
    username  : { type: 'string', unique: true, required: true },
    pin: { type: 'string'}
  }
};

module.exports = User;

然后,您可以在引导文件中手动创建缺少的索引,如下所示:

module.exports.bootstrap = async function(done) {
  console.log("Loading bootstrap...")

  if (process.env.NODE_ENV === 'test') {

  }

  if (process.env.NODE_ENV === 'production') {
      console.log("CREATING DATABASE INDEX BY HAND")

      // USER MODEL
      var db = User.getDatastore().manager;
      await db.collection(User.tableName).createIndex( { email: 1 }, {unique: true} );
      await db.collection(User.tableName).createIndex( { username: 1 }, {unique: true} );
      // PANDA MODEL
      // db = Panda.getDatastore().manager;
      // await db.collection(Panda.tableName).createIndex( { name: 1 }, {unique: true} );
  }

  // await initializeDatabase() // custom DB initialization...

  return done();
};

索引仅创建一次,后续运行将不会重新创建那些索引. ensureIndex 是createIndex函数的别名,它具有不推荐使用.

参考:

水线管理器参考

MongoDB创建索引参考

I was using sailsjs 0.12. It supported index attributes on models, also i was using npm package Sails-hooks-mongoat to create inverse indexes and so.

It wasn't ideal, but it worked. Right now they dropped the index attribute and mongoat is currently unsafe and pending updates to work on Sails.js 1.0.

I would like to know the best approach to:

  • Create Indexes on new deployments.
  • Migrate (ensure?) indexes on deployment updates.

解决方案

Since you are not allowed to run 'migrate: alter' in production (even if you try), one option is to create those index in bootstrap file ('config/bootstrap.js').

Imagine you have an User model like this:

var User = {
  attributes: {
    email     : { type: 'string', unique: true, required: true },
    username  : { type: 'string', unique: true, required: true },
    pin: { type: 'string'}
  }
};

module.exports = User;

Then you can manually create the missing indexes like this in bootstrap file:

module.exports.bootstrap = async function(done) {
  console.log("Loading bootstrap...")

  if (process.env.NODE_ENV === 'test') {

  }

  if (process.env.NODE_ENV === 'production') {
      console.log("CREATING DATABASE INDEX BY HAND")

      // USER MODEL
      var db = User.getDatastore().manager;
      await db.collection(User.tableName).createIndex( { email: 1 }, {unique: true} );
      await db.collection(User.tableName).createIndex( { username: 1 }, {unique: true} );
      // PANDA MODEL
      // db = Panda.getDatastore().manager;
      // await db.collection(Panda.tableName).createIndex( { name: 1 }, {unique: true} );
  }

  // await initializeDatabase() // custom DB initialization...

  return done();
};

The index are created only once, subsequent runs will not recreate those indexes. The ensureIndex was an alias to createIndex function and it has been deprecated.

References:

Waterline manager reference

MongoDB create index reference

这篇关于Sailsjs.在Sails-Mongo(MongoDB)上创建(和管理)索引的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆