@zinn-dev/core 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/config.ts +14 -0
- package/src/constant.ts +18 -0
- package/src/db.ts +34 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zinn-dev/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Shared logic for zinn, a kanban workflow in the terminal.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"index.ts"
|
|
20
|
+
"index.ts",
|
|
21
|
+
"src"
|
|
21
22
|
],
|
|
22
23
|
"publishConfig": {
|
|
23
24
|
"access": "public"
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { DATA_DIR, MAIN_DIR, CONFIG_PATH, defaultConfig } from "./constant";
|
|
3
|
+
|
|
4
|
+
if (!existsSync(MAIN_DIR)) {
|
|
5
|
+
mkdirSync(MAIN_DIR);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (!existsSync(DATA_DIR)) {
|
|
9
|
+
mkdirSync(DATA_DIR);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
13
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(defaultConfig) + "\n", { encoding: "utf-8" });
|
|
14
|
+
}
|
package/src/constant.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export const MAIN_FOLDER_NAME = ".zinn";
|
|
5
|
+
export const CONFIG_FILE_NAME = "settings.json";
|
|
6
|
+
export const DB_FILE_NAME = "zinn.sqlite";
|
|
7
|
+
|
|
8
|
+
export const MAIN_DIR = join(homedir(), MAIN_FOLDER_NAME);
|
|
9
|
+
export const CONFIG_PATH = join(MAIN_DIR, CONFIG_FILE_NAME);
|
|
10
|
+
export const DATA_DIR = join(MAIN_DIR, "data");
|
|
11
|
+
// TODO: environment variable based path overriding for tests
|
|
12
|
+
export const DB_PATH = join(DATA_DIR, DB_FILE_NAME);
|
|
13
|
+
export const DB_TABLE = {
|
|
14
|
+
project: "project",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// TODO: implement zod schema for setting.json
|
|
18
|
+
export const defaultConfig = {};
|
package/src/db.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { DB_PATH, DB_TABLE } from "./constant";
|
|
3
|
+
import { randomUUIDv7 } from "bun";
|
|
4
|
+
|
|
5
|
+
export const db = new Database(DB_PATH, { create: true });
|
|
6
|
+
db.run("PRAGMA journal_mode = WAL;");
|
|
7
|
+
// TODO: https://bun.com/docs/runtime/sqlite#wal-sidecar-file-cleanup macOS does not auto cleanup
|
|
8
|
+
|
|
9
|
+
const projectTableSetup = db.query(`CREATE TABLE IF NOT EXISTS ${DB_TABLE.project} (
|
|
10
|
+
id TEXT PRIMARY KEY,
|
|
11
|
+
key TEXT NOT NULL UNIQUE,
|
|
12
|
+
name TEXT NOT NULL,
|
|
13
|
+
created_at INTEGER NOT NULL,
|
|
14
|
+
updated_at INTEGER NOT NULL,
|
|
15
|
+
archived_at INTEGER);`);
|
|
16
|
+
projectTableSetup.run();
|
|
17
|
+
|
|
18
|
+
export function getProjectByKey(key: string) {
|
|
19
|
+
return db.query(`SELECT * FROM ${DB_TABLE.project} WHERE key = $key`).get({ $key: key });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// TODO: switch to object param
|
|
23
|
+
export function addProject(key: string, name: string) {
|
|
24
|
+
const query = db.query(`INSERT INTO
|
|
25
|
+
${DB_TABLE.project} (id, key, name, created_at, updated_at)
|
|
26
|
+
VALUES ($id, $key, $name, $created, $updated);`);
|
|
27
|
+
const time = Date.now();
|
|
28
|
+
|
|
29
|
+
return query.run({ $id: randomUUIDv7(), $key: key, $name: name, $created: time, $updated: time });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function deleteProjectByKey(key: string) {
|
|
33
|
+
return db.query(`DELETE FROM ${DB_TABLE.project} WHERE key = $key;`).run({ $key: key });
|
|
34
|
+
}
|