-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'drizzle-team:main' into fix-alg-typo
- Loading branch information
Showing
205 changed files
with
36,587 additions
and
21,587 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
## New Features | ||
|
||
### 🎉 **Neon HTTP Batch** | ||
|
||
For more info you can check [Neon docs](https://neon.tech/docs/serverless/serverless-driver#issue-multiple-queries-with-the-transaction-function) | ||
|
||
**Example** | ||
|
||
```ts | ||
const batchResponse: BatchType = await db.batch([ | ||
db.insert(usersTable).values({ id: 1, name: 'John' }).returning({ | ||
id: usersTable.id, | ||
}), | ||
db.insert(usersTable).values({ id: 2, name: 'Dan' }), | ||
db.query.usersTable.findMany({}), | ||
db.query.usersTable.findFirst({}), | ||
]); | ||
``` | ||
|
||
```ts | ||
type BatchType = [ | ||
{ | ||
id: number; | ||
}[], | ||
NeonHttpQueryResult<never>, | ||
{ | ||
id: number; | ||
name: string; | ||
verified: number; | ||
invitedBy: number | null; | ||
}[], | ||
{ | ||
id: number; | ||
name: string; | ||
verified: number; | ||
invitedBy: number | null; | ||
} | undefined, | ||
]; | ||
``` | ||
|
||
## Improvements | ||
|
||
Thanks to the `database-js` and `PlanetScale` teams, we have updated the default behavior and instances of `database-js`. | ||
|
||
As suggested by the `database-js` core team, you should use the `Client` instance instead of `connect()`: | ||
|
||
```typescript | ||
import { Client } from '@planetscale/database'; | ||
import { drizzle } from 'drizzle-orm/planetscale-serverless'; | ||
|
||
// create the connection | ||
const client = new Client({ | ||
host: process.env['DATABASE_HOST'], | ||
username: process.env['DATABASE_USERNAME'], | ||
password: process.env['DATABASE_PASSWORD'], | ||
}); | ||
|
||
const db = drizzle(client); | ||
``` | ||
|
||
> Warning: In this version, there are no breaking changes, but starting from version `0.30.0`, you will encounter an error if you attempt to use anything other than a `Client` instance. | ||
> | ||
> We suggest starting to change connections to PlanetScale now to prevent any runtime errors in the future. | ||
Previously our docs stated to use `connect()` and only this function was can be passed to drizzle. In this realase we are adding support for `new Client()` and deprecating `connect()`, by suggesting from `database-js` team. In this release you will see a `warning` when trying to pass `connect()` function result: | ||
|
||
**Warning text** | ||
|
||
```mdx | ||
Warning: You need to pass an instance of Client: | ||
|
||
import { Client } from "@planetscale/database"; | ||
|
||
const client = new Client({ | ||
host: process.env["DATABASE_HOST"], | ||
username: process.env["DATABASE_USERNAME"], | ||
password: process.env["DATABASE_PASSWORD"], | ||
}); | ||
|
||
const db = drizzle(client); | ||
|
||
Starting from version 0.30.0, you will encounter an error if you attempt to use anything other than a Client instance. | ||
|
||
Please make the necessary changes now to prevent any runtime errors in the future | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
## New Features | ||
|
||
### 🎉 WITH UPDATE, WITH DELETE, WITH INSERT - thanks @L-Mario564 | ||
|
||
You can now use `WITH` statements with [INSERT](https://orm.drizzle.team/docs/insert#with-insert-clause), [UPDATE](https://orm.drizzle.team/docs/update#with-update-clause) and [DELETE](https://orm.drizzle.team/docs/delete#with-delete-clause) statements | ||
|
||
Usage examples | ||
|
||
```ts | ||
const averageAmount = db.$with('average_amount').as( | ||
db.select({ value: sql`avg(${orders.amount})`.as('value') }).from(orders), | ||
); | ||
const result = await db | ||
.with(averageAmount) | ||
.delete(orders) | ||
.where(gt(orders.amount, sql`(select * from ${averageAmount})`)) | ||
.returning({ | ||
id: orders.id, | ||
}); | ||
``` | ||
|
||
Generated SQL: | ||
|
||
```sql | ||
with "average_amount" as (select avg("amount") as "value" from "orders") | ||
delete from "orders" | ||
where "orders"."amount" > (select * from "average_amount") | ||
returning "id" | ||
``` | ||
|
||
For more examples for all statements, check docs: | ||
|
||
- [with insert docs](https://orm.drizzle.team/docs/insert#with-insert-clause) | ||
- [with update docs](https://orm.drizzle.team/docs/update#with-update-clause) | ||
- [with delete docs](https://orm.drizzle.team/docs/delete#with-delete-clause) | ||
|
||
### 🎉 Possibility to specify custom schema and custom name for migrations table - thanks @g3r4n | ||
|
||
- **Custom table for migrations** | ||
|
||
By default, all information about executed migrations will be stored in the database inside the `__drizzle_migrations` table, | ||
and for PostgreSQL, inside the `drizzle` schema. However, you can configure where to store those records. | ||
|
||
To add a custom table name for migrations stored inside your database, you should use the `migrationsTable` option | ||
|
||
Usage example | ||
|
||
```ts | ||
await migrate(db, { | ||
migrationsFolder: './drizzle', | ||
migrationsTable: 'my_migrations', | ||
}); | ||
``` | ||
|
||
- **Custom schema for migrations** | ||
|
||
> Works only with PostgreSQL databases | ||
To add a custom schema name for migrations stored inside your database, you should use the `migrationsSchema` option | ||
|
||
Usage example | ||
|
||
```ts | ||
await migrate(db, { | ||
migrationsFolder: './drizzle', | ||
migrationsSchema: 'custom', | ||
}); | ||
``` | ||
|
||
### 🎉 SQLite Proxy bacth and Relational Queries support | ||
|
||
- You can now use `.query.findFirst` and `.query.findMany` syntax with sqlite proxy driver | ||
|
||
- SQLite Proxy supports batch requests, the same as it's done for all other drivers. Check full [docs](https://orm.drizzle.team/docs/batch-api) | ||
|
||
You will need to specify a specific callback for batch queries and handle requests to proxy server: | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/sqlite-proxy'; | ||
|
||
type ResponseType = { rows: any[][] | any[] }[]; | ||
|
||
const db = drizzle( | ||
async (sql, params, method) => { | ||
// single query logic | ||
}, | ||
// new batch callback | ||
async ( | ||
queries: { | ||
sql: string; | ||
params: any[]; | ||
method: 'all' | 'run' | 'get' | 'values'; | ||
}[], | ||
) => { | ||
try { | ||
const result: ResponseType = await axios.post( | ||
'http://localhost:3000/batch', | ||
{ queries }, | ||
); | ||
|
||
return result; | ||
} catch (e: any) { | ||
console.error('Error from sqlite proxy server:', e); | ||
throw e; | ||
} | ||
}, | ||
); | ||
``` | ||
|
||
And then you can use `db.batch([])` method, that will proxy all queries | ||
|
||
> Response from the batch should be an array of raw values (an array within an array), in the same order as they were sent to the proxy server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
## Breaking Changes | ||
|
||
The Postgres timestamp mapping has been changed to align all drivers with the same behavior. | ||
|
||
❗ We've modified the `postgres.js` driver instance to always return strings for dates, and then Drizzle will provide you with either strings of mapped dates, depending on the selected `mode`. The only issue you may encounter is that once you provide the `postgres.js`` driver instance inside Drizzle, the behavior of this object will change for dates, which will always be strings. | ||
|
||
We've made this change as a minor release, just as a warning, that: | ||
|
||
- If you were using timestamps and were waiting for a specific response, the behavior will now be changed. | ||
When mapping to the driver, we will always use `.toISOString` for both timestamps with timezone and without timezone. | ||
|
||
- If you were using the `postgres.js` driver outside of Drizzle, all `postgres.js` clients passed to Drizzle will have mutated behavior for dates. All dates will be strings in the response. | ||
|
||
Parsers that were changed for `postgres.js`. | ||
|
||
```ts | ||
const transparentParser = (val: any) => val; | ||
|
||
// Override postgres.js default date parsers: https://github.com/porsager/postgres/discussions/761 | ||
for (const type of ['1184', '1082', '1083', '1114']) { | ||
client.options.parsers[type as any] = transparentParser; | ||
client.options.serializers[type as any] = transparentParser; | ||
} | ||
``` | ||
|
||
Ideally, as is the case with almost all other drivers, we should have the possibility to mutate mappings on a per-query basis, which means that the driver client won't be mutated. We will be reaching out to the creator of the `postgres.js` library to inquire about the possibility of specifying per-query mapping interceptors and making this flow even better for all users. | ||
|
||
If we've overlooked this capability and it is already available with `postgres.js``, please ping us in our Discord! | ||
|
||
A few more references for timestamps without and with timezones can be found in our [docs](http://orm.drizzle.team/docs/column-types/pg#timestamp) | ||
|
||
## Bug fixed in this release | ||
|
||
- [BUG]: timestamp with mode string is returned as Date object instead of string - #806 | ||
- [BUG]: Dates are always dates #971 | ||
- [BUG]: Inconsistencies when working with timestamps and corresponding datetime objects in javascript. #1176 | ||
- [BUG]: timestamp columns showing string type, however actually returning a Date object. #1185 | ||
- [BUG]: Wrong data type for postgres date colum #1407 | ||
- [BUG]: invalid timestamp conversion when using PostgreSQL with TimeZone set to UTC #1587 | ||
- [BUG]: Postgres insert into timestamp with time zone removes milliseconds #1061 | ||
- [BUG]: update timestamp field (using AWS Data API) #1164 | ||
- [BUG]: Invalid date from relational queries #895 |
Oops, something went wrong.