Skip to content

Commit

Permalink
Switch back to sqlite (#12)
Browse files Browse the repository at this point in the history
Simple switch back to sqlite before we deploy the backend.
  • Loading branch information
wilwade authored Apr 2, 2024
1 parent a78e9b4 commit 01a2195
Show file tree
Hide file tree
Showing 7 changed files with 672 additions and 67 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"prom-client": "^14.2.0",
"reflect-metadata": "^0.1.13",
"request": "^2.88.2",
"typeorm": "^0.3.17"
"sqlite3": "^5.1.7",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@eng-automation/js-style": "^2.3.0",
Expand Down
10 changes: 2 additions & 8 deletions src/db/dataSource.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { DataSource } from "typeorm";

import { config } from "src/config";

import { Drip } from "./entity/Drip";
import { migrations } from "./migration/migrations";

export const AppDataSource = new DataSource({
type: "postgres",
host: config.Get("DB_HOST"),
port: config.Get("DB_PORT"),
username: config.Get("DB_USERNAME"),
password: config.Get("DB_PASSWORD"),
database: config.Get("DB_DATABASE_NAME"),
type: "sqlite",
database: "sqlite.db",
synchronize: false,
logging: ["error", "warn"],
entities: [Drip],
Expand Down
6 changes: 3 additions & 3 deletions src/db/entity/Drip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class Drip {
@PrimaryGeneratedColumn()
id: number;
id: string;

@Column({ type: "timestamp", default: () => "CURRENT_TIMESTAMP" })
timestamp: string;
@Column({ type: "integer" })
timestamp: number;

@Column({ nullable: true })
@Index()
Expand Down
2 changes: 1 addition & 1 deletion src/db/migration/1692350473907-initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class Initial1692350473907 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "drip" ("id" SERIAL NOT NULL, "timestamp" TIMESTAMP NOT NULL DEFAULT now(), "usernameSha256" character varying, "addressSha256" character varying NOT NULL, CONSTRAINT "PK_869cd028514a4d3cd3e359d3844" PRIMARY KEY ("id"))`,
`CREATE TABLE "drip" ("id" STRING NOT NULL, "timestamp" INTEGER NOT NULL, "usernameSha256" character varying, "addressSha256" character varying NOT NULL, CONSTRAINT "PK_869cd028514a4d3cd3e359d3844" PRIMARY KEY ("id"))`,
);
await queryRunner.query(`CREATE INDEX "IDX_94572562aeb9809c368c2a5db1" ON "drip" ("usernameSha256") `);
await queryRunner.query(`CREATE INDEX "IDX_7d20734bce30abacefeed7dd4d" ON "drip" ("addressSha256") `);
Expand Down
4 changes: 3 additions & 1 deletion src/dripper/dripperStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from "crypto";
import crypto, { randomUUID } from "crypto";

import { AppDataSource } from "src/db/dataSource";
import { Drip } from "src/db/entity/Drip";
Expand All @@ -11,6 +11,8 @@ const dripRepository = AppDataSource.getRepository(Drip);

export async function saveDrip(opts: { username?: string; addr: string }) {
const freshDrip = new Drip();
freshDrip.id = randomUUID();
freshDrip.timestamp = Date.now();
freshDrip.addressSha256 = sha256(opts.addr);
if (opts.username) freshDrip.usernameSha256 = sha256(opts.username);
await dripRepository.insert(freshDrip);
Expand Down
4 changes: 2 additions & 2 deletions src/faucet.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe("Faucet E2E", () => {

const oldDrip = new Drip();
oldDrip.addressSha256 = sha256(userAddress);
oldDrip.timestamp = new Date(Date.now() - 30 * 60 * 60 * 1000).toISOString();
oldDrip.timestamp = BigInt(Date.now() - 30 * 60 * 60 * 1000);
dripRepository.insert(oldDrip);

const result = await drip(webEndpoint, userAddress);
Expand All @@ -227,7 +227,7 @@ describe("Faucet E2E", () => {

const oldDrip = new Drip();
oldDrip.addressSha256 = sha256(userAddress);
oldDrip.timestamp = new Date(Date.now() - 5 * 60 * 60 * 1000).toISOString();
oldDrip.timestamp = BigInt(Date.now() - 5 * 60 * 60 * 1000);
await dripRepository.insert(oldDrip);

await expect(drip(webEndpoint, userAddress)).rejects.toThrow(
Expand Down
Loading

0 comments on commit 01a2195

Please sign in to comment.