You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a body field that contains a huge amount of text that slows down pouch very quickly. So we've moved that into it's own simple IndexDB.
But we also really love using RxDB syntax for subscribe as well as exec (both are used extensively). And since we previously had all that field it would be really nice to be able to not have to rewrite a bunch of views to add in custom logic to get that. Especially in this case since we both subscribe in certain places, and use promises in others.
For now I've come up with this extraordinarily hacky solution, that fails on the .exec() case, but at least lets us continue developing:
constresolvedBodies={}constdb=newIndexDB()// methods defined on collectionexportconstmethods={// hacky way to have async resolving of bodies for nowgetbody(){if(this.bodyTEMP){;(async()=>{constres=awaitdb.get(this.id)if(resolvedBodies[this.id]!==res.body){resolvedBodies[this.id]=res.body// hacky, triggers updatethis.bodyTEMP=`${Math.random()}`this.save()}})()returnresolvedBodies[this.id]||''}return''},}exportconstpreInsert=doc=>{if(typeofdoc.body!=='undefined'){db.put({id: doc.id,body: doc.body})doc.bodyTEMP=`${Math.random()}`deletedoc.body}}
I know, it's horrible. Anyway, I brought it up in chat but figure issues are best left for the issue tracker.
To me the simplest solution would be to support async methods:
constheroes=awaitmyDatabase.collection({name: 'heroes',schema: mySchema,customAsyncMethods: {description: function(){returnnewPromise.resolve('hello mate')}}});awaitheroes.insert({name: 'Skeletor'});constdoc=awaitheroes.findOne().exec();console.log(doc.description);// logs out "hello mate"// customAsyncMethods are resolved onto docs before returning
But I believe you answer was that async postCreate is not possible, due to newDocument().
The text was updated successfully, but these errors were encountered:
I still think this is an edge-case. You should generate the description where you need it, not at document-creation.
I now added option-parameters and a new plugin-hook postCreateRxDocument.
With both of them you can do the following solution:
// create a rxdb-plugin that uses the hook and optionsconstmyPlugin={rxdb: true,hooks: {postCreateRxDocument: async(doc)=>{// get the options from the collection-optionsconstasyncMethods=doc.collection.options.customAsyncMethods;if(asyncMethods){// resolve all async-methodsawaitPromise.all(Object.keys(asyncMethods).map(async(key)=>{constfun=asyncMethods[key];constvalue=awaitfun();doc.key=value;}));}}}};// add the plugin to rxdbRxDB.plugin(myPlugin);// now you can create collections with the optionsmyDatabase.collection({name: 'foobar',schema: mySchema,options: {customAsyncMethods: {description: ()=>newPromise.resolve('hello mate')}}});// and later access description of documentmyDocument.description;// 'hello mate'
This is perfect, and I really appreciate the help here. In a perfect world pouch would be a bit better with large docs, but this really helps until then!
We have a body field that contains a huge amount of text that slows down pouch very quickly. So we've moved that into it's own simple IndexDB.
But we also really love using RxDB syntax for subscribe as well as exec (both are used extensively). And since we previously had all that field it would be really nice to be able to not have to rewrite a bunch of views to add in custom logic to get that. Especially in this case since we both subscribe in certain places, and use promises in others.
For now I've come up with this extraordinarily hacky solution, that fails on the
.exec()
case, but at least lets us continue developing:I know, it's horrible. Anyway, I brought it up in chat but figure issues are best left for the issue tracker.
To me the simplest solution would be to support async methods:
But I believe you answer was that async postCreate is not possible, due to
newDocument()
.The text was updated successfully, but these errors were encountered: