diff --git a/.gitignore b/.gitignore index a547bf3..415a046 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ dist-ssr *.njsproj *.sln *.sw? + +# Project-specific +data.db \ No newline at end of file diff --git a/create-db.sh b/create-db.sh new file mode 100755 index 0000000..9f7494c --- /dev/null +++ b/create-db.sh @@ -0,0 +1,7 @@ +read -p "This command will destroy the database and re-create it. Are you sure? " -n 1 -r +echo # (optional) move to a new line +if [[ $REPLY =~ ^[Yy]$ ]] +then + rm -f data.db + sqlite3 data.db < ./src/server/db/schema.sql +fi diff --git a/package.json b/package.json index c6bf768..df199c1 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "express": "^5.1.0", + "kysely": "^0.29.2", "react": "^19.1.0", "react-dom": "^19.1.0", "tsx": "^4.19.3", @@ -25,4 +26,4 @@ "nodemon": "^3.1.10", "vite": "^6.3.3" } -} \ No newline at end of file +} diff --git a/src/server/db/database.ts b/src/server/db/database.ts new file mode 100644 index 0000000..19caad5 --- /dev/null +++ b/src/server/db/database.ts @@ -0,0 +1,11 @@ +import SQLite from "better-sqlite3"; +import { Kysely, SqliteDialect } from "kysely"; +import type { Database } from "./types.ts"; + +const dialect = new SqliteDialect({ + database: new SQLite("./data.db"), +}); + +export const db = new Kysely({ + dialect, +}); diff --git a/src/server/db/schema.sql b/src/server/db/schema.sql new file mode 100644 index 0000000..c0aca68 --- /dev/null +++ b/src/server/db/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS announcements ( + id INTEGER PRIMARY KEY, + title STRING, + text STRING, + image STRING, + publication_date TEXT +); + +CREATE INDEX IF NOT EXISTS announcements_publication_date_index ON announcements ( + publication_date +); \ No newline at end of file diff --git a/src/server/db/types.ts b/src/server/db/types.ts new file mode 100644 index 0000000..b2d5684 --- /dev/null +++ b/src/server/db/types.ts @@ -0,0 +1,17 @@ +import type { Generated, Insertable, Selectable, Updateable } from "kysely"; + +export interface Database { + announcements: AnnouncementTable; +} + +export interface AnnouncementTable { + id: Generated; + title: string; + text: string; + image: string; + publication_date: Date; +} + +export type Announcement = Selectable; +export type NewAnnouncement = Insertable; +export type AnnouncementUpdate = Updateable; diff --git a/yarn.lock b/yarn.lock index b24e98b..528736a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1241,6 +1241,11 @@ json5@^2.2.3: resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283" integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg== +kysely@^0.29.2: + version "0.29.2" + resolved "https://registry.yarnpkg.com/kysely/-/kysely-0.29.2.tgz#4a07da18db3bba2911693c2b3bd4af415104a85f" + integrity sha512-s6WVJyEZrbm6jhBpiKHsGHyePMrVQKJ85wZCFCr9W4QHv6WTjWIrdvTmO9hDEA3bNK0xkrE2DqrHsXMLWuZpQg== + lru-cache@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"