Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db-postgres)!: relationship column #6339

Merged
merged 67 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
8b97a09
wip: postgres relationship table schema
DanRibbens Apr 9, 2024
c13fe08
fix(db-postgres): use formatted column name in relationships
DanRibbens Apr 9, 2024
faf0fa3
chore(db-postgres): relationship transform for write
DanRibbens Apr 9, 2024
c579669
chore(db-postgres): simple relationship transform for read
DanRibbens Apr 9, 2024
7dc911c
chore(db-postgres): transform relationships objects to id for write
DanRibbens Apr 9, 2024
4261d0a
chore(db-postgres): handle localized relationship
DanRibbens Apr 10, 2024
da7687f
chore(db-postgres): WIP query simple relationship
DanRibbens Apr 11, 2024
bdc972f
chore: test config to be reverted
DanRibbens Apr 11, 2024
4663f4b
chore(db-postgres): continued WIP of simplified relationship columns
DanRibbens Apr 29, 2024
b68393e
chore(db-postgres): read hasMany relationship fix
DanRibbens Apr 30, 2024
a08508f
chore(db-postgres): read transform simple relationship
DanRibbens Apr 30, 2024
b010519
chore: WIP versions working
DanRibbens May 7, 2024
55333a3
chore: localized versions relationship
DanRibbens May 7, 2024
207cfae
chore: localized relationship in array
DanRibbens May 7, 2024
8df3591
chore: localized relationship in blocks
DanRibbens May 7, 2024
ef44748
chore: find relations array locales columns
DanRibbens May 8, 2024
07d6272
test: wip
DanRibbens May 10, 2024
8abc4d2
chore: blocks and arrays localize relations to base table
DanRibbens May 10, 2024
6b0fce2
chore: fix localized hasMany select
DanRibbens May 10, 2024
9f79cfb
chore: fix localized relationships regression
DanRibbens May 10, 2024
0c13a94
chore(db-postgres): join aliases before joins in query chaining
DanRibbens May 13, 2024
4c0a492
chore(db-postgres): fix custom db name with versions
DanRibbens May 13, 2024
7be66b9
chore(db-postgres): WIP relationships migration
DanRibbens May 14, 2024
ca300ea
chore(db-postgres): wip relations migration
DanRibbens May 15, 2024
6d4859f
fix: loader support for server-only
jmikrut May 16, 2024
6eb9941
chore: begins work on migration for pg v2-v3
jmikrut May 16, 2024
66575fc
Merge branch 'beta' of github.com:payloadcms/payload into chore/pg-v2…
jmikrut May 17, 2024
31d256f
chore: progress
jmikrut May 17, 2024
e837a0d
chore: progress
jmikrut May 17, 2024
e6cd806
chore(db-postgres): merge v2-v3-migration
DanRibbens May 18, 2024
72ca92d
chore(db-postgres): delete rows from rels tables
DanRibbens May 18, 2024
ae730fe
chore(db-postgres): v2-v3 migrate use transactions
DanRibbens May 18, 2024
664f1ef
chore(db-postgres): optimize v2-v3 migration
DanRibbens May 18, 2024
b11c33b
chore: merge main
DanRibbens May 20, 2024
3f1ebbb
chore: test config
DanRibbens May 21, 2024
9c60232
feat: add predefinedMigrations to core (#6437)
DanRibbens May 21, 2024
66b4873
Merge branch 'feat/postgres-relationship-column' of github.com:payloa…
DanRibbens May 21, 2024
3e54a45
chore: wip create relationships migrations
DanRibbens May 21, 2024
2f0a645
chore(db-postgres): predefinedMigrations esm imports
DanRibbens May 22, 2024
8bc58fb
chore(db-postgres): cleanup createMigration
DanRibbens May 22, 2024
acf5043
chore(db-postgres): remove json generation for predefined migration
DanRibbens May 22, 2024
5b10693
chore(db-postgres): use drizzle generated statements in migration
DanRibbens May 24, 2024
1fee6a0
chore(db-postgres): v2-v3 migration sql raw
DanRibbens May 24, 2024
c23b434
chore(db-postgres): cleanup v3 migration functions
DanRibbens May 24, 2024
c2789e4
chore(db-postgres): cleanup v3 migration functions
DanRibbens May 25, 2024
df7d37a
merge beta
DanRibbens May 25, 2024
5571323
chore(db-postgres): prevent population simple relationship depth 0
DanRibbens May 25, 2024
4c98d9f
Merge branch 'beta' into feat/postgres-relationship-column
DanRibbens May 25, 2024
001cb29
chore: skip pagination in restoreVersion
DanRibbens May 25, 2024
65b012d
chore: remove unnecessary ts-expect-error
DanRibbens May 25, 2024
66e68fe
chore(db-postgres): onDelete set not null
DanRibbens May 25, 2024
881fc36
chore(db-postgres): transform custom ID type
DanRibbens May 25, 2024
afc4618
chore(db-postgres): transform custom ID type not null
DanRibbens May 25, 2024
231c69b
chore(db-postgres): transform bug from refactor
DanRibbens May 25, 2024
2a8f144
chore(db-postgres): fix nested join query
DanRibbens May 26, 2024
0c2fd92
chore(db-postgres): correct joins order
DanRibbens May 26, 2024
6764ec6
chore(db-postgres): fix localized relationship
DanRibbens May 26, 2024
b82791b
test(db-postgres): add localized group subfield query
DanRibbens May 26, 2024
67851f9
chore(db-postgres): merge joins and joinAliases to one array
DanRibbens May 26, 2024
c07c5ce
chore(db-postgres): skip postgres migration test
DanRibbens May 26, 2024
148b8c8
chore(db-postgres): handle null localized relationship
DanRibbens May 26, 2024
d97bacf
chore(db-postgres): fix relations to subBlocks
DanRibbens May 28, 2024
2d4d142
merge main
DanRibbens May 28, 2024
5d4863a
chore: revert _community test
DanRibbens May 28, 2024
4580969
fix: removes extra loop in fetchAndResave
PatrikKozak May 28, 2024
c50bf9b
fix: adds pagination
PatrikKozak May 28, 2024
400053d
chore: adds order by to migration script
jmikrut May 29, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CountryField } from 'payload-plugin-form-builder/dist/types'
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form';
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form'

import React from 'react'
import { Controller } from 'react-hook-form'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SelectField } from 'payload-plugin-form-builder/dist/types'
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form';
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form'

import React from 'react'
import { Controller } from 'react-hook-form'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StateField } from 'payload-plugin-form-builder/dist/types'
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form';
import type { Control, FieldErrorsImpl, FieldValues } from 'react-hook-form'

import React from 'react'
import { Controller } from 'react-hook-form'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import type React from 'react';
import type React from 'react'

import { useModal } from '@faceless-ui/modal'
import { usePathname } from 'next/navigation'
Expand Down
2 changes: 1 addition & 1 deletion examples/form-builder/next-app/components/Gutter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Ref } from 'react';
import type { Ref } from 'react'

import React, { forwardRef } from 'react'

Expand Down
6 changes: 3 additions & 3 deletions examples/live-preview/next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 3001",
"build": "next build",
"start": "next start -p 3001",
"lint": "next lint"
"dev": "next dev -p 3001",
"lint": "next lint",
"start": "next start -p 3001"
},
"dependencies": {
"@payloadcms/live-preview-react": "3.0.0-beta.28",
Expand Down
6 changes: 3 additions & 3 deletions examples/live-preview/next-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 3001",
"build": "next build",
"start": "next start -p 3001",
"lint": "next lint"
"dev": "next dev -p 3001",
"lint": "next lint",
"start": "next start -p 3001"
},
"dependencies": {
"@payloadcms/live-preview-react": "3.0.0-beta.28",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ElementType } from 'react';
import type { ElementType } from 'react'

import Link from 'next/link'
import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Ref } from 'react';
import type { Ref } from 'react'

import React, { forwardRef } from 'react'

Expand Down
2 changes: 1 addition & 1 deletion examples/live-preview/payload/src/fields/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const link: LinkType = ({ appearances, disableLabel = false, overrides = {} } =
]

if (appearances) {
appearanceOptionsToUse = appearances.map(appearance => appearanceOptions[appearance])
appearanceOptionsToUse = appearances.map((appearance) => appearanceOptions[appearance])
}

linkResult.fields.push({
Expand Down
50 changes: 15 additions & 35 deletions packages/db-mongodb/src/createMigration.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
/* eslint-disable no-restricted-syntax, no-await-in-loop */
import type { CreateMigration } from 'payload/database'
import type { CreateMigration, MigrationTemplateArgs } from 'payload/database'

import fs from 'fs'
import path from 'path'
import { getPredefinedMigration } from 'payload/database'
import { fileURLToPath } from 'url'

const migrationTemplate = (upSQL?: string, downSQL?: string) => `import {
const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {
MigrateUpArgs,
MigrateDownArgs,
} from "@payloadcms/db-mongodb";
} from '@payloadcms/db-mongodb'
${imports}

export async function up({ payload }: MigrateUpArgs): Promise<void> {
${upSQL ?? ` // Migration code`}
};
}

export async function down({ payload }: MigrateDownArgs): Promise<void> {
${downSQL ?? ` // Migration code`}
};
}
`

export const createMigration: CreateMigration = async function createMigration({
Expand All @@ -31,36 +33,14 @@ export const createMigration: CreateMigration = async function createMigration({
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}

let migrationFileContent: string | undefined

// Check for predefined migration.
// Either passed in via --file or prefixed with @payloadcms/db-mongodb/
if (file || migrationName?.startsWith('@payloadcms/db-mongodb/')) {
if (!file) file = migrationName

const predefinedMigrationName = file.replace('@payloadcms/db-mongodb/', '')
migrationName = predefinedMigrationName
const cleanPath = path.join(dirname, `../predefinedMigrations/${predefinedMigrationName}.js`)

// Check if predefined migration exists
if (fs.existsSync(cleanPath)) {
let migration = await eval(
`${typeof require === 'function' ? 'require' : 'import'}(${cleanPath})`,
)
if ('default' in migration) migration = migration.default
const { down, up } = migration

migrationFileContent = migrationTemplate(up, down)
} else {
payload.logger.error({
msg: `Canned migration ${predefinedMigrationName} not found.`,
})
process.exit(1)
}
} else {
migrationFileContent = migrationTemplate()
}
const predefinedMigration = await getPredefinedMigration({
dirname,
file,
migrationName,
payload,
})

const migrationFileContent = migrationTemplate(predefinedMigration)

const [yyymmdd, hhmmss] = new Date().toISOString().split('T')
const formattedDate = yyymmdd.replace(/\D/g, '')
Expand Down
15 changes: 13 additions & 2 deletions packages/db-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"import": "./src/types.ts",
"require": "./src/types.ts",
"types": "./src/types.ts"
},
"./migration-utils": {
"import": "./src/exports/migration-utils.ts",
"require": "./src/exports/migration-utils.ts",
"types": "./src/exports/migration-utils.ts"
}
},
"main": "./src/index.ts",
Expand All @@ -30,11 +35,12 @@
"mock.js"
],
"scripts": {
"build": "pnpm build:swc && pnpm build:types",
"build": "pnpm build:swc && pnpm build:types && pnpm renamePredefinedMigrations",
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
"build:types": "tsc --emitDeclarationOnly --outDir dist",
"clean": "rimraf {dist,*.tsbuildinfo}",
"prepublishOnly": "pnpm clean && pnpm turbo build"
"prepublishOnly": "pnpm clean && pnpm turbo build",
"renamePredefinedMigrations": "tsx ./scripts/renamePredefinedMigrations.ts"
},
"dependencies": {
"@libsql/client": "^0.5.2",
Expand Down Expand Up @@ -66,6 +72,11 @@
"import": "./dist/types.js",
"require": "./dist/types.js",
"types": "./dist/types.d.ts"
},
"./migration-utils": {
"import": "./dist/exports/migration-utils.js",
"require": "./dist/exports/migration-utils.js",
"types": "./dist/exports/migration-utils.d.ts"
}
},
"main": "./dist/index.js",
Expand Down
13 changes: 13 additions & 0 deletions packages/db-postgres/relationships-v2-v3.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const imports = `import { migratePostgresV2toV3 } from '@payloadcms/migratePostgresV2toV3'`;
const up = ` await migratePostgresV2toV3({
// enables logging of changes that will be made to the database
debug: false,
// skips calls that modify schema or data
dryRun: false,
payload,
req,
})
`;
export { imports, up };

//# sourceMappingURL=relationships-v2-v3.js.map
18 changes: 18 additions & 0 deletions packages/db-postgres/scripts/renamePredefinedMigrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'fs'
import path from 'path'

/**
* Changes built .js files to .mjs to for ESM imports
*/
const rename = () => {
fs.readdirSync(path.resolve('./dist/predefinedMigrations'))
.filter((f) => {
return f.endsWith('.js')
})
.forEach((file) => {
const newPath = path.join('./dist/predefinedMigrations', file)
fs.renameSync(newPath, newPath.replace('.js', '.mjs'))
})
}

rename()
9 changes: 1 addition & 8 deletions packages/db-postgres/src/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const count: Count = async function count(
const db = this.sessions[req.transactionID]?.db || this.drizzle
const table = this.tables[tableName]

const { joinAliases, joins, where } = await buildQuery({
const { joins, where } = await buildQuery({
adapter: this,
fields: collectionConfig.fields,
locale,
Expand All @@ -31,13 +31,6 @@ export const count: Count = async function count(

const selectCountMethods: ChainedMethods = []

joinAliases.forEach(({ condition, table }) => {
selectCountMethods.push({
args: [table, condition],
method: 'leftJoin',
})
})

Object.entries(joins).forEach(([joinTable, condition]) => {
if (joinTable) {
selectCountMethods.push({
Expand Down
Loading