forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
255 lines (250 loc) Β· 22.6 KB
/
Copy pathindex.html
File metadata and controls
255 lines (250 loc) Β· 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!doctype html>
<html class="default no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Replicache</title>
<meta name="description" content="Documentation for Replicache">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/main.css">
<script async src="assets/js/search.js" id="search-script"></script>
</head>
<body>
<header>
<div class="tsd-page-toolbar">
<div class="container">
<div class="table-wrap">
<div class="table-cell" id="tsd-search" data-index="assets/js/search.json" data-base=".">
<div class="field">
<label for="tsd-search-field" class="tsd-widget search no-caption">Search</label>
<input id="tsd-search-field" type="text" />
</div>
<ul class="results">
<li class="state loading">Preparing search index...</li>
<li class="state failure">The search index is not available</li>
</ul>
<a href="index.html" class="title">Replicache</a>
</div>
<div class="table-cell" id="tsd-widgets">
<div id="tsd-filter">
<a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a>
<div class="tsd-filter-group">
<div class="tsd-select" id="tsd-filter-visibility">
<span class="tsd-select-label">All</span>
<ul class="tsd-select-list">
<li data-value="public">Public</li>
<li data-value="protected">Public/Protected</li>
<li data-value="private" class="selected">All</li>
</ul>
</div>
<input type="checkbox" id="tsd-filter-inherited" checked />
<label class="tsd-widget" for="tsd-filter-inherited">Inherited</label>
</div>
</div>
<a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a>
</div>
</div>
</div>
</div>
<div class="tsd-page-title">
<div class="container">
<h1>Project Replicache</h1>
</div>
</div>
</header>
<div class="container container-main">
<div class="row">
<div class="col-8 col-content">
<div class="tsd-panel tsd-typography">
<a href="#replicache-js-sdk" id="replicache-js-sdk" style="color: inherit; text-decoration: none;">
<h1>Replicache JS SDK</h1>
</a>
<a href="#offline-first-for-every-application" id="offline-first-for-every-application" style="color: inherit; text-decoration: none;">
<h3>Offline-First for Every Application</h3>
</a>
<p><img src="https://github.com/rocicorp/replicache-sdk-js/workflows/Node.js%20CI/badge.svg" alt="Node.js CI"></p>
<a href="#π-quickstart" id="π-quickstart" style="color: inherit; text-decoration: none;">
<h2>π Quickstart</h2>
</a>
<p>This tutorial walks through creating the UI for a basic <a href="https://replicache.dev/">Replicache</a>-powered todo list.</p>
<p>It relies on the <a href="https://github.com/rocicorp/replicache-sample-todo">replicache-sample-todo</a> backend. To learn how to setup you own Replicache backend, see <a href="https://github.com/rocicorp/replicache/blob/master/SERVER_SETUP.md">Server Side Setup</a>.</p>
<p>If you have any problems or questions, please <a href="https://slack.replicache.dev/">join us on Slack</a>. We'd be happy to help.</p>
<p>You can also skip to the end and <a href="https://github.com/rocicorp/replicache-sdk-js/tree/master/sample/cal">check out the full working version of this sample</a>.</p>
<p><strong>Note:</strong> This document assumes you already know what Replicache is, why you might need it, and broadly how it works. If that's not true check out the <a href="https://github.com/rocicorp/replicache/blob/master/design.md">design document</a> for a detailed deep-dive.</p>
<a href="#πβοΈ-install" id="πβοΈ-install" style="color: inherit; text-decoration: none;">
<h2>πββοΈ Install</h2>
</a>
<pre><code><span style="color: #001080">npm</span><span style="color: #000000"> </span><span style="color: #001080">install</span><span style="color: #000000"> </span><span style="color: #001080">replicache</span>
</code></pre>
<a href="#π΄πΏβοΈ-instantiate" id="π΄πΏβοΈ-instantiate" style="color: inherit; text-decoration: none;">
<h2>π΄πΏββοΈ Instantiate</h2>
</a>
<pre><code class="language-html"><span style="color: #800000"><script</span><span style="color: #000000FF"> </span><span style="color: #FF0000">type</span><span style="color: #000000FF">=</span><span style="color: #0000FF">"module"</span><span style="color: #800000">></span>
<span style="color: #000000FF"> </span><span style="color: #AF00DB">import</span><span style="color: #000000FF"> </span><span style="color: #001080">Replicache</span><span style="color: #000000FF"> </span><span style="color: #AF00DB">from</span><span style="color: #000000FF"> </span><span style="color: #A31515">'replicache'</span><span style="color: #000000FF">; </span><span style="color: #008000">// Replace with a real module path as needed...</span>
<span style="color: #000000FF"> </span><span style="color: #0000FF">var</span><span style="color: #000000FF"> </span><span style="color: #001080">rep</span><span style="color: #000000FF"> </span><span style="color: #000000">=</span><span style="color: #000000FF"> </span><span style="color: #0000FF">new</span><span style="color: #000000FF"> </span><span style="color: #267F99">Replicache</span><span style="color: #000000FF">({</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// URL of the diff server to use. The diff server periodically fetches</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// the "client view" from your service and forwards any delta to the</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// client. You can use our hosted diff server (as here) or a local diff</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// server, which is useful during development. See</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// https://github.com/rocicorp/replicache#server-side for more</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// information on setting up your client view.</span>
<span style="color: #000000FF"> </span><span style="color: #001080">diffServerURL:</span><span style="color: #000000FF"> </span><span style="color: #A31515">'https://serve.replicache.dev/pull'</span><span style="color: #000000FF">,</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// Auth token for the diff server, if any.</span>
<span style="color: #000000FF"> </span><span style="color: #001080">diffServerAuth:</span><span style="color: #000000FF"> </span><span style="color: #A31515">'1'</span><span style="color: #000000FF">,</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// URL of your service's Replicache batch endpoint. Replicache</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// will send batches of mutations here for application.</span>
<span style="color: #000000FF"> </span><span style="color: #001080">batchURL:</span><span style="color: #000000FF"> </span><span style="color: #A31515">'https://replicache-sample-todo.now.sh/serve/replicache-batch'</span><span style="color: #000000FF">,</span>
<span style="color: #000000FF"> </span><span style="color: #008000">// Auth token for your client view and batch endpoints, if any.</span>
<span style="color: #000000FF"> </span><span style="color: #001080">dataLayerAuth:</span><span style="color: #000000FF"> </span><span style="color: #A31515">'2'</span><span style="color: #000000FF">,</span>
<span style="color: #000000FF"> });</span>
<span style="color: #800000"></script></span>
</code></pre>
<a href="#π-render-ui" id="π-render-ui" style="color: inherit; text-decoration: none;">
<h2>π Render UI</h2>
</a>
<p>Use <code>subscribe()</code> to open standing queries. Replicache calls <code>onData</code> whenever the result of the query changes, either because of local changes or sync.</p>
<pre><code class="language-js"><span style="color: #001080">rep</span><span style="color: #000000">.</span><span style="color: #795E26">subscribe</span><span style="color: #000000">(</span>
<span style="color: #000000"> </span><span style="color: #0000FF">async</span><span style="color: #000000"> </span><span style="color: #001080">tx</span><span style="color: #000000"> </span><span style="color: #0000FF">=></span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">return</span><span style="color: #000000"> </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #795E26">toArray</span><span style="color: #000000">(</span><span style="color: #001080">tx</span><span style="color: #000000">.</span><span style="color: #795E26">scan</span><span style="color: #000000">({</span><span style="color: #001080">prefix:</span><span style="color: #000000"> </span><span style="color: #A31515">'/todo/'</span><span style="color: #000000">}));</span>
<span style="color: #000000"> },</span>
<span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #795E26">onData</span><span style="color: #001080">:</span><span style="color: #000000"> </span><span style="color: #001080">result</span><span style="color: #000000"> </span><span style="color: #0000FF">=></span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #008000">// Using lit-html, but the principle is the same in any UI framework.</span>
<span style="color: #000000"> </span><span style="color: #008000">// See https://github.com/rocicorp/replicache-sdk-js/tree/master/sample/cal</span>
<span style="color: #000000"> </span><span style="color: #008000">// for an example using React.</span>
<span style="color: #000000"> </span><span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #795E26">toggle</span><span style="color: #000000"> = </span><span style="color: #001080">complete</span><span style="color: #000000"> </span><span style="color: #0000FF">=></span>
<span style="color: #000000"> </span><span style="color: #795E26">html</span><span style="color: #A31515">`<td><input type="checkbox" .checked=</span><span style="color: #0000FF">${</span><span style="color: #001080">complete</span><span style="color: #0000FF">}</span><span style="color: #A31515"> /></td>`</span><span style="color: #000000">;</span>
<span style="color: #000000"> </span><span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #795E26">title</span><span style="color: #000000"> = </span><span style="color: #001080">text</span><span style="color: #000000"> </span><span style="color: #0000FF">=></span><span style="color: #000000"> </span><span style="color: #795E26">html</span><span style="color: #A31515">`<td></span><span style="color: #0000FF">${</span><span style="color: #001080">text</span><span style="color: #0000FF">}</span><span style="color: #A31515"></td>`</span><span style="color: #000000">;</span>
<span style="color: #000000"> </span><span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #795E26">row</span><span style="color: #000000"> = </span><span style="color: #001080">todo</span><span style="color: #000000"> </span><span style="color: #0000FF">=></span>
<span style="color: #000000"> </span><span style="color: #795E26">html</span><span style="color: #A31515">`<tr></span>
<span style="color: #A31515"> </span><span style="color: #0000FF">${</span><span style="color: #795E26">toggle</span><span style="color: #000000FF">(</span><span style="color: #001080">todo</span><span style="color: #000000FF">.</span><span style="color: #001080">complete</span><span style="color: #000000FF">)</span><span style="color: #0000FF">}${</span><span style="color: #795E26">title</span><span style="color: #000000FF">(</span><span style="color: #001080">todo</span><span style="color: #000000FF">.</span><span style="color: #001080">text</span><span style="color: #000000FF">)</span><span style="color: #0000FF">}</span>
<span style="color: #A31515"> </tr>`</span><span style="color: #000000">;</span>
<span style="color: #000000"> </span><span style="color: #795E26">render</span><span style="color: #000000">(</span>
<span style="color: #000000"> </span><span style="color: #795E26">html</span><span style="color: #A31515">`<table></span>
<span style="color: #A31515"> </span><span style="color: #0000FF">${</span><span style="color: #001080">result</span><span style="color: #000000FF">.</span><span style="color: #795E26">map</span><span style="color: #000000FF">(</span><span style="color: #001080">row</span><span style="color: #000000FF">)</span><span style="color: #0000FF">}</span>
<span style="color: #A31515"> </table>`</span><span style="color: #000000">,</span>
<span style="color: #000000"> </span><span style="color: #001080">document</span><span style="color: #000000">.</span><span style="color: #001080">body</span><span style="color: #000000">,</span>
<span style="color: #000000"> );</span>
<span style="color: #000000"> },</span>
<span style="color: #000000"> },</span>
<span style="color: #000000">);</span>
</code></pre>
<a href="#π-mutate-data" id="π-mutate-data" style="color: inherit; text-decoration: none;">
<h2>π Mutate Data</h2>
</a>
<p>Register client-side <em>mutators</em> using <code>register()</code>.</p>
<p>Mutators run completely locally, without waiting on the server β online, offline, whatever! A record of the mutation is queued and sent to your service's batch endpoint when possible.</p>
<p>Replicache also invokes mutators itself, during sync, to replay unacknowledged changes on top of newly received server state.</p>
<pre><code class="language-js"><span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">updateTodo</span><span style="color: #000000"> = </span><span style="color: #001080">rep</span><span style="color: #000000">.</span><span style="color: #795E26">register</span><span style="color: #000000">(</span><span style="color: #A31515">'updateTodo'</span><span style="color: #000000">, </span><span style="color: #0000FF">async</span><span style="color: #000000"> (</span><span style="color: #001080">tx</span><span style="color: #000000">, {</span><span style="color: #001080">id</span><span style="color: #000000">, </span><span style="color: #001080">complete</span><span style="color: #000000">}) </span><span style="color: #0000FF">=></span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">key</span><span style="color: #000000"> = </span><span style="color: #A31515">`/todo/</span><span style="color: #0000FF">${</span><span style="color: #001080">id</span><span style="color: #0000FF">}</span><span style="color: #A31515">`</span><span style="color: #000000">;</span>
<span style="color: #000000"> </span><span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">todo</span><span style="color: #000000"> = </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">tx</span><span style="color: #000000">.</span><span style="color: #795E26">get</span><span style="color: #000000">(</span><span style="color: #001080">key</span><span style="color: #000000">);</span>
<span style="color: #000000"> </span><span style="color: #001080">todo</span><span style="color: #000000">.</span><span style="color: #001080">complete</span><span style="color: #000000"> = </span><span style="color: #001080">complete</span><span style="color: #000000">;</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">tx</span><span style="color: #000000">.</span><span style="color: #795E26">put</span><span style="color: #000000">(</span><span style="color: #001080">key</span><span style="color: #000000">, </span><span style="color: #001080">todo</span><span style="color: #000000">);</span>
<span style="color: #000000">});</span>
<span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #795E26">handleCheckbox</span><span style="color: #000000"> = </span><span style="color: #0000FF">async</span><span style="color: #000000"> (</span><span style="color: #001080">id</span><span style="color: #000000">, </span><span style="color: #001080">e</span><span style="color: #000000">) </span><span style="color: #0000FF">=></span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #795E26">updateTodo</span><span style="color: #000000">({</span><span style="color: #001080">id</span><span style="color: #000000">, </span><span style="color: #001080">complete:</span><span style="color: #000000"> </span><span style="color: #001080">e</span><span style="color: #000000">.</span><span style="color: #001080">srcElement</span><span style="color: #000000">.</span><span style="color: #001080">checked</span><span style="color: #000000">});</span>
<span style="color: #000000">};</span>
</code></pre>
<a href="#π«-tips" id="π«-tips" style="color: inherit; text-decoration: none;">
<h2>π« Tips</h2>
</a>
<ul>
<li>We recommend <a href="https://stackoverflow.com/questions/5327955/how-to-make-google-chrome-javascript-console-persistent">enabling console persistence</a> while developing replicache-enabled apps to make debugging easier.</li>
<li>Remember that data changes can happen "underneath" you and cause <code>subscribe()</code> to re-fire at any time. These changes can come from the server or from a different tab. If your UI is not reactive (driven solely by the data model) you need to take extra steps to ensure the UI is in sync with the data.</li>
</ul>
<a href="#π-next-steps" id="π-next-steps" style="color: inherit; text-decoration: none;">
<h2>π Next Steps</h2>
</a>
<p>That's it! You've built a fully-functioning offline-first todo app against our sample backend. What will you do next?</p>
<ul>
<li><a href="https://github.com/rocicorp/replicache-sdk-js/tree/master/sample/lit-todo">Check out the full version of this sample</a></li>
<li><a href="https://github.com/rocicorp/replicache#server-side">Learn how to add Replicache support to your own backend service</a></li>
<li><a href="https://github.com/rocicorp/replicache-sdk-js/tree/master/sample/cal">Check out the richer React/Babel/GCal sample</a></li>
<li><a href="https://replicache-sdk-js.now.sh/">Browse the full JS documentation</a></li>
</ul>
</div>
</div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<nav class="tsd-navigation primary">
<ul>
<li class=" ">
<a href="modules.html">Exports</a>
</li>
</ul>
</nav>
<nav class="tsd-navigation secondary menu-sticky">
<ul class="before-current">
<li class=" tsd-kind-class tsd-has-type-parameter">
<a href="classes/scanresult.html" class="tsd-kind-icon">Scan<wbr>Result</a>
</li>
<li class=" tsd-kind-class">
<a href="classes/transactionclosederror.html" class="tsd-kind-icon">Transaction<wbr>Closed<wbr>Error</a>
</li>
<li class=" tsd-kind-class">
<a href="classes/default.html" class="tsd-kind-icon">default</a>
</li>
<li class=" tsd-kind-interface">
<a href="interfaces/readtransaction.html" class="tsd-kind-icon">Read<wbr>Transaction</a>
</li>
<li class=" tsd-kind-interface">
<a href="interfaces/replicacheoptions.html" class="tsd-kind-icon">Replicache<wbr>Options</a>
</li>
<li class=" tsd-kind-interface">
<a href="interfaces/writetransaction.html" class="tsd-kind-icon">Write<wbr>Transaction</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#initinput" class="tsd-kind-icon">Init<wbr>Input</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#jsonobject" class="tsd-kind-icon">JSONObject</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#jsonvalue" class="tsd-kind-icon">JSONValue</a>
</li>
<li class=" tsd-kind-type-alias tsd-has-type-parameter">
<a href="modules.html#keytypeforscanoptions" class="tsd-kind-icon">Key<wbr>Type<wbr>For<wbr>Scan<wbr>Options</a>
</li>
<li class=" tsd-kind-type-alias tsd-has-type-parameter">
<a href="modules.html#maybepromise" class="tsd-kind-icon">Maybe<wbr>Promise</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#scanindexoptions" class="tsd-kind-icon">Scan<wbr>Index<wbr>Options</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#scannoindexoptions" class="tsd-kind-icon">Scan<wbr>NoIndex<wbr>Options</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#scanoptionindexedstartkey" class="tsd-kind-icon">Scan<wbr>Option<wbr>Indexed<wbr>Start<wbr>Key</a>
</li>
<li class=" tsd-kind-type-alias">
<a href="modules.html#scanoptions" class="tsd-kind-icon">Scan<wbr>Options</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<footer class="with-border-bottom">
<div class="container">
<h2>Legend</h2>
<div class="tsd-legend-group">
<ul class="tsd-legend">
<li class="tsd-kind-constructor tsd-parent-kind-class"><span class="tsd-kind-icon">Constructor</span></li>
<li class="tsd-kind-property tsd-parent-kind-class"><span class="tsd-kind-icon">Property</span></li>
<li class="tsd-kind-method tsd-parent-kind-class"><span class="tsd-kind-icon">Method</span></li>
<li class="tsd-kind-accessor tsd-parent-kind-class"><span class="tsd-kind-icon">Accessor</span></li>
</ul>
<ul class="tsd-legend">
<li class="tsd-kind-property tsd-parent-kind-interface"><span class="tsd-kind-icon">Property</span></li>
<li class="tsd-kind-method tsd-parent-kind-interface"><span class="tsd-kind-icon">Method</span></li>
</ul>
</div>
</div>
</footer>
<div class="container tsd-generator">
<p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p>
</div>
<div class="overlay"></div>
<script src="assets/js/main.js"></script>
</body>
</html>