UNPKG

cloudant-upsert

Version:

![](https://img.shields.io/badge/status-stable-green.svg) ![](https://img.shields.io/badge/license-MIT-blue.svg)

143 lines 6.69 kB
{ "name": "cdimascio/cloudant-upsert", "version": "0.1.4", "libraries": { "xv": "^1.1.25" }, "title": "", "branch": "", "style": { "name": "Default", "componentSet": { "nav": "nav/BasicNav", "header": "header/BannerHeader", "article": "article/BasicArticle", "footer": "footer/BasicFooter" }, "fontFamily": "-apple-system, BlinkMacSystemFont, sans-serif", "fontWeight": 400, "bold": 600, "lineHeight": 1.5, "typeScale": [ 72, 48, 24, 20, 16, 14, 12 ], "monospace": "Menlo, monospace", "heading": { "fontFamily": null, "fontStyle": null, "fontWeight": 600, "lineHeight": 1.25, "textTransform": null, "letterSpacing": null }, "h0": {}, "h1": {}, "h2": {}, "h3": {}, "h4": {}, "h5": {}, "h6": {}, "alternativeText": {}, "space": [ 0, 8, 16, 32, 48, 64, 96 ], "layout": { "maxWidth": 1024, "centered": false }, "colors": { "text": "#111", "background": "#fff", "primary": "#08e", "secondary": "#059", "highlight": "#e08", "border": "#ddd", "muted": "#eee" }, "border": { "width": 1, "radius": 2 }, "link": {}, "button": { "hover": { "boxShadow": "inset 0 0 0 999px rgba(0, 0, 0, .125)" } }, "input": {}, "body": { "margin": 0 }, "breakpoints": { "xs": "@media screen and (max-width:40em)", "sm": "@media screen and (min-width:40em)", "md": "@media screen and (min-width:52em)", "lg": "@media screen and (min-width:64em)" } }, "content": [ { "component": "nav", "links": [ { "href": "https://github.com/cdimascio/cloudant-upsert", "text": "GitHub" }, { "href": "https://npmjs.com/package/cloudant-upsert", "text": "npm" } ] }, { "component": "header", "heading": "cloudant-upsert", "subhead": "Adds an upsert to nodejs cloudant", "children": [ { "component": "ui/TweetButton", "text": "cloudant-upsert: Adds an upsert to nodejs cloudant", "url": "" }, { "component": "ui/GithubButton", "user": "cdimascio", "repo": "cloudant-upsert" } ], "text": "v0.9.4" }, { "component": "article", "metadata": { "source": "github.readme" }, "html": "\n<p><img src=\"https://img.shields.io/badge/status-stable-green.svg\"> <img src=\"https://img.shields.io/badge/license-MIT-blue.svg\"></p>\n<p>A no-dependency module that adds an <code>upsert</code> function to the <a href=\"https://github.com/cloudant/nodejs-cloudant/blob/master/cloudant.js\">nodejs-cloudant</a> module.</p>\n<p>\n<img src=\"https://raw.githubusercontent.com/cdimascio/cloudant-upsert/master/assets/cloudant-upsert.png\">\n</p>\n\n<h2>Install</h2>\n<pre>npm install cloudant-upsert</pre><h2>Usage</h2>\n<p>The following adds the <code>upsert</code> function to a <code>cloudant</code> instance</p>\n<pre><span class=\"hljs-keyword\">var</span> Cloudant = <span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">&apos;@cloudant/cloudant&apos;</span>);\n<span class=\"hljs-keyword\">var</span> cloudant = Cloudant(<span class=\"hljs-string\">&apos;&lt;YOUR_CLOUDANT_URI&gt;&apos;</span>);\n<span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">&apos;cloudant-upsert&apos;</span>)(cloudant);</pre><h2>Examples</h2>\n<h4>Promises</h4>\n<p>In the example below <code>prevdoc</code> contains <code>null</code> or the previous doc</p>\n<pre>cloudant.db.use(<span class=\"hljs-string\">&apos;mydb&apos;</span>).upsert(<span class=\"hljs-string\">&apos;carmine&apos;</span>, prevdoc =&gt; ({\n <span class=\"hljs-string\">&quot;text&quot;</span>: <span class=\"hljs-string\">&quot;Woop Woop! We&apos;re using promises&quot;</span>,\n }))\n .then(<span class=\"hljs-function\"><span class=\"hljs-params\">r</span> =&gt;</span> <span class=\"hljs-built_in\">console</span>.log(<span class=\"hljs-string\">&apos;result&apos;</span>, r))\n .catch(<span class=\"hljs-function\"><span class=\"hljs-params\">e</span> =&gt;</span> <span class=\"hljs-built_in\">console</span>.log(<span class=\"hljs-string\">&apos;error&apos;</span>, e.message));</pre><h4>Callbacks</h4>\n<p>In the example below <code>prevdoc</code> contains <code>null</code> or the previous doc</p>\n<pre>cloudant.db.use(<span class=\"hljs-string\">&apos;mydb&apos;</span>).upsert(<span class=\"hljs-string\">&apos;carmine&apos;</span>, prevdoc =&gt; ({\n <span class=\"hljs-string\">&quot;text&quot;</span>: <span class=\"hljs-string\">&quot;Woop! Woop! We&apos;re using callbacks&quot;</span>\n }), (err, data) =&gt; {\n <span class=\"hljs-keyword\">if</span> (err) <span class=\"hljs-built_in\">console</span>.log(<span class=\"hljs-string\">&apos;error&apos;</span>, err.reason)\n <span class=\"hljs-keyword\">else</span> <span class=\"hljs-built_in\">console</span>.log(<span class=\"hljs-string\">&apos;data&apos;</span>, data)\n })</pre><h2>API</h2>\n<h3>Promise API</h3>\n<h3><code>upsert(id, function(prevdoc)</code>)</h3>\n<table>\n<thead>\n<tr>\n<th>param</th>\n<th>description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>id</code></td>\n<td>The document id to upsert</td>\n</tr>\n<tr>\n<td><code>function(prevdoc): doc</code></td>\n<td>A function that provides the previous document (or null when inserting a new doc) as input. You must return the document to upsert.</td>\n</tr>\n</tbody>\n</table>\n<h3>Callback API</h3>\n<h3><code>upsert(id, function(prevdoc), function(err, res)}</code></h3>\n<table>\n<thead>\n<tr>\n<th>param</th>\n<th>description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>id</code></td>\n<td>The document id to upsert</td>\n</tr>\n<tr>\n<td><code>function(prevdoc): doc</code></td>\n<td>A function that provides the previous document (or null when inserting a new doc) and returns a new document to upsert</td>\n</tr>\n<tr>\n<td><code>function(err, res): void</code></td>\n<td>A Node.js error first callback</td>\n</tr>\n</tbody>\n</table>\n<h2>License</h2>\n<p><a href=\"LICENSE\">MIT</a></p>\n" }, { "component": "footer", "links": [ { "href": "https://github.com/cdimascio/cloudant-upsert", "text": "GitHub" }, { "href": "https://github.com/cdimascio", "text": "cdimascio" } ] } ] }