@otwdesign/db-next 0.0.0-stage → 1.4.1
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 +88 -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 +195 -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 +18 -0
- package/dist/sign-in.js +88 -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 +259 -0
- package/src/session.ts +116 -0
- package/src/sign-in.ts +119 -0
- package/src/supabase.ts +133 -0
- package/src/users.ts +26 -0
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@otwdesign/db-next",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.4.1",
|
|
4
|
+
"description": "OTW DB in a Next.js app: one route file, a password sign-in or your app's own users, and the drivers Vercel needs. npx @otwdesign/db-next init sets it up.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "OTW SRL",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/OTWSRL/otw-db.git",
|
|
10
|
+
"directory": "packages/next"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/OTWSRL/otw-db/tree/main/packages/next#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/OTWSRL/otw-db/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"database",
|
|
18
|
+
"admin",
|
|
19
|
+
"nextjs",
|
|
20
|
+
"vercel",
|
|
21
|
+
"supabase",
|
|
22
|
+
"postgresql"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.18"
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"@otwdesign/source": "./src/index.ts",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./cli": {
|
|
38
|
+
"@otwdesign/source": "./src/cli.ts",
|
|
39
|
+
"types": "./dist/cli.d.ts",
|
|
40
|
+
"default": "./dist/cli.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"bin": {
|
|
44
|
+
"otw-db-next": "./bin/otw-db-next.js"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"src",
|
|
49
|
+
"bin",
|
|
50
|
+
"NOTICE"
|
|
51
|
+
],
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"pg": "^8.23.1",
|
|
54
|
+
"pg-cursor": "^2.22.1",
|
|
55
|
+
"@otwdesign/db-core": "1.4.1"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@supabase/ssr": "^0.12.7",
|
|
59
|
+
"@supabase/supabase-js": "^2.119.0",
|
|
60
|
+
"@types/pg": "^8.23.1",
|
|
61
|
+
"@types/pg-cursor": "^2.7.2"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsc -p tsconfig.build.json",
|
|
65
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
66
|
+
}
|
|
6
67
|
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util'
|
|
2
|
+
import { apply, detect, plan, report } from './init.ts'
|
|
3
|
+
|
|
4
|
+
const HELP = `otw-db-next: OTW DB in a Next.js app
|
|
5
|
+
|
|
6
|
+
Usage: npx @otwdesign/db-next init [--path /db] [--dry-run] [--no-install]
|
|
7
|
+
|
|
8
|
+
Run it in the app's directory. It writes one route file (app/db/[[...path]]/route.ts), installs
|
|
9
|
+
@otwdesign/db-next, and says which variables to set: the password is yours to choose, and it
|
|
10
|
+
writes none. It never writes over a file.
|
|
11
|
+
|
|
12
|
+
--path /admin/db where the tool goes; /db unless you say
|
|
13
|
+
--dry-run say what it would do, and do nothing
|
|
14
|
+
--no-install write the files, install nothing
|
|
15
|
+
`
|
|
16
|
+
|
|
17
|
+
/** What was typed, as options; null when it's none this command has. */
|
|
18
|
+
function options(argv: string[]) {
|
|
19
|
+
try {
|
|
20
|
+
return parseArgs({
|
|
21
|
+
args: argv,
|
|
22
|
+
allowPositionals: true,
|
|
23
|
+
options: {
|
|
24
|
+
path: { type: 'string' },
|
|
25
|
+
'dry-run': { type: 'boolean' },
|
|
26
|
+
'no-install': { type: 'boolean' },
|
|
27
|
+
help: { type: 'boolean', short: 'h' },
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
} catch {
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The `otw-db-next` command. */
|
|
36
|
+
export async function main(argv = process.argv.slice(2), cwd = process.cwd()): Promise<void> {
|
|
37
|
+
const given = options(argv)
|
|
38
|
+
if (!given) {
|
|
39
|
+
process.stdout.write(HELP)
|
|
40
|
+
process.exitCode = 1
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
const { values, positionals } = given
|
|
44
|
+
if (values.help || positionals[0] !== 'init' || positionals.length > 1) {
|
|
45
|
+
process.stdout.write(HELP)
|
|
46
|
+
if (!values.help) process.exitCode = 1
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
let detected: ReturnType<typeof detect>
|
|
50
|
+
let planned: ReturnType<typeof plan>
|
|
51
|
+
try {
|
|
52
|
+
detected = detect(cwd)
|
|
53
|
+
planned = plan(detected, {
|
|
54
|
+
...(values.path === undefined ? {} : { path: values.path }),
|
|
55
|
+
install: !values['no-install'],
|
|
56
|
+
})
|
|
57
|
+
} catch (error) {
|
|
58
|
+
// The code Node gives, and none of its own words: those carry paths as the folder has them.
|
|
59
|
+
const code = (error as NodeJS.ErrnoException).code ?? 'an error'
|
|
60
|
+
process.stderr.write(`Couldn’t read this folder (${code}). Nothing was written.\n`)
|
|
61
|
+
process.exitCode = 1
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
if (planned.refused) {
|
|
65
|
+
process.stderr.write(`${planned.refused}\n`)
|
|
66
|
+
process.exitCode = 1
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
let outcome: ReturnType<typeof apply> | undefined
|
|
70
|
+
try {
|
|
71
|
+
outcome = values['dry-run'] ? undefined : apply(detected, planned)
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// In a line: the code Node gives, and the path only as this command made it.
|
|
74
|
+
const failed = error as NodeJS.ErrnoException
|
|
75
|
+
const at = planned.files[0]?.path ?? 'the route file'
|
|
76
|
+
process.stderr.write(`Couldn’t write the route file (${failed.code ?? 'it failed'}): ${at}\n`)
|
|
77
|
+
process.exitCode = 1
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
process.stdout.write(report(detected, planned, values['dry-run'] === true, outcome))
|
|
81
|
+
if (outcome?.installFailed) process.exitCode = 1
|
|
82
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { connectionsFromEnv, driverKind, type ConnectionConfig } from '@otwdesign/db-core'
|
|
2
|
+
|
|
3
|
+
type Env = Readonly<Record<string, string | undefined>>
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Where an app's own PostgreSQL is, by the names hosts and their database integrations give it,
|
|
7
|
+
* the ones that don't go through a transaction pooler first: Neon's DATABASE_URL_UNPOOLED,
|
|
8
|
+
* Vercel Postgres's and Supabase's POSTGRES_URL_NON_POOLING. The tool reads through cursors and
|
|
9
|
+
* sets things for a session, which a pooler that hands each statement to another connection
|
|
10
|
+
* doesn't keep.
|
|
11
|
+
*/
|
|
12
|
+
export const OWN_DATABASE = [
|
|
13
|
+
'DATABASE_URL_UNPOOLED',
|
|
14
|
+
'POSTGRES_URL_NON_POOLING',
|
|
15
|
+
'DATABASE_URL',
|
|
16
|
+
'POSTGRES_URL',
|
|
17
|
+
] as const
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The connections of an app: those it names (`DB_ADMIN_<NAME>_URL`), or, when it names none, its
|
|
21
|
+
* own database, read-only unless `DB_ADMIN_APP_ACCESS=write`. An address the tool can't open
|
|
22
|
+
* (Prisma's `prisma://`, MongoDB's) is passed over, not refused: it's the app's, set for the app.
|
|
23
|
+
*/
|
|
24
|
+
export function appConnections(env: Env = process.env): ConnectionConfig[] {
|
|
25
|
+
const named = connectionsFromEnv(env)
|
|
26
|
+
if (named.length) return named
|
|
27
|
+
for (const variable of OWN_DATABASE) {
|
|
28
|
+
const url = env[variable]?.trim()
|
|
29
|
+
if (!url) continue
|
|
30
|
+
try {
|
|
31
|
+
driverKind(url)
|
|
32
|
+
} catch {
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
return [
|
|
36
|
+
{
|
|
37
|
+
name: 'app',
|
|
38
|
+
label: 'This app',
|
|
39
|
+
url,
|
|
40
|
+
access: env.DB_ADMIN_APP_ACCESS?.trim() === 'write' ? 'write' : 'read',
|
|
41
|
+
},
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
return []
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { appConnections, OWN_DATABASE } from './connections.ts'
|
|
2
|
+
export { createDbRoute, type DbRoute, type DbRouteOptions } from './route.ts'
|
|
3
|
+
export { MIN_PASSWORD, SESSION_COOKIE } from './session.ts'
|
|
4
|
+
export { supabaseUser, type SignedIn, type SupabaseUser } from './supabase.ts'
|
|
5
|
+
export { listedUsers } from './users.ts'
|