From ddbd862f9ebb7b553b1a345942fb0bdb8bc847d6 Mon Sep 17 00:00:00 2001 From: Tim Mikeladze Date: Sat, 2 Dec 2023 06:18:15 +0300 Subject: [PATCH] Wip --- .../app/upload/drizzle-node-postgres/nup.ts | 8 +--- .../src/app/upload/drizzle-postgres-js/nup.ts | 4 +- .../app/upload/edge-with-drizzle-neon/nup.ts | 7 +-- package.json | 12 ++++- src/store/drizzle/neon/DrizzlePgStore.ts | 7 +++ src/store/drizzle/neon/index.ts | 2 + .../drizzle/node-postgres/DrizzlePgStore.ts | 4 ++ src/store/drizzle/node-postgres/index.ts | 2 + .../DrizzlePgCoreStore.ts} | 45 +++++++++---------- src/store/drizzle/pg-core/DrizzlePgSchema.ts | 33 ++++++++++++++ .../drizzle/postgres-js/DrizzlePgStore.ts | 4 ++ .../postgres-js/DrizzlePostgresSchema.ts | 36 --------------- src/store/drizzle/postgres-js/index.ts | 4 +- tests/NextUpload.test.ts | 8 ++-- 14 files changed, 97 insertions(+), 79 deletions(-) create mode 100644 src/store/drizzle/neon/DrizzlePgStore.ts create mode 100644 src/store/drizzle/neon/index.ts create mode 100644 src/store/drizzle/node-postgres/DrizzlePgStore.ts create mode 100644 src/store/drizzle/node-postgres/index.ts rename src/store/drizzle/{postgres-js/DrizzlePostgresStore.ts => pg-core/DrizzlePgCoreStore.ts} (70%) create mode 100644 src/store/drizzle/pg-core/DrizzlePgSchema.ts create mode 100644 src/store/drizzle/postgres-js/DrizzlePgStore.ts delete mode 100644 src/store/drizzle/postgres-js/DrizzlePostgresSchema.ts diff --git a/examples/next-upload-example/src/app/upload/drizzle-node-postgres/nup.ts b/examples/next-upload-example/src/app/upload/drizzle-node-postgres/nup.ts index 53ea642..4b59836 100644 --- a/examples/next-upload-example/src/app/upload/drizzle-node-postgres/nup.ts +++ b/examples/next-upload-example/src/app/upload/drizzle-node-postgres/nup.ts @@ -1,7 +1,7 @@ import { nextUploadConfig } from '@/app/nextUploadConfig'; import { getDbNodePostgres } from '@/db/getDbNodePostgres'; import { NextUpload } from 'next-upload'; -import { DrizzlePostgresStore } from 'next-upload/store/drizzle/postgres-js'; +import { DrizzlePgStore } from 'next-upload/store/drizzle/node-postgres'; export const nup = new NextUpload( { @@ -11,9 +11,5 @@ export const nup = new NextUpload( ['drizzle-node-postgres']: {}, }, }, - async () => - new DrizzlePostgresStore( - // @ts-ignore - await getDbNodePostgres() - ) + async () => new DrizzlePgStore(await getDbNodePostgres()) ); diff --git a/examples/next-upload-example/src/app/upload/drizzle-postgres-js/nup.ts b/examples/next-upload-example/src/app/upload/drizzle-postgres-js/nup.ts index b925771..89d6b22 100644 --- a/examples/next-upload-example/src/app/upload/drizzle-postgres-js/nup.ts +++ b/examples/next-upload-example/src/app/upload/drizzle-postgres-js/nup.ts @@ -1,7 +1,7 @@ import { nextUploadConfig } from '@/app/nextUploadConfig'; import { getDbPostgresJs } from '@/db/getDbPostgresJs'; import { NextUpload } from 'next-upload'; -import { DrizzlePostgresStore } from 'next-upload/store/drizzle/postgres-js'; +import { DrizzlePgStore } from 'next-upload/store/drizzle/postgres-js'; export const nup = new NextUpload( { @@ -11,5 +11,5 @@ export const nup = new NextUpload( ['drizzle-postgres-js']: {}, }, }, - new DrizzlePostgresStore(getDbPostgresJs()) + new DrizzlePgStore(getDbPostgresJs()) ); diff --git a/examples/next-upload-example/src/app/upload/edge-with-drizzle-neon/nup.ts b/examples/next-upload-example/src/app/upload/edge-with-drizzle-neon/nup.ts index d8cd470..42dfe32 100644 --- a/examples/next-upload-example/src/app/upload/edge-with-drizzle-neon/nup.ts +++ b/examples/next-upload-example/src/app/upload/edge-with-drizzle-neon/nup.ts @@ -1,7 +1,7 @@ import { nextUploadConfig } from '@/app/nextUploadConfig'; import { getDbServerless } from '@/db/getDbServerless'; import { NextUpload } from 'next-upload'; -import { DrizzlePostgresStore } from 'next-upload/store/drizzle/postgres-js'; +import { DrizzlePgStore } from 'next-upload/store/drizzle/neon'; export const nup = new NextUpload( { @@ -12,8 +12,5 @@ export const nup = new NextUpload( }, }, - new DrizzlePostgresStore( - // @ts-ignore - getDbServerless() - ) + new DrizzlePgStore(getDbServerless()) ); diff --git a/package.json b/package.json index 11676df..92f1a57 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,9 @@ "src/client", "src/react", "src/store/keyv", - "src/store/drizzle/postgres-js" + "src/store/drizzle/postgres-js", + "src/store/drizzle/neon", + "src/store/drizzle/node-postgres" ], "treeshake": true, "sourcemap": "inline", @@ -82,6 +84,14 @@ "./store/drizzle/postgres-js": { "require": "./dist/store/drizzle/postgres-js/index.js", "import": "./dist/store/drizzle/postgres-js/index.mjs" + }, + "./store/drizzle/neon": { + "require": "./dist/store/drizzle/neon/index.js", + "import": "./dist/store/drizzle/neon/index.mjs" + }, + "./store/drizzle/node-postgres": { + "require": "./dist/store/drizzle/node-postgres/index.js", + "import": "./dist/store/drizzle/node-postgres/index.mjs" } }, "files": [ diff --git a/src/store/drizzle/neon/DrizzlePgStore.ts b/src/store/drizzle/neon/DrizzlePgStore.ts new file mode 100644 index 0000000..673eebe --- /dev/null +++ b/src/store/drizzle/neon/DrizzlePgStore.ts @@ -0,0 +1,7 @@ +import { drizzle } from 'drizzle-orm/neon-http'; +import { DrizzlePgCoreStore } from '../pg-core/DrizzlePgCoreStore'; + +export class DrizzlePgStore extends DrizzlePgCoreStore< + // @ts-ignore + typeof drizzle +> {} diff --git a/src/store/drizzle/neon/index.ts b/src/store/drizzle/neon/index.ts new file mode 100644 index 0000000..679732d --- /dev/null +++ b/src/store/drizzle/neon/index.ts @@ -0,0 +1,2 @@ +export * from './DrizzlePgStore'; +export * from '../pg-core/DrizzlePgSchema'; diff --git a/src/store/drizzle/node-postgres/DrizzlePgStore.ts b/src/store/drizzle/node-postgres/DrizzlePgStore.ts new file mode 100644 index 0000000..80889ad --- /dev/null +++ b/src/store/drizzle/node-postgres/DrizzlePgStore.ts @@ -0,0 +1,4 @@ +import { drizzle } from 'drizzle-orm/node-postgres'; +import { DrizzlePgCoreStore } from '../pg-core/DrizzlePgCoreStore'; + +export class DrizzlePgStore extends DrizzlePgCoreStore {} diff --git a/src/store/drizzle/node-postgres/index.ts b/src/store/drizzle/node-postgres/index.ts new file mode 100644 index 0000000..679732d --- /dev/null +++ b/src/store/drizzle/node-postgres/index.ts @@ -0,0 +1,2 @@ +export * from './DrizzlePgStore'; +export * from '../pg-core/DrizzlePgSchema'; diff --git a/src/store/drizzle/postgres-js/DrizzlePostgresStore.ts b/src/store/drizzle/pg-core/DrizzlePgCoreStore.ts similarity index 70% rename from src/store/drizzle/postgres-js/DrizzlePostgresStore.ts rename to src/store/drizzle/pg-core/DrizzlePgCoreStore.ts index 223e430..f0c6488 100644 --- a/src/store/drizzle/postgres-js/DrizzlePostgresStore.ts +++ b/src/store/drizzle/pg-core/DrizzlePgCoreStore.ts @@ -3,12 +3,14 @@ import { eq } from 'drizzle-orm'; import { nanoid } from 'nanoid'; import { Asset, NextUploadStore } from '../../../types'; import { NextUpload } from '../../../NextUpload'; -import { drizzlePostgresNextUploadAssetsTable } from './DrizzlePostgresSchema'; +import { DrizzlePgNextUploadAssetsTable } from './DrizzlePgSchema'; -export class DrizzlePostgresStore implements NextUploadStore { - private db: ReturnType; +export class DrizzlePgCoreStore + implements NextUploadStore +{ + private db: ReturnType; - constructor(db: ReturnType) { + constructor(db: ReturnType) { this.db = db; } @@ -18,12 +20,11 @@ export class DrizzlePostgresStore implements NextUploadStore { } | null> { const rows = await this.db .select({ - presignedUrl: drizzlePostgresNextUploadAssetsTable.presignedUrl, - presignedUrlExpires: - drizzlePostgresNextUploadAssetsTable.presignedUrlExpires, + presignedUrl: DrizzlePgNextUploadAssetsTable.presignedUrl, + presignedUrlExpires: DrizzlePgNextUploadAssetsTable.presignedUrlExpires, }) - .from(drizzlePostgresNextUploadAssetsTable) - .where(eq(drizzlePostgresNextUploadAssetsTable.id, id)); + .from(DrizzlePgNextUploadAssetsTable) + .where(eq(DrizzlePgNextUploadAssetsTable.id, id)); if (!rows?.[0]) { return null; @@ -38,12 +39,12 @@ export class DrizzlePostgresStore implements NextUploadStore { async deletePresignedUrl(id: string): Promise { await this.db - .update(drizzlePostgresNextUploadAssetsTable) + .update(DrizzlePgNextUploadAssetsTable) .set({ presignedUrl: null, presignedUrlExpires: null, }) - .where(eq(drizzlePostgresNextUploadAssetsTable.id, id)); + .where(eq(DrizzlePgNextUploadAssetsTable.id, id)); } async savePresignedUrl( @@ -52,20 +53,18 @@ export class DrizzlePostgresStore implements NextUploadStore { presignedUrlExpirationSeconds?: number ): Promise { await this.db - .update(drizzlePostgresNextUploadAssetsTable) + .update(DrizzlePgNextUploadAssetsTable) .set({ presignedUrl: url, presignedUrlExpires: presignedUrlExpirationSeconds ? NextUpload.calculateExpires(presignedUrlExpirationSeconds * 1000) : undefined, }) - .where(eq(drizzlePostgresNextUploadAssetsTable.id, id)); + .where(eq(DrizzlePgNextUploadAssetsTable.id, id)); } async all(): Promise { - const rows = await this.db - .select() - .from(drizzlePostgresNextUploadAssetsTable); + const rows = await this.db.select().from(DrizzlePgNextUploadAssetsTable); return rows.map((row) => ({ ...(row.data as Asset), @@ -87,12 +86,12 @@ export class DrizzlePostgresStore implements NextUploadStore { throw new Error(`Asset expired and was deleted`); } const rows = await this.db - .update(drizzlePostgresNextUploadAssetsTable) + .update(DrizzlePgNextUploadAssetsTable) .set({ data: args, expires, }) - .where(eq(drizzlePostgresNextUploadAssetsTable.id, args.id)) + .where(eq(DrizzlePgNextUploadAssetsTable.id, args.id)) .returning(); return { @@ -104,7 +103,7 @@ export class DrizzlePostgresStore implements NextUploadStore { }; } const rows = await this.db - .insert(drizzlePostgresNextUploadAssetsTable) + .insert(DrizzlePgNextUploadAssetsTable) .values({ id: args?.id || nanoid(), data: args, @@ -126,15 +125,15 @@ export class DrizzlePostgresStore implements NextUploadStore { async delete(id: string): Promise { await this.db - .delete(drizzlePostgresNextUploadAssetsTable) - .where(eq(drizzlePostgresNextUploadAssetsTable.id, id)); + .delete(DrizzlePgNextUploadAssetsTable) + .where(eq(DrizzlePgNextUploadAssetsTable.id, id)); } async find(id: string): Promise { const rows = await this.db .select() - .from(drizzlePostgresNextUploadAssetsTable) - .where(eq(drizzlePostgresNextUploadAssetsTable.id, id)); + .from(DrizzlePgNextUploadAssetsTable) + .where(eq(DrizzlePgNextUploadAssetsTable.id, id)); if (rows?.[0]) { if (NextUpload.isExpired(rows?.[0]?.expires)) { diff --git a/src/store/drizzle/pg-core/DrizzlePgSchema.ts b/src/store/drizzle/pg-core/DrizzlePgSchema.ts new file mode 100644 index 0000000..75c4bfd --- /dev/null +++ b/src/store/drizzle/pg-core/DrizzlePgSchema.ts @@ -0,0 +1,33 @@ +import { + pgTable, + jsonb, + timestamp, + varchar, + bigint, +} from 'drizzle-orm/pg-core'; + +const createdAt = timestamp(`createdAt`, { + withTimezone: true, +}) + .notNull() + .defaultNow(); + +const updatedAt = timestamp(`updatedAt`, { + withTimezone: true, +}) + .notNull() + .defaultNow(); + +export const DrizzlePgNextUploadAssetsTable = pgTable(`next_upload_assets`, { + createdAt, + updatedAt, + id: varchar(`id`).primaryKey(), + data: jsonb(`data`).notNull(), + expires: bigint(`expires`, { + mode: 'number', + }), + presignedUrl: varchar(`presignedUrl`), + presignedUrlExpires: bigint(`presignedUrlExpires`, { + mode: 'number', + }), +}); diff --git a/src/store/drizzle/postgres-js/DrizzlePgStore.ts b/src/store/drizzle/postgres-js/DrizzlePgStore.ts new file mode 100644 index 0000000..be99357 --- /dev/null +++ b/src/store/drizzle/postgres-js/DrizzlePgStore.ts @@ -0,0 +1,4 @@ +import { drizzle } from 'drizzle-orm/postgres-js'; +import { DrizzlePgCoreStore } from '../pg-core/DrizzlePgCoreStore'; + +export class DrizzlePgStore extends DrizzlePgCoreStore {} diff --git a/src/store/drizzle/postgres-js/DrizzlePostgresSchema.ts b/src/store/drizzle/postgres-js/DrizzlePostgresSchema.ts deleted file mode 100644 index 078b9c4..0000000 --- a/src/store/drizzle/postgres-js/DrizzlePostgresSchema.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { - pgTable, - jsonb, - timestamp, - varchar, - bigint, -} from 'drizzle-orm/pg-core'; - -const createdAt = timestamp(`createdAt`, { - withTimezone: true, -}) - .notNull() - .defaultNow(); - -const updatedAt = timestamp(`updatedAt`, { - withTimezone: true, -}) - .notNull() - .defaultNow(); - -export const drizzlePostgresNextUploadAssetsTable = pgTable( - `next_upload_assets`, - { - createdAt, - updatedAt, - id: varchar(`id`).primaryKey(), - data: jsonb(`data`).notNull(), - expires: bigint(`expires`, { - mode: 'number', - }), - presignedUrl: varchar(`presignedUrl`), - presignedUrlExpires: bigint(`presignedUrlExpires`, { - mode: 'number', - }), - } -); diff --git a/src/store/drizzle/postgres-js/index.ts b/src/store/drizzle/postgres-js/index.ts index 67ae9d6..679732d 100644 --- a/src/store/drizzle/postgres-js/index.ts +++ b/src/store/drizzle/postgres-js/index.ts @@ -1,2 +1,2 @@ -export * from './DrizzlePostgresStore'; -export * from './DrizzlePostgresSchema'; +export * from './DrizzlePgStore'; +export * from '../pg-core/DrizzlePgSchema'; diff --git a/tests/NextUpload.test.ts b/tests/NextUpload.test.ts index 367a7ed..e245c4c 100644 --- a/tests/NextUpload.test.ts +++ b/tests/NextUpload.test.ts @@ -12,10 +12,10 @@ import { NextUploadConfig, NextUploadAction, } from '../src'; -import { DrizzlePostgresStore } from '../src/store/drizzle/postgres-js/DrizzlePostgresStore'; +import { DrizzlePostgresStore } from '../src/store/drizzle/postgres-js/DrizzlePgStore'; import { getDb } from './db/getDb'; import { KeyvStore } from '../src/store/keyv'; -import { drizzlePostgresNextUploadAssetsTable } from '../src/store/drizzle/postgres-js'; +import { DrizzlePgNextUploadAssetsTable } from '../src/store/drizzle/postgres-js'; const runTests = async ( name: string, @@ -446,9 +446,9 @@ runTests(`DrizzlePostgresStore`, { await migrate(await getDb(), { migrationsFolder: resolve(`tests/db/migrations`), }); - (await getDb()).delete(drizzlePostgresNextUploadAssetsTable); + (await getDb()).delete(DrizzlePgNextUploadAssetsTable); }, afterEach: async () => { - (await getDb()).delete(drizzlePostgresNextUploadAssetsTable); + (await getDb()).delete(DrizzlePgNextUploadAssetsTable); }, });