Jsx
Jsx
Explore posts from servers
DTDrizzle Team
Created by Jsx on 3/24/2024 in #help
working with many to many
I have a user table, course table along with the course_member which is a join table. How to get all the courses where userId is provided Tables schema along with relation
export const user = sqliteTable("user", {
id: text("id")
.$default(() => createId())
.primaryKey(),
userName: text("user_name").notNull().unique(),
email: text("email").notNull().unique(),
createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`),
});

export const userRelations = relations(user, ({ one, many }) => ({
courseMember: many(courseMember),
}));

export const course = sqliteTable("course", {
id: text("id")
.$defaultFn(() => createId())
.primaryKey(),
title: text("title").notNull(),
slug: text("slug").unique().notNull(),
});

export const courseRelations = relations(course, ({ many, one }) => ({
courseMember: many(courseMember),
}));

export const courseMember = sqliteTable(
"course_member",
{
courseId: text("course_id")
.notNull()
.references(() => course.id),
userId: text("user_id")
.notNull()
.references(() => user.id),
role: text("role", { enum: ["owner", "admin", "teacher"] }).notNull(),
},
(table) => {
return {
pk: primaryKey({ columns: [table.courseId, table.userId] }),
};
}
);

export const courseMemberRelations = relations(courseMember, ({ one }) => ({
user: one(user, {
fields: [courseMember.userId],
references: [user.id],
}),
course: one(course, {
fields: [courseMember.courseId],
references: [course.id],
}),
}));
export const user = sqliteTable("user", {
id: text("id")
.$default(() => createId())
.primaryKey(),
userName: text("user_name").notNull().unique(),
email: text("email").notNull().unique(),
createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`),
});

export const userRelations = relations(user, ({ one, many }) => ({
courseMember: many(courseMember),
}));

export const course = sqliteTable("course", {
id: text("id")
.$defaultFn(() => createId())
.primaryKey(),
title: text("title").notNull(),
slug: text("slug").unique().notNull(),
});

export const courseRelations = relations(course, ({ many, one }) => ({
courseMember: many(courseMember),
}));

export const courseMember = sqliteTable(
"course_member",
{
courseId: text("course_id")
.notNull()
.references(() => course.id),
userId: text("user_id")
.notNull()
.references(() => user.id),
role: text("role", { enum: ["owner", "admin", "teacher"] }).notNull(),
},
(table) => {
return {
pk: primaryKey({ columns: [table.courseId, table.userId] }),
};
}
);

export const courseMemberRelations = relations(courseMember, ({ one }) => ({
user: one(user, {
fields: [courseMember.userId],
references: [user.id],
}),
course: one(course, {
fields: [courseMember.courseId],
references: [course.id],
}),
}));
3 replies
CDCloudflare Developers
Created by Jsx on 7/14/2023 in #workers-help
Caching R2 using worker cache api
Hello I was going through the docs and saw we can cache r2 resource through workers using the cache api. I have tried to implement. It's working fine in local developement. I am getting CF-Cache-Status in local but when I am deploying I am not seeing any Cache Status header with the worker domain. Here's the code
const handler: ExportedHandler = {
async fetch(request, env, ctx) {
const cacheUrl = new URL(request.url);


const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;

// Check whether the value is already available in the cache
// if not, you will need to fetch it from origin, and store it in the cache
let response = await cache.match(cacheKey);

if (!response) {
console.log(`Response for request url: ${request.url} not present in cache. Fetching and caching request.`);
const url = new URL(request.url);

// If not in cache, get it from origin
response = await fetch(`https://pub-92d2c9da4.....r2.dev${url.pathname}`, {
cf: {
cacheTtl: 500,
cacheEverything: true,
},
});

response = new Response(response.body, response);

response.headers.append('Cache-Control', 's-maxage=500');
response.headers.append('Cache-Control', 'max-age=86400');
response.headers.append('Cache-Control', 'public');

ctx.waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
},
};

export default handler;
const handler: ExportedHandler = {
async fetch(request, env, ctx) {
const cacheUrl = new URL(request.url);


const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;

// Check whether the value is already available in the cache
// if not, you will need to fetch it from origin, and store it in the cache
let response = await cache.match(cacheKey);

if (!response) {
console.log(`Response for request url: ${request.url} not present in cache. Fetching and caching request.`);
const url = new URL(request.url);

// If not in cache, get it from origin
response = await fetch(`https://pub-92d2c9da4.....r2.dev${url.pathname}`, {
cf: {
cacheTtl: 500,
cacheEverything: true,
},
});

response = new Response(response.body, response);

response.headers.append('Cache-Control', 's-maxage=500');
response.headers.append('Cache-Control', 'max-age=86400');
response.headers.append('Cache-Control', 'public');

ctx.waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
},
};

export default handler;
4 replies
CDCloudflare Developers
Created by Jsx on 5/31/2023 in #workers-help
Accessing Environment Variable in router.ts
Hello I have configured my worker using create cloudflare and worker with route. I am able to access it in worker.ts but I want the access in the router.ts as there I am performing action. Please help me how can I access those env vars. I am using itty-router
3 replies