Skip to content

Instantly share code, notes, and snippets.

View PaulieScanlon's full-sized avatar

Paul Scanlon PaulieScanlon

View GitHub Profile
@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);
@PaulieScanlon
PaulieScanlon / creact-table.ts
Created May 17, 2024 21:27
Example of Drizzle ORM create table
import { pgTable, serial, varchar } 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 }),
});
@PaulieScanlon
PaulieScanlon / create-table.sql
Created May 17, 2024 21:26
Example of SQL create table
CREATE TABLE USERS (
user_id SERIAL PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(80) UNIQUE,
role VARCHAR(20)
);
@PaulieScanlon
PaulieScanlon / happy-cities.js
Last active May 17, 2024 19:14
Query to join reactions with analytics data from paulie.dev
.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 / xata.js
Created May 13, 2024 19:09
Example Xata query
xata.db['users'].select(['name', 'country', 'email']).getMany();
@PaulieScanlon
PaulieScanlon / supabase.js
Created May 13, 2024 19:09
Example Supabase query
supabase.from('users').select('name, country, email');
@PaulieScanlon
PaulieScanlon / query.sql
Created May 13, 2024 19:07
Simple SELECT query
SELECT name, country, email FROM users
@PaulieScanlon
PaulieScanlon / schema.sql
Created May 13, 2024 19:04
Schema for analytics table
CREATE TABLE analytics (
id SERIAL PRIMARY KEY,
date TIMESTAMP WITH TIME ZONE NOT NULL,
slug VARCHAR NOT NULL,
referrer VARCHAR,
flag VARCHAR,
country VARCHAR,
city VARCHAR,
latitude DECIMAL,
longitude DECIMAL
@PaulieScanlon
PaulieScanlon / query.ts
Created April 25, 2024 19:43
Example of typed query
import type { Users } from '../../pg-to-ts-db';
const response = await client.query<Users[]>('SELECT * FROM users');
@PaulieScanlon
PaulieScanlon / pg-to-ts-db.d.t
Created April 25, 2024 19:42
pg-to-ts generated types
// ./pg-to-ts-db.d.ts
// Table users
export interface Users {
id: number;
first_name: string;
last_name: string;
email: string;
country: string | null;
}