@@ -31,7 +31,7 @@ function parseFrontmatter(content: string): { data: Record<string, any>; body: s
3131}
3232
3333/**
34- * Process micro posts from D1 database
34+ * Process micro posts from R2 JSONL
3535 */
3636async function processMicroCollection (
3737 manifest : UrlManifest ,
@@ -40,53 +40,41 @@ async function processMicroCollection(
4040) : Promise < { processed : number ; skipped : number ; errors : number } > {
4141 console . log ( "Processing collection: micro" ) ;
4242
43- const databaseId = process . env . D1_DATABASE_ID ;
44- const accountId = process . env . CLOUDFLARE_ACCOUNT_ID ;
45- const apiToken = process . env . CLOUDFLARE_API_TOKEN ;
46-
47- // Skip if credentials are missing
48- if ( ! databaseId || ! accountId || ! apiToken ) {
49- console . log (
50- " ⚠️ Skipping micro posts: Missing D1 credentials (D1_DATABASE_ID, CLOUDFLARE_ACCOUNT_ID, or CLOUDFLARE_API_TOKEN)\n" ,
51- ) ;
52- return { processed : 0 , skipped : 0 , errors : 0 } ;
53- }
54-
5543 try {
56- const query = "SELECT id, created_at FROM micro_posts ORDER BY created_at DESC" ;
57-
58- const response = await fetch (
59- `https://api.cloudflare.com/client/v4/accounts/${ accountId } /d1/database/${ databaseId } /query` ,
60- {
61- method : "POST" ,
62- headers : {
63- Authorization : `Bearer ${ apiToken } ` ,
64- "Content-Type" : "application/json" ,
65- } ,
66- body : JSON . stringify ( {
67- sql : query ,
68- } ) ,
69- } ,
70- ) ;
71-
72- if ( ! response . ok ) {
73- throw new Error ( `D1 API request failed: ${ response . status } ${ await response . text ( ) } ` ) ;
44+ const { getPlatformProxy } = await import ( "wrangler" ) ;
45+ const projectRoot = import . meta. dir + "/.." ;
46+ const { env, dispose } = await getPlatformProxy < { MICRO_BUCKET : R2Bucket } > ( {
47+ configPath : `${ projectRoot } /wrangler.toml` ,
48+ persist : { path : `${ projectRoot } /.wrangler/state/v3` } ,
49+ } ) ;
50+
51+ if ( ! env . MICRO_BUCKET ) {
52+ console . log ( " ⚠️ Skipping micro posts: MICRO_BUCKET not available\n" ) ;
53+ await dispose ( ) ;
54+ return { processed : 0 , skipped : 0 , errors : 0 } ;
7455 }
7556
76- const data = await response . json ( ) ;
57+ const obj = await env . MICRO_BUCKET . get ( "micro-posts.jsonl" ) ;
58+ await dispose ( ) ;
7759
78- if ( ! data . success ) {
79- throw new Error ( `D1 query failed: ${ JSON . stringify ( data . errors ) } ` ) ;
60+ if ( ! obj ) {
61+ console . log ( " ⚠️ Skipping micro posts: micro-posts.jsonl not found in R2\n" ) ;
62+ return { processed : 0 , skipped : 0 , errors : 0 } ;
8063 }
8164
82- const results = data . result [ 0 ] ?. results || [ ] ;
65+ const text = await obj . text ( ) ;
66+ const posts = text
67+ . trim ( )
68+ . split ( "\n" )
69+ . filter ( Boolean )
70+ . map ( ( line ) => JSON . parse ( line ) as { id : number ; createdAt : string } ) ;
8371
8472 let processedCount = 0 ;
8573 let errorCount = 0 ;
8674
87- for ( const post of results ) {
75+ for ( const post of posts ) {
8876 const postId = String ( post . id ) ;
89- const createdAt = new Date ( post . created_at * 1000 ) ;
77+ const createdAt = new Date ( post . createdAt ) ;
9078
9179 // Generate code from creation date with 'M' kind
9280 const code = Code . fromDate ( createdAt , "M" ) . toString ( ) . toLowerCase ( ) ;
@@ -369,7 +357,7 @@ async function genUrlManifest(collections: string[]) {
369357 for ( const collection of collections ) {
370358 let result ;
371359 if ( collection === "micro" ) {
372- // Special handling for micro posts from D1
360+ // Special handling for micro posts from R2
373361 result = await processMicroCollection ( manifest , existingSlugToCode , seenSlugsInCurrentRun ) ;
374362 } else {
375363 // File-based collections
0 commit comments