@wervt/platform 0.0.3 → 0.0.5
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/dist/dev.d.ts +14 -0
- package/dist/dev.js +35 -8
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/provision.d.ts +10 -2
- package/dist/provision.js +31 -4
- package/dist/storage.d.ts +1 -0
- package/dist/storage.js +11 -0
- package/package.json +2 -2
package/dist/dev.d.ts
CHANGED
|
@@ -27,6 +27,15 @@ export interface DevOptions {
|
|
|
27
27
|
s3Url?: string;
|
|
28
28
|
garageAdminUrl?: string;
|
|
29
29
|
garageAdminToken?: string;
|
|
30
|
+
/** The local imgproxy, for `imageUrl()`. */
|
|
31
|
+
imgproxyUrl?: string;
|
|
32
|
+
/** Garage as imgproxy reaches it: inside the compose network, not through localhost. */
|
|
33
|
+
imgproxyS3Url?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Set up a database even without `drizzle/`, like a bundle with `database: true`: `wervt dev`
|
|
36
|
+
* passes on the app's setting. Adapters leave it out and wait for the first migration.
|
|
37
|
+
*/
|
|
38
|
+
database?: boolean;
|
|
30
39
|
/**
|
|
31
40
|
* Runs the app's `db:seed` task (`server/tasks/db/seed.ts`) once its database was just created
|
|
32
41
|
* and migrated. Defaults to true; `false` leaves seeding to `nuxt task run db:seed` (or nitro's).
|
|
@@ -45,6 +54,11 @@ export interface DevOptions {
|
|
|
45
54
|
role?: WervtRole;
|
|
46
55
|
};
|
|
47
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Drops the app's local schema and role; the next dev start creates them again. Refuses any
|
|
59
|
+
* other host: `WERVT_PG_ADMIN_URL` may well be set to a real server in someone's shell.
|
|
60
|
+
*/
|
|
61
|
+
export declare function resetDevDatabase(app: string, options?: DevOptions): Promise<string>;
|
|
48
62
|
/**
|
|
49
63
|
* The dev server against the local compose stack: provisions the app's role and schema,
|
|
50
64
|
* runs its migrations (again whenever `drizzle/` changes), seeds a new database and
|
package/dist/dev.js
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import { appDatabaseUrl, dbRole, migrateAs, provisionDatabase, storageCredentials } from "./provision.js";
|
|
1
|
+
import { appDatabaseUrl, dbRole, dropDatabase, hmac, migrateAs, provisionDatabase, storageCredentials } from "./provision.js";
|
|
2
2
|
import { createStorage } from "./storage.js";
|
|
3
3
|
import { SEED_TASK, runDevSeed, unscheduleSeed } from "./seed.js";
|
|
4
4
|
import { existsSync, watch } from "node:fs";
|
|
5
5
|
import { readFile } from "node:fs/promises";
|
|
6
6
|
import { join } from "node:path";
|
|
7
7
|
// src/dev.ts
|
|
8
|
+
const LOCAL_HOSTS = /* @__PURE__ */ new Set([
|
|
9
|
+
"localhost",
|
|
10
|
+
"127.0.0.1",
|
|
11
|
+
"[::1]",
|
|
12
|
+
"host.docker.internal"
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* Drops the app's local schema and role; the next dev start creates them again. Refuses any
|
|
16
|
+
* other host: `WERVT_PG_ADMIN_URL` may well be set to a real server in someone's shell.
|
|
17
|
+
*/
|
|
18
|
+
async function resetDevDatabase(app, options = {}) {
|
|
19
|
+
const adminUrl = process.env.WERVT_PG_ADMIN_URL ?? options.adminUrl ?? DEFAULTS.adminUrl;
|
|
20
|
+
const { hostname } = new URL(adminUrl);
|
|
21
|
+
if (!LOCAL_HOSTS.has(hostname)) throw new Error(`Won't reset a database on ${hostname}, only on the local stack`);
|
|
22
|
+
await dropDatabase({
|
|
23
|
+
app,
|
|
24
|
+
adminUrl
|
|
25
|
+
});
|
|
26
|
+
return dbRole(app);
|
|
27
|
+
}
|
|
8
28
|
const DEFAULTS = {
|
|
9
29
|
adminUrl: "postgres://postgres:postgres@localhost:5432/wervt",
|
|
10
30
|
dbSecret: "dev-secret",
|
|
@@ -12,7 +32,9 @@ const DEFAULTS = {
|
|
|
12
32
|
electricSecret: "dev-electric-secret",
|
|
13
33
|
s3Url: "http://localhost:3900",
|
|
14
34
|
garageAdminUrl: "http://localhost:3903",
|
|
15
|
-
garageAdminToken: "dev-garage-admin"
|
|
35
|
+
garageAdminToken: "dev-garage-admin",
|
|
36
|
+
imgproxyUrl: "http://localhost:3910",
|
|
37
|
+
imgproxyS3Url: "http://garage:3900"
|
|
16
38
|
};
|
|
17
39
|
/**
|
|
18
40
|
* The dev server against the local compose stack: provisions the app's role and schema,
|
|
@@ -36,7 +58,8 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
36
58
|
});
|
|
37
59
|
if (features.storage) await setupStorage(host, app, config.dbSecret, options);
|
|
38
60
|
const migrationsFolder = join(host.rootDir, "drizzle");
|
|
39
|
-
|
|
61
|
+
const hasMigrations = existsSync(migrationsFolder);
|
|
62
|
+
if (!hasMigrations) {
|
|
40
63
|
const watcher = watch(host.rootDir, (_event, file) => {
|
|
41
64
|
if (file !== "drizzle" || !existsSync(migrationsFolder)) return;
|
|
42
65
|
watcher.close();
|
|
@@ -44,13 +67,13 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
44
67
|
host.restart();
|
|
45
68
|
});
|
|
46
69
|
host.onClose(() => watcher.close());
|
|
47
|
-
return;
|
|
70
|
+
if (!options.database) return;
|
|
48
71
|
}
|
|
49
72
|
const provision = {
|
|
50
73
|
app,
|
|
51
74
|
adminUrl: config.adminUrl,
|
|
52
75
|
secret: config.dbSecret,
|
|
53
|
-
migrationsFolder
|
|
76
|
+
migrationsFolder: hasMigrations ? migrationsFolder : void 0
|
|
54
77
|
};
|
|
55
78
|
const given = process.env.DATABASE_URL && process.env.WERVT_DB_SCHEMA ? {
|
|
56
79
|
url: process.env.DATABASE_URL,
|
|
@@ -74,10 +97,10 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
74
97
|
});
|
|
75
98
|
const migrate = async () => {
|
|
76
99
|
try {
|
|
77
|
-
const { fresh } = given ? await migrateAs({
|
|
100
|
+
const { fresh } = given ? hasMigrations ? await migrateAs({
|
|
78
101
|
...given,
|
|
79
102
|
migrationsFolder
|
|
80
|
-
}) : await provisionDatabase(provision);
|
|
103
|
+
}) : { fresh: false } : await provisionDatabase(provision);
|
|
81
104
|
logger.success(`Database \`${database}\` is up to date`);
|
|
82
105
|
if (fresh && options.seed !== false) {
|
|
83
106
|
seedPending = true;
|
|
@@ -97,6 +120,7 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
97
120
|
const set = Object.fromEntries(Object.entries(own).filter(([key]) => process.env[key] === void 0));
|
|
98
121
|
Object.assign(process.env, set);
|
|
99
122
|
process.env[OWN_ENV] = JSON.stringify(set);
|
|
123
|
+
if (!hasMigrations) return;
|
|
100
124
|
let timer;
|
|
101
125
|
const watcher = watch(migrationsFolder, { recursive: true }, (_event, file) => {
|
|
102
126
|
if (!file?.endsWith(".sql")) return;
|
|
@@ -141,6 +165,9 @@ async function setupStorage(host, app, secret, options) {
|
|
|
141
165
|
process.env.WERVT_S3_BUCKET ??= creds.bucket;
|
|
142
166
|
process.env.WERVT_S3_ACCESS_KEY_ID ??= creds.accessKeyId;
|
|
143
167
|
process.env.WERVT_S3_SECRET_ACCESS_KEY ??= creds.secretAccessKey;
|
|
168
|
+
process.env.WERVT_IMAGE_KEY ??= await hmac(secret, `image:${app}`);
|
|
169
|
+
process.env.WERVT_DEV_IMGPROXY_URL ??= options.imgproxyUrl ?? DEFAULTS.imgproxyUrl;
|
|
170
|
+
process.env.WERVT_DEV_IMGPROXY_S3_URL ??= options.imgproxyS3Url ?? DEFAULTS.imgproxyS3Url;
|
|
144
171
|
}
|
|
145
172
|
/**
|
|
146
173
|
* App name, normalised to `[a-z0-9-]+`: `WERVT_APP` first, which `wervt deploy` sets for its
|
|
@@ -155,4 +182,4 @@ async function resolveAppName(rootDir, name) {
|
|
|
155
182
|
}
|
|
156
183
|
return name.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
157
184
|
}
|
|
158
|
-
export { SEED_TASK, resolveAppName, runDevSeed, setupDev, unscheduleSeed };
|
|
185
|
+
export { SEED_TASK, resetDevDatabase, resolveAppName, runDevSeed, setupDev, unscheduleSeed };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DEFAULT_ROLE_TIMEOUTS, POOLER_ROLE, ProvisionOptions, RoleTimeouts, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
|
|
1
|
+
import { APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, POOLER_ROLE, ProvisionOptions, RoleTimeouts, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
|
|
2
2
|
import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, ObjectDetails, ObjectListing, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
|
|
3
|
-
export { ABANDONED_UPLOAD_GRACE_MS, DEFAULT_ROLE_TIMEOUTS, MAX_KEY_BYTES, ObjectDetails, ObjectListing, POOLER_ROLE, ProvisionOptions, RoleTimeouts, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
|
3
|
+
export { ABANDONED_UPLOAD_GRACE_MS, APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, MAX_KEY_BYTES, ObjectDetails, ObjectListing, POOLER_ROLE, ProvisionOptions, RoleTimeouts, StorageError, StorageOptions, StorageQuota, StorageUsage, StoredObject, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DEFAULT_ROLE_TIMEOUTS, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
|
|
1
|
+
import { APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials } from "./provision.js";
|
|
2
2
|
import { ABANDONED_UPLOAD_GRACE_MS, MAX_KEY_BYTES, StorageError, companionKeyOf, createStorage, metaKeyOf, parseObjectList } from "./storage.js";
|
|
3
|
-
export { ABANDONED_UPLOAD_GRACE_MS, DEFAULT_ROLE_TIMEOUTS, MAX_KEY_BYTES, POOLER_ROLE, StorageError, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
|
3
|
+
export { ABANDONED_UPLOAD_GRACE_MS, APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, MAX_KEY_BYTES, POOLER_ROLE, StorageError, appDatabaseUrl, companionKeyOf, createStorage, dbPassword, dbRole, dropDatabase, hmac, metaKeyOf, migrateAs, parseDuration, parseObjectList, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
package/dist/provision.d.ts
CHANGED
|
@@ -36,11 +36,19 @@ export declare function storageCredentials(app: string, secret: string): Promise
|
|
|
36
36
|
export declare function dbPassword(app: string, secret: string): Promise<string>;
|
|
37
37
|
/** Connection URL for the app's own role, on the same host and database as `adminUrl`. */
|
|
38
38
|
export declare function appDatabaseUrl({ app, adminUrl, secret }: ProvisionOptions): Promise<string>;
|
|
39
|
+
/** Where the extensions every app may use live; on each app role's search_path. */
|
|
40
|
+
export declare const EXTENSIONS_SCHEMA = "extensions";
|
|
41
|
+
/**
|
|
42
|
+
* Extensions apps may use. An extension exists once per database, in one schema, so apps can't
|
|
43
|
+
* create their own: they share these. `vector` (pgvector) isn't part of Postgres; it's there when
|
|
44
|
+
* the image brings it (pgvector/pgvector), and left out otherwise.
|
|
45
|
+
*/
|
|
46
|
+
export declare const APP_EXTENSIONS: readonly ['vector', 'pg_trgm', 'unaccent', 'citext'];
|
|
39
47
|
/**
|
|
40
48
|
* Creates (or updates) the app's role and schema and runs its Drizzle migrations as
|
|
41
49
|
* that role. Each app gets a role and schema named `app_<name>`; the role's
|
|
42
|
-
* search_path is its own schema, it can't create schemas or touch
|
|
43
|
-
* only connect to the wervt database.
|
|
50
|
+
* search_path is its own schema and the shared extensions, it can't create schemas or touch
|
|
51
|
+
* `public`, and it can only connect to the wervt database.
|
|
44
52
|
*
|
|
45
53
|
* `fresh` is true when no migration had been applied before, so `nuxt dev` knows when to seed.
|
|
46
54
|
*/
|
package/dist/provision.js
CHANGED
|
@@ -76,11 +76,36 @@ async function appDatabaseUrl({ app, adminUrl, secret }) {
|
|
|
76
76
|
url.password = await dbPassword(app, secret);
|
|
77
77
|
return url.href;
|
|
78
78
|
}
|
|
79
|
+
/** Where the extensions every app may use live; on each app role's search_path. */
|
|
80
|
+
const EXTENSIONS_SCHEMA = "extensions";
|
|
81
|
+
/**
|
|
82
|
+
* Extensions apps may use. An extension exists once per database, in one schema, so apps can't
|
|
83
|
+
* create their own: they share these. `vector` (pgvector) isn't part of Postgres; it's there when
|
|
84
|
+
* the image brings it (pgvector/pgvector), and left out otherwise.
|
|
85
|
+
*/
|
|
86
|
+
const APP_EXTENSIONS = [
|
|
87
|
+
"vector",
|
|
88
|
+
"pg_trgm",
|
|
89
|
+
"unaccent",
|
|
90
|
+
"citext"
|
|
91
|
+
];
|
|
92
|
+
/**
|
|
93
|
+
* Creates the shared extensions that this Postgres has, in their own schema. Their types and
|
|
94
|
+
* functions resolve unqualified for every app (`vector(512)`, `similarity()`), while apps can't
|
|
95
|
+
* add to the schema or change it. Safe to run on every deploy.
|
|
96
|
+
*/
|
|
97
|
+
async function provisionExtensions(admin) {
|
|
98
|
+
await admin.unsafe(`create schema if not exists "${EXTENSIONS_SCHEMA}"`);
|
|
99
|
+
const available = await admin`
|
|
100
|
+
select name from pg_available_extensions
|
|
101
|
+
where name in ${admin(APP_EXTENSIONS)} and installed_version is null`;
|
|
102
|
+
for (const { name } of available) await admin.unsafe(`create extension if not exists "${name}" schema "${EXTENSIONS_SCHEMA}"`);
|
|
103
|
+
}
|
|
79
104
|
/**
|
|
80
105
|
* Creates (or updates) the app's role and schema and runs its Drizzle migrations as
|
|
81
106
|
* that role. Each app gets a role and schema named `app_<name>`; the role's
|
|
82
|
-
* search_path is its own schema, it can't create schemas or touch
|
|
83
|
-
* only connect to the wervt database.
|
|
107
|
+
* search_path is its own schema and the shared extensions, it can't create schemas or touch
|
|
108
|
+
* `public`, and it can only connect to the wervt database.
|
|
84
109
|
*
|
|
85
110
|
* `fresh` is true when no migration had been applied before, so `nuxt dev` knows when to seed.
|
|
86
111
|
*/
|
|
@@ -99,7 +124,9 @@ async function provisionDatabase(options) {
|
|
|
99
124
|
await admin.unsafe(`${exists ? "alter" : "create"} role "${role}" login password '${password}' connection limit 10`);
|
|
100
125
|
await admin.unsafe(`create schema if not exists "${role}" authorization "${role}"`);
|
|
101
126
|
await claimSchema(admin, role);
|
|
102
|
-
await admin
|
|
127
|
+
await provisionExtensions(admin);
|
|
128
|
+
await admin.unsafe(`grant usage on schema "${EXTENSIONS_SCHEMA}" to "${role}"`);
|
|
129
|
+
await admin.unsafe(`alter role "${role}" set search_path = "${role}", "${EXTENSIONS_SCHEMA}"`);
|
|
103
130
|
await applyRoleTimeouts(admin, role, roleTimeouts(options.timeouts));
|
|
104
131
|
for (const db of [
|
|
105
132
|
database,
|
|
@@ -234,4 +261,4 @@ async function dropDatabase({ app, adminUrl }) {
|
|
|
234
261
|
await admin.end();
|
|
235
262
|
}
|
|
236
263
|
}
|
|
237
|
-
export { DEFAULT_ROLE_TIMEOUTS, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
|
264
|
+
export { APP_EXTENSIONS, DEFAULT_ROLE_TIMEOUTS, EXTENSIONS_SCHEMA, POOLER_ROLE, appDatabaseUrl, dbPassword, dbRole, dropDatabase, hmac, migrateAs, parseDuration, provisionDatabase, setRoleTimeouts, setupPooler, storageCredentials };
|
package/dist/storage.d.ts
CHANGED
|
@@ -84,6 +84,7 @@ export declare function createStorage(options: StorageOptions): {
|
|
|
84
84
|
publicUrl: string;
|
|
85
85
|
expiresIn?: number;
|
|
86
86
|
}) => Promise<string>;
|
|
87
|
+
put: (app: string, key: string, body: Uint8Array, contentType: string) => Promise<void>;
|
|
87
88
|
remove: (app: string, key: string) => Promise<{
|
|
88
89
|
deleted: string[];
|
|
89
90
|
} | undefined>;
|
package/dist/storage.js
CHANGED
|
@@ -345,6 +345,16 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
|
|
|
345
345
|
}
|
|
346
346
|
return { deleted: keys };
|
|
347
347
|
}
|
|
348
|
+
/** Stores `body` under `key`. Garage refuses it when the bucket's quota is used up. */
|
|
349
|
+
async function put(app, key, body, contentType) {
|
|
350
|
+
const { creds, client, base } = await s3(app);
|
|
351
|
+
const res = await client.fetch(objectUrl(base, key), {
|
|
352
|
+
method: "PUT",
|
|
353
|
+
headers: { "content-type": contentType },
|
|
354
|
+
body
|
|
355
|
+
});
|
|
356
|
+
if (!res.ok) throw new StorageError(`writing ${key} to ${creds.bucket}: ${await res.text()}`);
|
|
357
|
+
}
|
|
348
358
|
return {
|
|
349
359
|
ensureLayout,
|
|
350
360
|
provision,
|
|
@@ -353,6 +363,7 @@ ${[appOrigins].flat().map((origin) => `<AllowedOrigin>${origin}</AllowedOrigin>\
|
|
|
353
363
|
list,
|
|
354
364
|
describe,
|
|
355
365
|
downloadUrl,
|
|
366
|
+
put,
|
|
356
367
|
remove,
|
|
357
368
|
sweepAbandonedUploads,
|
|
358
369
|
hasBucket,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wervt/platform",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Provisioning for wervt apps: database roles, migrations, PgBouncer and object storage. Used by the control plane and local development.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"garage",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@wervt/core": "^0.0.
|
|
35
|
+
"@wervt/core": "^0.0.4",
|
|
36
36
|
"aws4fetch": "^1.0.20",
|
|
37
37
|
"postgres": "^3.4.9"
|
|
38
38
|
},
|