diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..edc569b --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1766622938, + "narHash": "sha256-Eovt/DOCYjFFBZuYbbG9j5jhklzxdNbUGVYYxh3lG3s=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5900a0a8850cbba98e16d5a7a6ed389402dfcf4f", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-25.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..7dc8eb0 --- /dev/null +++ b/flake.nix @@ -0,0 +1,38 @@ +{ + description = "Zamaan dev shell"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11"; + }; + + outputs = { self, nixpkgs }: + let + allSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + + forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { + pkgs = import nixpkgs { inherit system; }; + }); + in + { + devShells = forAllSystems ({ pkgs }: { + default = pkgs.mkShell { + packages = with pkgs; [ + nodejs_24 + nodePackages.pnpm + sqlite + ]; + + shellHook = '' + echo "Zamaan dev shell" + echo "Node version: $(node --version)" + echo "pnpm version: $(pnpm --version)" + ''; + }; + }); + }; +} diff --git a/src/db/index.ts b/src/db/index.ts index e89f19d..3608b40 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -3,6 +3,20 @@ import { drizzle } from 'drizzle-orm/better-sqlite3'; import * as schema from './schema'; import path from 'path'; -const dbUrl = process.env.DATABASE_URL || path.resolve(process.cwd(), 'zamaan.db'); -const sqlite = new Database(dbUrl, { readonly: false }); -export const db = drizzle(sqlite, { schema }); +let _db: ReturnType | null = null; + +function initDb() { + if (!_db) { + const dbUrl = process.env.DATABASE_URL || path.resolve(process.cwd(), 'zamaan.db'); + const sqlite = new Database(dbUrl, { readonly: false }); + _db = drizzle(sqlite, { schema }); + } + return _db; +} + +export const db = new Proxy({} as ReturnType, { + get(_target, prop) { + const database = initDb(); + return database[prop as keyof typeof database]; + } +});