-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48a4149
commit 4dde154
Showing
7 changed files
with
122 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { drizzle } from 'drizzle-orm/d1'; | ||
import * as models from './models'; | ||
import cleanup from './cleanup'; | ||
import { createShortUrl } from './controller'; | ||
import { appEnv } from './index.spec'; | ||
|
||
test('cleanup', async () => { | ||
const { DB } = appEnv; | ||
const d = drizzle(DB, { schema: models }); | ||
|
||
const validDate = new Date(); | ||
validDate.setMonth(validDate.getMonth() + 1); | ||
|
||
const expiredShortLink = await createShortUrl(d, 'https://example.com', new Date(2000, 1, 1).getTime()); | ||
const validShortLink = await createShortUrl(d, 'https://example.com', validDate.getTime()); | ||
const noExpiryShortLink = await createShortUrl(d, 'https://example.com', null); | ||
|
||
const deleted = await cleanup(d, { | ||
accessKeyId: appEnv.S3_ACCESS_KEY_ID, | ||
secretAccessKey: appEnv.S3_SECRET_ACCESS_KEY, | ||
bucket: appEnv.S3_BUCKET, | ||
endpointUrl: appEnv.S3_ENDPOINT_URL, | ||
region: appEnv.S3_REGION, | ||
}); | ||
|
||
expect(deleted).toContain(expiredShortLink.id); | ||
expect(deleted).not.toContain(validShortLink.id); | ||
expect(deleted).not.toContain(noExpiryShortLink.id); | ||
}); |
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,38 @@ | ||
import { type DrizzleD1Database } from 'drizzle-orm/d1'; | ||
import { eq, lt } from 'drizzle-orm'; | ||
import * as models from './models'; | ||
import { deleteObject, type S3Configuration } from './s3'; | ||
|
||
export default async function cleanup( | ||
db: DrizzleD1Database<typeof models>, | ||
s3Config: S3Configuration, | ||
): Promise<string[]> { | ||
const deleted: string[] = []; | ||
const toCleanUp = await db.query.shortLinks.findMany({ | ||
where: lt(models.shortLinks.expiresAt, new Date()), | ||
}); | ||
|
||
await Promise.all(toCleanUp.map(async (shortLink) => { | ||
switch (shortLink.type) { | ||
case 'url': | ||
await db.delete(models.shortLinkUrls).where(eq(models.shortLinkUrls.id, shortLink.id)); | ||
await db.delete(models.shortLinks).where(eq(models.shortLinks.id, shortLink.id)); | ||
break; | ||
case 'paste': | ||
await db.delete(models.shortLinkPastes).where(eq(models.shortLinkPastes.id, shortLink.id)); | ||
await db.delete(models.shortLinks).where(eq(models.shortLinks.id, shortLink.id)); | ||
break; | ||
case 'upload': | ||
await db.delete(models.shortLinkUploads) | ||
.where(eq(models.shortLinkUploads.id, shortLink.id)); | ||
await db.delete(models.shortLinks).where(eq(models.shortLinks.id, shortLink.id)); | ||
await deleteObject(s3Config, shortLink.id); | ||
break; | ||
default: | ||
throw new Error(`Unexpected short link type ${shortLink.type}`); | ||
} | ||
deleted.push(shortLink.id); | ||
})); | ||
|
||
return deleted; | ||
} |
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
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
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
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
/// <reference types="vitest" /> | ||
import { defineConfig } from 'vitest/config' | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true | ||
} | ||
}) | ||
globals: true, | ||
fileParallelism: false, | ||
}, | ||
}); |
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