-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uploads): Add setup command (#11423)
- Loading branch information
1 parent
18dca8d
commit 061ded0
Showing
10 changed files
with
433 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/cli/src/commands/setup/uploads/__codemod_tests__/dbCodemod.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import path from 'node:path' | ||
|
||
import { describe, it, expect } from 'vitest' | ||
|
||
import { runTransform } from '../../../../lib/runTransform' | ||
|
||
describe('Db codemod', () => { | ||
it('Handles the default db case', async () => { | ||
await matchTransformSnapshot('dbCodemod', 'defaultDb') | ||
}) | ||
|
||
it('will throw an error if the db file has the old format', async () => { | ||
const transformResult = await runTransform({ | ||
transformPath: path.join(__dirname, '../dbCodemod.ts'), // Use TS here! | ||
targetPaths: [ | ||
path.join(__dirname, '../__testfixtures__/oldFormat.input.ts'), | ||
], | ||
}) | ||
|
||
expect(transformResult.error).toContain('ERR_OLD_FORMAT') | ||
}) | ||
}) |
26 changes: 26 additions & 0 deletions
26
packages/cli/src/commands/setup/uploads/__testfixtures__/defaultDb.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// See https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/constructor | ||
// for options. | ||
|
||
import { PrismaClient } from '@prisma/client' | ||
|
||
import { emitLogLevels, handlePrismaLogging } from '@redwoodjs/api/logger' | ||
|
||
import { logger } from './logger' | ||
|
||
const prismaClient = new PrismaClient({ | ||
log: emitLogLevels(['info', 'warn', 'error']), | ||
}) | ||
|
||
handlePrismaLogging({ | ||
db: prismaClient, | ||
logger, | ||
logLevels: ['info', 'warn', 'error'], | ||
}) | ||
|
||
/** | ||
* Global Prisma client extensions should be added here, as $extend | ||
* returns a new instance. | ||
* export const db = prismaClient.$extend(...) | ||
* Add any .$on hooks before using $extend | ||
*/ | ||
export const db = prismaClient |
28 changes: 28 additions & 0 deletions
28
packages/cli/src/commands/setup/uploads/__testfixtures__/defaultDb.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// See https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/constructor | ||
// for options. | ||
|
||
import { PrismaClient } from '@prisma/client' | ||
|
||
import { emitLogLevels, handlePrismaLogging } from '@redwoodjs/api/logger' | ||
|
||
import { logger } from './logger' | ||
|
||
import { storagePrismaExtension } from './uploads' | ||
|
||
const prismaClient = new PrismaClient({ | ||
log: emitLogLevels(['info', 'warn', 'error']), | ||
}) | ||
|
||
handlePrismaLogging({ | ||
db: prismaClient, | ||
logger, | ||
logLevels: ['info', 'warn', 'error'], | ||
}) | ||
|
||
/** | ||
* Global Prisma client extensions should be added here, as $extend | ||
* returns a new instance. | ||
* export const db = prismaClient.$extend(...) | ||
* Add any .$on hooks before using $extend | ||
*/ | ||
export const db = prismaClient.$extends(storagePrismaExtension) |
18 changes: 18 additions & 0 deletions
18
packages/cli/src/commands/setup/uploads/__testfixtures__/oldFormat.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// See https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/constructor | ||
// for options. | ||
|
||
import { PrismaClient } from '@prisma/client' | ||
|
||
import { emitLogLevels, handlePrismaLogging } from '@redwoodjs/api/logger' | ||
|
||
import { logger } from './logger' | ||
|
||
export const db = new PrismaClient({ | ||
log: emitLogLevels(['info', 'warn', 'error']), | ||
}) | ||
|
||
handlePrismaLogging({ | ||
db, | ||
logger, | ||
logLevels: ['info', 'warn', 'error'], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import j from 'jscodeshift' | ||
|
||
module.exports = function transform(fileInfo: j.FileInfo) { | ||
const root = j(fileInfo.source) | ||
|
||
// Add the import statement for storagePrismaExtension | ||
const imports = root.find(j.ImportDeclaration) | ||
|
||
imports | ||
.at(-1) // add it after the last one | ||
.insertAfter( | ||
j.importDeclaration( | ||
[j.importSpecifier(j.identifier('storagePrismaExtension'))], | ||
j.literal('./uploads'), | ||
), | ||
) | ||
|
||
// Find the export statement for db and modify it | ||
root | ||
.find(j.VariableDeclaration, { declarations: [{ id: { name: 'db' } }] }) | ||
.forEach((path) => { | ||
const dbDeclaration = path.node.declarations[0] | ||
|
||
if ( | ||
j.VariableDeclarator.check(dbDeclaration) && | ||
j.NewExpression.check(dbDeclaration.init) | ||
) { | ||
throw new Error('RW_CODEMOD_ERR_OLD_FORMAT') | ||
} | ||
|
||
if ( | ||
j.VariableDeclarator.check(dbDeclaration) && | ||
j.Expression.check(dbDeclaration.init) | ||
) { | ||
const newInit = j.callExpression( | ||
j.memberExpression(dbDeclaration.init, j.identifier('$extends')), | ||
[j.identifier('storagePrismaExtension')], | ||
) | ||
dbDeclaration.init = newInit | ||
} | ||
}) | ||
|
||
return root.toSource() | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/cli/src/commands/setup/uploads/templates/signedUrl.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { APIGatewayEvent, Context } from 'aws-lambda' | ||
|
||
import type { SignatureValidationArgs } from '@redwoodjs/storage/UrlSigner' | ||
|
||
import { urlSigner, fsStorage } from 'src/lib/uploads' | ||
|
||
export const handler = async (event: APIGatewayEvent, _context: Context) => { | ||
const fileToReturn = urlSigner.validateSignature( | ||
event.queryStringParameters as SignatureValidationArgs | ||
) | ||
|
||
const { contents, type } = await fsStorage.read(fileToReturn) | ||
|
||
return { | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': type, | ||
}, | ||
body: contents, | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/cli/src/commands/setup/uploads/templates/srcLibUploads.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createUploadsConfig, setupStorage } from '@redwoodjs/storage' | ||
import { FileSystemStorage } from '@redwoodjs/storage/FileSystemStorage' | ||
import { UrlSigner } from '@redwoodjs/storage/UrlSigner' | ||
|
||
const uploadsConfig = createUploadsConfig({ | ||
// Configure your fields here | ||
// e.g. modelName: { fields: ['fieldWithUpload']} | ||
}) | ||
|
||
export const fsStorage = new FileSystemStorage({ | ||
baseDir: './uploads', | ||
}) | ||
|
||
export const urlSigner = new UrlSigner({ | ||
secret: process.env.UPLOADS_SECRET, | ||
endpoint: '/signedUrl', | ||
}) | ||
|
||
const { saveFiles, storagePrismaExtension } = setupStorage({ | ||
uploadsConfig, | ||
storageAdapter: fsStorage, | ||
urlSigner, | ||
}) | ||
|
||
export { saveFiles, storagePrismaExtension } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers' | ||
|
||
export const command = 'uploads' | ||
|
||
export const description = | ||
'Setup uploads and storage. This will install the required packages and add the required initial configuration to your redwood app.' | ||
|
||
export const builder = (yargs) => { | ||
yargs.option('force', { | ||
alias: 'f', | ||
default: false, | ||
description: 'Overwrite existing configuration', | ||
type: 'boolean', | ||
}) | ||
} | ||
|
||
export const handler = async (options) => { | ||
recordTelemetryAttributes({ | ||
command: 'setup uploads', | ||
force: options.force, | ||
skipExamples: options.skipExamples, | ||
}) | ||
const { handler } = await import('./uploadsHandler.js') | ||
return handler(options) | ||
} |
Oops, something went wrong.