@otwdesign/db-next 0.0.0-stage → 1.4.0
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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +87 -2
- package/bin/otw-db-next.js +4 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +85 -0
- package/dist/cli.js.map +1 -0
- package/dist/connections.d.ts +17 -0
- package/dist/connections.js +45 -0
- package/dist/connections.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +90 -0
- package/dist/init.js +407 -0
- package/dist/init.js.map +1 -0
- package/dist/route.d.ts +47 -0
- package/dist/route.js +212 -0
- package/dist/route.js.map +1 -0
- package/dist/session.d.ts +27 -0
- package/dist/session.js +90 -0
- package/dist/session.js.map +1 -0
- package/dist/sign-in.d.ts +20 -0
- package/dist/sign-in.js +93 -0
- package/dist/sign-in.js.map +1 -0
- package/dist/supabase.d.ts +30 -0
- package/dist/supabase.js +108 -0
- package/dist/supabase.js.map +1 -0
- package/dist/users.d.ts +4 -0
- package/dist/users.js +21 -0
- package/dist/users.js.map +1 -0
- package/package.json +64 -3
- package/src/cli.ts +82 -0
- package/src/connections.ts +45 -0
- package/src/index.ts +5 -0
- package/src/init.ts +556 -0
- package/src/route.ts +278 -0
- package/src/session.ts +116 -0
- package/src/sign-in.ts +132 -0
- package/src/supabase.ts +133 -0
- package/src/users.ts +26 -0
package/dist/init.js
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { accessSync, constants, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { MIN_PASSWORD } from "./session.js";
|
|
5
|
+
const PATH = /^(?:\/[A-Za-z0-9._~-]+)+$/;
|
|
6
|
+
/** The app's own PostgreSQL, by the names `appConnections()` reads. */
|
|
7
|
+
const OWN = ['DATABASE_URL_UNPOOLED', 'POSTGRES_URL_NON_POOLING', 'DATABASE_URL', 'POSTGRES_URL'];
|
|
8
|
+
/** Whether `path` is `root` or inside it. */
|
|
9
|
+
function within(root, path) {
|
|
10
|
+
const from = relative(root, path);
|
|
11
|
+
return from === '' || (!from.startsWith('..') && !isAbsolute(from));
|
|
12
|
+
}
|
|
13
|
+
/** More than any file read here holds: a package.json, a next.config, an env file. */
|
|
14
|
+
const MOST = 1024 * 1024;
|
|
15
|
+
/**
|
|
16
|
+
* A file's text, without a byte-order mark; null when it isn't there, isn't a file (a pipe of
|
|
17
|
+
* that name would be waited on for ever) or is larger than such files are (read whole, it is a
|
|
18
|
+
* wait as long as its author likes).
|
|
19
|
+
*/
|
|
20
|
+
function read(path) {
|
|
21
|
+
try {
|
|
22
|
+
const file = statSync(path);
|
|
23
|
+
if (!file.isFile() || file.size > MOST)
|
|
24
|
+
return null;
|
|
25
|
+
return readFileSync(path, 'utf8').replace(/^\uFEFF/, '');
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** The package manager, by the nearest lockfile on the way up (a monorepo keeps it at its root). */
|
|
32
|
+
function managerOf(dir, packageManager) {
|
|
33
|
+
const locks = [
|
|
34
|
+
['pnpm-lock.yaml', 'pnpm'],
|
|
35
|
+
['yarn.lock', 'yarn'],
|
|
36
|
+
['bun.lock', 'bun'],
|
|
37
|
+
['bun.lockb', 'bun'],
|
|
38
|
+
['package-lock.json', 'npm'],
|
|
39
|
+
];
|
|
40
|
+
for (let at = dir;; at = dirname(at)) {
|
|
41
|
+
for (const [file, manager] of locks)
|
|
42
|
+
if (existsSync(join(at, file)))
|
|
43
|
+
return manager;
|
|
44
|
+
if (dirname(at) === at)
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
const named = typeof packageManager === 'string' ? packageManager.split('@')[0] : '';
|
|
48
|
+
return named === 'pnpm' || named === 'yarn' || named === 'bun' ? named : 'npm';
|
|
49
|
+
}
|
|
50
|
+
/** The names a dotenv file sets, comments and blank lines aside. */
|
|
51
|
+
function envNames(text) {
|
|
52
|
+
return (text ?? '')
|
|
53
|
+
.split('\n')
|
|
54
|
+
.map((line) => /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line)?.[1])
|
|
55
|
+
.filter((name) => !!name);
|
|
56
|
+
}
|
|
57
|
+
/** Whether `path` is a folder, or leads to one. */
|
|
58
|
+
function isDirectory(path) {
|
|
59
|
+
try {
|
|
60
|
+
return statSync(path).isDirectory();
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Whether files can be made in the folder `path`. */
|
|
67
|
+
function writable(path) {
|
|
68
|
+
try {
|
|
69
|
+
accessSync(path, constants.W_OK | constants.X_OK);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** The names in the folder `path`; null when it can't be listed. */
|
|
77
|
+
function namesIn(path) {
|
|
78
|
+
try {
|
|
79
|
+
return readdirSync(path);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Whether there is anything at `path`, a link that leads nowhere included. */
|
|
86
|
+
function entry(path) {
|
|
87
|
+
try {
|
|
88
|
+
lstatSync(path);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** What a file of JSON holds, when it holds an object: nothing else has anything to read. */
|
|
96
|
+
function objectIn(text) {
|
|
97
|
+
try {
|
|
98
|
+
const value = JSON.parse(text ?? '{}');
|
|
99
|
+
return typeof value === 'object' && value !== null ? value : {};
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return {};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** A path as `--path` and a route take it: short, segments of plain characters, none of them dots alone. */
|
|
106
|
+
function plainPath(path) {
|
|
107
|
+
// Of a length paths have: it's written into a file and printed, as often as it's long.
|
|
108
|
+
if (path.length > 200)
|
|
109
|
+
return false;
|
|
110
|
+
return PATH.test(path) && !path.split('/').some((segment) => /^\.+$/.test(segment));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `basePath` as next.config sets it: the path, an empty string when it sets none, null when what
|
|
114
|
+
* the file says of it can't be taken for one plain path. It is taken only in the one shape that
|
|
115
|
+
* needs no reading of JavaScript: the word once in the file, as a key with a short quoted value,
|
|
116
|
+
* on a line with no comment before it and nothing joined on after it. Anything else (a variable, two mentions, a value built
|
|
117
|
+
* from parts) gets a note and no guess: a wrong address is worse than one to fill in. A block
|
|
118
|
+
* comment over several lines isn't seen for what it is; the note says where the address goes.
|
|
119
|
+
*/
|
|
120
|
+
function basePathIn(config) {
|
|
121
|
+
const first = config.indexOf('basePath');
|
|
122
|
+
if (first < 0)
|
|
123
|
+
return '';
|
|
124
|
+
if (config.indexOf('basePath', first + 1) >= 0)
|
|
125
|
+
return null;
|
|
126
|
+
const before = config.slice(config.lastIndexOf('\n', first) + 1, first);
|
|
127
|
+
const set = /^basePath\s*:\s*(['"`])([^'"`\n\\$]{0,201})\1(?=[ \t]*(?:[,}\r\n]|$))/.exec(config.slice(first, first + 300));
|
|
128
|
+
if (!set || /\/\/|\/\*|^\s*\*/.test(before))
|
|
129
|
+
return null;
|
|
130
|
+
const value = set[2].replace(/\/$/, '');
|
|
131
|
+
return value === '' || plainPath(value) ? value : null;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The Node major `engines.node` holds an app to, when it names one version line: `20`, `20.x`,
|
|
135
|
+
* `^18.17`. A range open upwards (`>=18`) names none.
|
|
136
|
+
*/
|
|
137
|
+
function pinnedMajor(range) {
|
|
138
|
+
// Text only: package.json is someone's file.
|
|
139
|
+
if (typeof range !== 'string')
|
|
140
|
+
return null;
|
|
141
|
+
const pinned = /^[\^~=v]?\s*(\d{1,4})(?:\.(?:x|\*|\d+))*$/.exec(range.trim());
|
|
142
|
+
return pinned ? Number(pinned[1]) : null;
|
|
143
|
+
}
|
|
144
|
+
const CONFIGS = ['next.config.ts', 'next.config.mjs', 'next.config.js', 'next.config.cjs'];
|
|
145
|
+
/** The env files Next.js reads, on a machine and in a build. */
|
|
146
|
+
const ENV_FILES = [
|
|
147
|
+
'.env.local',
|
|
148
|
+
'.env',
|
|
149
|
+
'.env.development.local',
|
|
150
|
+
'.env.development',
|
|
151
|
+
'.env.production.local',
|
|
152
|
+
'.env.production',
|
|
153
|
+
];
|
|
154
|
+
/** Looks at the app in `dir`. */
|
|
155
|
+
export function detect(dir) {
|
|
156
|
+
const root = resolve(dir);
|
|
157
|
+
const manifest = objectIn(read(join(root, 'package.json')));
|
|
158
|
+
const dependencies = { ...manifest.dependencies, ...manifest.devDependencies };
|
|
159
|
+
const has = (path) => existsSync(join(root, path));
|
|
160
|
+
const src = has('src/app') || has('src/pages') ? 'src' : '';
|
|
161
|
+
const config = CONFIGS.map((file) => read(join(root, file))).find((text) => text !== null);
|
|
162
|
+
const middleware = ['middleware.ts', 'middleware.js', 'proxy.ts', 'proxy.js']
|
|
163
|
+
.flatMap((file) => [file, join('src', file)])
|
|
164
|
+
.find(has) ?? null;
|
|
165
|
+
const basePath = basePathIn(config ?? '');
|
|
166
|
+
const envFiles = ENV_FILES.map((file) => read(join(root, file)));
|
|
167
|
+
const unread = ['package.json', ...CONFIGS, ...ENV_FILES].filter((file) => entry(join(root, file)) && read(join(root, file)) === null);
|
|
168
|
+
const names = new Set(envFiles.flatMap(envNames));
|
|
169
|
+
const mentions = new Set(envFiles.flatMap((text) => text?.match(/DB_ADMIN_[A-Z0-9_]+/g) ?? []));
|
|
170
|
+
return {
|
|
171
|
+
dir: root,
|
|
172
|
+
next: 'next' in dependencies,
|
|
173
|
+
src,
|
|
174
|
+
typescript: has('tsconfig.json'),
|
|
175
|
+
manager: managerOf(root, manifest.packageManager),
|
|
176
|
+
nextBasePath: basePath ?? '',
|
|
177
|
+
basePathUnread: basePath === null,
|
|
178
|
+
unread,
|
|
179
|
+
middleware,
|
|
180
|
+
passwordInDotEnv: envNames(read(join(root, '.env'))).includes('DB_ADMIN_PASSWORD'),
|
|
181
|
+
envNames: names,
|
|
182
|
+
envMentions: mentions,
|
|
183
|
+
supabase: '@supabase/supabase-js' in dependencies ||
|
|
184
|
+
'@supabase/ssr' in dependencies ||
|
|
185
|
+
names.has('NEXT_PUBLIC_SUPABASE_URL') ||
|
|
186
|
+
names.has('SUPABASE_URL'),
|
|
187
|
+
nodeMajor: pinnedMajor(manifest.engines?.node),
|
|
188
|
+
installed: '@otwdesign/db-next' in dependencies,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The route file: all of the tool's setup an app holds. The comments are for whoever opens it
|
|
193
|
+
* months later and wonders where the password and the databases come from.
|
|
194
|
+
*/
|
|
195
|
+
export function routeFile(basePath) {
|
|
196
|
+
const options = basePath === '/db' ? '' : `{ basePath: '${basePath}' }`;
|
|
197
|
+
return `import { createDbRoute } from '@otwdesign/db-next'
|
|
198
|
+
|
|
199
|
+
// OTW DB at ${basePath}: its page, its files and its API, all in this one route.
|
|
200
|
+
// Who gets in: anyone with DB_ADMIN_PASSWORD, and the app's signed-in users listed in
|
|
201
|
+
// DB_ADMIN_USERS (their emails, comma-separated), who need no password.
|
|
202
|
+
// Which databases: DB_ADMIN_<NAME>_URL, or the app's own (DATABASE_URL), read-only.
|
|
203
|
+
export const runtime = 'nodejs'
|
|
204
|
+
export const dynamic = 'force-dynamic'
|
|
205
|
+
|
|
206
|
+
export const { GET, POST } = createDbRoute(${options})
|
|
207
|
+
`;
|
|
208
|
+
}
|
|
209
|
+
/** What to do in the app `detect` found. Writes nothing. */
|
|
210
|
+
export function plan(detected, flags = {}) {
|
|
211
|
+
const nothing = { files: [], env: [], install: null, notes: [], url: '' };
|
|
212
|
+
if (detected.unread.includes('package.json')) {
|
|
213
|
+
return {
|
|
214
|
+
...nothing,
|
|
215
|
+
refused: 'package.json couldn’t be read: it isn’t a file, or is larger than such a file is.',
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if (!detected.next) {
|
|
219
|
+
return {
|
|
220
|
+
...nothing,
|
|
221
|
+
refused: 'This isn’t a Next.js app: package.json has no "next" among its dependencies. Run it in the app’s own directory.',
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
const path = (flags.path ?? '/db').replace(/\/+$/, '');
|
|
225
|
+
// A segment of dots alone is this folder, or the one above it: out of the app, by two of them.
|
|
226
|
+
if (!plainPath(path)) {
|
|
227
|
+
return {
|
|
228
|
+
...nothing,
|
|
229
|
+
refused: `--path is a path such as /db or /admin/db: segments of letters, digits, . _ ~ and -, not "${flags.path}".`,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
const app = join(detected.src, 'app');
|
|
233
|
+
const route = join(app, ...path.split('/').filter(Boolean));
|
|
234
|
+
const pages = join(detected.src, 'pages', ...path.split('/').filter(Boolean));
|
|
235
|
+
// As the browser has it: next.config's basePath comes before every route of the app.
|
|
236
|
+
const basePath = `${detected.nextBasePath.replace(/\/+$/, '')}${path}`;
|
|
237
|
+
const file = join(route, '[[...path]]', detected.typescript ? 'route.ts' : 'route.js');
|
|
238
|
+
// An earlier run's own route, as it wrote it: this one goes on from there (an install that
|
|
239
|
+
// failed, a password still to set) and writes over nothing.
|
|
240
|
+
const written = read(join(detected.dir, file)) === routeFile(basePath) &&
|
|
241
|
+
namesIn(join(detected.dir, route))?.length === 1;
|
|
242
|
+
const taken = written
|
|
243
|
+
? undefined
|
|
244
|
+
: [
|
|
245
|
+
route,
|
|
246
|
+
pages,
|
|
247
|
+
...['tsx', 'ts', 'jsx', 'js'].map((extension) => `${pages}.${extension}`),
|
|
248
|
+
].find((candidate) => existsSync(join(detected.dir, candidate)));
|
|
249
|
+
// Through a link, the route would be written outside the app, where the link leads.
|
|
250
|
+
let inside = detected.dir;
|
|
251
|
+
for (const segment of file.split(sep).slice(0, -1)) {
|
|
252
|
+
const next = join(inside, segment);
|
|
253
|
+
if (!entry(next))
|
|
254
|
+
break;
|
|
255
|
+
inside = next;
|
|
256
|
+
}
|
|
257
|
+
if (inside !== detected.dir && !isDirectory(inside)) {
|
|
258
|
+
return {
|
|
259
|
+
...nothing,
|
|
260
|
+
refused: `${relative(detected.dir, inside)} isn’t a folder: the route can’t be written under it.`,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
let leadsOut;
|
|
264
|
+
try {
|
|
265
|
+
leadsOut = !within(realpathSync(detected.dir), realpathSync(inside));
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// A link that leads nowhere: nothing can be made in it either.
|
|
269
|
+
leadsOut = true;
|
|
270
|
+
}
|
|
271
|
+
if (leadsOut) {
|
|
272
|
+
return {
|
|
273
|
+
...nothing,
|
|
274
|
+
refused: `${relative(detected.dir, inside)} is a link to somewhere outside this app: the route would be written there. Run it in the app the link leads to.`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (!written && !taken && !writable(inside)) {
|
|
278
|
+
// Said now, as a dry run would have to: the run itself would stop here.
|
|
279
|
+
return {
|
|
280
|
+
...nothing,
|
|
281
|
+
refused: `${relative(detected.dir, inside) || 'This folder'} can’t be written in: the route can’t be made there. Check its permissions.`,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
if (taken) {
|
|
285
|
+
return {
|
|
286
|
+
...nothing,
|
|
287
|
+
refused: `${taken} is there already: the app has something at ${path}. Choose another place with --path, such as --path /admin/db.`,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
const notes = [];
|
|
291
|
+
const lines = [];
|
|
292
|
+
if (!detected.envNames.has('DB_ADMIN_PASSWORD')) {
|
|
293
|
+
lines.push(`# OTW DB (${basePath}). A password of your own, ${MIN_PASSWORD} characters at least: whoever has it can open the database.`, 'DB_ADMIN_PASSWORD=');
|
|
294
|
+
notes.push(`Choose a strong password of your own and set it as DB_ADMIN_PASSWORD: ${MIN_PASSWORD} characters at least, from your password manager or a command such as openssl rand -base64 32. It’s set in none of the app’s env files; until it’s set somewhere, nobody signs in with a password. Keep it out of git: in .env.local on your machine (git check-ignore .env.local names the file when git leaves it alone), in your host’s settings where the app runs.`);
|
|
295
|
+
}
|
|
296
|
+
if (!detected.envMentions.has('DB_ADMIN_USERS')) {
|
|
297
|
+
lines.push('# The app’s signed-in users who get in without it: their emails, comma-separated.', '# DB_ADMIN_USERS=you@example.com,colleague@example.com');
|
|
298
|
+
}
|
|
299
|
+
const named = [...detected.envNames].some((name) => /^DB_ADMIN_[A-Z0-9_]+_URL$/.test(name));
|
|
300
|
+
const own = OWN.find((name) => detected.envNames.has(name));
|
|
301
|
+
if (!named && !detected.envMentions.has('DB_ADMIN_MAIN_URL')) {
|
|
302
|
+
lines.push(...(own
|
|
303
|
+
? [
|
|
304
|
+
`# The database it opens: the app’s own (${own}), read-only. To choose another, or more:`,
|
|
305
|
+
'# DB_ADMIN_MAIN_URL=postgres://user:password@host:5432/database',
|
|
306
|
+
]
|
|
307
|
+
: [
|
|
308
|
+
'# The database it opens, such as postgres://user:password@host:5432/database:',
|
|
309
|
+
'DB_ADMIN_MAIN_URL=',
|
|
310
|
+
]), '# DB_ADMIN_MAIN_ACCESS=write # read, unless you say so');
|
|
311
|
+
}
|
|
312
|
+
const unread = detected.unread.filter((file) => file !== 'package.json');
|
|
313
|
+
if (unread.length) {
|
|
314
|
+
notes.push(`Not read, so what ${unread.length > 1 ? 'they set' : 'it sets'} isn’t counted above: ${unread.join(', ')} (not a file, or larger than such a file is). Check there for DB_ADMIN_PASSWORD and the database’s address yourself.`);
|
|
315
|
+
}
|
|
316
|
+
if (detected.passwordInDotEnv) {
|
|
317
|
+
notes.push('DB_ADMIN_PASSWORD is set in .env, a file apps usually keep in git. If git has it, whoever can read the repository has the password: move it to .env.local (or your host’s settings) and choose a new one.');
|
|
318
|
+
}
|
|
319
|
+
if (!named && !own) {
|
|
320
|
+
notes.push(detected.supabase
|
|
321
|
+
? 'This app talks to Supabase through its API, so it holds no address of the database itself. In Supabase, open Connect, copy the “Session pooler” connection string, and set it as DB_ADMIN_MAIN_URL. (Not “Direct connection”: Vercel can’t reach it. Not “Transaction pooler”: the tool keeps a session.)'
|
|
322
|
+
: 'No database address was found. Set DB_ADMIN_MAIN_URL to the database to open, such as postgres://user:password@host:5432/database.');
|
|
323
|
+
}
|
|
324
|
+
if (detected.middleware) {
|
|
325
|
+
notes.push(`This app has ${detected.middleware}. If it signs people in on every route, or redirects them, leave ${basePath} out of its matcher: the tool has its own sign-in, and its API must not be answered with a login page. For example: matcher: ['/((?!${basePath.slice(1)}|_next/static|_next/image|favicon.ico).*)']`);
|
|
326
|
+
}
|
|
327
|
+
if (detected.supabase) {
|
|
328
|
+
notes.push('On Supabase Auth, the users you list in DB_ADMIN_USERS are recognised by their session, with no code. List only people who already have an account, and keep “Confirm email” on in Supabase: an address nobody has registered yet could otherwise be signed up by anyone.');
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
notes.push('To let your own signed-in users in without the password, list their emails in DB_ADMIN_USERS and tell the route who is signed in: createDbRoute({ user: async () => (await auth())?.user?.email }).');
|
|
332
|
+
}
|
|
333
|
+
if (detected.nodeMajor !== null && detected.nodeMajor < 22) {
|
|
334
|
+
notes.push(`package.json asks for Node ${detected.nodeMajor}; OTW DB needs 22.18 or newer. On Vercel, set the project’s Node.js version to 22 or 24.`);
|
|
335
|
+
}
|
|
336
|
+
if (detected.basePathUnread) {
|
|
337
|
+
notes.push(`next.config mentions basePath in a way this couldn’t read as one plain path, so the route was written without it. If the app is served under one, name the whole address in the route file: createDbRoute({ basePath: '<your basePath>${path}' }).`);
|
|
338
|
+
}
|
|
339
|
+
notes.push('On your host, set the same variables. On Vercel: vercel env add DB_ADMIN_PASSWORD (and DB_ADMIN_USERS, DB_ADMIN_MAIN_URL where you use them), then deploy.');
|
|
340
|
+
const add = { npm: 'install', pnpm: 'add', yarn: 'add', bun: 'add' }[detected.manager];
|
|
341
|
+
return {
|
|
342
|
+
refused: null,
|
|
343
|
+
files: written ? [] : [{ path: file, content: routeFile(basePath) }],
|
|
344
|
+
env: lines,
|
|
345
|
+
install: flags.install === false || detected.installed
|
|
346
|
+
? null
|
|
347
|
+
: { command: detected.manager, args: [add, '@otwdesign/db-next'] },
|
|
348
|
+
notes,
|
|
349
|
+
url: basePath,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Does what the plan says, in the app's directory. `run` installs; a test stands in for it. An
|
|
354
|
+
* install that fails (no network, a registry that says no) is reported, not thrown: the files
|
|
355
|
+
* are written by then, and what's left to do must still be said.
|
|
356
|
+
*/
|
|
357
|
+
export function apply(detected, planned, run = (command, args, cwd) =>
|
|
358
|
+
// On Windows the package managers are .cmd files, which only a shell runs. The command and
|
|
359
|
+
// its arguments are this file's own words, never what someone typed.
|
|
360
|
+
void execFileSync(command, args, {
|
|
361
|
+
cwd,
|
|
362
|
+
stdio: 'inherit',
|
|
363
|
+
shell: process.platform === 'win32',
|
|
364
|
+
})) {
|
|
365
|
+
if (planned.refused)
|
|
366
|
+
throw new Error(planned.refused);
|
|
367
|
+
for (const file of planned.files) {
|
|
368
|
+
const target = join(detected.dir, file.path);
|
|
369
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
370
|
+
// Never over what's there: `plan` refused if it was, and this holds if it appeared since.
|
|
371
|
+
writeFileSync(target, file.content, { flag: 'wx' });
|
|
372
|
+
}
|
|
373
|
+
if (planned.install) {
|
|
374
|
+
try {
|
|
375
|
+
run(planned.install.command, planned.install.args, detected.dir);
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
return { installFailed: error.message.split('\n')[0] ?? 'it failed' };
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return { installFailed: null };
|
|
382
|
+
}
|
|
383
|
+
/** What was done, or would be, for the terminal. */
|
|
384
|
+
export function report(detected, planned, dryRun, outcome = { installFailed: null }) {
|
|
385
|
+
if (planned.refused)
|
|
386
|
+
return `${planned.refused}\n`;
|
|
387
|
+
const did = dryRun ? 'Would' : 'Did';
|
|
388
|
+
const out = [`OTW DB for this Next.js app, at ${planned.url}:`, ''];
|
|
389
|
+
for (const file of planned.files)
|
|
390
|
+
out.push(` ${did} write ${file.path}`);
|
|
391
|
+
if (planned.install) {
|
|
392
|
+
const command = `${planned.install.command} ${planned.install.args.join(' ')}`;
|
|
393
|
+
out.push(outcome.installFailed
|
|
394
|
+
? ` Couldn’t run ${command} (${outcome.installFailed}). Run it yourself, in this directory.`
|
|
395
|
+
: ` ${did} run ${command}`);
|
|
396
|
+
}
|
|
397
|
+
if (planned.env.length) {
|
|
398
|
+
out.push('', ' Yours to add to the app’s environment (.env.local on your machine, your host’s settings):', ...planned.env.map((line) => ` ${line}`));
|
|
399
|
+
}
|
|
400
|
+
out.push('', ...planned.notes.flatMap((note) => [`- ${note}`, '']));
|
|
401
|
+
out.push(dryRun
|
|
402
|
+
? 'Nothing was changed: run it again without --dry-run.'
|
|
403
|
+
: `${planned.env.includes('DB_ADMIN_PASSWORD=') ? 'Set DB_ADMIN_PASSWORD, start' : 'Start'} the app and open ${planned.url}.`);
|
|
404
|
+
// Text and line ends only. Nothing above quotes the app's files; this holds if a later line does.
|
|
405
|
+
return `${out.join('\n')}\n`.replace(/[^\n\P{Cc}]/gu, '');
|
|
406
|
+
}
|
|
407
|
+
//# sourceMappingURL=init.js.map
|
package/dist/init.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,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,kGAAkG;IAClG,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAA;AAC3D,CAAC"}
|
package/dist/route.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ConnectionConfig, type DbAdminOptions } from '@otwdesign/db-core';
|
|
2
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
3
|
+
/** What `createDbRoute` takes: all of it optional, for an app that sets its env and nothing else. */
|
|
4
|
+
export interface DbRouteOptions extends Partial<Omit<DbAdminOptions, 'authorize' | 'basePath' | 'connections'>> {
|
|
5
|
+
/**
|
|
6
|
+
* Where the route is: `/db` for `app/db/[[...path]]/route.ts`. With `basePath` set in
|
|
7
|
+
* next.config, that comes first, as the browser has it: `/docs/db`.
|
|
8
|
+
*/
|
|
9
|
+
basePath?: string;
|
|
10
|
+
/** The databases to open. Defaults to `appConnections()`: DB_ADMIN_<NAME>_URL, or the app's own. */
|
|
11
|
+
connections?: readonly ConnectionConfig[];
|
|
12
|
+
/** The password that signs anyone in. Defaults to DB_ADMIN_PASSWORD; 16 characters at least. */
|
|
13
|
+
password?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The app's own users who get in without the password, by email (or id), separated by commas.
|
|
16
|
+
* Defaults to DB_ADMIN_USERS.
|
|
17
|
+
*/
|
|
18
|
+
users?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Who is signed in to the app, as its own sign-in knows them: their email, or an id, to look up
|
|
21
|
+
* among `users`. Without it, an app on Supabase Auth has its session read; any other sign-in
|
|
22
|
+
* (Auth.js, Clerk, the app's own) is asked here.
|
|
23
|
+
*/
|
|
24
|
+
user?(request: Request): string | null | undefined | Promise<string | null | undefined>;
|
|
25
|
+
/**
|
|
26
|
+
* The app's own decision, instead of the password and the users list: who is asking, or null.
|
|
27
|
+
* With it, this package serves no sign-in page; set `signInUrl` to the app's.
|
|
28
|
+
*/
|
|
29
|
+
authorize?: DbAdminOptions['authorize'];
|
|
30
|
+
/** The environment read for the defaults. `process.env`, unless a test says otherwise. */
|
|
31
|
+
env?: Env;
|
|
32
|
+
}
|
|
33
|
+
/** The route's handlers, as a Next.js route file exports them. */
|
|
34
|
+
export interface DbRoute {
|
|
35
|
+
GET(request: Request): Promise<Response>;
|
|
36
|
+
POST(request: Request): Promise<Response>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* OTW DB as a Next.js route: `export const { GET, POST } = createDbRoute()` in
|
|
40
|
+
* `app/db/[[...path]]/route.ts`. It signs people in itself: anyone with DB_ADMIN_PASSWORD, and
|
|
41
|
+
* the app's signed-in users listed in DB_ADMIN_USERS with no password at all.
|
|
42
|
+
*
|
|
43
|
+
* Nothing is read or opened until the first request: Next imports a route when it builds, where
|
|
44
|
+
* the app's environment isn't there yet.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createDbRoute(options?: DbRouteOptions): DbRoute;
|
|
47
|
+
export {};
|