Skip to content

Commit

Permalink
Update 3rd param for all dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiSherman committed Dec 9, 2024
1 parent 6dabeb2 commit 3256029
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 19 deletions.
93 changes: 87 additions & 6 deletions drizzle-orm/src/mysql-core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import type { AnyIndexBuilder } from './indexes.ts';
import type { PrimaryKeyBuilder } from './primary-keys.ts';
import type { UniqueConstraintBuilder } from './unique-constraint.ts';

export type MySqlTableExtraConfig = Record<
string,
export type MySqlTableExtraConfigValue =
| AnyIndexBuilder
| CheckBuilder
| ForeignKeyBuilder
| PrimaryKeyBuilder
| UniqueConstraintBuilder
| UniqueConstraintBuilder;

export type MySqlTableExtraConfig = Record<
string,
MySqlTableExtraConfigValue
>;

export type TableConfig = TableConfigBase<MySqlColumn>;
Expand Down Expand Up @@ -62,7 +65,11 @@ export function mysqlTableWithSchema<
>(
name: TTableName,
columns: TColumnsMap | ((columnTypes: MySqlColumnBuilders) => TColumnsMap),
extraConfig: ((self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfig) | undefined,
extraConfig:
| ((
self: BuildColumns<TTableName, TColumnsMap, 'mysql'>,
) => MySqlTableExtraConfig | MySqlTableExtraConfigValue[])
| undefined,
schema: TSchemaName,
baseName = name,
): MySqlTableWithColumns<{
Expand Down Expand Up @@ -109,13 +116,87 @@ export function mysqlTableWithSchema<
}

export interface MySqlTableFn<TSchemaName extends string | undefined = undefined> {
/**
* @deprecated The third parameter of mysqlTable is changing and will only accept an array instead of an object
*
* @example
* Deprecated version:
* ```ts
* export const users = mysqlTable("users", {
* id: int(),
* }, (t) => ({
* idx: index('custom_name').on(t.id)
* }));
* ```
*
* New API:
* ```ts
* export const users = mysqlTable("users", {
* id: int(),
* }, (t) => [
* index('custom_name').on(t.id)
* ]);
* ```
*/
<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfig,
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;

/**
* @deprecated The third parameter of mysqlTable is changing and will only accept an array instead of an object
*
* @example
* Deprecated version:
* ```ts
* export const users = mysqlTable("users", {
* id: int(),
* }, (t) => ({
* idx: index('custom_name').on(t.id)
* }));
* ```
*
* New API:
* ```ts
* export const users = mysqlTable("users", {
* id: int(),
* }, (t) => [
* index('custom_name').on(t.id)
* ]);
* ```
*/
<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: (columnTypes: MySqlColumnBuilders) => TColumnsMap,
extraConfig: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfig,
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'mysql'>;
dialect: 'mysql';
}>;

<
TTableName extends string,
TColumnsMap extends Record<string, MySqlColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfig,
extraConfig?: (
self: BuildColumns<TTableName, TColumnsMap, 'mysql'>,
) => MySqlTableExtraConfigValue[],
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
Expand All @@ -129,7 +210,7 @@ export interface MySqlTableFn<TSchemaName extends string | undefined = undefined
>(
name: TTableName,
columns: (columnTypes: MySqlColumnBuilders) => TColumnsMap,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfig,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'mysql'>) => MySqlTableExtraConfigValue[],
): MySqlTableWithColumns<{
name: TTableName;
schema: TSchemaName;
Expand Down
3 changes: 2 additions & 1 deletion drizzle-orm/src/mysql-core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function getTableConfig(table: MySqlTable) {

if (extraConfigBuilder !== undefined) {
const extraConfig = extraConfigBuilder(table[MySqlTable.Symbol.Columns]);
for (const builder of Object.values(extraConfig)) {
const extraValues = Array.isArray(extraConfig) ? extraConfig.flat(1) as any[] : Object.values(extraConfig);
for (const builder of Object.values(extraValues)) {
if (is(builder, IndexBuilder)) {
indexes.push(builder.build(table));
} else if (is(builder, CheckBuilder)) {
Expand Down
42 changes: 40 additions & 2 deletions drizzle-orm/src/pg-core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,26 @@ export function pgTableWithSchema<

export interface PgTableFn<TSchema extends string | undefined = undefined> {
/**
* @deprecated This overload is deprecated. Use the other method overload instead.
* @deprecated The third parameter of pgTable is changing and will only accept an array instead of an object
*
* @example
* Deprecated version:
* ```ts
* export const users = pgTable("users", {
* id: integer(),
* }, (t) => ({
* idx: index('custom_name').on(t.id)
* }));
* ```
*
* New API:
* ```ts
* export const users = pgTable("users", {
* id: integer(),
* }, (t) => [
* index('custom_name').on(t.id)
* ]);
* ```
*/
<
TTableName extends string,
Expand All @@ -154,7 +173,26 @@ export interface PgTableFn<TSchema extends string | undefined = undefined> {
}>;

/**
* @deprecated This overload is deprecated. Use the other method overload instead.
* @deprecated The third parameter of pgTable is changing and will only accept an array instead of an object
*
* @example
* Deprecated version:
* ```ts
* export const users = pgTable("users", {
* id: integer(),
* }, (t) => ({
* idx: index('custom_name').on(t.id)
* }));
* ```
*
* New API:
* ```ts
* export const users = pgTable("users", {
* id: integer(),
* }, (t) => [
* index('custom_name').on(t.id)
* ]);
* ```
*/
<
TTableName extends string,
Expand Down
87 changes: 83 additions & 4 deletions drizzle-orm/src/singlestore-core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import type { AnyIndexBuilder } from './indexes.ts';
import type { PrimaryKeyBuilder } from './primary-keys.ts';
import type { UniqueConstraintBuilder } from './unique-constraint.ts';

export type SingleStoreTableExtraConfig = Record<
string,
export type SingleStoreTableExtraConfigValue =
| AnyIndexBuilder
| PrimaryKeyBuilder
| UniqueConstraintBuilder
| UniqueConstraintBuilder;

export type SingleStoreTableExtraConfig = Record<
string,
SingleStoreTableExtraConfigValue
>;

export type TableConfig = TableConfigBase<SingleStoreColumn>;
Expand Down Expand Up @@ -51,7 +54,9 @@ export function singlestoreTableWithSchema<
name: TTableName,
columns: TColumnsMap | ((columnTypes: SingleStoreColumnBuilders) => TColumnsMap),
extraConfig:
| ((self: BuildColumns<TTableName, TColumnsMap, 'singlestore'>) => SingleStoreTableExtraConfig)
| ((
self: BuildColumns<TTableName, TColumnsMap, 'singlestore'>,
) => SingleStoreTableExtraConfig | SingleStoreTableExtraConfigValue[])
| undefined,
schema: TSchemaName,
baseName = name,
Expand Down Expand Up @@ -98,6 +103,28 @@ export function singlestoreTableWithSchema<
}

export interface SingleStoreTableFn<TSchemaName extends string | undefined = undefined> {
/**
* @deprecated The third parameter of singlestoreTable is changing and will only accept an array instead of an object
*
* @example
* Deprecated version:
* ```ts
* export const users = singlestoreTable("users", {
* id: int(),
* }, (t) => ({
* idx: index('custom_name').on(t.id)
* }));
* ```
*
* New API:
* ```ts
* export const users = singlestoreTable("users", {
* id: int(),
* }, (t) => [
* index('custom_name').on(t.id)
* ]);
* ```
*/
<
TTableName extends string,
TColumnsMap extends Record<string, SingleStoreColumnBuilderBase>,
Expand All @@ -112,6 +139,28 @@ export interface SingleStoreTableFn<TSchemaName extends string | undefined = und
dialect: 'singlestore';
}>;

/**
* @deprecated The third parameter of singlestoreTable is changing and will only accept an array instead of an object
*
* @example
* Deprecated version:
* ```ts
* export const users = singlestoreTable("users", {
* id: int(),
* }, (t) => ({
* idx: index('custom_name').on(t.id)
* }));
* ```
*
* New API:
* ```ts
* export const users = singlestoreTable("users", {
* id: int(),
* }, (t) => [
* index('custom_name').on(t.id)
* ]);
* ```
*/
<
TTableName extends string,
TColumnsMap extends Record<string, SingleStoreColumnBuilderBase>,
Expand All @@ -125,6 +174,36 @@ export interface SingleStoreTableFn<TSchemaName extends string | undefined = und
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}>;

<
TTableName extends string,
TColumnsMap extends Record<string, SingleStoreColumnBuilderBase>,
>(
name: TTableName,
columns: TColumnsMap,
extraConfig?: (
self: BuildColumns<TTableName, TColumnsMap, 'singlestore'>,
) => SingleStoreTableExtraConfigValue[],
): SingleStoreTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}>;

<
TTableName extends string,
TColumnsMap extends Record<string, SingleStoreColumnBuilderBase>,
>(
name: TTableName,
columns: (columnTypes: SingleStoreColumnBuilders) => TColumnsMap,
extraConfig?: (self: BuildColumns<TTableName, TColumnsMap, 'singlestore'>) => SingleStoreTableExtraConfigValue[],
): SingleStoreTableWithColumns<{
name: TTableName;
schema: TSchemaName;
columns: BuildColumns<TTableName, TColumnsMap, 'singlestore'>;
dialect: 'singlestore';
}>;
}

export const singlestoreTable: SingleStoreTableFn = (name, columns, extraConfig) => {
Expand Down
3 changes: 2 additions & 1 deletion drizzle-orm/src/singlestore-core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function getTableConfig(table: SingleStoreTable) {

if (extraConfigBuilder !== undefined) {
const extraConfig = extraConfigBuilder(table[SingleStoreTable.Symbol.Columns]);
for (const builder of Object.values(extraConfig)) {
const extraValues = Array.isArray(extraConfig) ? extraConfig.flat(1) as any[] : Object.values(extraConfig);
for (const builder of Object.values(extraValues)) {
if (is(builder, IndexBuilder)) {
indexes.push(builder.build(table));
} else if (is(builder, UniqueConstraintBuilder)) {
Expand Down
Loading

0 comments on commit 3256029

Please sign in to comment.