Skip to content

Commit 2835e1d

Browse files
authored
feat: abstract postgres base adapter (#7732)
Abstracts Postgres base adapter in order to allow future postgres-based adapters.
1 parent 4808e31 commit 2835e1d

26 files changed

+340
-226
lines changed

packages/db-postgres/src/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ import {
3232
updateOne,
3333
updateVersion,
3434
} from '@payloadcms/drizzle'
35+
import {
36+
convertPathToJSONTraversal,
37+
countDistinct,
38+
createJSONQuery,
39+
createMigration,
40+
defaultDrizzleSnapshot,
41+
deleteWhere,
42+
dropDatabase,
43+
execute,
44+
getMigrationTemplate,
45+
init,
46+
insert,
47+
requireDrizzleKit,
48+
} from '@payloadcms/drizzle/postgres'
3549
import { pgEnum, pgSchema, pgTable } from 'drizzle-orm/pg-core'
3650
import { createDatabaseAdapter } from 'payload'
3751

3852
import type { Args, PostgresAdapter } from './types.js'
3953

4054
import { connect } from './connect.js'
41-
import { countDistinct } from './countDistinct.js'
42-
import { convertPathToJSONTraversal } from './createJSONQuery/convertPathToJSONTraversal.js'
43-
import { createJSONQuery } from './createJSONQuery/index.js'
44-
import { createMigration } from './createMigration.js'
45-
import { defaultDrizzleSnapshot } from './defaultSnapshot.js'
46-
import { deleteWhere } from './deleteWhere.js'
47-
import { dropDatabase } from './dropDatabase.js'
48-
import { execute } from './execute.js'
49-
import { getMigrationTemplate } from './getMigrationTemplate.js'
50-
import { init } from './init.js'
51-
import { insert } from './insert.js'
52-
import { requireDrizzleKit } from './requireDrizzleKit.js'
53-
54-
export type { MigrateDownArgs, MigrateUpArgs } from './types.js'
5555

5656
export { sql } from 'drizzle-orm'
5757

packages/db-postgres/src/types.ts

Lines changed: 12 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
1-
import type { Operators } from '@payloadcms/drizzle'
21
import type {
3-
BuildQueryJoinAliases,
4-
DrizzleAdapter,
5-
TransactionPg,
6-
} from '@payloadcms/drizzle/types'
7-
import type { DrizzleSnapshotJSON } from 'drizzle-kit/api'
8-
import type {
9-
ColumnBaseConfig,
10-
ColumnDataType,
11-
DrizzleConfig,
12-
Relation,
13-
Relations,
14-
SQL,
15-
} from 'drizzle-orm'
16-
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
17-
import type {
18-
PgColumn,
19-
PgEnum,
20-
PgInsertOnConflictDoUpdateConfig,
21-
PgSchema,
22-
PgTableWithColumns,
23-
PgTransactionConfig,
24-
pgEnum,
25-
} from 'drizzle-orm/pg-core'
26-
import type { PgTableFn } from 'drizzle-orm/pg-core/table'
27-
import type { Payload, PayloadRequest } from 'payload'
28-
import type { Pool, PoolConfig, QueryResult } from 'pg'
2+
BasePostgresAdapter,
3+
GenericEnum,
4+
MigrateDownArgs,
5+
MigrateUpArgs,
6+
PostgresDB,
7+
} from '@payloadcms/drizzle/postgres'
8+
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
9+
import type { DrizzleConfig } from 'drizzle-orm'
10+
import type { PgSchema, PgTableFn, PgTransactionConfig } from 'drizzle-orm/pg-core'
11+
import type { Pool, PoolConfig } from 'pg'
2912

3013
export type Args = {
3114
idType?: 'serial' | 'uuid'
@@ -49,125 +32,10 @@ export type Args = {
4932
versionsSuffix?: string
5033
}
5134

52-
export type GenericColumn = PgColumn<
53-
ColumnBaseConfig<ColumnDataType, string>,
54-
Record<string, unknown>
55-
>
56-
57-
export type GenericColumns = {
58-
[x: string]: GenericColumn
59-
}
60-
61-
export type GenericTable = PgTableWithColumns<{
62-
columns: GenericColumns
63-
dialect: string
64-
name: string
65-
schema: string
66-
}>
67-
68-
export type GenericEnum = PgEnum<[string, ...string[]]>
69-
70-
export type GenericRelation = Relations<string, Record<string, Relation<string>>>
71-
72-
export type PostgresDB = NodePgDatabase<Record<string, unknown>>
73-
74-
export type CountDistinct = (args: {
75-
db: PostgresDB | TransactionPg
76-
joins: BuildQueryJoinAliases
77-
tableName: string
78-
where: SQL
79-
}) => Promise<number>
80-
81-
export type DeleteWhere = (args: {
82-
db: PostgresDB | TransactionPg
83-
tableName: string
84-
where: SQL
85-
}) => Promise<void>
86-
87-
export type DropDatabase = (args: { adapter: PostgresAdapter }) => Promise<void>
88-
89-
export type Execute<T> = (args: {
90-
db?: PostgresDB | TransactionPg
91-
drizzle?: PostgresDB
92-
raw?: string
93-
sql?: SQL<unknown>
94-
}) => Promise<QueryResult<Record<string, T>>>
95-
96-
export type Insert = (args: {
97-
db: PostgresDB | TransactionPg
98-
onConflictDoUpdate?: PgInsertOnConflictDoUpdateConfig<any>
99-
tableName: string
100-
values: Record<string, unknown> | Record<string, unknown>[]
101-
}) => Promise<Record<string, unknown>[]>
102-
103-
type PostgresDrizzleAdapter = Omit<
104-
DrizzleAdapter,
105-
| 'countDistinct'
106-
| 'deleteWhere'
107-
| 'drizzle'
108-
| 'dropDatabase'
109-
| 'execute'
110-
| 'insert'
111-
| 'operators'
112-
| 'relations'
113-
>
114-
115-
type Schema =
116-
| {
117-
enum: typeof pgEnum
118-
table: PgTableFn
119-
}
120-
| PgSchema
121-
12235
export type PostgresAdapter = {
123-
countDistinct: CountDistinct
124-
defaultDrizzleSnapshot: DrizzleSnapshotJSON
125-
deleteWhere: DeleteWhere
126-
drizzle: PostgresDB
127-
dropDatabase: DropDatabase
128-
enums: Record<string, GenericEnum>
129-
execute: Execute<unknown>
130-
/**
131-
* An object keyed on each table, with a key value pair where the constraint name is the key, followed by the dot-notation field name
132-
* Used for returning properly formed errors from unique fields
133-
*/
134-
fieldConstraints: Record<string, Record<string, string>>
135-
idType: Args['idType']
136-
initializing: Promise<void>
137-
insert: Insert
138-
localesSuffix?: string
139-
logger: DrizzleConfig['logger']
140-
operators: Operators
141-
pgSchema?: Schema
14236
pool: Pool
143-
poolOptions: Args['pool']
144-
prodMigrations?: {
145-
down: (args: MigrateDownArgs) => Promise<void>
146-
name: string
147-
up: (args: MigrateUpArgs) => Promise<void>
148-
}[]
149-
push: boolean
150-
rejectInitializing: () => void
151-
relations: Record<string, GenericRelation>
152-
relationshipsSuffix?: string
153-
resolveInitializing: () => void
154-
schemaName?: Args['schemaName']
155-
sessions: {
156-
[id: string]: {
157-
db: PostgresDB | TransactionPg
158-
reject: () => Promise<void>
159-
resolve: () => Promise<void>
160-
}
161-
}
162-
tableNameMap: Map<string, string>
163-
tables: Record<string, GenericTable>
164-
versionsSuffix?: string
165-
} & PostgresDrizzleAdapter
166-
167-
export type IDType = 'integer' | 'numeric' | 'uuid' | 'varchar'
168-
169-
export type MigrateUpArgs = { payload: Payload; req?: Partial<PayloadRequest> }
170-
export type MigrateDownArgs = { payload: Payload; req?: Partial<PayloadRequest> }
37+
poolOptions: PoolConfig
38+
} & BasePostgresAdapter
17139

17240
declare module 'payload' {
17341
export interface DatabaseAdapter

packages/drizzle/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"types": "./src/index.ts",
1818
"default": "./src/index.ts"
1919
},
20+
"./postgres": {
21+
"import": "./src/exports/postgres.ts",
22+
"types": "./src/exports/postgres.ts",
23+
"default": "./src/exports/postgres.ts"
24+
},
2025
"./types": {
2126
"import": "./src/types.ts",
2227
"types": "./src/types.ts",
@@ -61,6 +66,11 @@
6166
"types": "./dist/index.d.ts",
6267
"default": "./dist/index.js"
6368
},
69+
"./postgres": {
70+
"import": "./dist/exports/postgres.js",
71+
"types": "./dist/exports/postgres.d.ts",
72+
"default": "./dist/exports/postgres.js"
73+
},
6474
"./types": {
6575
"import": "./dist/types.js",
6676
"types": "./dist/types.d.ts",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export { countDistinct } from '../postgres/countDistinct.js'
2+
export { convertPathToJSONTraversal } from '../postgres/createJSONQuery/convertPathToJSONTraversal.js'
3+
export { createJSONQuery } from '../postgres/createJSONQuery/index.js'
4+
export { createMigration } from '../postgres/createMigration.js'
5+
export { defaultDrizzleSnapshot } from '../postgres/defaultSnapshot.js'
6+
export { deleteWhere } from '../postgres/deleteWhere.js'
7+
export { dropDatabase } from '../postgres/dropDatabase.js'
8+
export { execute } from '../postgres/execute.js'
9+
export { getMigrationTemplate } from '../postgres/getMigrationTemplate.js'
10+
export { init } from '../postgres/init.js'
11+
export { insert } from '../postgres/insert.js'
12+
export { requireDrizzleKit } from '../postgres/requireDrizzleKit.js'
13+
export * from '../postgres/types.js'

packages/db-postgres/src/countDistinct.ts renamed to packages/drizzle/src/postgres/countDistinct.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { ChainedMethods, TransactionPg } from '@payloadcms/drizzle/types'
2-
3-
import { chainMethods } from '@payloadcms/drizzle'
41
import { sql } from 'drizzle-orm'
52

6-
import type { CountDistinct, PostgresAdapter } from './types.js'
3+
import type { ChainedMethods, TransactionPg } from '../types.js'
4+
import type { BasePostgresAdapter, CountDistinct } from './types.js'
5+
6+
import { chainMethods } from '../find/chainMethods.js'
77

88
export const countDistinct: CountDistinct = async function countDistinct(
9-
this: PostgresAdapter,
9+
this: BasePostgresAdapter,
1010
{ db, joins, tableName, where },
1111
) {
1212
const chainedMethods: ChainedMethods = []

packages/db-postgres/src/createMigration.ts renamed to packages/drizzle/src/postgres/createMigration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import { getPredefinedMigration, writeMigrationIndex } from 'payload'
88
import prompts from 'prompts'
99
import { fileURLToPath } from 'url'
1010

11-
import type { PostgresAdapter } from './types.js'
11+
import type { BasePostgresAdapter } from './types.js'
1212

1313
import { defaultDrizzleSnapshot } from './defaultSnapshot.js'
1414
import { getMigrationTemplate } from './getMigrationTemplate.js'
1515

1616
const require = createRequire(import.meta.url)
1717

1818
export const createMigration: CreateMigration = async function createMigration(
19-
this: PostgresAdapter,
19+
this: BasePostgresAdapter,
2020
{ file, forceAcceptWarning, migrationName, payload },
2121
) {
2222
const filename = fileURLToPath(import.meta.url)

0 commit comments

Comments
 (0)