Skip to content

Commit 93fc57b

Browse files
committed
Persist event data
1 parent 1fa656a commit 93fc57b

File tree

8 files changed

+125
-246
lines changed

8 files changed

+125
-246
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE TABLE "event" (
2-
"id" text PRIMARY KEY NOT NULL,
1+
CREATE TABLE "events" (
2+
"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "events_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
33
"type" text NOT NULL,
44
"timestamp" bigint NOT NULL,
55
"properties" json NOT NULL,

migrations/0000_misty_leo.sql

Lines changed: 0 additions & 12 deletions
This file was deleted.

migrations/meta/0000_snapshot.json

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,64 @@
11
{
2-
"id": "013accf9-070a-47cd-9b49-1318a1c8a237",
2+
"id": "5ab333f5-a980-4564-a919-3d15608505d0",
33
"prevId": "00000000-0000-0000-0000-000000000000",
44
"version": "7",
55
"dialect": "postgresql",
66
"tables": {
7-
"public.organization": {
8-
"name": "organization",
7+
"public.events": {
8+
"name": "events",
99
"schema": "",
1010
"columns": {
1111
"id": {
1212
"name": "id",
13-
"type": "text",
13+
"type": "integer",
1414
"primaryKey": true,
15-
"notNull": true
16-
},
17-
"stripe_customer_id": {
18-
"name": "stripe_customer_id",
19-
"type": "text",
20-
"primaryKey": false,
21-
"notNull": false
15+
"notNull": true,
16+
"identity": {
17+
"type": "always",
18+
"name": "events_id_seq",
19+
"schema": "public",
20+
"increment": "1",
21+
"startWith": "1",
22+
"minValue": "1",
23+
"maxValue": "2147483647",
24+
"cache": "1",
25+
"cycle": false
26+
}
2227
},
23-
"stripe_subscription_id": {
24-
"name": "stripe_subscription_id",
28+
"type": {
29+
"name": "type",
2530
"type": "text",
2631
"primaryKey": false,
27-
"notNull": false
32+
"notNull": true
2833
},
29-
"stripe_subscription_price_id": {
30-
"name": "stripe_subscription_price_id",
31-
"type": "text",
34+
"timestamp": {
35+
"name": "timestamp",
36+
"type": "bigint",
3237
"primaryKey": false,
33-
"notNull": false
38+
"notNull": true
3439
},
35-
"stripe_subscription_status": {
36-
"name": "stripe_subscription_status",
37-
"type": "text",
40+
"properties": {
41+
"name": "properties",
42+
"type": "json",
3843
"primaryKey": false,
39-
"notNull": false
40-
},
41-
"stripe_subscription_current_period_end": {
42-
"name": "stripe_subscription_current_period_end",
43-
"type": "bigint",
44-
"primaryKey": false,
45-
"notNull": false
44+
"notNull": true
4645
},
47-
"updated_at": {
48-
"name": "updated_at",
46+
"created_at": {
47+
"name": "created_at",
4948
"type": "timestamp",
5049
"primaryKey": false,
5150
"notNull": true,
5251
"default": "now()"
5352
},
54-
"created_at": {
55-
"name": "created_at",
53+
"updated_at": {
54+
"name": "updated_at",
5655
"type": "timestamp",
5756
"primaryKey": false,
5857
"notNull": true,
5958
"default": "now()"
6059
}
6160
},
62-
"indexes": {
63-
"stripe_customer_id_idx": {
64-
"name": "stripe_customer_id_idx",
65-
"columns": [
66-
{
67-
"expression": "stripe_customer_id",
68-
"isExpression": false,
69-
"asc": true,
70-
"nulls": "last"
71-
}
72-
],
73-
"isUnique": true,
74-
"concurrently": false,
75-
"method": "btree",
76-
"with": {}
77-
}
78-
},
61+
"indexes": {},
7962
"foreignKeys": {},
8063
"compositePrimaryKeys": {},
8164
"uniqueConstraints": {},

migrations/meta/0001_snapshot.json

Lines changed: 0 additions & 149 deletions
This file was deleted.

migrations/meta/_journal.json

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55
{
66
"idx": 0,
77
"version": "7",
8-
"when": 1747031111132,
9-
"tag": "0000_misty_leo",
10-
"breakpoints": true
11-
},
12-
{
13-
"idx": 1,
14-
"version": "7",
15-
"when": 1747203099634,
16-
"tag": "0001_glamorous_thunderbird",
8+
"when": 1747204609794,
9+
"tag": "0000_hesitant_kylun",
1710
"breakpoints": true
1811
}
1912
]

src/app/api/events/route.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { auth } from '@clerk/nextjs/server';
22
import { NextRequest, NextResponse } from 'next/server';
33

4+
import { eventSchema } from '@/schemas';
5+
import { db } from '@/db';
6+
import { eventsTable } from '@/db/schema';
7+
48
export async function POST(request: NextRequest) {
59
const { userId } = await auth();
610

@@ -11,7 +15,14 @@ export async function POST(request: NextRequest) {
1115
);
1216
}
1317

14-
console.log(await request.json());
18+
const payload = await request.json();
19+
const result = eventSchema.safeParse(payload);
20+
21+
if (!result.success) {
22+
return NextResponse.json({ success: false });
23+
}
24+
25+
const [record] = await db.insert(eventsTable).values(result.data).returning();
1526

16-
return NextResponse.json({ success: true });
27+
return NextResponse.json({ success: true, id: record?.id });
1728
}

src/db/schema.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@ import {
33
pgTable,
44
text,
55
timestamp,
6-
uniqueIndex,
76
json,
7+
integer,
88
} from 'drizzle-orm/pg-core';
99

10-
export const organizationSchema = pgTable(
11-
'organization',
12-
{
13-
id: text('id').primaryKey(),
14-
stripeCustomerId: text('stripe_customer_id'),
15-
stripeSubscriptionId: text('stripe_subscription_id'),
16-
stripeSubscriptionPriceId: text('stripe_subscription_price_id'),
17-
stripeSubscriptionStatus: text('stripe_subscription_status'),
18-
stripeSubscriptionCurrentPeriodEnd: bigint(
19-
'stripe_subscription_current_period_end',
20-
{ mode: 'number' },
21-
),
22-
updatedAt: timestamp('updated_at', { mode: 'date' })
23-
.defaultNow()
24-
.$onUpdate(() => new Date())
25-
.notNull(),
26-
createdAt: timestamp('created_at', { mode: 'date' }).defaultNow().notNull(),
27-
},
28-
(table) => [uniqueIndex('stripe_customer_id_idx').on(table.stripeCustomerId)],
29-
);
10+
// export const organizationTable = pgTable(
11+
// 'organizations',
12+
// {
13+
// id: integer().primaryKey().generatedAlwaysAsIdentity(),
14+
// stripeCustomerId: text('stripe_customer_id'),
15+
// stripeSubscriptionId: text('stripe_subscription_id'),
16+
// stripeSubscriptionPriceId: text('stripe_subscription_price_id'),
17+
// stripeSubscriptionStatus: text('stripe_subscription_status'),
18+
// stripeSubscriptionCurrentPeriodEnd: bigint(
19+
// 'stripe_subscription_current_period_end',
20+
// { mode: 'number' },
21+
// ),
22+
// updatedAt: timestamp('updated_at', { mode: 'date' })
23+
// .defaultNow()
24+
// .$onUpdate(() => new Date())
25+
// .notNull(),
26+
// createdAt: timestamp('created_at', { mode: 'date' }).defaultNow().notNull(),
27+
// },
28+
// (table) => [uniqueIndex('stripe_customer_id_idx').on(table.stripeCustomerId)],
29+
// );
3030

31-
export const eventSchema = pgTable('event', {
32-
id: text('id').primaryKey(),
31+
export const eventsTable = pgTable('events', {
32+
id: integer().primaryKey().generatedAlwaysAsIdentity(),
3333
type: text('type').notNull(),
3434
timestamp: bigint('timestamp', { mode: 'number' }).notNull(),
3535
properties: json('properties').notNull(),

0 commit comments

Comments
 (0)