简体   繁体   English

向PouchDB文档添加其他附件

[英]Adding additional attachment to PouchDB document

I tried to add additional attachment to my document in PouchDB in my electron application. 我试图在电子应用程序中为PouchDB中的文档添加其他附件。 However I can only add last attachment and the old one is overwritten. 但是,我只能添加最后一个附件,而旧的附件将被覆盖。

The following data is not amended in a way which add new file: 以下数据未通过添加新文件的方式进行修改:

_attachments":{"someFile.jpg":{"content_type":"image/jpeg","revpos":5,"length":38718,"digest":"md5-X+MOUwdHmNeORSl6xdtZUg=="}

Should I read document first and recreate it adding additional file using multiple attachments with the following method: 我应该首先阅读文档并使用以下方法使用多个附件添加其他文件来重新创建它:

db.put({
  _id: 'mydoc',
  _attachments: {
    'myattachment1.txt': {
      content_type: 'text/plain',
      data: blob1
    },
    'myattachment2.txt': {
      content_type: 'text/plain',
      data: blob2
    },
    'myattachment3.txt': {
      content_type: 'text/plain',
      data: blob3
    },
    // etc.
  }
}); 

?

Below you can see part of the code i try to run to check if i can add two attachments to one document (actually i try to use the same file twice to simplify test): 在下面,您可以看到我尝试运行的部分代码,以检查是否可以将两个附件添加到一个文档中(实际上,我尝试两次使用同一文件来简化测试):

pdb.putAttachment(id, name, rev, file, type).then(function (result) {
                                                                              console.log("att saved:");
                                                                              console.log(result);
                                                                            }).catch(function (err) {
                                                                              console.log(err);
                                                                            });

var newFileName = "new" + name;
pdb.putAttachment(id, newFileName, rev, file, type).then(function (result) {
                                                                                  console.log("att saved 2:");
                                                                                  console.log(result);
                                                                                }).catch(function (err) {
                                                                                  console.log(err);
                                                                                });

The outcome is: 结果是:

Object {ok: true, id: "1489351796004", rev: "28-a4c41eff6fbdde8a722a920c9d5a1390"}
id
:
"1489351796004"
ok
:
true
rev
:
"28-a4c41eff6fbdde8a722a920c9d5a1390"

CustomPouchError {status: 409, name: "conflict", message: "Document update conflict", error: true, id: "1489351796004"}
error
:
true
id
:
"1489351796004"
message
:
"Document update conflict"
name
:
"conflict"
status
:
409

It looks I don't understand something or I do not know how to use putAttachment properly. 看来我听不懂或不知道如何正确使用putAttachment。

I would also add what how data in sqlite looks like(by-sequence table, json row): 我还将添加sqlite中的数据的外观(按顺序表,json行):

{...,"_attachments":{"testPicture.jpg":{"content_type":"image/jpeg","revpos":34,"length":357677,"digest":"md5-Bjqd6RHsvlCsDkBKe0r7bg=="}}}

The problem here is how to add another attachment to the structure. 这里的问题是如何向该结构添加另一个附件。 Somehow I cannot achive that via putAttachment 我以某种方式无法通过putAttachment实现

put replaces the document. put替换文件。 If you want to add an attachment to an existing document without overwriting its contents you should use putAttachment . 如果要向现有文档添加附件而不覆盖其内容,则应使用putAttachment

Your question and especially the code are quite hard to read, so the error was not so easy to spot: You didn't wait for the promise to be resolved. 您的问题,尤其是代码很难阅读,因此错误并不是那么容易发现:您没有等待承诺被解决。 When you update a document with revision 1, you have to wait for the results, read the revision from there, and only then write the second attachment. 当您使用修订版1更新文档时,您必须等待结果,从那里读取修订版,然后再编写第二个附件。 This would be my (untested) take on your code: 这是我(未经测试)对您的代码的看法:

pdb.putAttachment(id, name, rev, file, type)
.then(function (result) {
  // Use the new revision here:
  return putAttachment(id, newFileName, result.rev, file, type);
}).then(function (result) {
  console.log(result);
}).catch(function (err) {
  console.log(err);
});

Adding two attachments at once is possible if you encode them correctly, but you're on your own with it. 如果您正确编码了两个附件,则可以一次添加两个附件,但是您可以自己添加它。 I'd recommend that you shouldn't do that – better use the abstractions that PouchDB provides. 我建议您不要这样做–更好地使用PouchDB提供的抽象。

Also don't analyze the underlying data structures too much, because depending on the storage adapter used the data storage might differ drastically. 也不要对基础数据结构进行过多分析,因为根据所使用的存储适配器,数据存储可能会大不相同。 It's quite interesting how different adapters store their data, but never rely on anything you find out – data formats might change. 有趣的是,不同的适配器如何存储它们的数据,但绝不依赖于您发现的任何内容-数据格式可能会发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM