Skip to content

Commit

Permalink
Move createDatabase to data.ts since it's not pg-generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Dec 3, 2021
1 parent a8adc39 commit 5e57e37
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
3 changes: 2 additions & 1 deletion backend/data.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { expect } from "chai";
import { setup, test } from "mocha";
import {
createDatabase,
delObject,
getCookie,
getLastMutationID,
getObject,
putObject,
setLastMutationID,
} from "./data";
import { createDatabase, withExecutor } from "./pg";
import { withExecutor } from "./pg";

setup(async () => {
await withExecutor(async () => {
Expand Down
27 changes: 26 additions & 1 deletion backend/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { JSONValue } from "replicache";
import { Executor } from "./pg";
import { Executor, transact } from "./pg";

export async function createDatabase() {
await transact(async (executor) => {
// TODO: Proper versioning for schema.
await executor("drop table if exists client cascade");
await executor("drop table if exists object cascade");

await executor(`create table client (
id varchar(100) primary key not null,
lastmutationid int not null)`);

await executor(`create table object (
k varchar(100) not null,
v text not null,
documentid varchar(100) not null,
deleted bool not null default false,
lastmodified timestamp(6) not null,
unique (documentid, k)
)`);

await executor(`create index on object (documentid)`);
await executor(`create index on object (deleted)`);
await executor(`create index on object (lastmodified)`);
});
}

export async function getCookie(
executor: Executor,
Expand Down
25 changes: 0 additions & 25 deletions backend/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,6 @@ async function transactWithExecutor<R>(
throw new Error("Tried to execute transacation too many times. Giving up.");
}

export async function createDatabase() {
await transact(async (executor) => {
// TODO: Proper versioning for schema.
await executor("drop table if exists client cascade");
await executor("drop table if exists object cascade");

await executor(`create table client (
id varchar(100) primary key not null,
lastmutationid int not null)`);

await executor(`create table object (
k varchar(100) not null,
v text not null,
documentid varchar(100) not null,
deleted bool not null default false,
lastmodified timestamp(6) not null,
unique (documentid, k)
)`);

await executor(`create index on object (documentid)`);
await executor(`create index on object (deleted)`);
await executor(`create index on object (lastmodified)`);
});
}

//stackoverflow.com/questions/60339223/node-js-transaction-coflicts-in-postgresql-optimistic-concurrency-control-and
function shouldRetryTransaction(err: unknown) {
const code = typeof err === "object" ? String((err as any).code) : null;
Expand Down
2 changes: 1 addition & 1 deletion pages/api/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDatabase } from "../../backend/pg";
import { createDatabase } from "../../backend/data";
import type { NextApiRequest, NextApiResponse } from "next";

export default async (req: NextApiRequest, res: NextApiResponse) => {
Expand Down

0 comments on commit 5e57e37

Please sign in to comment.