Skip to content

Commit

Permalink
Various documentation fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Jul 2, 2021
1 parent 470cb2e commit 0c1525f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doc/docs/guide-database-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async (_, res) => {
await t.none('DROP SEQUENCE IF EXISTS version');
// Stores chat messages
await t.none(`CREATE TABLE message (
id VARCHAR(20) PRIMARY KEY NOT NULL,
id VARCHAR(21) PRIMARY KEY NOT NULL,
sender VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
ord BIGINT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion doc/docs/guide-install-replicache.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ slug: /guide/install
Nothing to it...

```bash
npm install replicache replicache-react
npm install replicache replicache-react nanoid
```

... well, _almost_ nothing.
Expand Down
2 changes: 1 addition & 1 deletion doc/docs/guide-local-mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const onSubmit = e => {
const last = messages.length && messages[messages.length - 1][1];
const order = (last?.order ?? 0) + 1;
rep.mutate.createMessage({
id: nanoid(), // From https://www.npmjs.com/package/nanoid
id: nanoid(),
from: usernameRef.current.value,
content: contentRef.current.value,
order,
Expand Down
41 changes: 22 additions & 19 deletions doc/docs/guide-remote-mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,9 @@ export default async (req, res) => {
const t0 = Date.now();
try {
await db.tx(async t => {
const {nextval: version} = await db.one("SELECT nextval('version')");
let lastMutationID = parseInt(
(
await db.oneOrNone(
'SELECT last_mutation_id FROM replicache_client WHERE id = $1',
push.clientID,
)
)?.last_mutation_id ?? '0',
);
const {nextval: version} = await t.one("SELECT nextval('version')");
let lastMutationID = await getLastMutationID(t, push.clientID);

if (!lastMutationID) {
await db.none(
'INSERT INTO replicache_client (id, last_mutation_id) VALUES ($1, $2)',
[push.clientID, lastMutationID],
);
}
console.log('version', version, 'lastMutationID:', lastMutationID);

for (const mutation of push.mutations) {
Expand All @@ -53,7 +40,7 @@ export default async (req, res) => {

switch (mutation.name) {
case 'createMessage':
await createMessage(db, mutation.args, version);
await createMessage(t, mutation.args, version);
break;
default:
throw new Error(`Unknown mutation: ${mutation.name}`);
Expand All @@ -71,7 +58,7 @@ export default async (req, res) => {
'last_mutation_id to',
lastMutationID,
);
await db.none(
await t.none(
'UPDATE replicache_client SET last_mutation_id = $2 WHERE id = $1',
[push.clientID, lastMutationID],
);
Expand All @@ -85,8 +72,24 @@ export default async (req, res) => {
}
};

async function createMessage(db, {id, from, content, order}, version) {
await db.none(
async function getLastMutationID(t, clientID) {
const clientRow = await t.oneOrNone(
'SELECT last_mutation_id FROM replicache_client WHERE id = $1', clientID,
);
if (clientRow) {
return parseInt(clientRow.last_mutation_id);
}

console.log('Creating new client', clientID);
await t.none(
'INSERT INTO replicache_client (id, last_mutation_id) VALUES ($1, 0)',
clientID,
);
return 0;
}

async function createMessage(t, {id, from, content, order}, version) {
await t.none(
`INSERT INTO message (
id, sender, content, ord, version) values
($1, $2, $3, $4, $5)`,
Expand Down
1 change: 1 addition & 0 deletions doc/docs/guide-render-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Let's use a subscription to implement our chat UI. Replace `index.js` with this:
import React, {useEffect, useRef, useState} from 'react';
import {Replicache} from 'replicache';
import {useSubscribe} from 'replicache-react';
import {nanoid} from 'nanoid';

export default function Home() {
const [rep, setRep] = useState(null);
Expand Down

0 comments on commit 0c1525f

Please sign in to comment.