MCPcopy Index your code
hub / github.com/emmnull/drizzle-orm-helpers

github.com/emmnull/drizzle-orm-helpers @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
91 symbols 128 edges 40 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Drizzle ORM Helpers

⚠️ Use at your own risk ⚠️

🚧 This package is under construction, expect some things to be broken! 🚧

This is a collection of unofficial typescript-friendly helpers for use with Drizzle ORM. Provided items include common SQL/dialect-specific values, functions, operators, and column types. Note that most the work is currently oriented towards Postgres and that most "solutions" provided by this package should be seen as provisory, in wait of being replaced by implementations provided officially and with more stability through Drizzle-ORM package(s).

Documentation

A crude auto-generated documentation is available here.

Examples

Aggregating translations

const APP_LANGUAGES = ['fr', 'en', 'es'] as const;
type AppLanguage = (typeof APP_LANGUAGES)[number]; // 'fr' | 'en' | 'es';

const languages = pgTable('languages', {
  lang: textenum('lang', { enum: APP_LANGUAGES }),
});

const projects = pgTable('projects', {
  id: text('id')
    .default(nanoid({ size: 12 }))
    .primaryKey(),
  createdById: text('created_by_id').references(() => users.id, {
    onDelete: 'cascade',
    onUpdate: 'cascade',
  }),
});

const projectsTranslations = pgTable(
  'projects_t',
  {
    id: text('id')
      .references(() => projects.id, { onDelete: 'cascade', onUpdate: 'cascade' })
      .notNull(),
    lang: textenum('lang', { enum: APP_LANGUAGES })
      .references(() => languages.lang, { onDelete: 'cascade', onUpdate: 'cascade' })
      .notNull(),
    title: text('title'),
    description: text('description'),
  },
  (table) => {
    return {
      pk: primaryKey({ columns: [table.id, table.lang] }),
    };
  }
);

/**
 * Build a json object aggregating translations with languages as keys and joined columns as nested
 * properties.
 */
export function aggTranslations<TSelection extends ColumnsSelection>(selection: TSelection) {
  return jsonObjectAgg(languages.lang, jsonBuildObject(selection));
}

/**
 * Join translations through the languages table.
 */
export function joinTranslations<
  TSelect extends PgSelect,
  TTranslations extends
    | (AnyTable<TableConfig> & LangColumn)
    | (Subquery<string, ColumnSelection> & LangColumn),
>(select: TSelect, translations: TTranslations, on: SQL) {
  return select
    .leftJoin(languages, $true)
    .leftJoin(translations, and(on, eq(languages.lang, translations.lang)));
}

const projectsWithTranslations = await joinTranslations(
  db
    .select({
      ...getColumns(projects),
      translations: aggTranslations(getColumns(projectsTranslations)),
    })
    .from(projects),
  projectsTranslations,
  eq(projects.id, projectsTranslations.id)
);

Would return aggregated data with expected types as:

[
  {
    id: string,
    created_by_id: string,
    translations: {
      fr: {
        id: string,
        lang: string,
        title?: string,
        description?: string
      },
      en: {
        id: string,
        lang: string,
        title?: string,
        description?: string
      }
      es: {
        id: string,
        lang: string,
        title?: string,
        description?: string
      }
    }
  },
  //...
]

Core symbols most depended-on inside this repo

isEnumMember
called by 2
src/pg/custom-types.ts
dateToString
called by 2
src/pg/validation.ts
jsonbBuildObject
called by 1
src/pg/functions.ts
coalesce
called by 0
src/functions.ts
nullIf
called by 0
src/functions.ts
greatest
called by 0
src/functions.ts
least
called by 0
src/functions.ts
add
called by 0
src/operators.ts

Shape

Function 91

Languages

TypeScript100%

Modules by API surface

src/pg/functions.ts21 symbols
src/pg/cube/functions.ts10 symbols
src/pg/custom-types.ts9 symbols
src/pg/validation.ts6 symbols
src/pg/postgis/functions.ts5 symbols
src/pg/postgis/custom-types.ts5 symbols
src/pg/cube/operators.ts5 symbols
src/pg/utilities.ts4 symbols
src/operators.ts4 symbols
src/functions.ts4 symbols
src/utilities.ts3 symbols
src/pg/fuzzystrmatch/functions.ts3 symbols

For agents

$ claude mcp add drizzle-orm-helpers \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact