Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Dec 2, 2023
1 parent c409b0a commit ddbd862
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -11,9 +11,5 @@ export const nup = new NextUpload(
['drizzle-node-postgres']: {},
},
},
async () =>
new DrizzlePostgresStore(
// @ts-ignore
await getDbNodePostgres()
)
async () => new DrizzlePgStore(await getDbNodePostgres())
);
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -11,5 +11,5 @@ export const nup = new NextUpload(
['drizzle-postgres-js']: {},
},
},
new DrizzlePostgresStore(getDbPostgresJs())
new DrizzlePgStore(getDbPostgresJs())
);
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -12,8 +12,5 @@ export const nup = new NextUpload(
},
},

new DrizzlePostgresStore(
// @ts-ignore
getDbServerless()
)
new DrizzlePgStore(getDbServerless())
);
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": [
Expand Down
7 changes: 7 additions & 0 deletions src/store/drizzle/neon/DrizzlePgStore.ts
Original file line number Diff line number Diff line change
@@ -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
> {}
2 changes: 2 additions & 0 deletions src/store/drizzle/neon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './DrizzlePgStore';
export * from '../pg-core/DrizzlePgSchema';
4 changes: 4 additions & 0 deletions src/store/drizzle/node-postgres/DrizzlePgStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { drizzle } from 'drizzle-orm/node-postgres';
import { DrizzlePgCoreStore } from '../pg-core/DrizzlePgCoreStore';

export class DrizzlePgStore extends DrizzlePgCoreStore<typeof drizzle> {}
2 changes: 2 additions & 0 deletions src/store/drizzle/node-postgres/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './DrizzlePgStore';
export * from '../pg-core/DrizzlePgSchema';
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof drizzle>;
export class DrizzlePgCoreStore<T extends typeof drizzle>
implements NextUploadStore
{
private db: ReturnType<T>;

constructor(db: ReturnType<typeof drizzle>) {
constructor(db: ReturnType<T>) {
this.db = db;
}

Expand All @@ -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;
Expand All @@ -38,12 +39,12 @@ export class DrizzlePostgresStore implements NextUploadStore {

async deletePresignedUrl(id: string): Promise<void> {
await this.db
.update(drizzlePostgresNextUploadAssetsTable)
.update(DrizzlePgNextUploadAssetsTable)
.set({
presignedUrl: null,
presignedUrlExpires: null,
})
.where(eq(drizzlePostgresNextUploadAssetsTable.id, id));
.where(eq(DrizzlePgNextUploadAssetsTable.id, id));
}

async savePresignedUrl(
Expand All @@ -52,20 +53,18 @@ export class DrizzlePostgresStore implements NextUploadStore {
presignedUrlExpirationSeconds?: number
): Promise<void> {
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<Asset[]> {
const rows = await this.db
.select()
.from(drizzlePostgresNextUploadAssetsTable);
const rows = await this.db.select().from(DrizzlePgNextUploadAssetsTable);

return rows.map((row) => ({
...(row.data as Asset),
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -126,15 +125,15 @@ export class DrizzlePostgresStore implements NextUploadStore {

async delete(id: string): Promise<void> {
await this.db
.delete(drizzlePostgresNextUploadAssetsTable)
.where(eq(drizzlePostgresNextUploadAssetsTable.id, id));
.delete(DrizzlePgNextUploadAssetsTable)
.where(eq(DrizzlePgNextUploadAssetsTable.id, id));
}

async find(id: string): Promise<Asset | undefined> {
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)) {
Expand Down
33 changes: 33 additions & 0 deletions src/store/drizzle/pg-core/DrizzlePgSchema.ts
Original file line number Diff line number Diff line change
@@ -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',
}),
});
4 changes: 4 additions & 0 deletions src/store/drizzle/postgres-js/DrizzlePgStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import { DrizzlePgCoreStore } from '../pg-core/DrizzlePgCoreStore';

export class DrizzlePgStore extends DrizzlePgCoreStore<typeof drizzle> {}
36 changes: 0 additions & 36 deletions src/store/drizzle/postgres-js/DrizzlePostgresSchema.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/store/drizzle/postgres-js/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './DrizzlePostgresStore';
export * from './DrizzlePostgresSchema';
export * from './DrizzlePgStore';
export * from '../pg-core/DrizzlePgSchema';
8 changes: 4 additions & 4 deletions tests/NextUpload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 15 in tests/NextUpload.test.ts

View workflow job for this annotation

GitHub Actions / Run Type Check & Linters

'"../src/store/drizzle/postgres-js/DrizzlePgStore"' has no exported member named 'DrizzlePostgresStore'. Did you mean '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,
Expand Down Expand Up @@ -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);
},
});

0 comments on commit ddbd862

Please sign in to comment.