forked from devmuhnnad/kutt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'day/feature_DAILY_VISIT' into kutt_dev5
- Loading branch information
Showing
6 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Knex } from "knex"; | ||
|
||
|
||
export async function createDailyVisitTable(knex: Knex) { | ||
const hasTable = await knex.schema.hasTable("daily_visit"); | ||
// console.log("TRYING TO CREATE A TABLE"); | ||
if (!hasTable) { | ||
await knex.schema.createTable("daily_visit", table => { | ||
table.increments("id").primary(); // Primary key, autoincrement | ||
table.integer("link_id").notNullable(); // Foreign key to link_id | ||
table.date("date").notNullable().defaultTo(knex.fn.now()); // Current date | ||
table.integer("visit_count").notNullable().defaultTo(0); // Visit count | ||
table.timestamp("created_at").defaultTo(knex.fn.now()); // Timestamp for entry creation | ||
table.timestamp("updated_at").defaultTo(knex.fn.now()); // Timestamp for entry update | ||
|
||
// Ensure the combination of link_id and date is unique | ||
table.unique(["link_id", "date"]); | ||
}); | ||
|
||
await knex.raw(` | ||
CREATE OR REPLACE FUNCTION update_updated_at_column() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
NEW.updated_at = NOW(); | ||
RETURN NEW; | ||
END; | ||
$$ language 'plpgsql'; | ||
CREATE TRIGGER update_daily_visit_updated_at | ||
BEFORE UPDATE ON daily_visit | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE update_updated_at_column(); | ||
` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import knex from "../knex"; | ||
|
||
interface DailyVisit { | ||
link_id: number; | ||
date: Date; | ||
visit_count: number; | ||
} | ||
|
||
interface LinkData { | ||
id: number; // Assuming id is of type number | ||
// Add other properties from Link if needed | ||
} | ||
|
||
export const insertDailyVisit = async (data: LinkData) => { | ||
const currentDate = new Date(); | ||
console.log("incrementing Daily Visit"); | ||
const dailyVisit: DailyVisit = { | ||
link_id: data.id, // Extracting link_id from the data object | ||
date: currentDate, | ||
visit_count: 1, | ||
}; | ||
|
||
// Insert a new row into the 'dailyVisit' table with link_id, current date, and visit_count of 1 | ||
await knex<DailyVisit>("daily_visit") | ||
.insert(dailyVisit) | ||
.onConflict(["link_id", "date"]) // Specify the unique constraint columns | ||
.merge({ visit_count: knex.raw("daily_visit.visit_count + 1") }); // Increment visit_count on conflict | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters