Skip to content

Commit

Permalink
Add view counter
Browse files Browse the repository at this point in the history
  • Loading branch information
timsexperiments committed Apr 20, 2024
1 parent 992546c commit c707976
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
8 changes: 4 additions & 4 deletions packages/view-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"module": "src/index.ts",
"types": "src/index.ts",
"type": "module",
"dependencies": {
"@libsql/client": "^0.6.0",
"drizzle-orm": "^0.30.7"
},
"devDependencies": {
"@types/bun": "latest",
"drizzle-kit": "^0.20.14"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@planetscale/database": "^1.16.0",
"drizzle-orm": "^0.29.4"
}
}
18 changes: 8 additions & 10 deletions packages/view-storage/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Client } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";
import { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";

export type CreateDbOptions = {
host: string;
username: string;
password: string;
authToken: string;
url: string;
};

export function createDb({ host, username, password }: CreateDbOptions) {
const client = new Client({
host: host,
username: username,
password: password,
export function createDb({ authToken, url }: CreateDbOptions) {
const client = createClient({
authToken,
url,
});
return drizzle(client);
}
Expand Down
19 changes: 9 additions & 10 deletions packages/view-storage/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { sql } from "drizzle-orm";
import {
index,
mysqlTableCreator,
serial,
timestamp,
varchar,
} from "drizzle-orm/mysql-core";
integer,
sqliteTableCreator,
text,
} from "drizzle-orm/sqlite-core";

const mysqlTable = mysqlTableCreator((name) => `timsexperiments_${name}`);
const mysqlTable = sqliteTableCreator((name) => `timsexperiments_${name}`);

export const views = mysqlTable(
"views",
{
id: serial("id").primaryKey(),
page: varchar("page", { length: 256 }).notNull(),
ipAddress: varchar("ip_address", { length: 40 }).notNull(),
viewedAt: timestamp("viewed_at")
id: integer("id").primaryKey({ autoIncrement: true }),
page: text("page", { length: 256 }).notNull(),
ipAddress: text("ip_address", { length: 40 }).notNull(),
viewedAt: integer("viewedAt", { mode: "timestamp_ms" })
.default(sql`CURRENT_TIMESTAMP()`)
.notNull(),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/view-storage/src/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ViewsStorage {
* @param {ViewsDatabase} db - The ViewsDatabase instance used for storing views data.
*/
async add(...viewList: View[]) {
return (await this.db.insert(views).values(viewList)).insertId;
return (await this.db.insert(views).values(viewList)).lastInsertRowid;
}

/**
Expand Down

0 comments on commit c707976

Please sign in to comment.