Skip to content

Commit 2e64d0b

Browse files
Merge pull request #1085 from drizzle-team/beta
Beta
2 parents 515afb0 + 0d6a8b6 commit 2e64d0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+980
-421
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ dist.new
99
.turbo
1010
.rollup.cache
1111
dist-dts
12+
rollup.config-*.mjs

changelogs/drizzle-orm/0.28.3.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
- 🎉 Added SQLite simplified query API
2+
3+
- 🎉 Added `.$defaultFn()` / `.$default()` methods to column builders
4+
5+
You can specify any logic and any implementation for a function like `cuid()` for runtime defaults. Drizzle won't limit you in the number of implementations you can add.
6+
7+
> Note: This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`
8+
9+
```ts
10+
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";
11+
import { createId } from '@paralleldrive/cuid2';
12+
13+
const table = mysqlTable('table', {
14+
id: varchar('id', { length: 128 }).$defaultFn(() => createId()),
15+
});
16+
```
17+
18+
- 🎉 Added `table.$inferSelect` / `table._.inferSelect` and `table.$inferInsert` / `table._.inferInsert` for more convenient table model type inference
19+
20+
- 🛠 Deprecated `InferModel` type in favor of more explicit `InferSelectModel` and `InferInsertModel`
21+
22+
```ts
23+
import { InferSelectModel, InferInsertModel } from 'drizzle-orm'
24+
25+
const usersTable = pgTable('users', {
26+
id: serial('id').primaryKey(),
27+
name: text('name').notNull(),
28+
verified: boolean('verified').notNull().default(false),
29+
jsonb: jsonb('jsonb').$type<string[]>(),
30+
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
31+
});
32+
33+
type SelectUser = typeof usersTable.$inferSelect;
34+
type InsertUser = typeof usersTable.$inferInsert;
35+
36+
type SelectUser2 = InferSelectModel<typeof usersTable>;
37+
type InsertUser2 = InferInsertModel<typeof usersTable>;
38+
```
39+
40+
- 🛠 Disabled `.d.ts` files bundling
41+
- 🐛 Fixed sqlite-proxy and SQL.js response from `.get()` when the result is empty

drizzle-orm/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-orm",
3-
"version": "0.28.2",
3+
"version": "0.28.3",
44
"description": "Drizzle ORM package for SQL databases",
55
"type": "module",
66
"scripts": {
@@ -141,11 +141,13 @@
141141
"better-sqlite3": "^8.4.0",
142142
"bun-types": "^0.6.6",
143143
"concurrently": "^8.1.0",
144+
"cpy-cli": "^5.0.0",
144145
"knex": "^2.4.2",
145146
"kysely": "^0.25.0",
146147
"mysql2": "^3.3.3",
147148
"pg": "^8.11.0",
148149
"postgres": "^3.3.5",
150+
"rimraf": "^5.0.0",
149151
"rollup": "^3.27.2",
150152
"rollup-plugin-dts": "^5.3.1",
151153
"sql.js": "^1.8.0",

drizzle-orm/rollup.cjs.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default defineConfig([
3131
typescript({
3232
tsconfig: 'tsconfig.cjs.json',
3333
outputToFilesystem: true,
34+
incremental: false,
3435
}),
3536
],
3637
},

drizzle-orm/rollup.dts.config.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

drizzle-orm/rollup.esm.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default defineConfig([
2626
typescript({
2727
tsconfig: 'tsconfig.esm.json',
2828
outputToFilesystem: true,
29+
incremental: false,
2930
}),
3031
],
3132
},

drizzle-orm/scripts/build.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env -S pnpm tsx
22
import 'zx/globals';
33
import concurrently from 'concurrently';
4+
45
import { entries } from '../rollup.common';
56

67
function updateAndCopyPackageJson() {
@@ -36,9 +37,8 @@ await concurrently([
3637
name: 'esm',
3738
},
3839
{
39-
command: `tsc -p tsconfig.esm.json --declaration --outDir dist-dts --emitDeclarationOnly &&
40-
resolve-tspaths --out dist-dts &&
41-
rollup --config rollup.dts.config.ts --configPlugin typescript`,
40+
command:
41+
`rimraf dist-dts && tsc -p tsconfig.dts.json --declaration --outDir dist-dts --emitDeclarationOnly && resolve-tspaths --out dist-dts && cpy 'dist-dts/**/*' dist.new && rimraf dist-dts`,
4242
name: 'dts',
4343
},
4444
], {

drizzle-orm/src/column-builder.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export type ColumnBuilderRuntimeConfig<TData, TRuntimeConfig extends object = ob
6565
name: string;
6666
notNull: boolean;
6767
default: TData | SQL | undefined;
68+
defaultFn: (() => TData | SQL) | undefined;
6869
hasDefault: boolean;
6970
primaryKey: boolean;
7071
isUnique: boolean;
@@ -98,7 +99,9 @@ export type $Type<T extends ColumnBuilder, TType> = T & {
9899

99100
// To understand how to use `ColumnBuilder` and `AnyColumnBuilder`, see `Column` and `AnyColumn` documentation.
100101
export abstract class ColumnBuilder<
101-
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string>,
102+
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string> & {
103+
data: any;
104+
},
102105
TRuntimeConfig extends object = object,
103106
TTypeConfig extends object = object,
104107
TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig,
@@ -124,21 +127,66 @@ export abstract class ColumnBuilder<
124127
} as ColumnBuilderRuntimeConfig<T['data'], TRuntimeConfig>;
125128
}
126129

130+
/**
131+
* Changes the data type of the column. Commonly used with `json` columns. Also, useful for branded types.
132+
*
133+
* @example
134+
* ```ts
135+
* const users = pgTable('users', {
136+
* id: integer('id').$type<UserId>().primaryKey(),
137+
* details: json('details').$type<UserDetails>().notNull(),
138+
* });
139+
* ```
140+
*/
127141
$type<TType>(): $Type<this, TType> {
128142
return this as $Type<this, TType>;
129143
}
130144

145+
/**
146+
* Adds a `not null` clause to the column definition.
147+
*
148+
* Affects the `select` model of the table - columns *without* `not null` will be nullable on select.
149+
*/
131150
notNull(): NotNull<this> {
132151
this.config.notNull = true;
133152
return this as NotNull<this>;
134153
}
135154

155+
/**
156+
* Adds a `default <value>` clause to the column definition.
157+
*
158+
* Affects the `insert` model of the table - columns *with* `default` are optional on insert.
159+
*
160+
* If you need to set a dynamic default value, use {@link $defaultFn} instead.
161+
*/
136162
default(value: (this['_'] extends { $type: infer U } ? U : T['data']) | SQL): HasDefault<this> {
137163
this.config.default = value;
138164
this.config.hasDefault = true;
139165
return this as HasDefault<this>;
140166
}
141167

168+
/**
169+
* Adds a dynamic default value to the column.
170+
* The function will be called when the row is inserted, and the returned value will be used as the column value.
171+
*
172+
* **Note:** This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`.
173+
*/
174+
$defaultFn(fn: () => (this['_'] extends { $type: infer U } ? U : T['data']) | SQL): HasDefault<this> {
175+
this.config.defaultFn = fn;
176+
this.config.hasDefault = true;
177+
return this as HasDefault<this>;
178+
}
179+
180+
/**
181+
* Alias for {@link $defaultFn}.
182+
*/
183+
$default = this.$defaultFn;
184+
185+
/**
186+
* Adds a `primary key` clause to the column definition. This implicitly makes the column `not null`.
187+
*
188+
* In SQLite, `integer primary key` implicitly makes the column auto-incrementing.
189+
*/
142190
primaryKey(): TExtraConfig['primaryKeyHasDefault'] extends true ? HasDefault<NotNull<this>> : NotNull<this> {
143191
this.config.primaryKey = true;
144192
this.config.notNull = true;

drizzle-orm/src/column.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export abstract class Column<
5757
readonly primary: boolean;
5858
readonly notNull: boolean;
5959
readonly default: T['data'] | SQL | undefined;
60+
readonly defaultFn: (() => T['data'] | SQL) | undefined;
6061
readonly hasDefault: boolean;
6162
readonly isUnique: boolean;
6263
readonly uniqueName: string | undefined;
@@ -75,6 +76,7 @@ export abstract class Column<
7576
this.name = config.name;
7677
this.notNull = config.notNull;
7778
this.default = config.default;
79+
this.defaultFn = config.defaultFn;
7880
this.hasDefault = config.hasDefault;
7981
this.primary = config.primaryKey;
8082
this.isUnique = config.isUnique;

drizzle-orm/src/mysql-core/checks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { entityKind } from '~/entity';
22
import type { SQL } from '~/sql';
3-
import type { AnyMySqlTable } from './table';
3+
import type { MySqlTable } from './table';
44

55
export class CheckBuilder {
66
static readonly [entityKind]: string = 'MySqlCheckBuilder';
@@ -10,7 +10,7 @@ export class CheckBuilder {
1010
constructor(public name: string, public value: SQL) {}
1111

1212
/** @internal */
13-
build(table: AnyMySqlTable): Check {
13+
build(table: MySqlTable): Check {
1414
return new Check(table, this);
1515
}
1616
}
@@ -21,7 +21,7 @@ export class Check {
2121
readonly name: string;
2222
readonly value: SQL;
2323

24-
constructor(public table: AnyMySqlTable, builder: CheckBuilder) {
24+
constructor(public table: MySqlTable, builder: CheckBuilder) {
2525
this.name = builder.name;
2626
this.value = builder.value;
2727
}

drizzle-orm/src/mysql-core/columns/common.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ import { type Update } from '~/utils';
1717
import { uniqueKeyName } from '../unique-constraint';
1818

1919
export interface ReferenceConfig {
20-
ref: () => AnyMySqlColumn;
20+
ref: () => MySqlColumn;
2121
actions: {
2222
onUpdate?: UpdateDeleteAction;
2323
onDelete?: UpdateDeleteAction;
2424
};
2525
}
2626

2727
export abstract class MySqlColumnBuilder<
28-
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string>,
28+
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string> & {
29+
data: any;
30+
},
2931
TRuntimeConfig extends object = object,
3032
TTypeConfig extends object = object,
3133
TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig,
@@ -46,7 +48,7 @@ export abstract class MySqlColumnBuilder<
4648
}
4749

4850
/** @internal */
49-
buildForeignKeys(column: AnyMySqlColumn, table: AnyMySqlTable): ForeignKey[] {
51+
buildForeignKeys(column: MySqlColumn, table: MySqlTable): ForeignKey[] {
5052
return this.foreignKeyConfigs.map(({ ref, actions }) => {
5153
return ((ref, actions) => {
5254
const builder = new ForeignKeyBuilder(() => {

drizzle-orm/src/mysql-core/db.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type {
2626
QueryResultKind,
2727
} from './session';
2828
import type { WithSubqueryWithSelection } from './subquery';
29-
import type { AnyMySqlTable } from './table';
29+
import type { MySqlTable } from './table';
3030

3131
export class MySqlDatabase<
3232
TQueryResult extends QueryResultHKT,
@@ -66,7 +66,7 @@ export class MySqlDatabase<
6666
schema!.fullSchema,
6767
this._.schema,
6868
this._.tableNamesMap,
69-
schema!.fullSchema[tableName] as AnyMySqlTable,
69+
schema!.fullSchema[tableName] as MySqlTable,
7070
columns,
7171
dialect,
7272
session,
@@ -147,15 +147,15 @@ export class MySqlDatabase<
147147
});
148148
}
149149

150-
update<TTable extends AnyMySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT> {
150+
update<TTable extends MySqlTable>(table: TTable): MySqlUpdateBuilder<TTable, TQueryResult, TPreparedQueryHKT> {
151151
return new MySqlUpdateBuilder(table, this.session, this.dialect);
152152
}
153153

154-
insert<TTable extends AnyMySqlTable>(table: TTable): MySqlInsertBuilder<TTable, TQueryResult, TPreparedQueryHKT> {
154+
insert<TTable extends MySqlTable>(table: TTable): MySqlInsertBuilder<TTable, TQueryResult, TPreparedQueryHKT> {
155155
return new MySqlInsertBuilder(table, this.session, this.dialect);
156156
}
157157

158-
delete<TTable extends AnyMySqlTable>(table: TTable): MySqlDelete<TTable, TQueryResult, TPreparedQueryHKT> {
158+
delete<TTable extends MySqlTable>(table: TTable): MySqlDelete<TTable, TQueryResult, TPreparedQueryHKT> {
159159
return new MySqlDelete(table, this.session, this.dialect);
160160
}
161161

0 commit comments

Comments
 (0)