Skip to content

Commit

Permalink
Merge branch 'beta' into remove-initializer-import-check
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiSherman authored Nov 21, 2024
2 parents 6a28660 + 236368e commit 188be8e
Show file tree
Hide file tree
Showing 84 changed files with 151,058 additions and 7,594 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-seed
- drizzle-typebox
- drizzle-valibot
- eslint-plugin-drizzle
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-seed
- drizzle-typebox
- drizzle-valibot
- eslint-plugin-drizzle
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dist-dts
rollup.config-*.mjs
*.log
.DS_Store
drizzle-seed/src/test.ts
drizzle-seed/src/schemaTest.ts
4 changes: 2 additions & 2 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.36.3",
"version": "0.36.4",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -203,4 +203,4 @@
"zod": "^3.20.2",
"zx": "^7.2.2"
}
}
}
7 changes: 4 additions & 3 deletions drizzle-orm/src/column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type MakeColumnConfig<
enumValues: T['enumValues'];
baseColumn: T extends { baseBuilder: infer U extends ColumnBuilderBase } ? BuildColumn<TTableName, U, 'common'>
: never;
identity: T extends { identity: 'always' } ? 'always' : T extends { identity: 'byDefault' } ? 'byDefault' : undefined;
generated: T extends { generated: infer G } ? unknown extends G ? undefined
: G extends undefined ? undefined
: G
Expand All @@ -84,6 +85,7 @@ export type ColumnBuilderTypeConfig<
notNull: T extends { notNull: infer U } ? U : boolean;
hasDefault: T extends { hasDefault: infer U } ? U : boolean;
enumValues: T['enumValues'];
identity: T extends { identity: infer U } ? U : unknown;
generated: T extends { generated: infer G } ? G extends undefined ? unknown : G : unknown;
}
& TTypeConfig
Expand Down Expand Up @@ -154,17 +156,16 @@ export type HasGenerated<T extends ColumnBuilderBase, TGenerated extends {} = {}
};
};

export type IsIdentityByDefault<
export type IsIdentity<
T extends ColumnBuilderBase,
TType extends 'always' | 'byDefault',
> = T & {
_: {
notNull: true;
hasDefault: true;
generated: { as: any; type: TType };
identity: TType;
};
};

export interface ColumnBuilderBase<
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string>,
TTypeConfig extends object = object,
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type ColumnTypeConfig<T extends ColumnBaseConfig<ColumnDataType, string>,
enumValues: T['enumValues'];
baseColumn: T extends { baseColumn: infer U } ? U : unknown;
generated: GeneratedColumnConfig<T['data']> | undefined;
identity: undefined | 'always' | 'byDefault';
} & TTypeConfig;

export type ColumnRuntimeConfig<TData, TRuntimeConfig extends object> = ColumnBuilderRuntimeConfig<
Expand Down
10 changes: 6 additions & 4 deletions drizzle-orm/src/operations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { AnyColumn, Column } from './column.ts';
import type { GeneratedColumnConfig } from './index.ts';
import type { SQL } from './sql/sql.ts';
import type { Table } from './table.ts';

Expand All @@ -12,13 +11,16 @@ export type RequiredKeyOnly<TKey extends string, T extends Column> = T extends A
export type OptionalKeyOnly<
TKey extends string,
T extends Column,
OverrideT extends boolean | undefined = false,
> = TKey extends RequiredKeyOnly<TKey, T> ? never
: T extends {
_: {
generated: infer G;
generated: undefined;
};
} ? G extends GeneratedColumnConfig<any> ? G['type'] extends 'always' ? never : TKey
: TKey
} ? (
T['_']['identity'] extends 'always' ? OverrideT extends true ? TKey : never
: TKey
)
: never;

// TODO: SQL -> SQLWrapper
Expand Down
2 changes: 0 additions & 2 deletions drizzle-orm/src/pg-core/columns/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
ColumnBuilderExtraConfig,
ColumnBuilderRuntimeConfig,
ColumnDataType,
GeneratedColumnConfig,
HasGenerated,
MakeColumnConfig,
} from '~/column-builder.ts';
Expand Down Expand Up @@ -56,7 +55,6 @@ export abstract class PgColumnBuilder<
data: T['data'][];
driverParam: T['driverParam'][] | string;
enumValues: T['enumValues'];
generated: GeneratedColumnConfig<T['data']>;
}
& (T extends { notNull: true } ? { notNull: true } : {})
& (T extends { hasDefault: true } ? { hasDefault: true } : {}),
Expand Down
15 changes: 5 additions & 10 deletions drizzle-orm/src/pg-core/columns/int.common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
ColumnBuilderBaseConfig,
ColumnDataType,
GeneratedIdentityConfig,
IsIdentityByDefault,
} from '~/column-builder.ts';
import type { ColumnBuilderBaseConfig, ColumnDataType, GeneratedIdentityConfig, IsIdentity } from '~/column-builder.ts';
import { entityKind } from '~/entity.ts';
import type { PgSequenceOptions } from '../sequence.ts';
import { PgColumnBuilder } from './common.ts';
Expand All @@ -18,7 +13,7 @@ export abstract class PgIntColumnBaseBuilder<

generatedAlwaysAsIdentity(
sequence?: PgSequenceOptions & { name?: string },
): IsIdentityByDefault<this, 'always'> {
): IsIdentity<this, 'always'> {
if (sequence) {
const { name, ...options } = sequence;
this.config.generatedIdentity = {
Expand All @@ -35,12 +30,12 @@ export abstract class PgIntColumnBaseBuilder<
this.config.hasDefault = true;
this.config.notNull = true;

return this as any;
return this as IsIdentity<this, 'always'>;
}

generatedByDefaultAsIdentity(
sequence?: PgSequenceOptions & { name?: string },
): IsIdentityByDefault<this, 'byDefault'> {
): IsIdentity<this, 'byDefault'> {
if (sequence) {
const { name, ...options } = sequence;
this.config.generatedIdentity = {
Expand All @@ -57,6 +52,6 @@ export abstract class PgIntColumnBaseBuilder<
this.config.hasDefault = true;
this.config.notNull = true;

return this as any;
return this as IsIdentity<this, 'byDefault'>;
}
}
12 changes: 9 additions & 3 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ export class PgDialect {
return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
}

buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select }: PgInsertConfig): SQL {
buildInsertQuery(
{ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }: PgInsertConfig,
): SQL {
const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = [];
const columns: Record<string, PgColumn> = table[Table.Symbol.Columns];

Expand Down Expand Up @@ -553,7 +555,9 @@ export class PgDialect {

const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : undefined;

return sql`${withSql}insert into ${table} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
const overridingSql = overridingSystemValue_ === true ? sql`overriding system value ` : undefined;

return sql`${withSql}insert into ${table} ${insertOrder} ${overridingSql}${valuesSql}${onConflictSql}${returningSql}`;
}

buildRefreshMaterializedViewQuery(
Expand Down Expand Up @@ -1163,7 +1167,9 @@ export class PgDialect {
}));
} else {
const aliasedColumns = Object.fromEntries(
Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]),
Object.entries(tableConfig.columns).map((
[key, value],
) => [key, aliasedTableColumn(value, tableAlias)]),
);

if (config.where) {
Expand Down
44 changes: 35 additions & 9 deletions drizzle-orm/src/pg-core/query-builders/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import type {
PgSession,
PreparedQueryConfig,
} from '~/pg-core/session.ts';
import type { PgTable } from '~/pg-core/table.ts';
import type { PgTable, TableConfig } from '~/pg-core/table.ts';
import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';
import type { SelectResultFields } from '~/query-builders/select.types.ts';
import { QueryPromise } from '~/query-promise.ts';
import type { RunnableQuery } from '~/runnable-query.ts';
import type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts';
import { Param, SQL, sql } from '~/sql/sql.ts';
import type { Subquery } from '~/subquery.ts';
import type { InferInsertModel } from '~/table.ts';
import { Columns, Table } from '~/table.ts';
import { tracer } from '~/tracing.ts';
import { haveSameKeys, mapUpdateSet, orderSelectedFields } from '~/utils.ts';
Expand All @@ -31,31 +32,47 @@ export interface PgInsertConfig<TTable extends PgTable = PgTable> {
onConflict?: SQL;
returning?: SelectedFieldsOrdered;
select?: boolean;
overridingSystemValue_?: boolean;
}

export type PgInsertValue<TTable extends PgTable> =
export type PgInsertValue<TTable extends PgTable<TableConfig>, OverrideT extends boolean = false> =
& {
[Key in keyof TTable['$inferInsert']]: TTable['$inferInsert'][Key] | SQL | Placeholder;
[Key in keyof InferInsertModel<TTable, { dbColumnNames: false; override: OverrideT }>]:
| InferInsertModel<TTable, { dbColumnNames: false; override: OverrideT }>[Key]
| SQL
| Placeholder;
}
& {};

export type PgInsertSelectQueryBuilder<TTable extends PgTable> = TypedQueryBuilder<
{ [K in keyof TTable['$inferInsert']]: AnyPgColumn | SQL | SQL.Aliased | TTable['$inferInsert'][K] }
>;

export class PgInsertBuilder<TTable extends PgTable, TQueryResult extends PgQueryResultHKT> {
export class PgInsertBuilder<
TTable extends PgTable,
TQueryResult extends PgQueryResultHKT,
OverrideT extends boolean = false,
> {
static readonly [entityKind]: string = 'PgInsertBuilder';

constructor(
private table: TTable,
private session: PgSession,
private dialect: PgDialect,
private withList?: Subquery[],
private overridingSystemValue_?: boolean,
) {}

values(value: PgInsertValue<TTable>): PgInsertBase<TTable, TQueryResult>;
values(values: PgInsertValue<TTable>[]): PgInsertBase<TTable, TQueryResult>;
values(values: PgInsertValue<TTable> | PgInsertValue<TTable>[]): PgInsertBase<TTable, TQueryResult> {
overridingSystemValue(): Omit<PgInsertBuilder<TTable, TQueryResult, true>, 'overridingSystemValue'> {
this.overridingSystemValue_ = true;
return this as any;
}

values(value: PgInsertValue<TTable, OverrideT>): PgInsertBase<TTable, TQueryResult>;
values(values: PgInsertValue<TTable, OverrideT>[]): PgInsertBase<TTable, TQueryResult>;
values(
values: PgInsertValue<TTable, OverrideT> | PgInsertValue<TTable, OverrideT>[],
): PgInsertBase<TTable, TQueryResult> {
values = Array.isArray(values) ? values : [values];
if (values.length === 0) {
throw new Error('values() must be called with at least one value');
Expand All @@ -70,7 +87,15 @@ export class PgInsertBuilder<TTable extends PgTable, TQueryResult extends PgQuer
return result;
});

return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
return new PgInsertBase(
this.table,
mappedValues,
this.session,
this.dialect,
this.withList,
false,
this.overridingSystemValue_,
);
}

select(selectQuery: (qb: QueryBuilder) => PgInsertSelectQueryBuilder<TTable>): PgInsertBase<TTable, TQueryResult>;
Expand Down Expand Up @@ -208,9 +233,10 @@ export class PgInsertBase<
private dialect: PgDialect,
withList?: Subquery[],
select?: boolean,
overridingSystemValue_?: boolean,
) {
super();
this.config = { table, values: values as any, withList, select };
this.config = { table, values: values as any, withList, select, overridingSystemValue_ };
}

/**
Expand Down
7 changes: 4 additions & 3 deletions drizzle-orm/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export type MapColumnName<TName extends string, TColumn extends Column, TDBColum
export type InferModelFromColumns<
TColumns extends Record<string, Column>,
TInferMode extends 'select' | 'insert' = 'select',
TConfig extends { dbColumnNames: boolean } = { dbColumnNames: false },
TConfig extends { dbColumnNames: boolean; override?: boolean } = { dbColumnNames: false; override: false },
> = Simplify<
TInferMode extends 'insert' ?
& {
Expand All @@ -171,7 +171,8 @@ export type InferModelFromColumns<
[
Key in keyof TColumns & string as OptionalKeyOnly<
MapColumnName<Key, TColumns[Key], TConfig['dbColumnNames']>,
TColumns[Key]
TColumns[Key],
TConfig['override']
>
]?: GetColumnData<TColumns[Key], 'query'>;
}
Expand Down Expand Up @@ -201,5 +202,5 @@ export type InferSelectModel<

export type InferInsertModel<
TTable extends Table,
TConfig extends { dbColumnNames: boolean } = { dbColumnNames: false },
TConfig extends { dbColumnNames: boolean; override?: boolean } = { dbColumnNames: false; override: false },
> = InferModelFromColumns<TTable['_']['columns'], 'insert', TConfig>;
Loading

0 comments on commit 188be8e

Please sign in to comment.