Skip to content

Commit

Permalink
Merge pull request #778 from drizzle-team/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
AndriiSherman authored Jun 17, 2023
2 parents af6149d + bc04f91 commit fc84088
Show file tree
Hide file tree
Showing 189 changed files with 3,477 additions and 1,394 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ extends:
- 'plugin:@typescript-eslint/recommended'
- 'plugin:unicorn/recommended'
parser: '@typescript-eslint/parser'
parserOptions:
project: './tsconfig.json'
plugins:
- import
- unused-imports
- no-instanceof
- drizzle
rules:
'@typescript-eslint/consistent-type-imports':
- error
Expand Down Expand Up @@ -55,3 +59,6 @@ rules:
'unicorn/prefer-type-error': 'off'
'unicorn/relative-url-style': 'off'
'eqeqeq': 'error'
'no-instanceof/no-instanceof': 'error'
'drizzle/require-entity-kind': 'error'
'unicorn/prefer-string-replace-all': 'off'
3 changes: 1 addition & 2 deletions .github/workflows/release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ jobs:
npm dist-tag add ${{ matrix.package }}@$version $tag
else
echo "Publishing ${{ matrix.package }}@$tag using version $version"
npm version $version
cp package.json dist/package.json
(cd dist && npm version $version)
npm run pack
npm run publish -- --tag $tag
fi
Expand Down
58 changes: 58 additions & 0 deletions changelogs/drizzle-orm/0.27.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## Correct behavior when installed in a monorepo (multiple Drizzle instances)

Replacing all `instanceof` statements with a custom `is()` function allowed us to handle multiple Drizzle packages interacting properly.

**It also fixes one of our biggest Discord tickets: `maximum call stack exceeded` 🎉**

You should now use `is()` instead of `instanceof` to check if specific objects are instances of specific Drizzle types. It might be useful if you are building something on top of the Drizzle API.

```ts
import { is, Column } from 'drizzle-orm'

if (is(value, Column)) {
// value's type is narrowed to Column
}
```

## `distinct` clause support

```ts
await db.selectDistinct().from(usersDistinctTable).orderBy(
usersDistinctTable.id,
usersDistinctTable.name,
);
```

Also, `distinct on` clause is available for PostgreSQL:

```ts
await db.selectDistinctOn([usersDistinctTable.id]).from(usersDistinctTable).orderBy(
usersDistinctTable.id,
);

await db.selectDistinctOn([usersDistinctTable.name], { name: usersDistinctTable.name }).from(
usersDistinctTable,
).orderBy(usersDistinctTable.name);
```

## `bigint` and `boolean` support for SQLite

Contributed by @MrRahulRamkumar (#558), @raducristianpopa (#411) and @meech-ward (#725)

```ts
const users = sqliteTable('users', {
bigintCol: blob('bigint', { mode: 'bigint' }).notNull(),
boolCol: integer('bool', { mode: 'boolean' }).notNull(),
});
```

## DX improvements

- Added verbose type error when relational queries are used on a database type without a schema generic
- Fix `where` callback in RQB for tables without relations

## Various docs improvements

- Fix joins docs typo (#522) by @arjunyel
- Add Supabase guide to readme (#690) by @saltcod
- Make the column type in sqlite clearer (#717) by @shairez
1 change: 1 addition & 0 deletions changelogs/drizzle-zod/0.4.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed drizzle-zod not enforcing string lengths (#691) by @TiltedToast
14 changes: 7 additions & 7 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.26.5",
"version": "0.27.0",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -123,10 +123,10 @@
}
},
"devDependencies": {
"@aws-sdk/client-rds-data": "^3.341.0",
"@aws-sdk/client-rds-data": "^3.344.0",
"@cloudflare/workers-types": "^4.20230518.0",
"@libsql/client": "^0.1.6",
"@neondatabase/serverless": "^0.4.8",
"@neondatabase/serverless": "^0.4.9",
"@opentelemetry/api": "^1.4.1",
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@planetscale/database": "^1.7.0",
Expand All @@ -139,21 +139,21 @@
"@types/sql.js": "^1.4.4",
"@vercel/postgres": "^0.3.0",
"better-sqlite3": "^8.4.0",
"bun-types": "^0.6.4",
"concurrently": "^8.0.1",
"bun-types": "^0.6.6",
"concurrently": "^8.1.0",
"knex": "^2.4.2",
"kysely": "^0.25.0",
"mysql2": "^3.3.3",
"pg": "^8.11.0",
"postgres": "^3.3.3",
"postgres": "^3.3.5",
"rollup": "^3.23.0",
"rollup-plugin-dts": "^5.3.0",
"sql.js": "^1.8.0",
"sqlite3": "^5.1.2",
"tslib": "^2.5.2",
"tsx": "^3.12.7",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.31.1",
"vitest": "^0.31.4",
"zod": "^3.20.2",
"zx": "^7.2.2"
}
Expand Down
4 changes: 3 additions & 1 deletion drizzle-orm/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ resolve-tspaths --out dist-dts &&
rollup --config rollup.dts.config.ts --configPlugin typescript`,
name: 'dts',
},
]).result;
], {
killOthers: 'failure',
}).result.catch(() => process.exit(1));
fs.copySync('../README.md', 'dist.new/README.md');
updateAndCopyPackageJson();
fs.removeSync('dist');
Expand Down
17 changes: 12 additions & 5 deletions drizzle-orm/src/alias.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { AnyColumn } from './column';
import { Column } from './column';
import { entityKind, is } from './entity';
import type { Relation } from './relations';
import { SQL, sql } from './sql';
import { Table } from './table';
import { type View, ViewBaseConfig } from './view';

export class ColumnAliasProxyHandler<TColumn extends AnyColumn> implements ProxyHandler<TColumn> {
static readonly [entityKind]: string = 'ColumnAliasProxyHandler';

constructor(private table: Table | View) {}

get(columnObj: TColumn, prop: string | symbol): any {
Expand All @@ -18,6 +21,8 @@ export class ColumnAliasProxyHandler<TColumn extends AnyColumn> implements Proxy
}

export class TableAliasProxyHandler<T extends Table | View> implements ProxyHandler<T> {
static readonly [entityKind]: string = 'TableAliasProxyHandler';

constructor(private alias: string, private replaceOriginalName: boolean) {}

get(target: T, prop: string | symbol): any {
Expand Down Expand Up @@ -60,15 +65,17 @@ export class TableAliasProxyHandler<T extends Table | View> implements ProxyHand
}

const value = target[prop as keyof typeof target];
if (value instanceof Column) {
return new Proxy(value, new ColumnAliasProxyHandler(new Proxy(target, this)));
if (is(value, Column)) {
return new Proxy(value as AnyColumn, new ColumnAliasProxyHandler(new Proxy(target, this)));
}

return value;
}
}

export class RelationTableAliasProxyHandler<T extends Relation> implements ProxyHandler<T> {
static readonly [entityKind]: string = 'RelationTableAliasProxyHandler';

constructor(private alias: string) {}

get(target: T, prop: string | symbol): any {
Expand Down Expand Up @@ -101,13 +108,13 @@ export function mapColumnsInAliasedSQLToAlias(query: SQL.Aliased, alias: string)

export function mapColumnsInSQLToAlias(query: SQL, alias: string): SQL {
return sql.fromList(query.queryChunks.map((c) => {
if (c instanceof Column) {
if (is(c, Column)) {
return aliasedTableColumn(c, alias);
}
if (c instanceof SQL) {
if (is(c, SQL)) {
return mapColumnsInSQLToAlias(c, alias);
}
if (c instanceof SQL.Aliased) {
if (is(c, SQL.Aliased)) {
return mapColumnsInAliasedSQLToAlias(c, alias);
}
return c;
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/aws-data-api/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function toValueParam(value: any, typings?: QueryTypingsValue): { value:
response.value = { doubleValue: value };
} else if (typeof value === 'boolean') {
response.value = { booleanValue: value };
} else if (value instanceof Date) {
} else if (value instanceof Date) { // eslint-disable-line no-instanceof/no-instanceof
response.value = { stringValue: value.toISOString().replace('T', ' ').replace('Z', '') };
} else {
throw new Error(`Unknown type for ${value}`);
Expand Down
3 changes: 3 additions & 0 deletions drizzle-orm/src/aws-data-api/pg/driver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { entityKind } from '~/entity';
import type { Logger } from '~/logger';
import { DefaultLogger } from '~/logger';
import { PgDatabase } from '~/pg-core/db';
Expand Down Expand Up @@ -32,6 +33,8 @@ export type AwsDataApiPgDatabase<
> = PgDatabase<AwsDataApiPgQueryResultHKT, TSchema>;

export class AwsPgDialect extends PgDialect {
static readonly [entityKind]: string = 'AwsPgDialect';

override escapeParam(num: number): string {
return `:${num + 1}`;
}
Expand Down
7 changes: 7 additions & 0 deletions drizzle-orm/src/aws-data-api/pg/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ExecuteStatementCommand,
RollbackTransactionCommand,
} from '@aws-sdk/client-rds-data';
import { entityKind } from '~/entity';
import type { Logger } from '~/logger';
import {
type PgDialect,
Expand All @@ -24,6 +25,8 @@ import { getValueFromDataApi, toValueParam } from '../common';
export type AwsDataApiClient = RDSDataClient;

export class AwsDataApiPreparedQuery<T extends PreparedQueryConfig> extends PreparedQuery<T> {
static readonly [entityKind]: string = 'AwsDataApiPreparedQuery';

private rawQuery: ExecuteStatementCommand;

constructor(
Expand Down Expand Up @@ -105,6 +108,8 @@ export class AwsDataApiSession<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends PgSession<AwsDataApiPgQueryResultHKT, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'AwsDataApiSession';

/** @internal */
readonly rawQuery: AwsDataApiQueryBase;

Expand Down Expand Up @@ -176,6 +181,8 @@ export class AwsDataApiTransaction<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends PgTransaction<AwsDataApiPgQueryResultHKT, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'AwsDataApiTransaction';

override transaction<T>(transaction: (tx: AwsDataApiTransaction<TFullSchema, TSchema>) => Promise<T>): Promise<T> {
const savepointName = `sp${this.nestedIndex + 1}`;
const tx = new AwsDataApiTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
Expand Down
7 changes: 7 additions & 0 deletions drizzle-orm/src/better-sqlite3/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Database, RunResult, Statement } from 'better-sqlite3';
import { entityKind } from '~/entity';
import type { Logger } from '~/logger';
import { NoopLogger } from '~/logger';
import { type RelationalSchemaConfig, type TablesRelationalConfig } from '~/relations';
Expand All @@ -20,6 +21,8 @@ export class BetterSQLiteSession<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteSession<'sync', RunResult, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'BetterSQLiteSession';

private logger: Logger;

constructor(
Expand Down Expand Up @@ -55,6 +58,8 @@ export class BetterSQLiteTransaction<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteTransaction<'sync', RunResult, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'BetterSQLiteTransaction';

override transaction<T>(transaction: (tx: BetterSQLiteTransaction<TFullSchema, TSchema>) => T): T {
const savepointName = `sp${this.nestedIndex}`;
const tx = new BetterSQLiteTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);
Expand All @@ -73,6 +78,8 @@ export class BetterSQLiteTransaction<
export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: RunResult; all: T['all']; get: T['get']; values: T['values'] }
> {
static readonly [entityKind]: string = 'BetterSQLitePreparedQuery';

constructor(
private stmt: Statement,
private queryString: string,
Expand Down
7 changes: 7 additions & 0 deletions drizzle-orm/src/bun-sqlite/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="bun-types" />

import type { Database, Statement as BunStatement } from 'bun:sqlite';
import { entityKind } from '~/entity';
import type { Logger } from '~/logger';
import { NoopLogger } from '~/logger';
import { type RelationalSchemaConfig, type TablesRelationalConfig } from '~/relations';
Expand All @@ -23,6 +24,8 @@ export class SQLiteBunSession<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteSession<'sync', void, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'SQLiteBunSession';

private logger: Logger;

constructor(
Expand Down Expand Up @@ -66,6 +69,8 @@ export class SQLiteBunTransaction<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteTransaction<'sync', void, TFullSchema, TSchema> {
static readonly [entityKind]: string = 'SQLiteBunTransaction';

override transaction<T>(transaction: (tx: SQLiteBunTransaction<TFullSchema, TSchema>) => T): T {
const savepointName = `sp${this.nestedIndex}`;
const tx = new SQLiteBunTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);
Expand All @@ -84,6 +89,8 @@ export class SQLiteBunTransaction<
export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values'] }
> {
static readonly [entityKind]: string = 'SQLiteBunPreparedQuery';

constructor(
private stmt: Statement,
private queryString: string,
Expand Down
Loading

0 comments on commit fc84088

Please sign in to comment.