@cubicecho/server-kit 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +43 -2
- package/_agents/docs/01-database.md +31 -0
- package/_agents/docs/02-columns.md +25 -0
- package/_agents/docs/03-testing.md +18 -0
- package/_agents/docs/04-auth.md +41 -0
- package/_agents/docs/common-mistakes.md +23 -0
- package/dist/auth/defaults.d.ts +20 -0
- package/dist/auth/defaults.js +12 -0
- package/dist/auth/index.d.ts +3 -0
- package/dist/auth/index.js +3 -0
- package/dist/auth/plugins.d.ts +1270 -0
- package/dist/auth/plugins.js +39 -0
- package/dist/auth/preset.d.ts +53 -0
- package/dist/auth/preset.js +21 -0
- package/dist/columns/index.d.ts +38 -0
- package/dist/columns/index.js +53 -0
- package/dist/consts.d.ts +4 -0
- package/dist/consts.js +4 -0
- package/dist/db/connection.d.ts +18 -0
- package/dist/db/connection.js +15 -0
- package/dist/db/create.d.ts +40 -0
- package/dist/db/create.js +91 -0
- package/dist/db/defaults.d.ts +13 -0
- package/dist/db/defaults.js +8 -0
- package/dist/db/errors.d.ts +11 -0
- package/dist/db/errors.js +19 -0
- package/dist/db/index.d.ts +9 -0
- package/dist/db/index.js +8 -0
- package/dist/db/prepare.d.ts +32 -0
- package/dist/db/prepare.js +75 -0
- package/dist/db/ssl.d.ts +21 -0
- package/dist/db/ssl.js +91 -0
- package/dist/db/version.d.ts +22 -0
- package/dist/db/version.js +57 -0
- package/dist/db/wait.d.ts +23 -0
- package/dist/db/wait.js +52 -0
- package/dist/testing/index.d.ts +17 -0
- package/dist/testing/index.js +51 -0
- package/dist/testing/postgres-problems.d.ts +38 -0
- package/dist/testing/postgres-problems.js +57 -0
- package/dist/testing/schema-problems.d.ts +18 -0
- package/dist/testing/schema-problems.js +153 -0
- package/llms.txt +68 -0
- package/package.json +99 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benjamin Van Treese
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- AI agents: read llms.txt and _agents/docs/ for the full, version-matched API. -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @cubicecho/server-kit
|
|
4
|
+
|
|
5
|
+
The server code every cubicecho GraphQL backend shares. Each server used to copy it, and the copies drifted: a fix reached one repo and not the next. It is functions an app calls, not a framework: the app keeps its own schema, auth setup and boot order.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i @cubicecho/server-kit drizzle-orm@1.0.0-rc.5 postgres
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { connectionOptions, DatabaseBootError, prepareDatabase } from '@cubicecho/server-kit/db';
|
|
17
|
+
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
18
|
+
|
|
19
|
+
const url = process.env.DATABASE_URL ?? '';
|
|
20
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
21
|
+
const db = drizzle({ connection: { url, ...connectionOptions(url, isProduction) } });
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
await prepareDatabase(db, { url, isProduction });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (error instanceof DatabaseBootError) {
|
|
27
|
+
console.error(`[db] ${error.message}`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
It waits for Postgres, creates the database when it is missing, and refuses a server older than 18, with a message that says what to do.
|
|
35
|
+
|
|
36
|
+
## Documentation
|
|
37
|
+
|
|
38
|
+
- [llms.txt](https://cdn.jsdelivr.net/npm/@cubicecho/server-kit/llms.txt)
|
|
39
|
+
- [Pages](https://github.com/cubicecho/server-kit/tree/main/_agents/docs)
|
|
40
|
+
- [Changelog](https://github.com/cubicecho/server-kit/blob/main/CHANGELOG.md)
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Database boot
|
|
2
|
+
|
|
3
|
+
`@cubicecho/server-kit/db` makes a Postgres database ready to migrate: it waits for the server, creates the database when it is missing, and refuses a server the app cannot run on.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { connectionOptions, DatabaseBootError, prepareDatabase } from '@cubicecho/server-kit/db';
|
|
7
|
+
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
8
|
+
import { migrate } from 'drizzle-orm/postgres-js/migrator';
|
|
9
|
+
|
|
10
|
+
const url = process.env.DATABASE_URL ?? '';
|
|
11
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
12
|
+
const db = drizzle({ connection: { url, ...connectionOptions(url, isProduction) } });
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await prepareDatabase(db, { url, isProduction, extensions: ['vector'] });
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (error instanceof DatabaseBootError) {
|
|
18
|
+
console.error(`[db] ${error.message}`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
await migrate(db, { migrationsFolder: 'drizzle' });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- Call it before `migrate`. It checks the version and the extensions before it creates or changes anything.
|
|
27
|
+
- `extensions` names what the migrations create. It asks what the server can create, so the migration still creates it.
|
|
28
|
+
- A server older than `MIN_POSTGRES_MAJOR` is refused. One newer than `testedMajor` gets a warning through `log`.
|
|
29
|
+
- `connectTimeoutMs`, `firstRetryDelayMs`, `maxRetryDelayMs` and `retryBackoffFactor` override `DATABASE_DEFAULTS`.
|
|
30
|
+
- Any other error is the driver's own, such as a wrong password. Let it surface.
|
|
31
|
+
- `requiresSsl(url)` is false for a private host and for a URL with its own `sslmode`.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Columns
|
|
2
|
+
|
|
3
|
+
`@cubicecho/server-kit/columns` has the Drizzle columns that keep a table to the conventions: a UUIDv7 id, moments with a time zone, and a table limited to one row.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { id, moment, singleton, singletonRules, timestamps } from '@cubicecho/server-kit/columns';
|
|
7
|
+
import { integer, pgTable, text } from 'drizzle-orm/pg-core';
|
|
8
|
+
|
|
9
|
+
export const todos = pgTable('todos', {
|
|
10
|
+
id: id(),
|
|
11
|
+
title: text('title').notNull(),
|
|
12
|
+
completedAt: moment('completed_at'),
|
|
13
|
+
...timestamps(),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const instanceSettings = pgTable(
|
|
17
|
+
'instance_settings',
|
|
18
|
+
{ id: id(), singleton: singleton(), runLimit: integer('run_limit').notNull().default(2) },
|
|
19
|
+
(table) => singletonRules('instance_settings', table.singleton),
|
|
20
|
+
);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `id()` defaults to the database's `uuidv7()`, which needs Postgres 18 or PGlite 0.5.
|
|
24
|
+
- An upsert of a singleton targets `table.singleton`, never `table.id`.
|
|
25
|
+
- A join table has no `id()`. Its primary key is its foreign keys.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Test checks
|
|
2
|
+
|
|
3
|
+
`@cubicecho/server-kit/testing` registers Vitest tests that hold an app to the backend standard: its tables, and the Postgres it runs.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { resolve } from 'node:path';
|
|
7
|
+
import { checkPostgresVersion, checkSchemaConventions } from '@cubicecho/server-kit/testing';
|
|
8
|
+
import * as dbSchema from '../../../../db/src/schema.ts';
|
|
9
|
+
|
|
10
|
+
checkSchemaConventions(dbSchema, {
|
|
11
|
+
derived: { change_log: 'Triggers write it from the other tables.' },
|
|
12
|
+
});
|
|
13
|
+
checkPostgresVersion({ root: resolve(import.meta.dirname, '../../../..') });
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
- `checkSchemaConventions` checks ids, join tables, time zones and foreign-key indexes, one test per table. `derived` and `legacyIds` excuse a table from the id rule, by SQL name with the reason, and an exception nothing needs fails.
|
|
17
|
+
- `checkPostgresVersion` fails unless both compose files and CI run one major, 18 or later, and PGlite is not behind it. PGlite ahead is a warning.
|
|
18
|
+
- `schemaProblems` and `postgresProblems` return the findings for another runner.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Auth preset
|
|
2
|
+
|
|
3
|
+
`@cubicecho/server-kit/auth` has the better-auth options and sign-in plugins every server shares: sessions in Postgres, bearer tokens, emailed links, and OIDC with pairing. The app still calls `betterAuth`, so it adds its own plugins and hooks and keeps exact types.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { apiKey } from '@better-auth/api-key';
|
|
7
|
+
import { authPreset, signInPlugins } from '@cubicecho/server-kit/auth';
|
|
8
|
+
import { betterAuth } from 'better-auth';
|
|
9
|
+
|
|
10
|
+
const auth = betterAuth({
|
|
11
|
+
...authPreset({
|
|
12
|
+
db,
|
|
13
|
+
tables: {
|
|
14
|
+
user: users,
|
|
15
|
+
session: sessions,
|
|
16
|
+
account: accounts,
|
|
17
|
+
verification: verifications,
|
|
18
|
+
apikey: apikeys,
|
|
19
|
+
ssoProvider: ssoProviders,
|
|
20
|
+
},
|
|
21
|
+
secret,
|
|
22
|
+
serverUrl,
|
|
23
|
+
appUrl,
|
|
24
|
+
}),
|
|
25
|
+
databaseHooks: { user: { create: { after: (user) => claimFirstAdmin(db, user.id) } } },
|
|
26
|
+
plugins: [
|
|
27
|
+
apiKey({ defaultPrefix: 'todo_', requireName: true, rateLimit: { enabled: false } }),
|
|
28
|
+
...signInPlugins({
|
|
29
|
+
appUrl,
|
|
30
|
+
deliver: sendMagicLinkEmail,
|
|
31
|
+
oidc: { clientName: 'Todo', canPair: ({ userId }) => mayPair(userId) },
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- Set every `id` column with `id()`: the preset leaves ids to the database.
|
|
38
|
+
- `deliver: undefined` is an instance that cannot email. `sendsMagicLinks(auth)` says which it is.
|
|
39
|
+
- `paths` moves the app's `login`, `verify` and `signedIn` pages.
|
|
40
|
+
- An option after the spread replaces the preset's whole value, so `session: { … }` has to repeat `expiresIn`.
|
|
41
|
+
- `@better-auth/sso` and `better-auth` are peers: install both in the server.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Common mistakes
|
|
2
|
+
|
|
3
|
+
What goes wrong when the kit is wired up slightly differently, and the fix for each.
|
|
4
|
+
|
|
5
|
+
## A second copy of drizzle-orm
|
|
6
|
+
|
|
7
|
+
`drizzle-orm` is a peer at one exact version. Pin the same version in the root `overrides` and as a root dependency, so npm installs one copy.
|
|
8
|
+
|
|
9
|
+
## Migrating before `prepareDatabase`
|
|
10
|
+
|
|
11
|
+
On a server that is too old the migrations stop partway. Call `prepareDatabase` first: it changes nothing on a server it refuses.
|
|
12
|
+
|
|
13
|
+
## Upserting a singleton on its id
|
|
14
|
+
|
|
15
|
+
Each insert has a new id, so it never conflicts on it, and the unique index refuses the row. Target the column that does conflict.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
.onConflictDoUpdate({ target: instanceSettings.singleton, set: { runLimit: 5 } })
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Importing the testing entry in a server
|
|
22
|
+
|
|
23
|
+
`@cubicecho/server-kit/testing` imports Vitest and PGlite, which are dev dependencies. Import it only from tests.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Session and link lifetimes. */
|
|
2
|
+
export interface AuthSettings {
|
|
3
|
+
/** How long a session lasts after it was last used. Long: there is no password to fall back on. */
|
|
4
|
+
sessionTtlDays: number;
|
|
5
|
+
/** How long an emailed sign-in link works. Short: it travels by mail. */
|
|
6
|
+
magicLinkTtlMinutes: number;
|
|
7
|
+
}
|
|
8
|
+
/** What `authPreset` and `signInPlugins` use when the app overrides nothing. */
|
|
9
|
+
export declare const AUTH_DEFAULTS: Readonly<AuthSettings>;
|
|
10
|
+
/** The app's pages a sign-in passes through, as paths under `appUrl`. */
|
|
11
|
+
export interface AuthPaths {
|
|
12
|
+
/** The sign-in page, where a failed sign-in goes. */
|
|
13
|
+
login: string;
|
|
14
|
+
/** Spends an emailed link's token. */
|
|
15
|
+
verify: string;
|
|
16
|
+
/** Spends the one-time token an OIDC sign-in ends in. */
|
|
17
|
+
signedIn: string;
|
|
18
|
+
}
|
|
19
|
+
/** Where those pages are when the app overrides nothing. */
|
|
20
|
+
export declare const AUTH_PATHS: Readonly<AuthPaths>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Every value someone might tune, as plain data. Nothing here computes, reads the environment or imports.
|
|
2
|
+
/** What `authPreset` and `signInPlugins` use when the app overrides nothing. */
|
|
3
|
+
export const AUTH_DEFAULTS = Object.freeze({
|
|
4
|
+
sessionTtlDays: 30,
|
|
5
|
+
magicLinkTtlMinutes: 15,
|
|
6
|
+
});
|
|
7
|
+
/** Where those pages are when the app overrides nothing. */
|
|
8
|
+
export const AUTH_PATHS = Object.freeze({
|
|
9
|
+
login: '/login',
|
|
10
|
+
verify: '/auth/verify',
|
|
11
|
+
signedIn: '/auth/sso',
|
|
12
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { AUTH_DEFAULTS, AUTH_PATHS, type AuthPaths, type AuthSettings } from './defaults.ts';
|
|
2
|
+
export { type MagicLink, type SignInOptions, sendsMagicLinks, signInPlugins } from './plugins.ts';
|
|
3
|
+
export { type AuthPresetOptions, type AuthTables, authPreset } from './preset.ts';
|