Skip to content

Commit

Permalink
Merge pull request #1085 from drizzle-team/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
AndriiSherman authored Aug 22, 2023
2 parents 515afb0 + 0d6a8b6 commit 2e64d0b
Show file tree
Hide file tree
Showing 86 changed files with 980 additions and 421 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist.new
.turbo
.rollup.cache
dist-dts
rollup.config-*.mjs
41 changes: 41 additions & 0 deletions changelogs/drizzle-orm/0.28.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
- 🎉 Added SQLite simplified query API

- 🎉 Added `.$defaultFn()` / `.$default()` methods to column builders

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.

> Note: This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`
```ts
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";
import { createId } from '@paralleldrive/cuid2';

const table = mysqlTable('table', {
id: varchar('id', { length: 128 }).$defaultFn(() => createId()),
});
```

- 🎉 Added `table.$inferSelect` / `table._.inferSelect` and `table.$inferInsert` / `table._.inferInsert` for more convenient table model type inference

- 🛠 Deprecated `InferModel` type in favor of more explicit `InferSelectModel` and `InferInsertModel`

```ts
import { InferSelectModel, InferInsertModel } from 'drizzle-orm'

const usersTable = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
verified: boolean('verified').notNull().default(false),
jsonb: jsonb('jsonb').$type<string[]>(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});

type SelectUser = typeof usersTable.$inferSelect;
type InsertUser = typeof usersTable.$inferInsert;

type SelectUser2 = InferSelectModel<typeof usersTable>;
type InsertUser2 = InferInsertModel<typeof usersTable>;
```

- 🛠 Disabled `.d.ts` files bundling
- 🐛 Fixed sqlite-proxy and SQL.js response from `.get()` when the result is empty
4 changes: 3 additions & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.28.2",
"version": "0.28.3",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -141,11 +141,13 @@
"better-sqlite3": "^8.4.0",
"bun-types": "^0.6.6",
"concurrently": "^8.1.0",
"cpy-cli": "^5.0.0",
"knex": "^2.4.2",
"kysely": "^0.25.0",
"mysql2": "^3.3.3",
"pg": "^8.11.0",
"postgres": "^3.3.5",
"rimraf": "^5.0.0",
"rollup": "^3.27.2",
"rollup-plugin-dts": "^5.3.1",
"sql.js": "^1.8.0",
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/rollup.cjs.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default defineConfig([
typescript({
tsconfig: 'tsconfig.cjs.json',
outputToFilesystem: true,
incremental: false,
}),
],
},
Expand Down
26 changes: 0 additions & 26 deletions drizzle-orm/rollup.dts.config.ts

This file was deleted.

1 change: 1 addition & 0 deletions drizzle-orm/rollup.esm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default defineConfig([
typescript({
tsconfig: 'tsconfig.esm.json',
outputToFilesystem: true,
incremental: false,
}),
],
},
Expand Down
6 changes: 3 additions & 3 deletions drizzle-orm/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env -S pnpm tsx
import 'zx/globals';
import concurrently from 'concurrently';

import { entries } from '../rollup.common';

function updateAndCopyPackageJson() {
Expand Down Expand Up @@ -36,9 +37,8 @@ await concurrently([
name: 'esm',
},
{
command: `tsc -p tsconfig.esm.json --declaration --outDir dist-dts --emitDeclarationOnly &&
resolve-tspaths --out dist-dts &&
rollup --config rollup.dts.config.ts --configPlugin typescript`,
command:
`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`,
name: 'dts',
},
], {
Expand Down
50 changes: 49 additions & 1 deletion drizzle-orm/src/column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type ColumnBuilderRuntimeConfig<TData, TRuntimeConfig extends object = ob
name: string;
notNull: boolean;
default: TData | SQL | undefined;
defaultFn: (() => TData | SQL) | undefined;
hasDefault: boolean;
primaryKey: boolean;
isUnique: boolean;
Expand Down Expand Up @@ -98,7 +99,9 @@ export type $Type<T extends ColumnBuilder, TType> = T & {

// To understand how to use `ColumnBuilder` and `AnyColumnBuilder`, see `Column` and `AnyColumn` documentation.
export abstract class ColumnBuilder<
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string>,
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string> & {
data: any;
},
TRuntimeConfig extends object = object,
TTypeConfig extends object = object,
TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig,
Expand All @@ -124,21 +127,66 @@ export abstract class ColumnBuilder<
} as ColumnBuilderRuntimeConfig<T['data'], TRuntimeConfig>;
}

/**
* Changes the data type of the column. Commonly used with `json` columns. Also, useful for branded types.
*
* @example
* ```ts
* const users = pgTable('users', {
* id: integer('id').$type<UserId>().primaryKey(),
* details: json('details').$type<UserDetails>().notNull(),
* });
* ```
*/
$type<TType>(): $Type<this, TType> {
return this as $Type<this, TType>;
}

/**
* Adds a `not null` clause to the column definition.
*
* Affects the `select` model of the table - columns *without* `not null` will be nullable on select.
*/
notNull(): NotNull<this> {
this.config.notNull = true;
return this as NotNull<this>;
}

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

/**
* Adds a dynamic default value to the column.
* The function will be called when the row is inserted, and the returned value will be used as the column value.
*
* **Note:** This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`.
*/
$defaultFn(fn: () => (this['_'] extends { $type: infer U } ? U : T['data']) | SQL): HasDefault<this> {
this.config.defaultFn = fn;
this.config.hasDefault = true;
return this as HasDefault<this>;
}

/**
* Alias for {@link $defaultFn}.
*/
$default = this.$defaultFn;

/**
* Adds a `primary key` clause to the column definition. This implicitly makes the column `not null`.
*
* In SQLite, `integer primary key` implicitly makes the column auto-incrementing.
*/
primaryKey(): TExtraConfig['primaryKeyHasDefault'] extends true ? HasDefault<NotNull<this>> : NotNull<this> {
this.config.primaryKey = true;
this.config.notNull = true;
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export abstract class Column<
readonly primary: boolean;
readonly notNull: boolean;
readonly default: T['data'] | SQL | undefined;
readonly defaultFn: (() => T['data'] | SQL) | undefined;
readonly hasDefault: boolean;
readonly isUnique: boolean;
readonly uniqueName: string | undefined;
Expand All @@ -75,6 +76,7 @@ export abstract class Column<
this.name = config.name;
this.notNull = config.notNull;
this.default = config.default;
this.defaultFn = config.defaultFn;
this.hasDefault = config.hasDefault;
this.primary = config.primaryKey;
this.isUnique = config.isUnique;
Expand Down
6 changes: 3 additions & 3 deletions drizzle-orm/src/mysql-core/checks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { entityKind } from '~/entity';
import type { SQL } from '~/sql';
import type { AnyMySqlTable } from './table';
import type { MySqlTable } from './table';

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

/** @internal */
build(table: AnyMySqlTable): Check {
build(table: MySqlTable): Check {
return new Check(table, this);
}
}
Expand All @@ -21,7 +21,7 @@ export class Check {
readonly name: string;
readonly value: SQL;

constructor(public table: AnyMySqlTable, builder: CheckBuilder) {
constructor(public table: MySqlTable, builder: CheckBuilder) {
this.name = builder.name;
this.value = builder.value;
}
Expand Down
8 changes: 5 additions & 3 deletions drizzle-orm/src/mysql-core/columns/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import { type Update } from '~/utils';
import { uniqueKeyName } from '../unique-constraint';

export interface ReferenceConfig {
ref: () => AnyMySqlColumn;
ref: () => MySqlColumn;
actions: {
onUpdate?: UpdateDeleteAction;
onDelete?: UpdateDeleteAction;
};
}

export abstract class MySqlColumnBuilder<
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string>,
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string> & {
data: any;
},
TRuntimeConfig extends object = object,
TTypeConfig extends object = object,
TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig,
Expand All @@ -46,7 +48,7 @@ export abstract class MySqlColumnBuilder<
}

/** @internal */
buildForeignKeys(column: AnyMySqlColumn, table: AnyMySqlTable): ForeignKey[] {
buildForeignKeys(column: MySqlColumn, table: MySqlTable): ForeignKey[] {
return this.foreignKeyConfigs.map(({ ref, actions }) => {
return ((ref, actions) => {
const builder = new ForeignKeyBuilder(() => {
Expand Down
10 changes: 5 additions & 5 deletions drizzle-orm/src/mysql-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {
QueryResultKind,
} from './session';
import type { WithSubqueryWithSelection } from './subquery';
import type { AnyMySqlTable } from './table';
import type { MySqlTable } from './table';

export class MySqlDatabase<
TQueryResult extends QueryResultHKT,
Expand Down Expand Up @@ -66,7 +66,7 @@ export class MySqlDatabase<
schema!.fullSchema,
this._.schema,
this._.tableNamesMap,
schema!.fullSchema[tableName] as AnyMySqlTable,
schema!.fullSchema[tableName] as MySqlTable,
columns,
dialect,
session,
Expand Down Expand Up @@ -147,15 +147,15 @@ export class MySqlDatabase<
});
}

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

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

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

Expand Down
Loading

0 comments on commit 2e64d0b

Please sign in to comment.