PicardyTheThird
PicardyTheThird
DTDrizzle Team
Created by PicardyTheThird on 1/2/2025 in #help
drizzle-kit studio not reading typescript
Okay, I solved it. I needed to set up my other packages to build to js before being imported by my schemas
3 replies
DTDrizzle Team
Created by PicardyTheThird on 1/2/2025 in #help
drizzle-kit studio not reading typescript
For context, this is trying to read the schema files.
3 replies
DTDrizzle Team
Created by PicardyTheThird on 12/28/2024 in #help
Table Abstraction
Okay it took me digging deep through the weeds of these generics but I finally cracked it. Here's the answer:
import { pgTable, text, timestamp, boolean } from 'drizzle-orm/pg-core';
import type { PgColumnBuilderBase, PgTableWithColumns } from 'drizzle-orm/pg-core';
import { type BuildColumns } from 'drizzle-orm';

type Blank = Record<string, PgColumnBuilderBase>;

export type TableBuilderConfig<T extends Blank, Name extends string> = {
name: Name;
structure: T;
};

const globalCols = {
id: text('id').primaryKey(),
created: timestamp('created').defaultNow().notNull(),
updated: timestamp('updated').defaultNow().notNull(),
archived: boolean('archived').default(false).notNull(),
};

type Table<T extends Blank, TableName extends string> = PgTableWithColumns<{
name: TableName;
schema: undefined;
columns: BuildColumns<TableName, T, 'pg'>;
dialect: "pg";
}>;

export class TableBuilder<T extends Blank, Name extends string> {
public readonly table: Table<T & typeof globalCols, Name>;

constructor(public readonly data: TableBuilderConfig<T, Name>) {

this.table = pgTable(data.name, {
...globalCols,
...data.structure,
});
}
}

const s = new TableBuilder({
name: 'Accounts',
// database: null,
structure: {
username: text('username').notNull(),
email: text('email').notNull().unique(),
password: text('password').notNull(),
}
});
import { pgTable, text, timestamp, boolean } from 'drizzle-orm/pg-core';
import type { PgColumnBuilderBase, PgTableWithColumns } from 'drizzle-orm/pg-core';
import { type BuildColumns } from 'drizzle-orm';

type Blank = Record<string, PgColumnBuilderBase>;

export type TableBuilderConfig<T extends Blank, Name extends string> = {
name: Name;
structure: T;
};

const globalCols = {
id: text('id').primaryKey(),
created: timestamp('created').defaultNow().notNull(),
updated: timestamp('updated').defaultNow().notNull(),
archived: boolean('archived').default(false).notNull(),
};

type Table<T extends Blank, TableName extends string> = PgTableWithColumns<{
name: TableName;
schema: undefined;
columns: BuildColumns<TableName, T, 'pg'>;
dialect: "pg";
}>;

export class TableBuilder<T extends Blank, Name extends string> {
public readonly table: Table<T & typeof globalCols, Name>;

constructor(public readonly data: TableBuilderConfig<T, Name>) {

this.table = pgTable(data.name, {
...globalCols,
...data.structure,
});
}
}

const s = new TableBuilder({
name: 'Accounts',
// database: null,
structure: {
username: text('username').notNull(),
email: text('email').notNull().unique(),
password: text('password').notNull(),
}
});
3 replies
DTDrizzle Team
Created by PicardyTheThird on 12/28/2024 in #help
Table Abstraction
My other thought would be to use pgTable outside this class, pass in the output, then the class will take the cols and run pgTable again with its own columns and try to pull the right types from the original. But that feels kinda spaghetti
3 replies