Skip to content

Commit

Permalink
add script to fill the activity table
Browse files Browse the repository at this point in the history
  • Loading branch information
xerosanyam committed Jun 27, 2024
1 parent bb4633d commit c0d756b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions scripts/fill-activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';

// picks value from .env
export const turso_client = createClient({
url: process.env.TURSO_CONNECTION_URL!,
authToken: process.env.TURSO_AUTH_TOKEN!,
});

export const db = drizzle(turso_client,
// { logger: true }
);

import { activityTable, cardTable } from "$lib/db/turso.schema";

async function insertActivityFromCards() {
// Fetch all rows from the card table
const cards = await db.select().from(cardTable).all();

// Loop through each card and insert rows into the activity table
for (const card of cards) {
const activityInsertData = {
userId: card.userId,
cardId: card.id,
};

// Insert activity for creation
await db.insert(activityTable).values({
id: crypto.randomUUID(),
action: 'INSERT',
createdAt: card.createdAt,
...activityInsertData,
});

// If createdAt and updatedAt are different, insert activity for update
if (card.createdAt !== card.updatedAt) {
await db.insert(activityTable).values({
id: crypto.randomUUID(),
action: 'UPDATE',
createdAt: card.updatedAt,
...activityInsertData,
});
}
}
}

// Run the script
insertActivityFromCards()
.then(() => console.log('Activity records inserted successfully.'))
.catch((error) => console.error('Error inserting activity records:', error));

0 comments on commit c0d756b

Please sign in to comment.