-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1ac508
commit 60f94b6
Showing
13 changed files
with
920 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.