predaytor
predaytor
Explore posts from servers
CDCloudflare Developers
Created by Brod on 9/6/2024 in #workers-help
ERROR Could not resolve "cloudflare:workers"
setting in vite doesn't help either:
ssr: {
resolve: {
conditions: ['workerd', 'worker', 'browser'],
},
external: ['cloudflare:workers'],
},
ssr: {
resolve: {
conditions: ['workerd', 'worker', 'browser'],
},
external: ['cloudflare:workers'],
},
15 replies
CDCloudflare Developers
Created by Brod on 9/6/2024 in #workers-help
ERROR Could not resolve "cloudflare:workers"
same.. using @hono-rate-limiter/cloudflare
15 replies
HHono
Created by predaytor on 4/16/2024 in #help
How to setup Hono with Remix/Cloudflare for Vite dev server?
not sure why it doesn't work for you, I have both vars in .dev.vars as well as from Cloudflare Dashboard.
11 replies
DTDrizzle Team
Created by rubberduckies on 5/13/2024 in #help
magic sql column naming
same here
2 replies
DTDrizzle Team
Created by predaytor on 6/6/2024 in #help
Represent a table that contains an variants with strict keys (aka `Record` mapped type)?
up
2 replies
DTDrizzle Team
Created by predaytor on 5/30/2024 in #help
Error handling
upd
2 replies
DTDrizzle Team
Created by predaytor on 5/30/2024 in #help
The result after join (table alias)
upd
2 replies
HHono
Created by predaytor on 4/16/2024 in #help
How to setup Hono with Remix/Cloudflare for Vite dev server?
/functions/[[path]].ts:
import { handle } from 'hono/cloudflare-pages';

import server from '../server';

export const onRequest = handle(server);
import { handle } from 'hono/cloudflare-pages';

import server from '../server';

export const onRequest = handle(server);
/app/middleware/remix.ts:
import { type ServerBuild, createRequestHandler } from '@remix-run/cloudflare';
import { factory } from 'server/factory';
import { getLoadContext } from 'server/load-context';

export const remix = factory.createMiddleware(async c => {
const build = (await (process.env.NODE_ENV === 'development'
? import('virtual:remix/server-build' as never)
: import('build/server' as never))) as ServerBuild;

const requestHandler = createRequestHandler(build, process.env.NODE_ENV);

return await requestHandler(c.req.raw, getLoadContext(c));
});
import { type ServerBuild, createRequestHandler } from '@remix-run/cloudflare';
import { factory } from 'server/factory';
import { getLoadContext } from 'server/load-context';

export const remix = factory.createMiddleware(async c => {
const build = (await (process.env.NODE_ENV === 'development'
? import('virtual:remix/server-build' as never)
: import('build/server' as never))) as ServerBuild;

const requestHandler = createRequestHandler(build, process.env.NODE_ENV);

return await requestHandler(c.req.raw, getLoadContext(c));
});
/app/middleware/db.ts:
import { drizzle } from 'drizzle-orm/d1';
import type { Context } from 'hono';
import type { ContextEnv } from 'server';
import { factory } from 'server/factory';

import * as schema from '~/db/schema.server';

export const db = factory.createMiddleware(async (c, next) => {
const db = initializeDatabase(c);
c.set('db', db);
return await next();
});

declare module 'hono' {
interface ContextVariableMap {
db: DB;
}
}

function initializeDatabase(c: Context<ContextEnv>) {
return drizzle(c.env.DB, { schema });
}

export type DB = ReturnType<typeof initializeDatabase>;
import { drizzle } from 'drizzle-orm/d1';
import type { Context } from 'hono';
import type { ContextEnv } from 'server';
import { factory } from 'server/factory';

import * as schema from '~/db/schema.server';

export const db = factory.createMiddleware(async (c, next) => {
const db = initializeDatabase(c);
c.set('db', db);
return await next();
});

declare module 'hono' {
interface ContextVariableMap {
db: DB;
}
}

function initializeDatabase(c: Context<ContextEnv>) {
return drizzle(c.env.DB, { schema });
}

export type DB = ReturnType<typeof initializeDatabase>;
11 replies
HHono
Created by predaytor on 4/16/2024 in #help
How to setup Hono with Remix/Cloudflare for Vite dev server?
/server/load-context.ts:
import type { Context } from 'hono';
import type { ContextEnv } from 'server';
import { type PlatformProxy } from 'wrangler';

// When using `wrangler.toml` to configure bindings,
// `wrangler types` will generate types for those bindings
// into the global `Env` interface.

type Cloudflare = Omit<PlatformProxy<Env>, 'dispose'>;

export function getLoadContext(c: Context<ContextEnv>) {
const cloudflare = { env: c.env } as Cloudflare;

return {
cloudflare,
auth: c.get('auth'),
db: c.get('db'),
user: c.get('user'),
session: c.get('session'),
};
}

declare module '@remix-run/cloudflare' {
interface AppLoadContext extends ReturnType<typeof getLoadContext> {}
}
import type { Context } from 'hono';
import type { ContextEnv } from 'server';
import { type PlatformProxy } from 'wrangler';

// When using `wrangler.toml` to configure bindings,
// `wrangler types` will generate types for those bindings
// into the global `Env` interface.

type Cloudflare = Omit<PlatformProxy<Env>, 'dispose'>;

export function getLoadContext(c: Context<ContextEnv>) {
const cloudflare = { env: c.env } as Cloudflare;

return {
cloudflare,
auth: c.get('auth'),
db: c.get('db'),
user: c.get('user'),
session: c.get('session'),
};
}

declare module '@remix-run/cloudflare' {
interface AppLoadContext extends ReturnType<typeof getLoadContext> {}
}
11 replies
HHono
Created by predaytor on 4/16/2024 in #help
How to setup Hono with Remix/Cloudflare for Vite dev server?
for my setup I did something very similar but integrated a few middleware (eg. d1 database): /server/factory.ts:
import { createFactory } from 'hono/factory';
import type { ContextEnv } from 'server';

export const factory = createFactory<ContextEnv>();
import { createFactory } from 'hono/factory';
import type { ContextEnv } from 'server';

export const factory = createFactory<ContextEnv>();
/server/index.ts:
import { cors } from 'hono/cors';
import { csrf } from 'hono/csrf';
import { showRoutes } from 'hono/dev';
import { logger } from 'hono/logger';
import { prettyJSON } from 'hono/pretty-json';
import { secureHeaders } from 'hono/secure-headers';
import type { Session, User } from 'lucia';
import { staticAssets } from 'remix-hono/cloudflare';
import { trailingSlash } from 'remix-hono/trailing-slash';
import { factory } from 'server/factory';

import { auth } from '~/middleware/auth';
import { db } from '~/middleware/db';
import { remix } from '~/middleware/remix';
import { validateRequest } from '~/middleware/validate-request';

export type ContextEnv = {
Bindings: Required<Env>;
Variables: {
user: User | null;
session: Session | null;
};
};

const app = factory.createApp();

app.use(csrf());
app.use(logger());
app.use(prettyJSON({ space: 4 }));
app.use(secureHeaders());
app.use(trailingSlash());

app.use(db);
app.use(auth);
app.use(validateRequest);

app.use('/api/*', cors());

app.use(
async (c, next) => {
if (process.env.NODE_ENV !== 'development') {
return staticAssets()(c, next);
}
await next();
},

remix,
);

showRoutes(app);

export default app;
import { cors } from 'hono/cors';
import { csrf } from 'hono/csrf';
import { showRoutes } from 'hono/dev';
import { logger } from 'hono/logger';
import { prettyJSON } from 'hono/pretty-json';
import { secureHeaders } from 'hono/secure-headers';
import type { Session, User } from 'lucia';
import { staticAssets } from 'remix-hono/cloudflare';
import { trailingSlash } from 'remix-hono/trailing-slash';
import { factory } from 'server/factory';

import { auth } from '~/middleware/auth';
import { db } from '~/middleware/db';
import { remix } from '~/middleware/remix';
import { validateRequest } from '~/middleware/validate-request';

export type ContextEnv = {
Bindings: Required<Env>;
Variables: {
user: User | null;
session: Session | null;
};
};

const app = factory.createApp();

app.use(csrf());
app.use(logger());
app.use(prettyJSON({ space: 4 }));
app.use(secureHeaders());
app.use(trailingSlash());

app.use(db);
app.use(auth);
app.use(validateRequest);

app.use('/api/*', cors());

app.use(
async (c, next) => {
if (process.env.NODE_ENV !== 'development') {
return staticAssets()(c, next);
}
await next();
},

remix,
);

showRoutes(app);

export default app;
11 replies
HHono
Created by predaytor on 4/16/2024 in #help
How to setup Hono with Remix/Cloudflare for Vite dev server?
@makaron pelnoziarnisty yeah. here is a repo from the creator of hono (https://github.com/yusukebe/hono-and-remix-on-vite).
11 replies
DTDrizzle Team
Created by predaytor on 5/18/2024 in #help
Write SQL query in Drizzle?
@Sillvva do you think this is more performant than the previous example? Because it counts for each user row. Thx!
6 replies
DTDrizzle Team
Created by predaytor on 5/18/2024 in #help
Write SQL query in Drizzle?
thanks!
6 replies
DTDrizzle Team
Created by predaytor on 5/18/2024 in #help
Write SQL query in Drizzle?
guuyss, help pls
6 replies
HHono
Created by Chicha on 4/19/2024 in #help
use Context to get vars from wrangler.toml
for type infer use:
export type ContextEnv = {
Bindings: Required<Env>;
Variables: {
user: User | null;
session: Session | null;
};
};

const app = new Hono<ContextEnv>();
export type ContextEnv = {
Bindings: Required<Env>;
Variables: {
user: User | null;
session: Session | null;
};
};

const app = new Hono<ContextEnv>();
11 replies