You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR allows to have full type safety on `payload.drizzle` with a
single command
```sh
pnpm payload generate:db-schema
```
Which generates TypeScript code with Drizzle declarations based on the
current database schema.
Example of generated file with the website template:
https://gist.github.com/r1tsuu/b8687f211b51d9a3a7e78ba41e8fbf03
Video that shows the power:
https://github.com/user-attachments/assets/3ced958b-ec1d-49f5-9f51-d859d5fae236
We also now proxy drizzle package the same way we do for Lexical so you
don't have to install it (and you shouldn't because you may have version
mismatch).
Instead, you can import from Drizzle like this:
```ts
import {
pgTable,
index,
foreignKey,
integer,
text,
varchar,
jsonb,
boolean,
numeric,
serial,
timestamp,
uniqueIndex,
pgEnum,
} from '@payloadcms/db-postgres/drizzle/pg-core'
import { sql } from '@payloadcms/db-postgres/drizzle'
import { relations } from '@payloadcms/db-postgres/drizzle/relations'
```
Fixes #4318
In the future we can also support types generation for mongoose / raw
mongodb results.
Copy file name to clipboardExpand all lines: docs/database/postgres.mdx
+58-6Lines changed: 58 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,21 +65,33 @@ export default buildConfig({
65
65
|`schemaName` (experimental) | A string for the postgres schema to use, defaults to 'public'. |
66
66
|`idType`| A string of 'serial', or 'uuid' that is used for the data type given to id columns. |
67
67
|`transactionOptions`| A PgTransactionConfig object for transactions, or set to `false` to disable using transactions. [More details](https://orm.drizzle.team/docs/transactions)|
68
-
|`disableCreateDatabase`| Pass `true` to disable auto database creation if it doesn't exist. Defaults to `false`. |
68
+
|`disableCreateDatabase`| Pass `true` to disable auto database creation if it doesn't exist. Defaults to `false`. |
69
69
|`localesSuffix`| A string appended to the end of table names for storing localized fields. Default is '_locales'. |
70
70
|`relationshipsSuffix`| A string appended to the end of table names for storing relationships. Default is '_rels'. |
71
71
|`versionsSuffix`| A string appended to the end of table names for storing versions. Defaults to '_v'. |
72
72
|`beforeSchemaInit`| Drizzle schema hook. Runs before the schema is built. [More Details](#beforeschemainit)|
73
73
|`afterSchemaInit`| Drizzle schema hook. Runs after the schema is built. [More Details](#afterschemainit)|
74
+
|`generateSchemaOutputFile`| Override generated schema from `payload generate:db-schema` file path. Defaults to `{CWD}/src/payload-generated.schema.ts`|
74
75
75
76
## Access to Drizzle
76
77
77
78
After Payload is initialized, this adapter will expose the full power of Drizzle to you for use if you need it.
78
79
79
-
You can access Drizzle as follows:
80
+
To ensure type-safety, you need to generate Drizzle schema first with:
81
+
```sh
82
+
npx payload generate:db-schema
83
+
```
80
84
81
-
```text
82
-
payload.db.drizzle
85
+
Then, you can access Drizzle as follows:
86
+
```ts
87
+
import { posts } from'./payload-generated-schema'
88
+
// To avoid installing Drizzle, you can import everything that drizzle has from our re-export path.
89
+
import { eq, sql, and } from'@payloadcms/db-postgres/drizzle'
|`client`\*|[Client connection options](https://orm.drizzle.team/docs/get-started-sqlite#turso) that will be passed to `createClient` from `@libsql/client`. |
40
-
|`push`| Disable Drizzle's [`db push`](https://orm.drizzle.team/kit-docs/overview#prototyping-with-db-push) in development mode. By default, `push` is enabled for development mode only. |
41
-
|`migrationDir`| Customize the directory that migrations are stored. |
42
-
|`logger`| The instance of the logger to be passed to drizzle. By default Payload's will be used. |
43
-
|`idType`| A string of 'number', or 'uuid' that is used for the data type given to id columns. |
44
-
|`transactionOptions`| A SQLiteTransactionConfig object for transactions, or set to `false` to disable using transactions. [More details](https://orm.drizzle.team/docs/transactions)|
45
-
|`localesSuffix`| A string appended to the end of table names for storing localized fields. Default is '_locales'. |
46
-
|`relationshipsSuffix`| A string appended to the end of table names for storing relationships. Default is '_rels'. |
47
-
|`versionsSuffix`| A string appended to the end of table names for storing versions. Defaults to '_v'. |
48
-
|`beforeSchemaInit`| Drizzle schema hook. Runs before the schema is built. [More Details](#beforeschemainit)|
49
-
|`afterSchemaInit`| Drizzle schema hook. Runs after the schema is built. [More Details](#afterschemainit)|
|`client`\*|[Client connection options](https://orm.drizzle.team/docs/get-started-sqlite#turso) that will be passed to `createClient` from `@libsql/client`. |
40
+
|`push`| Disable Drizzle's [`db push`](https://orm.drizzle.team/kit-docs/overview#prototyping-with-db-push) in development mode. By default, `push` is enabled for development mode only. |
41
+
|`migrationDir`| Customize the directory that migrations are stored. |
42
+
|`logger`| The instance of the logger to be passed to drizzle. By default Payload's will be used. |
43
+
|`idType`| A string of 'number', or 'uuid' that is used for the data type given to id columns. |
44
+
|`transactionOptions`| A SQLiteTransactionConfig object for transactions, or set to `false` to disable using transactions. [More details](https://orm.drizzle.team/docs/transactions)|
45
+
|`localesSuffix`| A string appended to the end of table names for storing localized fields. Default is '_locales'. |
46
+
|`relationshipsSuffix`| A string appended to the end of table names for storing relationships. Default is '_rels'. |
47
+
|`versionsSuffix`| A string appended to the end of table names for storing versions. Defaults to '_v'. |
48
+
|`beforeSchemaInit`| Drizzle schema hook. Runs before the schema is built. [More Details](#beforeschemainit)|
49
+
|`afterSchemaInit`| Drizzle schema hook. Runs after the schema is built. [More Details](#afterschemainit)|
50
+
|`generateSchemaOutputFile`| Override generated schema from `payload generate:db-schema` file path. Defaults to `{CWD}/src/payload-generated.schema.ts`|
50
51
51
52
## Access to Drizzle
52
53
53
54
After Payload is initialized, this adapter will expose the full power of Drizzle to you for use if you need it.
54
55
55
-
You can access Drizzle as follows:
56
+
To ensure type-safety, you need to generate Drizzle schema first with:
57
+
```sh
58
+
npx payload generate:db-schema
59
+
```
56
60
57
-
```text
58
-
payload.db.drizzle
61
+
Then, you can access Drizzle as follows:
62
+
```ts
63
+
// Import table from the generated file
64
+
import { posts } from'./payload-generated-schema'
65
+
// To avoid installing Drizzle, you can import everything that drizzle has from our re-export path.
66
+
import { eq, sql, and } from'@payloadcms/db-sqlite/drizzle'
0 commit comments