Files
payload/packages/drizzle/src/migrateRefresh.ts
Sasha 0e5bda9a74 feat: make req partial and optional in DB / Local API operations (#9935)
### What?
Previously, the `req` argument:
In database operations (e.g `payload.db`) was required and you needed to
pass the whole `req` with all the properties. This is confusing because
in database operations we never use its properties outside of
`req.transactionID` and `req.t`, both of which should be optional as
well.

Now, you don't have to do that cast:
```ts
payload.db.findOne({
  collection: 'posts',
  req: {} as PayloadRequest,
  where: {
    id: {
      equals: 1,
    },
  },
})
```
Becomes:
```ts
payload.db.findOne({
  collection: 'posts',
  where: {
    id: {
      equals: 1,
    },
  },
})
```

If you need to use transactions, you're not required to do the `as` cast
as well now, as the `req` not only optional but also partial -
`Partial<PayloadRequest>`.
`initTransaction`, `commitTransaction`, `killTransaction` utilities are
typed better now as well. They do not require to you pass all the
properties of `req`, but only `payload` -
`MarkRequired<Partial<PayloadRequest>, 'payload'>`
```ts
const req = { payload }
await initTransaction(req)
await payload.db.create({
  collection: "posts",
  data: {},
  req
})
await commitTransaction(req)
```

The same for the Local API. Internal operations (for example
`packages/payload/src/collections/operations/find.ts`) still accept the
whole `req`, but local ones
(`packages/payload/src/collections/operations/local/find.ts`) which are
used through `payload.` now accept `Partial<PayloadRequest>`, as they
pass it through to internal operations with `createLocalReq`.

So now, this is also valid, while previously you had to do `as` cast for
`req`.
```ts
const req = { payload }
await initTransaction(req)
await payload.create({
  collection: "posts",
  data: {},
  req
})
await commitTransaction(req)
```


Marked as deprecated `PayloadRequest['transactionIDPromise']` to remove
in the next major version. It was never used anywhere.
Refactored `withSession` that returns an object to `getSession` that
returns just `ClientSession`. Better type safety for arguments
Deduplicated in all drizzle operations to `getTransaction(this, req)`
utility:
```ts
const db = this.sessions[await req?.transactionID]?.db || this.drizzle
```
Added fallback for throwing unique validation errors in database
operations when `req.t` is not available.

In migration `up` and `down` functions our `req` is not partial, while
we used to passed `req` with only 2 properties - `payload` and
`transactionID`. This is misleading and you can't access for example
`req.t`.
Now, to achieve "real" full `req` - we generate it with `createLocalReq`
in all migration functions.

This all is backwards compatible. In all public API places where you
expect the full `req` (like hooks) you still have it.

### Why?
Better DX, more expected types, less errors because of types casting.
2024-12-18 22:43:37 -05:00

108 lines
3.0 KiB
TypeScript

import {
commitTransaction,
createLocalReq,
getMigrations,
initTransaction,
killTransaction,
readMigrationFiles,
} from 'payload'
import type { DrizzleAdapter } from './types.js'
import { getTransaction } from './utilities/getTransaction.js'
import { migrationTableExists } from './utilities/migrationTableExists.js'
import { parseError } from './utilities/parseError.js'
/**
* Run all migration down functions before running up
*/
export async function migrateRefresh(this: DrizzleAdapter) {
const { payload } = this
const migrationFiles = await readMigrationFiles({ payload })
const { existingMigrations, latestBatch } = await getMigrations({
payload,
})
if (!existingMigrations?.length) {
payload.logger.info({ msg: 'No migrations to rollback.' })
return
}
payload.logger.info({
msg: `Rolling back batch ${latestBatch} consisting of ${existingMigrations.length} migration(s).`,
})
const req = await createLocalReq({}, payload)
// Reverse order of migrations to rollback
existingMigrations.reverse()
for (const migration of existingMigrations) {
try {
const migrationFile = migrationFiles.find((m) => m.name === migration.name)
if (!migrationFile) {
throw new Error(`Migration ${migration.name} not found locally.`)
}
payload.logger.info({ msg: `Migrating down: ${migration.name}` })
const start = Date.now()
await initTransaction(req)
const db = await getTransaction(this, req)
await migrationFile.down({ db, payload, req })
payload.logger.info({
msg: `Migrated down: ${migration.name} (${Date.now() - start}ms)`,
})
const tableExists = await migrationTableExists(this)
if (tableExists) {
await payload.delete({
collection: 'payload-migrations',
req,
where: {
name: {
equals: migration.name,
},
},
})
}
await commitTransaction(req)
} catch (err: unknown) {
await killTransaction(req)
payload.logger.error({
err,
msg: parseError(err, `Error running migration ${migration.name}. Rolling back.`),
})
process.exit(1)
}
}
// Run all migrate up
for (const migration of migrationFiles) {
payload.logger.info({ msg: `Migrating: ${migration.name}` })
try {
const start = Date.now()
await initTransaction(req)
await migration.up({ payload, req })
await payload.create({
collection: 'payload-migrations',
data: {
name: migration.name,
executed: true,
},
req,
})
await commitTransaction(req)
payload.logger.info({ msg: `Migrated: ${migration.name} (${Date.now() - start}ms)` })
} catch (err: unknown) {
await killTransaction(req)
payload.logger.error({
err,
msg: parseError(err, `Error running migration ${migration.name}. Rolling back.`),
})
process.exit(1)
}
}
}