Skip to content

Instantly share code, notes, and snippets.

View PaulieScanlon's full-sized avatar

Paul Scanlon PaulieScanlon

View GitHub Profile
@PaulieScanlon
PaulieScanlon / visitors-list-component.tsx
Created May 17, 2024 21:47
Example React component
import type { FunctionComponent } from 'react';
import type { AnalyticsType } from '../../schema';
type PickedProps = Pick<AnalyticsType, 'city' | 'country' | 'flag'>;
interface Props {
analyticsData: PickedProps[];
}
const VisitorsList: FunctionComponent<Props> = ({ analyticsData }) => {
@PaulieScanlon
PaulieScanlon / visitors-list-usage.tsx
Created May 17, 2024 21:46
Example React component usage
<VisitorsList analyticsData={results} />
@PaulieScanlon
PaulieScanlon / analytics-query.ts
Created May 17, 2024 21:45
Example analytics query
const results = await db
.select({
city: analytics.city,
country: analytics.country,
flag: analytics.flag,
})
.from(analytics)
.where(eq(analytics.country, 'US'))
.orderBy(analytics.date)
.limit(10);
@PaulieScanlon
PaulieScanlon / schema.diff
Last active May 17, 2024 21:58
Example InferSectionModel
import { pgTable, serial, varchar, timestamp, decimal} from 'drizzle-orm/pg-core';
+ import { type InferSelectModel } from 'drizzle-orm';
export const analytics = pgTable('analytics', {
id: serial('id').primaryKey(),
date: timestamp('date').notNull(),
slug: varchar('slug', { length: 255 }).notNull(),
referrer: varchar('referrer', { length: 255 }),
flag: varchar('flag', { length: 16 }),
@PaulieScanlon
PaulieScanlon / alter-schema.diff
Created May 17, 2024 21:42
Alter table example
- import { pgTable, serial, varchar } from 'drizzle-orm/pg-core';
+ import { pgTable, serial, varchar, date } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
user_id: serial('user_id').primaryKey(),
first_name: varchar('first_name', { length: 100 }),
last_name: varchar('last_name', { length: 100 }),
email: varchar('email', { length: 80 }).unique(),
role: varchar('role', { length: 20 }),
+ date_of_birth: date('date_of_birth')
@PaulieScanlon
PaulieScanlon / happiest-cities.ts
Created May 17, 2024 21:39
Happy Reactions Production Drizzle ORM query
.select({
city: analytics.city,
country: analytics.country,
flag: analytics.flag,
count: sql`COUNT(${reactions.id})`.as('count'),
})
.from(analytics)
.innerJoin(reactions, eq(analytics.slug, reactions.slug))
.where(and(eq(reactions.reaction, 'happy'), gte(analytics.date, new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000))))
.groupBy(analytics.city, analytics.country, analytics.flag)
@PaulieScanlon
PaulieScanlon / happiest-cities.sql
Created May 17, 2024 21:39
Happy Reactions Production SQL query
WITH happy_reactions AS (
SELECT analytics.city, analytics.country, analytics.flag, COUNT(reactions.id) AS count
FROM analytics
INNER JOIN reactions ON analytics.slug = reactions.slug
WHERE reactions.reaction = 'happy' AND analytics.date >= CURRENT_DATE - INTERVAL '30' DAY
GROUP BY analytics.city, analytics.country, analytics.flag
)
SELECT city, country, flag, count
FROM happy_reactions
ORDER BY count DESC
@PaulieScanlon
PaulieScanlon / drizzle-orm-join.ts
Created May 17, 2024 21:35
Join Example Drizzle ORM query
.select({
id: orders.order_id,
order_date: orders.order_date,
first_name: orders.first_name,
last_name: orders.last_name,
})
.from(orders)
.innerJoin(customers, eq(orders.order_id, customers.customer_id))
.limit(50);
@PaulieScanlon
PaulieScanlon / sql-join.sql
Created May 17, 2024 21:34
Join Example SQL query
SELECT
orders.order_id,
orders.order_date,
orders.first_name,
orders.last_name
FROM orders
INNER JOIN customers ON orders.order_id = customers.customer_id
LIMIT 50;
@PaulieScanlon
PaulieScanlon / drizzle-orm-select.ts
Created May 17, 2024 21:31
Select Example Drizzle ORM query
.select({
id: users.user_id,
first_name: users.first_name,
last_name: users.last_name,
email: users.email,
role: users.role,
})
.from(users)
.where(eq(users.role, 'admin'))
.orderBy(users.last_name);