@tinyshed/tinystore 0.1.0-rc.2 → 0.1.0-rc.3

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/README.md CHANGED
@@ -28,7 +28,9 @@ import { open } from '@tinyshed/tinystore'
28
28
 
29
29
  await using store = await open('./data')
30
30
 
31
- const db = await store.sql('app')
31
+ const db = await store.sql('app', {
32
+ migrations: { '0001_users.sql': 'create table users (id integer primary key, name text)' },
33
+ })
32
34
  await db.exec`insert into users (id, name) values (${42}, ${'Ada'})`
33
35
 
34
36
  const sessions = store.kv.bucket<string>('sessions', { sliding: '30d' })
package/dist/logger.js CHANGED
@@ -54,18 +54,26 @@ export const sayOwn = (level, message, fields) => {
54
54
  own ??= logger('tinystore');
55
55
  own[level](message, fields);
56
56
  };
57
+ function forceColor() {
58
+ const force = process.env.FORCE_COLOR;
59
+ return force !== undefined && force !== '' && force !== '0';
60
+ }
57
61
  /** How a logger prints its lines, or undefined when it prints none. */
58
62
  function printer(options, out) {
59
63
  if (options.console === 'off') {
60
64
  return undefined;
61
65
  }
62
66
  const stream = out ?? (options.stdout === true ? process.stdout : process.stderr);
67
+ // FORCE_COLOR other than 0 says a person reads a pipe, as an IDE's run console is one
68
+ const forced = out === undefined && forceColor();
63
69
  const terminal = stream.isTTY === true;
64
- const pretty = options.console === 'pretty' || (options.console === undefined && terminal);
70
+ const pretty = options.console === 'pretty' || (options.console === undefined && (terminal || forced));
65
71
  if (!pretty) {
66
72
  return line => void stream.write(jsonLine(line));
67
73
  }
68
- const color = terminal && out === undefined && !process.env.NO_COLOR && process.env.TERM !== 'dumb';
74
+ const color = (forced || (terminal && out === undefined)) &&
75
+ !process.env.NO_COLOR &&
76
+ process.env.TERM !== 'dumb';
69
77
  return line => void stream.write(prettyLine(line, color, out?.utc === true));
70
78
  }
71
79
  /**
package/dist/sql.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // The application's own SQLite databases, sql/<name>.db, as Go's sqldb keeps
2
2
  // them: the application writes every query, the server owns the file, its
3
3
  // connections and its migrations, which the first open applies and every
4
- // later one checks.
4
+ // later one checks. An open without migrations takes the file as it is.
5
5
  import { readdir, readFile } from 'node:fs/promises';
6
6
  import { join } from 'node:path';
7
7
  import { InvalidError } from './errors.js';
package/dist/store.d.ts CHANGED
@@ -68,7 +68,9 @@ export declare class Store implements AsyncDisposable {
68
68
  /**
69
69
  * Opens a database of the application's own, sql/<name>.db. The first open
70
70
  * in the server, on an admin connection, applies its migrations; every
71
- * later one checks them against what the file applied.
71
+ * later one checks them against what the file applied. Without migrations
72
+ * it opens the file as it is, an empty one if there is none, and checks
73
+ * nothing.
72
74
  */
73
75
  sql(name: string, options?: SqlOptions): Promise<Database>;
74
76
  /**
package/dist/store.js CHANGED
@@ -53,7 +53,9 @@ export class Store {
53
53
  /**
54
54
  * Opens a database of the application's own, sql/<name>.db. The first open
55
55
  * in the server, on an admin connection, applies its migrations; every
56
- * later one checks them against what the file applied.
56
+ * later one checks them against what the file applied. Without migrations
57
+ * it opens the file as it is, an empty one if there is none, and checks
58
+ * nothing.
57
59
  */
58
60
  sql(name, options) {
59
61
  return openDatabase(this.#link, name, options);
@@ -121,6 +123,7 @@ export async function open(dir, options = {}) {
121
123
  return openWith(runtime, dir, options);
122
124
  }
123
125
  export async function openWith(runtime, dir, options = {}) {
126
+ checkDirectory(dir);
124
127
  const binary = () => findBinary(runtime, options.binary);
125
128
  const idle = options.idle === undefined ? undefined : ms(options.idle);
126
129
  if (options.clock !== undefined && options.private !== true) {
@@ -143,6 +146,25 @@ export async function connect(url, options) {
143
146
  await link.connection();
144
147
  return new Store(link);
145
148
  }
149
+ // two letters or more before a colon: an address, never a Windows drive
150
+ const address = /^([a-z][a-z0-9+.-]+):/i;
151
+ /**
152
+ * Refuses an address where a directory belongs: open('tcp://…') would make a
153
+ * folder of that name and start a server in it, rather than reach the one meant.
154
+ *
155
+ * open('tcp://db.internal:7070') → use connect
156
+ * open('pipe:tinystore-d761f24b7e59') → open the directory it serves
157
+ * open('C:/data') → a directory
158
+ */
159
+ function checkDirectory(dir) {
160
+ const scheme = address.exec(dir)?.[1]?.toLowerCase();
161
+ if (scheme === 'tcp' || scheme === 'tls') {
162
+ throw new InvalidError(`open takes a store's directory, and ${dir} is a server's address: connect('${dir}', { token }) reaches it`);
163
+ }
164
+ if (scheme !== undefined) {
165
+ throw new InvalidError(`open takes a store's directory, and ${dir} is an address: open the directory its server serves, and it is found through SERVE`);
166
+ }
167
+ }
146
168
  function findBinary(runtime, given) {
147
169
  const found = given ?? process.env.TINYSTORE_BIN ?? runtime.packagedBinary() ?? runtime.which('tinystore');
148
170
  if (found === undefined || found === '') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyshed/tinystore",
3
- "version": "0.1.0-rc.2",
3
+ "version": "0.1.0-rc.3",
4
4
  "description": "TinyStore for Bun and Node: kv, jobs, blobs, SQL, records and metrics in one directory, served by a sidecar",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -30,11 +30,11 @@
30
30
  "README.md"
31
31
  ],
32
32
  "optionalDependencies": {
33
- "@tinyshed/tinystore-darwin-arm64": "0.1.0-rc.2",
34
- "@tinyshed/tinystore-darwin-x64": "0.1.0-rc.2",
35
- "@tinyshed/tinystore-linux-arm64": "0.1.0-rc.2",
36
- "@tinyshed/tinystore-linux-x64": "0.1.0-rc.2",
37
- "@tinyshed/tinystore-win32-arm64": "0.1.0-rc.2",
38
- "@tinyshed/tinystore-win32-x64": "0.1.0-rc.2"
33
+ "@tinyshed/tinystore-darwin-arm64": "0.1.0-rc.3",
34
+ "@tinyshed/tinystore-darwin-x64": "0.1.0-rc.3",
35
+ "@tinyshed/tinystore-linux-arm64": "0.1.0-rc.3",
36
+ "@tinyshed/tinystore-linux-x64": "0.1.0-rc.3",
37
+ "@tinyshed/tinystore-win32-arm64": "0.1.0-rc.3",
38
+ "@tinyshed/tinystore-win32-x64": "0.1.0-rc.3"
39
39
  }
40
40
  }
package/src/logger.ts CHANGED
@@ -112,19 +112,29 @@ export const sayOwn: Say = (level, message, fields) => {
112
112
  own[level](message, fields)
113
113
  }
114
114
 
115
+ function forceColor(): boolean {
116
+ const force = process.env.FORCE_COLOR
117
+ return force !== undefined && force !== '' && force !== '0'
118
+ }
119
+
115
120
  /** How a logger prints its lines, or undefined when it prints none. */
116
121
  function printer(options: LoggerOptions, out?: ConsoleOut & { utc?: boolean }): Shared['print'] {
117
122
  if (options.console === 'off') {
118
123
  return undefined
119
124
  }
120
125
  const stream: ConsoleOut = out ?? (options.stdout === true ? process.stdout : process.stderr)
126
+ // FORCE_COLOR other than 0 says a person reads a pipe, as an IDE's run console is one
127
+ const forced = out === undefined && forceColor()
121
128
  const terminal = stream.isTTY === true
122
- const pretty = options.console === 'pretty' || (options.console === undefined && terminal)
129
+ const pretty =
130
+ options.console === 'pretty' || (options.console === undefined && (terminal || forced))
123
131
  if (!pretty) {
124
132
  return line => void stream.write(jsonLine(line))
125
133
  }
126
134
  const color =
127
- terminal && out === undefined && !process.env.NO_COLOR && process.env.TERM !== 'dumb'
135
+ (forced || (terminal && out === undefined)) &&
136
+ !process.env.NO_COLOR &&
137
+ process.env.TERM !== 'dumb'
128
138
  return line => void stream.write(prettyLine(line, color, out?.utc === true))
129
139
  }
130
140
 
package/src/sql.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // The application's own SQLite databases, sql/<name>.db, as Go's sqldb keeps
2
2
  // them: the application writes every query, the server owns the file, its
3
3
  // connections and its migrations, which the first open applies and every
4
- // later one checks.
4
+ // later one checks. An open without migrations takes the file as it is.
5
5
 
6
6
  import { readdir, readFile } from 'node:fs/promises'
7
7
  import { join } from 'node:path'
package/src/store.ts CHANGED
@@ -98,7 +98,9 @@ export class Store implements AsyncDisposable {
98
98
  /**
99
99
  * Opens a database of the application's own, sql/<name>.db. The first open
100
100
  * in the server, on an admin connection, applies its migrations; every
101
- * later one checks them against what the file applied.
101
+ * later one checks them against what the file applied. Without migrations
102
+ * it opens the file as it is, an empty one if there is none, and checks
103
+ * nothing.
102
104
  */
103
105
  sql(name: string, options?: SqlOptions): Promise<Database> {
104
106
  return openDatabase(this.#link, name, options)
@@ -178,6 +180,7 @@ export async function openWith(
178
180
  dir: string,
179
181
  options: OpenOptions = {},
180
182
  ): Promise<Store> {
183
+ checkDirectory(dir)
181
184
  const binary = () => findBinary(runtime, options.binary)
182
185
  const idle = options.idle === undefined ? undefined : ms(options.idle)
183
186
  if (options.clock !== undefined && options.private !== true) {
@@ -206,6 +209,31 @@ export async function connect(url: string, options: ConnectOptions): Promise<Sto
206
209
  return new Store(link)
207
210
  }
208
211
 
212
+ // two letters or more before a colon: an address, never a Windows drive
213
+ const address = /^([a-z][a-z0-9+.-]+):/i
214
+
215
+ /**
216
+ * Refuses an address where a directory belongs: open('tcp://…') would make a
217
+ * folder of that name and start a server in it, rather than reach the one meant.
218
+ *
219
+ * open('tcp://db.internal:7070') → use connect
220
+ * open('pipe:tinystore-d761f24b7e59') → open the directory it serves
221
+ * open('C:/data') → a directory
222
+ */
223
+ function checkDirectory(dir: string): void {
224
+ const scheme = address.exec(dir)?.[1]?.toLowerCase()
225
+ if (scheme === 'tcp' || scheme === 'tls') {
226
+ throw new InvalidError(
227
+ `open takes a store's directory, and ${dir} is a server's address: connect('${dir}', { token }) reaches it`,
228
+ )
229
+ }
230
+ if (scheme !== undefined) {
231
+ throw new InvalidError(
232
+ `open takes a store's directory, and ${dir} is an address: open the directory its server serves, and it is found through SERVE`,
233
+ )
234
+ }
235
+ }
236
+
209
237
  function findBinary(runtime: Runtime, given: string | undefined): string {
210
238
  const found =
211
239
  given ?? process.env.TINYSTORE_BIN ?? runtime.packagedBinary() ?? runtime.which('tinystore')