Ottoman.js 自动创建集合和自适应索引

问题描述 投票:0回答:1

在 couchbase 中,当您使用 ottoman.js 时,您必须手动创建一个集合和索引,然后才能开始在数据库中查询您创建的新模型。有没有办法自动执行此操作?

为了进一步解释,这是我在 Nodejs 代码中创建的基本模型。

const { Schema, model } = require("ottoman");

const UserSchema = new Schema({
    username: {
        type: String,
        required: true,
        unique: true
    }
},
    {
        timestamps: true
    }
);

const User = model('User', UserSchema);

module.exports = User;

现在,当我们尝试插入时,我们会收到集合不存在的错误。如果我们在仪表板中手动创建集合,我们可以像这样开始插入:

const newUser = new User({ username:'HelloWorld' });
let result = await newUser.save();

为了查询数据,我们需要创建索引。假设我们只需要所有集合的自适应索引。当我们尝试查询时,我们收到索引错误。因此,我们再次需要手动运行查询来创建自适应索引,然后我们可以执行如下基本查找:

let users = await User.find({},{});

所以我的问题是,有没有一种方法可以在模式定义中指定 ottoman 应自动为您创建集合,并进一步指定它应创建哪种类型的索引?

我一直在使用 mongoose 和 mongodb,发现这非常相似。另一方面,使用 mongo,会自动创建集合,一旦定义了 mongoose 模式,您就可以开始查询。

node.js couchbase couchbase-ottoman
1个回答
0
投票

使用配置 API 创建存储桶、范围、集合、索引等。API 是异步的 - 它们在创建完成之前返回。如果您希望在创建后立即使用它们,则可能需要在使用它们之前进行测试/等待循环。

https://docs.couchbase.com/nodejs-sdk/current/howtos/provisioning-cluster-resources.html

© www.soinside.com 2019 - 2024. All rights reserved.