Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/beta' into feat/react-compile-pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed Aug 19, 2024
2 parents 3c74bfe + 2f38290 commit 79e92af
Show file tree
Hide file tree
Showing 53 changed files with 992 additions and 503 deletions.
28 changes: 14 additions & 14 deletions packages/db-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ import {
updateOne,
updateVersion,
} from '@payloadcms/drizzle'
import {
convertPathToJSONTraversal,
countDistinct,
createJSONQuery,
createMigration,
defaultDrizzleSnapshot,
deleteWhere,
dropDatabase,
execute,
getMigrationTemplate,
init,
insert,
requireDrizzleKit,
} from '@payloadcms/drizzle/postgres'
import { pgEnum, pgSchema, pgTable } from 'drizzle-orm/pg-core'
import { createDatabaseAdapter } from 'payload'

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

import { connect } from './connect.js'
import { countDistinct } from './countDistinct.js'
import { convertPathToJSONTraversal } from './createJSONQuery/convertPathToJSONTraversal.js'
import { createJSONQuery } from './createJSONQuery/index.js'
import { createMigration } from './createMigration.js'
import { defaultDrizzleSnapshot } from './defaultSnapshot.js'
import { deleteWhere } from './deleteWhere.js'
import { dropDatabase } from './dropDatabase.js'
import { execute } from './execute.js'
import { getMigrationTemplate } from './getMigrationTemplate.js'
import { init } from './init.js'
import { insert } from './insert.js'
import { requireDrizzleKit } from './requireDrizzleKit.js'

export type { MigrateDownArgs, MigrateUpArgs } from './types.js'

export { sql } from 'drizzle-orm'

Expand Down
156 changes: 12 additions & 144 deletions packages/db-postgres/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import type { Operators } from '@payloadcms/drizzle'
import type {
BuildQueryJoinAliases,
DrizzleAdapter,
TransactionPg,
} from '@payloadcms/drizzle/types'
import type { DrizzleSnapshotJSON } from 'drizzle-kit/api'
import type {
ColumnBaseConfig,
ColumnDataType,
DrizzleConfig,
Relation,
Relations,
SQL,
} from 'drizzle-orm'
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
import type {
PgColumn,
PgEnum,
PgInsertOnConflictDoUpdateConfig,
PgSchema,
PgTableWithColumns,
PgTransactionConfig,
pgEnum,
} from 'drizzle-orm/pg-core'
import type { PgTableFn } from 'drizzle-orm/pg-core/table'
import type { Payload, PayloadRequest } from 'payload'
import type { Pool, PoolConfig, QueryResult } from 'pg'
BasePostgresAdapter,
GenericEnum,
MigrateDownArgs,
MigrateUpArgs,
PostgresDB,
} from '@payloadcms/drizzle/postgres'
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
import type { DrizzleConfig } from 'drizzle-orm'
import type { PgSchema, PgTableFn, PgTransactionConfig } from 'drizzle-orm/pg-core'
import type { Pool, PoolConfig } from 'pg'

export type Args = {
idType?: 'serial' | 'uuid'
Expand All @@ -49,125 +32,10 @@ export type Args = {
versionsSuffix?: string
}

export type GenericColumn = PgColumn<
ColumnBaseConfig<ColumnDataType, string>,
Record<string, unknown>
>

export type GenericColumns = {
[x: string]: GenericColumn
}

export type GenericTable = PgTableWithColumns<{
columns: GenericColumns
dialect: string
name: string
schema: string
}>

export type GenericEnum = PgEnum<[string, ...string[]]>

export type GenericRelation = Relations<string, Record<string, Relation<string>>>

export type PostgresDB = NodePgDatabase<Record<string, unknown>>

export type CountDistinct = (args: {
db: PostgresDB | TransactionPg
joins: BuildQueryJoinAliases
tableName: string
where: SQL
}) => Promise<number>

export type DeleteWhere = (args: {
db: PostgresDB | TransactionPg
tableName: string
where: SQL
}) => Promise<void>

export type DropDatabase = (args: { adapter: PostgresAdapter }) => Promise<void>

export type Execute<T> = (args: {
db?: PostgresDB | TransactionPg
drizzle?: PostgresDB
raw?: string
sql?: SQL<unknown>
}) => Promise<QueryResult<Record<string, T>>>

export type Insert = (args: {
db: PostgresDB | TransactionPg
onConflictDoUpdate?: PgInsertOnConflictDoUpdateConfig<any>
tableName: string
values: Record<string, unknown> | Record<string, unknown>[]
}) => Promise<Record<string, unknown>[]>

type PostgresDrizzleAdapter = Omit<
DrizzleAdapter,
| 'countDistinct'
| 'deleteWhere'
| 'drizzle'
| 'dropDatabase'
| 'execute'
| 'insert'
| 'operators'
| 'relations'
>

type Schema =
| {
enum: typeof pgEnum
table: PgTableFn
}
| PgSchema

export type PostgresAdapter = {
countDistinct: CountDistinct
defaultDrizzleSnapshot: DrizzleSnapshotJSON
deleteWhere: DeleteWhere
drizzle: PostgresDB
dropDatabase: DropDatabase
enums: Record<string, GenericEnum>
execute: Execute<unknown>
/**
* 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
* Used for returning properly formed errors from unique fields
*/
fieldConstraints: Record<string, Record<string, string>>
idType: Args['idType']
initializing: Promise<void>
insert: Insert
localesSuffix?: string
logger: DrizzleConfig['logger']
operators: Operators
pgSchema?: Schema
pool: Pool
poolOptions: Args['pool']
prodMigrations?: {
down: (args: MigrateDownArgs) => Promise<void>
name: string
up: (args: MigrateUpArgs) => Promise<void>
}[]
push: boolean
rejectInitializing: () => void
relations: Record<string, GenericRelation>
relationshipsSuffix?: string
resolveInitializing: () => void
schemaName?: Args['schemaName']
sessions: {
[id: string]: {
db: PostgresDB | TransactionPg
reject: () => Promise<void>
resolve: () => Promise<void>
}
}
tableNameMap: Map<string, string>
tables: Record<string, GenericTable>
versionsSuffix?: string
} & PostgresDrizzleAdapter

export type IDType = 'integer' | 'numeric' | 'uuid' | 'varchar'

export type MigrateUpArgs = { payload: Payload; req?: Partial<PayloadRequest> }
export type MigrateDownArgs = { payload: Payload; req?: Partial<PayloadRequest> }
poolOptions: PoolConfig
} & BasePostgresAdapter

declare module 'payload' {
export interface DatabaseAdapter
Expand Down
10 changes: 10 additions & 0 deletions packages/drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"types": "./src/index.ts",
"default": "./src/index.ts"
},
"./postgres": {
"import": "./src/exports/postgres.ts",
"types": "./src/exports/postgres.ts",
"default": "./src/exports/postgres.ts"
},
"./types": {
"import": "./src/types.ts",
"types": "./src/types.ts",
Expand Down Expand Up @@ -61,6 +66,11 @@
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./postgres": {
"import": "./dist/exports/postgres.js",
"types": "./dist/exports/postgres.d.ts",
"default": "./dist/exports/postgres.js"
},
"./types": {
"import": "./dist/types.js",
"types": "./dist/types.d.ts",
Expand Down
13 changes: 13 additions & 0 deletions packages/drizzle/src/exports/postgres.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export { countDistinct } from '../postgres/countDistinct.js'
export { convertPathToJSONTraversal } from '../postgres/createJSONQuery/convertPathToJSONTraversal.js'
export { createJSONQuery } from '../postgres/createJSONQuery/index.js'
export { createMigration } from '../postgres/createMigration.js'
export { defaultDrizzleSnapshot } from '../postgres/defaultSnapshot.js'
export { deleteWhere } from '../postgres/deleteWhere.js'
export { dropDatabase } from '../postgres/dropDatabase.js'
export { execute } from '../postgres/execute.js'
export { getMigrationTemplate } from '../postgres/getMigrationTemplate.js'
export { init } from '../postgres/init.js'
export { insert } from '../postgres/insert.js'
export { requireDrizzleKit } from '../postgres/requireDrizzleKit.js'
export * from '../postgres/types.js'
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ChainedMethods, TransactionPg } from '@payloadcms/drizzle/types'

import { chainMethods } from '@payloadcms/drizzle'
import { sql } from 'drizzle-orm'

import type { CountDistinct, PostgresAdapter } from './types.js'
import type { ChainedMethods, TransactionPg } from '../types.js'
import type { BasePostgresAdapter, CountDistinct } from './types.js'

import { chainMethods } from '../find/chainMethods.js'

export const countDistinct: CountDistinct = async function countDistinct(
this: PostgresAdapter,
this: BasePostgresAdapter,
{ db, joins, tableName, where },
) {
const chainedMethods: ChainedMethods = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { getPredefinedMigration, writeMigrationIndex } from 'payload'
import prompts from 'prompts'
import { fileURLToPath } from 'url'

import type { PostgresAdapter } from './types.js'
import type { BasePostgresAdapter } from './types.js'

import { defaultDrizzleSnapshot } from './defaultSnapshot.js'
import { getMigrationTemplate } from './getMigrationTemplate.js'

const require = createRequire(import.meta.url)

export const createMigration: CreateMigration = async function createMigration(
this: PostgresAdapter,
this: BasePostgresAdapter,
{ file, forceAcceptWarning, migrationName, payload },
) {
const filename = fileURLToPath(import.meta.url)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TransactionPg } from '@payloadcms/drizzle/types'

import type { TransactionPg } from '../types.js'
import type { DeleteWhere } from './types.js'

export const deleteWhere: DeleteWhere = async function deleteWhere({ db, tableName, where }) {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { Init, SanitizedCollectionConfig } from 'payload'

import { createTableName } from '@payloadcms/drizzle'
import { uniqueIndex } from 'drizzle-orm/pg-core'
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'
import toSnakeCase from 'to-snake-case'

import type { BaseExtraConfig } from './schema/build.js'
import type { PostgresAdapter } from './types.js'
import type { BaseExtraConfig, BasePostgresAdapter } from './types.js'

import { createTableName } from '../createTableName.js'
import { buildTable } from './schema/build.js'

export const init: Init = function init(this: PostgresAdapter) {
export const init: Init = function init(this: BasePostgresAdapter) {
if (this.payload.config.localization) {
this.enums.enum__locales = this.pgSchema.enum(
'_locales',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TransactionPg } from '@payloadcms/drizzle/types'

import type { TransactionPg } from '../types.js'
import type { Insert } from './types.js'

export const insert: Insert = async function insert({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RequireDrizzleKit } from '@payloadcms/drizzle/types'

import { createRequire } from 'module'

import type { RequireDrizzleKit } from '../types.js'

const require = createRequire(import.meta.url)
export const requireDrizzleKit: RequireDrizzleKit = () => require('drizzle-kit/api')
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import type {
IndexBuilder,
PgColumnBuilder,
PgTableWithColumns,
UniqueConstraintBuilder,
} from 'drizzle-orm/pg-core'
import type { Field } from 'payload'

import { createTableName } from '@payloadcms/drizzle'
import { relations } from 'drizzle-orm'
import {
foreignKey,
Expand All @@ -22,21 +20,22 @@ import {
} from 'drizzle-orm/pg-core'
import toSnakeCase from 'to-snake-case'

import type { GenericColumns, GenericTable, IDType, PostgresAdapter } from '../types.js'

import type {
BaseExtraConfig,
BasePostgresAdapter,
GenericColumns,
GenericTable,
IDType,
RelationMap,
} from '../types.js'

import { createTableName } from '../../createTableName.js'
import { parentIDColumnMap } from './parentIDColumnMap.js'
import { setColumnID } from './setColumnID.js'
import { traverseFields } from './traverseFields.js'

export type BaseExtraConfig = Record<
string,
(cols: GenericColumns) => ForeignKeyBuilder | IndexBuilder | UniqueConstraintBuilder
>

export type RelationMap = Map<string, { localized: boolean; target: string; type: 'many' | 'one' }>

type Args = {
adapter: PostgresAdapter
adapter: BasePostgresAdapter
baseColumns?: Record<string, PgColumnBuilder>
/**
* After table is created, run these functions to add extra config to the table
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { numeric, serial, uuid, varchar } from 'drizzle-orm/pg-core'
import { type Field, flattenTopLevelFields } from 'payload'
import { fieldAffectsData } from 'payload/shared'

import type { IDType, PostgresAdapter } from '../types.js'
import type { BasePostgresAdapter, IDType } from '../types.js'

type Args = { adapter: PostgresAdapter; columns: Record<string, PgColumnBuilder>; fields: Field[] }
type Args = {
adapter: BasePostgresAdapter
columns: Record<string, PgColumnBuilder>
fields: Field[]
}
export const setColumnID = ({ adapter, columns, fields }: Args): IDType => {
const idField = flattenTopLevelFields(fields).find(
(field) => fieldAffectsData(field) && field.name === 'id',
Expand Down
Loading

0 comments on commit 79e92af

Please sign in to comment.