In couchbase, when you use ottoman.js you have to manually create a collection and an index before you can start querying the database for the new model you create. Is there a way to do this automatically?
To further explain, here is a basic model I create in my nodejs code.
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;
Now when we try to insert we will get errors that the collection does not exist. If we create the collection manually in the dashboard, we can start inserting like this:
const newUser = new User({ username:'HelloWorld' });
let result = await newUser.save();
For querying data, we need to create an index. Lets assume we just need adaptive indexes for all collections. When we try and query, we get an index error. So then again we need to manually run a query to create an adaptive index and then we can do a basic find like this:
let users = await User.find({},{});
So my question is, is there a way you can pherhaps specify in your schema definition that ottoman should automatically create the collection for you, and furthermore specify what kind of index it should create?
I have been using mongoose with mongodb and have found this to be very similar. With mongo on the other hand, collections are automatically created and you can start querying once you defined your mongoose schemas.
Use the provisioning APIs to create buckets, scopes, collections, indexes etc. The APIs are asynchronous - they return before creation completes. If you wish to use them immediately after creation, you may have to make a test/wait loop before you use them.
https://docs.couchbase.com/nodejs-sdk/current/howtos/provisioning-cluster-resources.html