more database design

This commit is contained in:
2024-11-20 09:02:38 -05:00
parent 429d6d2b26
commit 7ccc6d10e8
14 changed files with 41 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
import 'dotenv/config';
import { drizzle } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import { sql } from 'drizzle-orm';
@@ -8,5 +8,6 @@ import { sql } from 'drizzle-orm';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
export const db = drizzle(pool);

View File

@@ -1,23 +1,23 @@
// db/queries.ts
import { accounts } from '../schema/Account'
import { Account } from '../schema/Account'
import { db } from '../index';
// Fetch all accounts
// Fetch all account
export async function getAllAccounts() {
return await db.select().from(accounts);
return await db.select().from(Account);
}
// Add a new accounts
export async function addAcounts(name: string) {
return await db.insertInto(accounts).values({ name }).returning();
// Add a new account
export async function addAcount(first_name: string, last_name : string, username : string, password_hash: string ) {
return await db.insert(Account).values({ first_name, last_name, username, password_hash }).returning();
}
// Update a accounts
export async function updateAcounts(id: number, name: string) {
return await db.update(accounts).set({ name }).where(accounts.id.equals(id));
// Update a account
export async function updateAcount(id: number, first_name: string, last_name : string, username : string, password_hash: string ) {
return await db.update(Account).set({ first_name, last_name, username, password_hash }).where(Account.id.equals(id));
}
// Delete a accounts
// Delete a account
export async function deleteAccount(id: number) {
return await db.deleteFrom(accounts).where(accounts.id.equals(id));
return await db.delete(Account).where(Account.id.equals(id));
}

View File

@@ -1,7 +0,0 @@
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
export const usersTable = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
age: integer().notNull(),
email: varchar({ length: 255 }).notNull().unique(),
});

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const Account = pgTable("bal_accounts", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "accounts_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),

11
src/db/schema/Build.ts Normal file
View File

@@ -0,0 +1,11 @@
import { pgTable, integer, varchar, text } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./helpers/columns.helpers";
export const Build = pgTable("builds", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "build_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),
account_id: integer().notNull(),
name: varchar({length:255}).notNull(),
description: text(),
...timestamps
})

View File

@@ -0,0 +1,10 @@
import { pgTable, integer, varchar, text } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./helpers/columns.helpers";
export const BuildComponent = pgTable("builds_components", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "build_components_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),
build_id: integer().notNull(),
product_id: integer().notNull(),
...timestamps
})

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const Category = pgTable("categories", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "categories_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar, text, doublePrecision, timestamp } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const LipseyCatalog = pgTable("lipseycatalog", {

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar, text, decimal } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const Product = pgTable("products", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "products_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar, timestamp } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const Product_feed = pgTable("product_feeds", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "productfeeds_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const Reseller = pgTable("bal_resellers", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "resellers_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),

View File

@@ -1,6 +1,6 @@
import { pgTable, integer, varchar } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { timestamps } from "./columns.helpers";
import { timestamps } from "./helpers/columns.helpers";
export const accounts = pgTable("base_table", {
id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "base_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }),