Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/start-database/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Starts the required database for tests

inputs:
database:
description: 'Database type: mongodb, mongodb-atlas, cosmosdb, documentdb, firestore, postgres, postgres-custom-schema, postgres-uuid, postgres-read-replica, postgres-read-replicas, vercel-postgres-read-replica, sqlite, sqlite-uuid, supabase, d1'
description: 'Database type: mongodb, mongodb-atlas, cosmosdb, documentdb, firestore, postgres, postgres-custom-schema, postgres-uuid, postgres-uuidv7, postgres-read-replica, postgres-read-replicas, vercel-postgres-read-replica, sqlite, sqlite-uuid, sqlite-uuidv7, supabase, d1'
required: true

outputs:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/int.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ createIntConfig({
'postgres',
'postgres-custom-schema',
'postgres-uuid',
'postgres-uuidv7',
'postgres-read-replica',
'supabase',
'sqlite',
'sqlite-uuid',
'sqlite-uuidv7',
],
shards: 3,
})
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ Set `PAYLOAD_DATABASE` in your `.env` file to choose the database adapter:
- `postgres` - PostgreSQL with pgvector and PostGIS
- `postgres-custom-schema` - PostgreSQL with custom schema
- `postgres-uuid` - PostgreSQL with UUID primary keys
- `postgres-uuidv7` - PostgreSQL with UUID V7 primary keys
- `postgres-read-replica` - PostgreSQL with read replica
- `sqlite` - SQLite
- `sqlite-uuid` - SQLite with UUID primary keys
- `sqlite-uuidv7` - SQLite with UUID V7 primary keys
- `supabase` - Supabase (PostgreSQL)
- `d1` - D1 (SQLite)

Expand Down
3 changes: 1 addition & 2 deletions packages/db-d1-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@
"drizzle-orm": "0.44.7",
"prompts": "2.4.2",
"to-snake-case": "1.0.0",
"uuid": "9.0.0"
"uuid": "11.1.0"
},
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
"@types/pg": "8.10.2",
"@types/to-snake-case": "1.0.0",
"@types/uuid": "10.0.0",
"payload": "workspace:*"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/db-d1-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const filename = fileURLToPath(import.meta.url)

export function sqliteD1Adapter(args: Args): DatabaseAdapterObj<SQLiteD1Adapter> {
const sqliteIDType = args.idType || 'number'
const payloadIDType = sqliteIDType === 'uuid' ? 'text' : 'number'
const payloadIDType = sqliteIDType === 'uuid' || sqliteIDType === 'uuidv7' ? 'text' : 'number'
const allowIDOnCreate = args.allowIDOnCreate ?? false

function adapter({ payload }: { payload: Payload }) {
Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@
"mongoose": "8.15.1",
"mongoose-paginate-v2": "1.8.5",
"prompts": "2.4.2",
"uuid": "10.0.0"
"uuid": "11.1.0"
},
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
"@types/mongoose-aggregate-paginate-v2": "1.0.12",
"@types/prompts": "^2.4.5",
"@types/uuid": "10.0.0",
"mongodb": "6.16.0",
"mongodb-memory-server": "10.1.4",
"payload": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/db-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"pg": "8.16.3",
"prompts": "2.4.2",
"to-snake-case": "1.0.0",
"uuid": "10.0.0"
"uuid": "11.1.0"
},
"devDependencies": {
"@hyrious/esbuild-plugin-commonjs": "0.2.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/db-postgres/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type Args = {
extensions?: string[]
/** Generated schema from payload generate:db-schema file path */
generateSchemaOutputFile?: string
idType?: 'serial' | 'uuid'
idType?: 'serial' | 'uuid' | 'uuidv7'
localesSuffix?: string
logger?: DrizzleConfig['logger']
migrationDir?: string
Expand Down
3 changes: 1 addition & 2 deletions packages/db-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@
"drizzle-orm": "0.44.7",
"prompts": "2.4.2",
"to-snake-case": "1.0.0",
"uuid": "9.0.0"
"uuid": "11.1.0"
},
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
"@types/pg": "8.10.2",
"@types/to-snake-case": "1.0.0",
"@types/uuid": "10.0.0",
"payload": "workspace:*"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/db-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const filename = fileURLToPath(import.meta.url)

export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
const sqliteIDType = args.idType || 'number'
const payloadIDType = sqliteIDType === 'uuid' ? 'text' : 'number'
const payloadIDType = sqliteIDType === 'uuid' || sqliteIDType === 'uuidv7' ? 'text' : 'number'
const allowIDOnCreate = args.allowIDOnCreate ?? false

function adapter({ payload }: { payload: Payload }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/db-vercel-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"pg": "8.16.3",
"prompts": "2.4.2",
"to-snake-case": "1.0.0",
"uuid": "10.0.0"
"uuid": "11.1.0"
},
"devDependencies": {
"@hyrious/esbuild-plugin-commonjs": "0.2.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/db-vercel-postgres/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type Args = {
forceUseVercelPostgres?: boolean
/** Generated schema from payload generate:db-schema file path */
generateSchemaOutputFile?: string
idType?: 'serial' | 'uuid'
idType?: 'serial' | 'uuid' | 'uuidv7'
localesSuffix?: string
logger?: DrizzleConfig['logger']
migrationDir?: string
Expand Down
2 changes: 1 addition & 1 deletion packages/drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"drizzle-orm": "0.44.7",
"prompts": "2.4.2",
"to-snake-case": "1.0.0",
"uuid": "9.0.0"
"uuid": "11.1.0"
},
"devDependencies": {
"@libsql/client": "0.14.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/drizzle/src/postgres/columnToCodeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const columnToCodeConverter: ColumnToCodeConverter = ({
code = `${code}.defaultRandom()`
}

if (column.type === 'uuid' && column.defaultV7) {
addImport('uuid', 'v7 as uuidv7')
code = `${code}.$defaultFn(() => uuidv7())`
}

if (column.notNull) {
code = `${code}.notNull()`
}
Expand Down
5 changes: 5 additions & 0 deletions packages/drizzle/src/postgres/schema/buildDrizzleTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
varchar,
vector,
} from 'drizzle-orm/pg-core'
import { v7 as uuidv7 } from 'uuid'

import type { RawColumn, RawTable } from '../../types.js'
import type { BasePostgresAdapter } from '../types.js'
Expand Down Expand Up @@ -109,6 +110,10 @@ export const buildDrizzleTable = ({
builder = builder.defaultRandom()
}

if (column.defaultV7) {
builder = builder.$defaultFn(() => uuidv7())
}

columns[key] = builder
break
}
Expand Down
11 changes: 11 additions & 0 deletions packages/drizzle/src/postgres/schema/setColumnID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export const setColumnID: SetColumnID = ({ adapter, columns, fields }) => {
return 'uuid'
}

if (adapter.idType === 'uuidv7') {
columns.id = {
name: 'id',
type: 'uuid',
defaultV7: true,
primaryKey: true,
}

return 'uuid'
}

columns.id = {
name: 'id',
type: 'serial',
Expand Down
2 changes: 1 addition & 1 deletion packages/drizzle/src/postgres/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export type BasePostgresAdapter = {
* Used for returning properly formed errors from unique fields
*/
fieldConstraints: Record<string, Record<string, string>>
idType: 'serial' | 'uuid'
idType: 'serial' | 'uuid' | 'uuidv7'
initializing: Promise<void>
insert: Insert
localesSuffix?: string
Expand Down
12 changes: 7 additions & 5 deletions packages/drizzle/src/queries/getTableColumnFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { DrizzleAdapter, GenericColumn } from '../types.js'
import type { BuildQueryJoinAliases } from './buildQuery.js'

import { isPolymorphicRelationship } from '../utilities/isPolymorphicRelationship.js'
import { isUUIDType } from '../utilities/isUUIDType.js'
import { jsonBuildObject } from '../utilities/json.js'
import { DistinctSymbol } from '../utilities/rawConstraint.js'
import { resolveBlockTableName } from '../utilities/validateExistingBlockIsIdentical.js'
Expand Down Expand Up @@ -111,7 +112,7 @@ export const getTableColumnFromPath = ({
constraints,
field: {
name: 'id',
type: adapter.idType === 'uuid' ? 'text' : 'number',
type: isUUIDType(adapter.idType) ? 'text' : 'number',
} as NumberField | TextField,
table: adapter.tables[newTableName],
}
Expand Down Expand Up @@ -414,7 +415,7 @@ export const getTableColumnFromPath = ({
constraints,
field: {
name: 'id',
type: adapter.idType === 'uuid' ? 'text' : 'number',
type: isUUIDType(adapter.idType) ? 'text' : 'number',
} as NumberField | TextField,
table: aliasRelationshipTable,
}
Expand Down Expand Up @@ -531,7 +532,7 @@ export const getTableColumnFromPath = ({
constraints,
field: {
name: 'id',
type: adapter.idType === 'uuid' ? 'text' : 'number',
type: isUUIDType(adapter.idType) ? 'text' : 'number',
} as NumberField | TextField,
table: newAliasTable,
}
Expand Down Expand Up @@ -706,8 +707,9 @@ export const getTableColumnFromPath = ({

const columns: TableColumn['columns'] = field.relationTo
.map((relationTo) => {
let idType: 'number' | 'text' | 'uuid' =
adapter.idType === 'uuid' ? 'uuid' : 'number'
let idType: 'number' | 'text' | 'uuid' = isUUIDType(adapter.idType)
? 'uuid'
: 'number'

const { customIDType } = adapter.payload.collections[relationTo]

Expand Down
3 changes: 2 additions & 1 deletion packages/drizzle/src/queries/sanitizeQueryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { DrizzleAdapter } from '../types.js'

import { getCollectionIdType } from '../utilities/getCollectionIdType.js'
import { isPolymorphicRelationship } from '../utilities/isPolymorphicRelationship.js'
import { isUUIDType } from '../utilities/isUUIDType.js'
import { isRawConstraint } from '../utilities/rawConstraint.js'

type SanitizeQueryValueArgs = {
Expand Down Expand Up @@ -59,7 +60,7 @@ export const sanitizeQueryValue = ({
) {
const allPossibleIDTypes: (number | string)[] = []
formattedValue.forEach((val) => {
if (adapter.idType !== 'uuid' && typeof val === 'string') {
if (!isUUIDType(adapter.idType) && typeof val === 'string') {
allPossibleIDTypes.push(val, parseInt(val))
} else if (typeof val === 'string') {
allPossibleIDTypes.push(val)
Expand Down
6 changes: 4 additions & 2 deletions packages/drizzle/src/schema/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
import { createTableName } from '../createTableName.js'
import { buildForeignKeyName } from '../utilities/buildForeignKeyName.js'
import { buildIndexName } from '../utilities/buildIndexName.js'
import { isUUIDType } from '../utilities/isUUIDType.js'
import { traverseFields } from './traverseFields.js'

type Args = {
Expand Down Expand Up @@ -619,8 +620,9 @@ export const buildTable = ({
config: relationshipConfig,
throwValidationError: true,
})
let colType: 'integer' | 'numeric' | 'uuid' | 'varchar' =
adapter.idType === 'uuid' ? 'uuid' : 'integer'
let colType: 'integer' | 'numeric' | 'uuid' | 'varchar' = isUUIDType(adapter.idType)
? 'uuid'
: 'integer'
const relatedCollectionCustomIDType =
adapter.payload.collections[relationshipConfig.slug]?.customIDType

Expand Down
3 changes: 2 additions & 1 deletion packages/drizzle/src/schema/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { createTableName } from '../createTableName.js'
import { buildIndexName } from '../utilities/buildIndexName.js'
import { getArrayRelationName } from '../utilities/getArrayRelationName.js'
import { hasLocalesTable } from '../utilities/hasLocalesTable.js'
import { isUUIDType } from '../utilities/isUUIDType.js'
import {
InternalBlockTableNameIndex,
setInternalBlockIndex,
Expand Down Expand Up @@ -945,7 +946,7 @@ export const traverseFields = ({
const tableName = adapter.tableNameMap.get(toSnakeCase(field.relationTo))

// get the id type of the related collection
let colType: IDType = adapter.idType === 'uuid' ? 'uuid' : 'integer'
let colType: IDType = isUUIDType(adapter.idType) ? 'uuid' : 'integer'
const relatedCollectionCustomID = relationshipConfig.fields.find(
(field) => fieldAffectsData(field) && field.name === 'id',
)
Expand Down
5 changes: 5 additions & 0 deletions packages/drizzle/src/sqlite/columnToCodeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export const columnToCodeConverter: ColumnToCodeConverter = ({
defaultStatement = `$defaultFn(() => randomUUID())`
}

if (column.defaultV7) {
addImport('uuid', 'v7 as uuidv7')
defaultStatement = `$defaultFn(() => uuidv7())`
}

break
}

Expand Down
6 changes: 5 additions & 1 deletion packages/drizzle/src/sqlite/schema/buildDrizzleTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
text,
uniqueIndex,
} from 'drizzle-orm/sqlite-core'
import { v4 as uuidv4 } from 'uuid'
import { v4 as uuidv4, v7 as uuidv7 } from 'uuid'

import type { BuildDrizzleTable, RawColumn } from '../../types.js'

Expand Down Expand Up @@ -72,6 +72,10 @@ export const buildDrizzleTable: BuildDrizzleTable = ({ adapter, locales, rawTabl
builder = builder.$defaultFn(() => uuidv4())
}

if (column.defaultV7) {
builder = builder.$defaultFn(() => uuidv7())
}

columns[key] = builder
break
}
Expand Down
11 changes: 11 additions & 0 deletions packages/drizzle/src/sqlite/schema/setColumnID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export const setColumnID: SetColumnID = ({ adapter, columns, fields }) => {
return 'uuid'
}

if (adapter.idType === 'uuidv7') {
columns.id = {
name: 'id',
type: 'uuid',
defaultV7: true,
primaryKey: true,
}

return 'uuid'
}

columns.id = {
name: 'id',
type: 'integer',
Expand Down
2 changes: 1 addition & 1 deletion packages/drizzle/src/sqlite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type BaseSQLiteArgs = {
blocksAsJSON?: boolean
/** Generated schema from payload generate:db-schema file path */
generateSchemaOutputFile?: string
idType?: 'number' | 'uuid'
idType?: 'number' | 'uuid' | 'uuidv7'
localesSuffix?: string
logger?: DrizzleConfig['logger']
migrationDir?: string
Expand Down
4 changes: 3 additions & 1 deletion packages/drizzle/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ export type TimestampRawColumn = {
*/
export type UUIDRawColumn = {
defaultRandom?: boolean
/** App-side UUID v7 default (Postgres & SQLite); mutually exclusive with defaultRandom in practice */
defaultV7?: boolean
type: 'uuid'
} & BaseRawColumn

Expand Down Expand Up @@ -404,7 +406,7 @@ export interface DrizzleAdapter extends BaseDatabaseAdapter {
fieldConstraints: Record<string, Record<string, string>>

foreignKeys: Set<string>
idType: 'serial' | 'uuid'
idType: 'serial' | 'uuid' | 'uuidv7'
indexes: Set<string>
initializing: Promise<void>
insert: Insert
Expand Down
1 change: 1 addition & 0 deletions packages/drizzle/src/utilities/getCollectionIdType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const typeMap: Record<string, 'number' | 'text'> = {
serial: 'number',
text: 'text',
uuid: 'text',
uuidv7: 'text',
}

export const getCollectionIdType = ({
Expand Down
1 change: 1 addition & 0 deletions packages/drizzle/src/utilities/isUUIDType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isUUIDType = (idType: string): boolean => idType === 'uuid' || idType === 'uuidv7'
Loading