Skip to content

Commit

Permalink
chore(drizzle): update schema
Browse files Browse the repository at this point in the history
  • Loading branch information
conradbekondo committed Jan 3, 2025
1 parent bf95d7a commit 477416a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
8 changes: 6 additions & 2 deletions netlify/functions/campaigns/index.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Request, Response, Router } from 'express';
import { prepareHandler } from '@functions/helpers/handler.mjs';
import {
prepareHandler
} from '@functions/helpers/handler.mjs';
import { useCampaignsDb } from '@functions/helpers/db.mjs';
import { auth } from '@functions/middleware/auth.mjs';
import {
auth
} from '@functions/middleware/auth.mjs';
import {
campaignPublications,
campaigns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CREATE TABLE "campaigns" (
CREATE TABLE "categories" (
"id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "categories_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),
"title" varchar(255) NOT NULL,
"description" varchar(500) NOT NULL,
"description" text NOT NULL,
"image" varchar(500)
);
--> statement-breakpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "91c90baf-68d8-4a78-9504-b5021aafd097",
"id": "bd1941a9-8ef1-4b5a-b620-fc61f1f8ad60",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
Expand Down Expand Up @@ -222,7 +222,7 @@
},
"description": {
"name": "description",
"type": "varchar(500)",
"type": "text",
"primaryKey": false,
"notNull": true
},
Expand Down
4 changes: 2 additions & 2 deletions netlify/functions/config/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1735913596060,
"tag": "0000_strong_saracen",
"when": 1735926205005,
"tag": "0000_orange_true_believers",
"breakpoints": true
}
]
Expand Down
10 changes: 5 additions & 5 deletions netlify/functions/config/db/schema/categories.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bigint, pgTable, varchar } from 'drizzle-orm/pg-core';
import { bigint, pgTable, text, varchar } from 'drizzle-orm/pg-core';

export const categories = pgTable('categories', {
id: bigint({ mode: 'number' }).generatedAlwaysAsIdentity().primaryKey(),
title: varchar({length: 255}).notNull(),
description: varchar({length: 500}).notNull(),
image: varchar({length: 500}),
})
title: varchar({ length: 255 }).notNull(),
description: text().notNull(),
image: varchar({ length: 500 }),
});
10 changes: 8 additions & 2 deletions netlify/functions/config/db/seed/categories.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useCategoriesDb } from '@functions/helpers/db.mjs';
import data from './categories.json';
import { categories } from '@functions/config/db/schema/categories';
import { sql } from 'drizzle-orm';

export async function seed() {
const db = useCategoriesDb();
await db.delete(categories);
await db.insert(categories).values(data);
const entries = data.map((d, i) => ({ id: i + 1, ...d }));
await db.transaction(async t => {
for await (const entry of entries) {
await t.execute(sql`INSERT INTO ${categories}(id, title, description) OVERRIDING SYSTEM VALUE
VALUES (${entry.id}, ${entry.title}, ${entry.description}) ON CONFLICT (id) DO NOTHING`);
}
});
}

export const name = 'categories'
Expand Down
4 changes: 1 addition & 3 deletions netlify/functions/config/db/seed/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dotenv/config'
import * as categories from './categories';
import { concatMap, from, map } from 'rxjs';
import {config} from 'dotenv'

config();

type Seeder = { name: string, seed: () => Promise<void> }
const seeders: Seeder[] = [categories];
Expand Down
12 changes: 6 additions & 6 deletions netlify/functions/helpers/db.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import { neonConfig } from '@neondatabase/serverless';
import ws from 'ws';

neonConfig.webSocketConstructor = global.WebSocket ?? ws;
const connection = String(process.env['DATABASE_URL']);
const casing = 'snake_case';
const logger = String(process.env['NODE_ENV']) == 'development';

export function useUsersDb() {
return drizzle({
schema: { ...users }, casing: 'snake_case',
connection: String(process.env['DATABASE_URL'])
schema: { ...users }, casing, connection, logger
})
}

export function useCategoriesDb() {
return drizzle({
schema: { ...categories }, casing: 'snake_case',
connection: String(process.env['DATABASE_URL'])
schema: { ...categories }, casing, connection, logger
})
}

export function useCampaignsDb() {
return drizzle({
schema: { ...campaigns }, casing: 'snake_case',
connection: String(process.env['DATABASE_URL'])
schema: { ...campaigns }, casing, connection, logger
});
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
"migrate.dev": "npx drizzle-kit generate --config=netlify/functions/config/drizzle.config.ts && npx drizzle-kit migrate --config=netlify/functions/config/drizzle.config.ts",
"migrate.prod": "npx drizzle-kit migrate --config=netlify/functions/config/drizzle.config.ts",
"migrate.ci": "npx drizzle-kit migrate --config=netlify/functions/config/drizzle.config.ts",
"postmigrate.ci": "pnpm db:seed",
"postmigrate.prod": "pnpm db:seed",
"postmigrate.dev": "pnpm db:seed",
"prestart": "pnpm migrate.dev",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"release": "standard-version",
"seed": "npx tsx netlify/functions/config/db/seed/index.ts"
"db:seed": "npx tsx netlify/functions/config/db/seed/index.ts"
},
"private": true,
"dependencies": {
Expand Down

0 comments on commit 477416a

Please sign in to comment.