Skip to content

Commit

Permalink
wip: trying to integrate drizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkarimoff committed Jan 25, 2024
1 parent a1ac508 commit 60f94b6
Show file tree
Hide file tree
Showing 13 changed files with 920 additions and 40 deletions.
16 changes: 16 additions & 0 deletions apps/analytics/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "~/lib/config";
import dotenv from "dotenv";
import { defineConfig } from "drizzle-kit";

dotenv.config();

export default defineConfig({
schema: "./lib/schema.ts",
out: "./drizzle",
driver: "pg",
dbCredentials: {
connectionString: process.env.POSTGRES_URL!,
},
verbose: true,
strict: true,
});
13 changes: 13 additions & 0 deletions apps/analytics/drizzle/0000_known_menace.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS "events" (
"id" serial PRIMARY KEY NOT NULL,
"type" text NOT NULL,
"installationId" text NOT NULL,
"timestamp" text NOT NULL,
"isocode" text,
"message" text,
"name" text,
"stack" text,
"metadata" json
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "unique_idx" ON "events" ("id");
87 changes: 87 additions & 0 deletions apps/analytics/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"id": "94097300-298d-42a1-9333-f2973dd0e898",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
"tables": {
"events": {
"name": "events",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"type": {
"name": "type",
"type": "text",
"primaryKey": false,
"notNull": true
},
"installationId": {
"name": "installationId",
"type": "text",
"primaryKey": false,
"notNull": true
},
"timestamp": {
"name": "timestamp",
"type": "text",
"primaryKey": false,
"notNull": true
},
"isocode": {
"name": "isocode",
"type": "text",
"primaryKey": false,
"notNull": false
},
"message": {
"name": "message",
"type": "text",
"primaryKey": false,
"notNull": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false
},
"stack": {
"name": "stack",
"type": "text",
"primaryKey": false,
"notNull": false
},
"metadata": {
"name": "metadata",
"type": "json",
"primaryKey": false,
"notNull": false
}
},
"indexes": {
"unique_idx": {
"name": "unique_idx",
"columns": [
"id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
13 changes: 13 additions & 0 deletions apps/analytics/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1706192371067,
"tag": "0000_known_menace",
"breakpoints": true
}
]
}
5 changes: 5 additions & 0 deletions apps/analytics/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dotenv from "dotenv";
import { join } from "node:path";

const envPath = join(process.cwd(), ".env");
dotenv.config({ path: envPath });
6 changes: 6 additions & 0 deletions apps/analytics/lib/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import * as schema from "./schema";

// Use this object to send drizzle queries to your DB
export const db = drizzle(sql, { schema });
22 changes: 22 additions & 0 deletions apps/analytics/lib/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { json, pgTable, serial, text, uniqueIndex } from "drizzle-orm/pg-core";

// Create a pgTable that maps to a table in your DB
export const events = pgTable(
"events",
{
id: serial("id").primaryKey(),
type: text("type").notNull(),
installationId: text("installationId").notNull(),
timestamp: text("timestamp").notNull(),
isocode: text("isocode"),
message: text("message"),
name: text("name"),
stack: text("stack"),
metadata: json("metadata"),
},
(events) => {
return {
uniqueIdx: uniqueIndex("unique_idx").on(events.id),
};
}
);
2 changes: 2 additions & 0 deletions apps/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.29.3",
"i18n-iso-countries": "^7.7.0",
"lucide-react": "^0.292.0",
"next": "14.0.1",
Expand All @@ -41,6 +42,7 @@
"@types/papaparse": "^5.3.14",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"drizzle-kit": "^0.20.13",
"eslint-config-custom": "workspace:*",
"tailwindcss": "^3.3.0",
"tsconfig": "workspace:*",
Expand Down
13 changes: 13 additions & 0 deletions apps/analytics/scripts/getEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dotenv from "dotenv";
dotenv.config();

import { db } from "~/lib/db";
import { events } from "~/lib/schema";

async function getEvent() {
const result = await db.select().from(events);

console.log("Events: ", result);
}

getEvent();
15 changes: 15 additions & 0 deletions apps/analytics/scripts/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dotenv from "dotenv";
dotenv.config();

import { migrate } from "drizzle-orm/vercel-postgres/migrator";
import { db } from "~/lib/db";

export async function runMigration() {
try {
await migrate(db, { migrationsFolder: "./drizzle" });
} catch (error) {
console.error("Error running migration:", error);
}
}

runMigration();
76 changes: 76 additions & 0 deletions apps/analytics/scripts/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import dotenv from "dotenv";
dotenv.config();

import { db } from "~/lib/db";
import { events } from "~/lib/schema";
import { faker } from "@faker-js/faker";
import { sql } from "@vercel/postgres";

type Event = typeof events.$inferInsert;

let installationIds: string[] = [];
for (let i = 0; i < 20; i++) {
installationIds.push(faker.string.uuid());
}

const eventTypes = [
"AppSetup",
"ProtocolInstalled",
"InterviewStarted",
"InterviewCompleted",
"InterviewCompleted",
"DataExported",
"Error",
];

async function seedEvents() {
console.info("Starting to seed events");

try {
for (let i = 0; i < 100; i++) {
const type = faker.helpers.arrayElement(eventTypes);
const installationId = faker.helpers.arrayElement(installationIds);
const timestamp = faker.date.recent().toDateString();
const metadata = {
details: faker.lorem.sentence(),
path: faker.lorem.sentence(),
};
const isocode = faker.location.countryCode();
const message = faker.lorem.sentence();
const name = faker.lorem.sentence();
const stack = faker.lorem.sentence();

const event: Event = {
type,
metadata,
timestamp,
installationId,
isocode,
message,
name,
stack,
};

await db.insert(events).values(event).returning();
}
} catch (error) {
console.error("Error seeding events", error);
}

process.exit();
}

async function checkTables() {
const result = await sql`
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
`;

console.log(result.rows);
}

(async () => {
await seedEvents();
})();
25 changes: 6 additions & 19 deletions apps/analytics/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"extends": "tsconfig/nextjs.json",
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"target": "ES6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -17,18 +13,9 @@
"resolveJsonModule": true,
"isolatedModules": true,
"paths": {
"~/*": [
"./*"
]
"~/*": ["./*"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 60f94b6

Please sign in to comment.