0.7.1 • Published 8 days ago

drizzle-orm-helpers v0.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 days ago

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, Record<string, unknown>> & 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
      }
    }
  },
  //...
]
0.7.1

8 days ago

0.7.0

14 days ago

0.6.6

24 days ago

0.6.5

25 days ago

0.6.3

1 month ago

0.6.2

1 month ago

0.6.4

1 month ago

0.6.1

1 month ago

0.6.0

1 month ago

0.5.21

1 month ago

0.5.22

1 month ago

0.5.20

1 month ago

0.5.23

1 month ago

0.5.18

1 month ago

0.5.19

1 month ago

0.5.16

1 month ago

0.5.17

1 month ago

0.5.15

1 month ago

0.5.14

1 month ago

0.5.10

2 months ago

0.5.11

2 months ago

0.5.8

2 months ago

0.5.9

2 months ago

0.5.12

2 months ago

0.5.13

2 months ago

0.5.7

2 months ago

0.5.4

2 months ago

0.5.6

2 months ago

0.5.5

2 months ago

0.4.9

2 months ago

0.4.8

2 months ago

0.4.10

2 months ago

0.4.11

2 months ago

0.4.12

2 months ago

0.5.3

2 months ago

0.5.0

2 months ago

0.5.2

2 months ago

0.5.1

2 months ago

0.4.5

2 months ago

0.4.4

2 months ago

0.4.7

2 months ago

0.4.6

2 months ago

0.4.1

2 months ago

0.4.0

2 months ago

0.4.3

2 months ago

0.4.2

2 months ago

0.3.0

3 months ago

0.3.2

3 months ago

0.3.1

3 months ago

0.2.5

3 months ago

0.2.1

3 months ago

0.2.0

3 months ago

0.2.3

3 months ago

0.2.2

3 months ago

0.2.4

3 months ago

0.1.9

3 months ago

0.1.8

3 months ago

0.1.7

3 months ago

0.1.6

3 months ago

0.1.5

3 months ago

0.1.4

3 months ago

0.1.3

3 months ago

0.1.2

3 months ago

0.1.1

3 months ago

0.1.0

3 months ago