dbcat 0.0.8 → 0.0.10

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.
Files changed (3) hide show
  1. package/README.md +22 -0
  2. package/package.json +1 -1
  3. package/src/index.ts +17 -9
package/README.md CHANGED
@@ -65,6 +65,28 @@ Pipe to `less -R` for scrollable output with colors:
65
65
  bunx dbcat ./data.db --full | less -R
66
66
  ```
67
67
 
68
+ ### Git diffable
69
+
70
+ Do these things to make git use `dbcat` to diff your sqlite databases:
71
+
72
+ ```sh
73
+ # .gitattributes
74
+ *.sqlite binary diff=dbcat
75
+ *.db binary diff=dbcat
76
+ ```
77
+
78
+ ```sh
79
+ git config diff.lockb.textconv "bunx dbcat --full"
80
+ git config diff.lockb.binary true
81
+ ```
82
+
83
+ And now you should be able to use
84
+
85
+ ```sh
86
+ git diff ./data.db
87
+ ```
88
+
89
+
68
90
  ## Requirements
69
91
 
70
92
  [Bun](https://bun.sh) v1.3+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbcat",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "A simple CLI to view database tables. Supports PostgreSQL, MySQL, and SQLite.",
5
5
  "author": "RiskyMH",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -9,19 +9,24 @@ import { join } from "node:path";
9
9
  const SQLITE_EXTENSIONS = [".db", ".sqlite", ".sqlite3", ".db3", ".s3db"];
10
10
  const REMOTE_PROTOCOLS = ["http://", "https://", "s3://"];
11
11
 
12
- export function createConnection(input?: string): InstanceType<typeof Bun.SQL> {
12
+ export function createConnection(
13
+ input?: string,
14
+ options?: { readonly?: boolean }
15
+ ): InstanceType<typeof Bun.SQL> {
16
+ const opts = { readonly: true, ...options };
17
+
13
18
  if (!input) {
14
- return new Bun.SQL();
19
+ return new Bun.SQL(opts);
15
20
  }
16
21
 
17
22
  if (
18
23
  !input.includes("://") &&
19
24
  SQLITE_EXTENSIONS.some((ext) => input.endsWith(ext))
20
25
  ) {
21
- return new Bun.SQL(`sqlite://${input}`);
26
+ return new Bun.SQL(`sqlite://${input}`, opts);
22
27
  }
23
28
 
24
- return new Bun.SQL(input);
29
+ return new Bun.SQL(input, opts);
25
30
  }
26
31
 
27
32
  async function downloadToTmp(url: string): Promise<string> {
@@ -141,7 +146,7 @@ function displayQueryResult(rows: Record<string, unknown>[]) {
141
146
  }
142
147
 
143
148
  async function readStdin(): Promise<string | null> {
144
- if (process.stdin.isTTY) {
149
+ if (process.stdin.isTTY || Bun.stdin.size === 0) {
145
150
  return null;
146
151
  }
147
152
 
@@ -157,11 +162,14 @@ function parseArgs() {
157
162
  const args = process.argv.slice(2);
158
163
  let input: string | undefined;
159
164
  let full = false;
165
+ let help = false
160
166
  let json: false | "plain" | "color" = false;
161
167
 
162
168
  for (const arg of args) {
163
169
  if (arg === "--full" || arg === "-f") {
164
170
  full = true;
171
+ } else if (arg === "--help" || arg === "-h") {
172
+ help = true;
165
173
  } else if (arg === "--json") {
166
174
  json = "plain";
167
175
  } else if (arg === "--json=color") {
@@ -171,7 +179,7 @@ function parseArgs() {
171
179
  }
172
180
  }
173
181
 
174
- return { input, full, json };
182
+ return { input, full, json, help };
175
183
  }
176
184
 
177
185
  const DEFAULT_LIMIT = 100;
@@ -199,9 +207,9 @@ function outputJson(data: unknown, color: boolean) {
199
207
  }
200
208
 
201
209
  async function main() {
202
- const { input, full, json } = parseArgs();
210
+ const { input, full, json, help } = parseArgs();
203
211
 
204
- if (!input && !process.env.DATABASE_URL) {
212
+ if ((!input && !process.env.DATABASE_URL) || help) {
205
213
  showUsageAndExit();
206
214
  }
207
215
 
@@ -238,7 +246,7 @@ async function main() {
238
246
 
239
247
  let sql: InstanceType<typeof Bun.SQL>;
240
248
  try {
241
- sql = createConnection(connectionInput);
249
+ sql = createConnection(connectionInput, { readonly: Bun.stdin.size == 0 });
242
250
  } catch (error) {
243
251
  console.error("Failed to connect:");
244
252
  console.error(error instanceof Error ? error.message : String(error));