Skip to content

Commit

Permalink
fix: change schema to speed up queries
Browse files Browse the repository at this point in the history
bucket_id is no longer uuid and is user configurable. By default it is the same as bucket_name
  • Loading branch information
inian committed Mar 18, 2021
1 parent cdddae0 commit 4fc4b03
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 182 deletions.
7 changes: 7 additions & 0 deletions src/routes/bucket/createBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const createBucketBodySchema = {
type: 'object',
properties: {
name: { type: 'string' },
id: { type: 'string' },
},
required: ['name'],
} as const
Expand Down Expand Up @@ -37,11 +38,17 @@ export default async function routes(fastify: FastifyInstance) {
const owner = await getOwner(jwt)

const { name: bucketName } = request.body
let id = request.body.id
if (!id) {
//by default set the id as the name of the bucket
id = bucketName
}

const { data: results, error, status } = await postgrest
.from<Bucket>('buckets')
.insert([
{
id,
name: bucketName,
owner,
},
Expand Down
15 changes: 2 additions & 13 deletions src/routes/object/copyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export default async function routes(fastify: FastifyInstance) {
const postgrest = getPostgrestClient(jwt)
const objectResponse = await postgrest
.from<Obj>('objects')
.select('*, buckets(*)')
.select('*')
.match({
name: sourceKey,
'buckets.name': bucketName,
bucket_id: bucketName,
})
.single()

Expand All @@ -66,17 +66,6 @@ export default async function routes(fastify: FastifyInstance) {
const { data: origObject } = objectResponse
console.log('origObject', origObject)

if (!origObject.buckets) {
// @todo why is this check necessary?
// if corresponding bucket is not found, i want the object also to not be returned
// is it cos of https://github.com/PostgREST/postgrest/issues/1075 ?
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}

const newObject = Object.assign({}, origObject, {
name: destinationKey,
id: undefined,
Expand Down
25 changes: 3 additions & 22 deletions src/routes/object/createObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
import { getPostgrestClient, getOwner, transformPostgrestError } from '../../utils'
import { uploadObject, initClient, deleteObject } from '../../utils/s3'
import { getConfig } from '../../utils/config'
import { Obj, Bucket, AuthenticatedRequest } from '../../types/types'
import { Obj, AuthenticatedRequest } from '../../types/types'
import { FromSchema } from 'json-schema-to-ts'

const { region, projectRef, globalS3Bucket, globalS3Endpoint, serviceKey } = getConfig()
Expand Down Expand Up @@ -67,25 +67,6 @@ export default async function routes(fastify: FastifyInstance) {
message: err.message,
})
}
// @todo how to merge these into one query?
// i can create a view and add INSTEAD OF triggers..is that the way to do it?
const bucketResponse = await postgrest
.from<Bucket>('buckets')
.select('id')
.eq('name', bucketName)
.single()

if (bucketResponse.error) {
const { error } = bucketResponse
console.log(error)
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}

const { data: bucket } = bucketResponse

const { data: results, error, status } = await postgrest
.from<Obj>('objects')
Expand All @@ -94,7 +75,7 @@ export default async function routes(fastify: FastifyInstance) {
{
name: objectName,
owner: owner,
bucket_id: bucket.id,
bucket_id: bucketName,
metadata: {
mimetype: data.mimetype,
cacheControl,
Expand Down Expand Up @@ -136,7 +117,7 @@ export default async function routes(fastify: FastifyInstance) {
.delete()
.match({
name: objectName,
bucket_id: bucket.id,
bucket_id: bucketName,
})
.single()
await deleteObject(client, globalS3Bucket, s3Key)
Expand Down
22 changes: 2 additions & 20 deletions src/routes/object/deleteObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { deleteObject, initClient } from '../../utils/s3'
import { getConfig } from '../../utils/config'
import { Obj, Bucket, AuthenticatedRequest } from '../../types/types'
import { Obj, AuthenticatedRequest } from '../../types/types'
import { FromSchema } from 'json-schema-to-ts'

const { region, projectRef, globalS3Bucket, globalS3Endpoint } = getConfig()
Expand Down Expand Up @@ -48,32 +48,14 @@ export default async function routes(fastify: FastifyInstance) {
const objectName = request.params['*']

const postgrest = getPostgrestClient(jwt)
// @todo how to merge these into one query?
const bucketResponse = await postgrest
.from<Bucket>('buckets')
.select('id')
.eq('name', bucketName)
.single()

if (bucketResponse.error) {
const { error } = bucketResponse
console.log(error)
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}
console.log(bucketResponse.body)
const { data: bucket } = bucketResponse

// todo what if objectName is * or something
const objectResponse = await postgrest
.from<Obj>('objects')
.delete()
.match({
name: objectName,
bucket_id: bucket.id,
bucket_id: bucketName,
})
.single()

Expand Down
17 changes: 1 addition & 16 deletions src/routes/object/deleteObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,11 @@ export default async function routes(fastify: FastifyInstance) {
const prefixes = request.body['prefixes']

const postgrest = getPostgrestClient(jwt)
// @todo how to merge these into one query?
const { data: bucket, error: bucketError } = await postgrest
.from('buckets')
.select('id')
.eq('name', bucketName)
.single()

console.log(bucket, bucketError)
if (bucketError) {
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}

const objectResponse = await postgrest
.from<Obj>('objects')
.delete()
.eq('bucket_id', bucket.id)
.eq('bucket_id', bucketName)
.in('name', prefixes)

if (objectResponse.error) {
Expand Down
14 changes: 2 additions & 12 deletions src/routes/object/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export default async function routes(fastify: FastifyInstance) {

const objectResponse = await postgrest
.from<Obj>('objects')
.select('*, buckets(*)')
.select('*')
.match({
name: objectName,
'buckets.name': bucketName,
bucket_id: bucketName,
})
.single()

Expand All @@ -56,16 +56,6 @@ export default async function routes(fastify: FastifyInstance) {
console.log(error)
return response.status(400).send(transformPostgrestError(error, status))
}
const { data: results } = objectResponse

if (!results.buckets) {
// @todo why is this check necessary?
return response.status(400).send({
statusCode: 404,
error: 'Not found',
message: 'The requested bucket was not found',
})
}

// send the object from s3
const s3Key = `${projectRef}/${bucketName}/${objectName}`
Expand Down
11 changes: 1 addition & 10 deletions src/routes/object/getSignedURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default async function routes(fastify: FastifyInstance) {
.select('*, buckets(*)')
.match({
name: objectName,
'buckets.name': bucketName,
bucket_id: bucketName,
})
.single()

Expand All @@ -71,15 +71,6 @@ export default async function routes(fastify: FastifyInstance) {
const { data: results } = objectResponse
console.log(results)

if (!results.buckets) {
// @todo why is this check necessary?
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}

console.log(`going to sign ${request.url}`)
const urlParts = request.url.split('/')
const urlToSign = decodeURI(urlParts.splice(3).join('/'))
Expand Down
23 changes: 2 additions & 21 deletions src/routes/object/renameObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { initClient, copyObject, deleteObject } from '../../utils/s3'
import { getConfig } from '../../utils/config'
import { Obj, Bucket, AuthenticatedRequest } from '../../types/types'
import { Obj, AuthenticatedRequest } from '../../types/types'
import { FromSchema } from 'json-schema-to-ts'

const { region, projectRef, globalS3Bucket, globalS3Endpoint } = getConfig()
Expand Down Expand Up @@ -49,33 +49,14 @@ export default async function routes(fastify: FastifyInstance) {
const { destinationKey, sourceKey, bucketName } = request.body

const postgrest = getPostgrestClient(jwt)
// @todo how to merge these into one query?
const bucketResponse = await postgrest
.from<Bucket>('buckets')
.select('id')
.eq('name', bucketName)
.single()

if (bucketResponse.error) {
const { error } = bucketResponse
console.log(error)
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}

const { data: bucket } = bucketResponse
console.log(bucket)

const objectResponse = await postgrest
.from<Obj>('objects')
.update({
last_accessed_at: new Date().toISOString(),
name: destinationKey,
})
.match({ bucket_id: bucket.id, name: sourceKey })
.match({ bucket_id: bucketName, name: sourceKey })
.single()

if (objectResponse.error) {
Expand Down
26 changes: 4 additions & 22 deletions src/routes/object/updateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
import { getPostgrestClient, getOwner, transformPostgrestError } from '../../utils'
import { uploadObject, initClient } from '../../utils/s3'
import { getConfig } from '../../utils/config'
import { Obj, Bucket, AuthenticatedRequest } from '../../types/types'
import { Obj, AuthenticatedRequest } from '../../types/types'
import { FromSchema } from 'json-schema-to-ts'

const { region, projectRef, globalS3Bucket, globalS3Endpoint } = getConfig()
Expand Down Expand Up @@ -47,32 +47,14 @@ export default async function routes(fastify: FastifyInstance) {
const data = await request.file()
/* @ts-expect-error: https://github.com/aws/aws-sdk-js-v3/issues/2085 */
const cacheTime = data.fields.cacheControl?.value
const cacheControl: string = `max-age=${cacheTime}` ?? 'no-cache'
// @todo maintain the old cache time if nothing is provided here
const cacheControl: string = cacheTime ? `max-age=${cacheTime}` : 'no-cache'

const { bucketName } = request.params
const objectName = request.params['*']

const postgrest = getPostgrestClient(jwt)
const owner = await getOwner(jwt)
// @todo how to merge these into one query?
const bucketResponse = await postgrest
.from<Bucket>('buckets')
.select('id')
.eq('name', bucketName)
.single()

if (bucketResponse.error) {
const { error } = bucketResponse
console.log(error)
return response.status(400).send({
statusCode: '404',
error: 'Not found',
message: 'The requested bucket was not found',
})
}

const { data: bucket } = bucketResponse
console.log(bucket)

const objectResponse = await postgrest
.from<Obj>('objects')
Expand All @@ -84,7 +66,7 @@ export default async function routes(fastify: FastifyInstance) {
cacheControl,
},
})
.match({ bucket_id: bucket.id, name: objectName })
.match({ bucket_id: bucketName, name: objectName })
.single()

if (objectResponse.error) {
Expand Down
Loading

0 comments on commit 4fc4b03

Please sign in to comment.