predaytor
predaytor
Explore posts from servers
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)?
I have a table that represents words (it's an English learning platform) where each word should have two examples (eg US and UK). How to display such a connection? This is not a repeater field where we can create many records attached to a word, but rather an already defined dataset with (in our case) two keys.
export const word = sqliteTable('word', {
id: integer('id').notNull().primaryKey(),
name: text('name', { mode: 'text', length: 255 }).notNull().unique(),

// ?
// examples: text('examples', { mode: 'json' }).$type<Record<'us' | 'uk', { value: string }>>().notNull(),
// ?
// examples: text('examples', { mode: 'json' }).$type<Array<{ value: string }>>().notNull(),
});

// ?
export const wordExample = sqliteTable('word_example', {
id: integer('id').notNull().primaryKey(),
wordId: integer('word_id').references(() => word.id, { onDelete: 'cascade' }).unique(),
type: text('type', { enum: ['us', 'uk'] }).notNull().unique(),
value: text('value', { mode: 'text' }).notNull(),
});
export const word = sqliteTable('word', {
id: integer('id').notNull().primaryKey(),
name: text('name', { mode: 'text', length: 255 }).notNull().unique(),

// ?
// examples: text('examples', { mode: 'json' }).$type<Record<'us' | 'uk', { value: string }>>().notNull(),
// ?
// examples: text('examples', { mode: 'json' }).$type<Array<{ value: string }>>().notNull(),
});

// ?
export const wordExample = sqliteTable('word_example', {
id: integer('id').notNull().primaryKey(),
wordId: integer('word_id').references(() => word.id, { onDelete: 'cascade' }).unique(),
type: text('type', { enum: ['us', 'uk'] }).notNull().unique(),
value: text('value', { mode: 'text' }).notNull(),
});
2 replies
CDCloudflare Developers
Created by predaytor on 6/2/2024 in #general-help
Cache API vs KV
Which is better for server-side caching of database (d1) calls (aka direct data responses from the backend) - Cache API or KV Storage? The API is quite similar, but the Cache API relies directly on the CDN. This is a CRM-like application with users, roles, etc., so the usual cache-control cannot be applied here, as we need a way to programmatically invalidate on some CRUD operations (e.g. display a list of users while invalidate the cache when adding new ones to the database, with routes like /users/students, /users/students/new).
3 replies
DTDrizzle Team
Created by predaytor on 5/30/2024 in #help
Error handling
Should execute be used to correctly throw errors, or is it the same in this case?
const result = await context.db
.delete(user)
.where(and(eq(user.role, 'student'), eq(user.id, userId)))
.execute();

if (!result.success && result.error) throw new Error(result.error);
const result = await context.db
.delete(user)
.where(and(eq(user.role, 'student'), eq(user.id, userId)))
.execute();

if (!result.success && result.error) throw new Error(result.error);
await context.db
.delete(user)
.where(and(eq(user.role, 'student'), eq(user.id, userId)));
await context.db
.delete(user)
.where(and(eq(user.role, 'student'), eq(user.id, userId)));
2 replies
DTDrizzle Team
Created by predaytor on 5/30/2024 in #help
The result after join (table alias)
How to configure alias here? The result of the join return the original table name (library_level), but I expect libraryLevel, which is defined at the application level.
export const libraryLevel = sqliteTable('library_level', {
id: integer('id').notNull().primaryKey(),
name: text('name', { mode: 'text', length: 255 }).notNull().unique(),
});

export const user = sqliteTable('user', {
id: integer('id').notNull().primaryKey(),
email: text('email', { mode: 'text', length: 320 }).notNull().unique(),
libraryLevelId: integer('library_level_id').references(() => libraryLevel.id),
});

///

const users = await context.db
.select()
.from(user)
.leftJoin(libraryLevel, eq(user.libraryLevelId, libraryLevel.id))
.all();

users[0]?.library_level

// instead of

users[0]?.libraryError
export const libraryLevel = sqliteTable('library_level', {
id: integer('id').notNull().primaryKey(),
name: text('name', { mode: 'text', length: 255 }).notNull().unique(),
});

export const user = sqliteTable('user', {
id: integer('id').notNull().primaryKey(),
email: text('email', { mode: 'text', length: 320 }).notNull().unique(),
libraryLevelId: integer('library_level_id').references(() => libraryLevel.id),
});

///

const users = await context.db
.select()
.from(user)
.leftJoin(libraryLevel, eq(user.libraryLevelId, libraryLevel.id))
.all();

users[0]?.library_level

// instead of

users[0]?.libraryError
2 replies
DTDrizzle Team
Created by predaytor on 5/18/2024 in #help
Write SQL query in Drizzle?
Hey! How to write a similar query in drizzle? Basically, I need a way to count the total number of pages in a single query (should I?) for offset pagination?
const nodesCount = await context.db.select({ count: count() }).from(user).where(eq(user.role, 'student'));

const users = await context.db.query.user.findMany({
limit: pageSize,
offset: pageOffset,
where: user => eq(user.role, 'student'),
orderBy: (user, { asc }) => [asc(user.fullName), asc(user.id)],
});
const nodesCount = await context.db.select({ count: count() }).from(user).where(eq(user.role, 'student'));

const users = await context.db.query.user.findMany({
limit: pageSize,
offset: pageOffset,
where: user => eq(user.role, 'student'),
orderBy: (user, { asc }) => [asc(user.fullName), asc(user.id)],
});
vs. what I found on stackoveflow:
with cte as(
select count(*) total from table
)
select *, (select total from cte) total
from table limit 0, 100
with cte as(
select count(*) total from table
)
select *, (select total from cte) total
from table limit 0, 100
6 replies
HHono
Created by predaytor on 4/16/2024 in #help
How to setup Hono with Remix/Cloudflare for Vite dev server?
From the documentation for Cloudflare Pages, there is an adapter and a plugin, but it is not clear how we can configure both Hono and Remix to run on the Vite dev server.
import devServer from '@hono/vite-dev-server'
import adapter from '@hono/vite-dev-server/cloudflare'
import build from '@hono/vite-cloudflare-pages'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
devServer({
entry: 'src/index.tsx',
adapter, // Cloudflare Adapter
}),
build(),
],
})
import devServer from '@hono/vite-dev-server'
import adapter from '@hono/vite-dev-server/cloudflare'
import build from '@hono/vite-cloudflare-pages'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
devServer({
entry: 'src/index.tsx',
adapter, // Cloudflare Adapter
}),
build(),
],
})
11 replies
CDCloudflare Developers
Created by predaytor on 3/18/2024 in #general-help
Cloudflare for high load multiuser application
I'm thinking of Cloudflare's stack for a web app (generally a learning platform with admin features, users, chats, full-text search etc.). Is there anything I should be aware of, specifically the limitations of the D1 database for such case? What about user concurrency? Is there any potential problems integrating solutions like Durable Objects and Websockets? The application framework is Remix (Vite).
8 replies