@wervt/platform 0.0.4 → 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 CHANGED
@@ -27,6 +27,10 @@ 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;
30
34
  /**
31
35
  * Set up a database even without `drizzle/`, like a bundle with `database: true`: `wervt dev`
32
36
  * passes on the app's setting. Adapters leave it out and wait for the first migration.
package/dist/dev.js CHANGED
@@ -1,4 +1,4 @@
1
- import { appDatabaseUrl, dbRole, dropDatabase, 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";
@@ -32,7 +32,9 @@ const DEFAULTS = {
32
32
  electricSecret: "dev-electric-secret",
33
33
  s3Url: "http://localhost:3900",
34
34
  garageAdminUrl: "http://localhost:3903",
35
- garageAdminToken: "dev-garage-admin"
35
+ garageAdminToken: "dev-garage-admin",
36
+ imgproxyUrl: "http://localhost:3910",
37
+ imgproxyS3Url: "http://garage:3900"
36
38
  };
37
39
  /**
38
40
  * The dev server against the local compose stack: provisions the app's role and schema,
@@ -163,6 +165,9 @@ async function setupStorage(host, app, secret, options) {
163
165
  process.env.WERVT_S3_BUCKET ??= creds.bucket;
164
166
  process.env.WERVT_S3_ACCESS_KEY_ID ??= creds.accessKeyId;
165
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;
166
171
  }
167
172
  /**
168
173
  * App name, normalised to `[a-z0-9-]+`: `WERVT_APP` first, which `wervt deploy` sets for its
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 };
@@ -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 `public`, and it can
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 `public`, and it can
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.unsafe(`alter role "${role}" set search_path = "${role}"`);
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wervt/platform",
3
- "version": "0.0.4",
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.3",
35
+ "@wervt/core": "^0.0.4",
36
36
  "aws4fetch": "^1.0.20",
37
37
  "postgres": "^3.4.9"
38
38
  },