Skip to content

Commit

Permalink
Remove pgconfig since we only support real pg now
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Feb 25, 2024
1 parent 96fe9fe commit fa90475
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 87 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ The server will correctly send to the requesting client the difference from its

## 1. Setup

#### Install Postgres

You will need a local Postgres database to use this sample.
On MacOS, we recommend [Postgres.app](https://postgresapp.com/).

#### Get your Replicache License Key

```bash
Expand All @@ -45,10 +50,18 @@ $ export VITE_REPLICACHE_LICENSE_KEY="<your license key>"
$ npm install; npm run build;
```

## 2. Start frontend and backend watcher
## 2. Develop

#### Create a new, empty database

```bash
psql -c 'create database todo'
```

### Start frontend and backend watcher

```bash
$ npm run watch --ws
$ DATABASE_URL='postgresql://localhost/todo' npm run watch --ws
```

Provides an example integrating replicache with react in a simple todo application.
Expand Down
21 changes: 17 additions & 4 deletions server/src/pg.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Low-level config and utilities for Postgres.

import pg from 'pg';
import type {Pool, QueryResult} from 'pg';
import {createDatabase} from './schema.js';
import {getDBConfig} from './pgconfig/pgconfig.js';

const pool = getPool();

Expand All @@ -20,8 +20,21 @@ async function getPool() {
async function initPool() {
console.log('creating global pool');

const dbConfig = getDBConfig();
const pool = dbConfig.initPool();
const url = process.env.DATABASE_URL;
if (!url) {
throw new Error('Required env var DATABASE_URL not set');
}

const ssl =
process.env.NODE_ENV === 'production'
? {
rejectUnauthorized: false,
}
: undefined;
const pool = new pg.Pool({
connectionString: url,
ssl,
});

// the pool will emit an error on behalf of any idle clients
// it contains if a backend error or network partition happens
Expand All @@ -39,7 +52,7 @@ async function initPool() {

await withExecutorAndPool(async executor => {
await transactWithExecutor(executor, async executor => {
await createDatabase(executor, dbConfig);
await createDatabase(executor);
});
}, pool);

Expand Down
18 changes: 0 additions & 18 deletions server/src/pgconfig/pgconfig.ts

This file was deleted.

20 changes: 0 additions & 20 deletions server/src/pgconfig/pgmem.ts

This file was deleted.

40 changes: 0 additions & 40 deletions server/src/pgconfig/postgres.ts

This file was deleted.

17 changes: 14 additions & 3 deletions server/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type {PGConfig} from './pgconfig/pgconfig.js';
import type {Executor} from './pg.js';

const schemaVersion = 3;

export async function createDatabase(executor: Executor, dbConfig: PGConfig) {
export async function createDatabase(executor: Executor) {
console.log('creating database');
const actualSchemaVersion = await dbConfig.getSchemaVersion(executor);
const actualSchemaVersion = await getSchemaVersion(executor);
if (schemaVersion !== actualSchemaVersion) {
await createSchema(executor);
}
Expand Down Expand Up @@ -62,3 +61,15 @@ export async function createSchema(executor: Executor) {
lastmodified timestamp(6) not null
)`);
}

async function getSchemaVersion(executor: Executor) {
const metaExists = await executor(`select exists(
select from pg_tables where schemaname = 'public' and tablename = 'replicache_meta')`);
if (!metaExists.rows[0].exists) {
return 0;
}
const qr = await executor(
`select value from replicache_meta where key = 'schemaVersion'`,
);
return qr.rows[0].value;
}

0 comments on commit fa90475

Please sign in to comment.