Closed bwgjoseph closed 3 years ago
hi @bwgjoseph
try to update the plugins to return the modified document:
const pluginLog = (pSchema: any) => {
pSchema.pre('save', function (doc: any) {
doc.operational = false;
return doc;
});
};
const pluginLog2 = (pSchema: any) => {
pSchema.pre('save', function (doc: any) {
doc.plugin = 'registered from plugin 2!'
return doc;
});
};
Hi,
I've tried but still not working
@gsi-alejandro
In case you missed this, it's not working
Hi @bwgjoseph I spoke to @gsi-alejandro he said there was no issue but he is going to look at your code repo and see what is missing. @gsi-alejandro any updates ?
hi @bwgjoseph
I include this test in the Ottoman test suite:
test('global plugin with hook to modify document', async () => {
const pluginLog = (pSchema: any) => {
pSchema.pre('save', function (doc: any) {
doc.operational = false;
return doc;
});
};
const pluginLog2 = (pSchema: any) => {
pSchema.pre('save', function (doc: any) {
doc.plugin = 'registered from plugin 2!';
return doc;
});
};
registerGlobalPlugin(pluginLog);
registerGlobalPlugin(pluginLog2);
const schema = new Schema({
name: String,
operational: Boolean,
});
const schemaData = {
name: 'hello',
operational: true,
};
const GlobalPlugins = model('globalPlugins', schema);
await startInTest(getDefaultInstance());
const doc = await GlobalPlugins.create(schemaData);
console.log(doc);
expect(doc.operational).toBe(false); // plugin
expect(doc.plugin).toBe('registered from plugin 2!'); // plugin2
});
the test succeeds.
I think there isn't a problem with the plugin behavior, seems you are facing a timeout connection issue.
I deep into your code and change the initOttoman to:
const initOttoman = async (consistency: SearchConsistency = SearchConsistency.NONE) => {
ottoman = new Ottoman({ consistency });
await ottoman.connect({
connectionString: 'couchbase://localhost',
bucketName: 'travel-sample',
username: 'Administrator',
password: 'password'
});
model('myschema', schema, { idKey: '_id' });
await ottoman.start();
}
And it works
The change was register the model
before execute the start
function.
Thanks @gsi-alejandro for spotting the mistake.
I made the changes, and had to run 4 times before I can finally get it running.
Hi,
While testing for
registerGlobalPlugin
api, I couldn't get it to run. I remember this test was running fine previously.See https://github.com/bwgjoseph/mongoose-vs-ottoman/blob/main/test/ts/registerGlobalPlugin.test.ts
I kept getting this error, and no matter how many times I run it, it's still the same