@wervt/platform 0.0.3 → 0.0.4
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 +10 -0
- package/dist/dev.js +29 -7
- package/dist/storage.d.ts +1 -0
- package/dist/storage.js +11 -0
- package/package.json +1 -1
package/dist/dev.d.ts
CHANGED
|
@@ -27,6 +27,11 @@ export interface DevOptions {
|
|
|
27
27
|
s3Url?: string;
|
|
28
28
|
garageAdminUrl?: string;
|
|
29
29
|
garageAdminToken?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Set up a database even without `drizzle/`, like a bundle with `database: true`: `wervt dev`
|
|
32
|
+
* passes on the app's setting. Adapters leave it out and wait for the first migration.
|
|
33
|
+
*/
|
|
34
|
+
database?: boolean;
|
|
30
35
|
/**
|
|
31
36
|
* Runs the app's `db:seed` task (`server/tasks/db/seed.ts`) once its database was just created
|
|
32
37
|
* and migrated. Defaults to true; `false` leaves seeding to `nuxt task run db:seed` (or nitro's).
|
|
@@ -45,6 +50,11 @@ export interface DevOptions {
|
|
|
45
50
|
role?: WervtRole;
|
|
46
51
|
};
|
|
47
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Drops the app's local schema and role; the next dev start creates them again. Refuses any
|
|
55
|
+
* other host: `WERVT_PG_ADMIN_URL` may well be set to a real server in someone's shell.
|
|
56
|
+
*/
|
|
57
|
+
export declare function resetDevDatabase(app: string, options?: DevOptions): Promise<string>;
|
|
48
58
|
/**
|
|
49
59
|
* The dev server against the local compose stack: provisions the app's role and schema,
|
|
50
60
|
* 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, 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",
|
|
@@ -36,7 +56,8 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
36
56
|
});
|
|
37
57
|
if (features.storage) await setupStorage(host, app, config.dbSecret, options);
|
|
38
58
|
const migrationsFolder = join(host.rootDir, "drizzle");
|
|
39
|
-
|
|
59
|
+
const hasMigrations = existsSync(migrationsFolder);
|
|
60
|
+
if (!hasMigrations) {
|
|
40
61
|
const watcher = watch(host.rootDir, (_event, file) => {
|
|
41
62
|
if (file !== "drizzle" || !existsSync(migrationsFolder)) return;
|
|
42
63
|
watcher.close();
|
|
@@ -44,13 +65,13 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
44
65
|
host.restart();
|
|
45
66
|
});
|
|
46
67
|
host.onClose(() => watcher.close());
|
|
47
|
-
return;
|
|
68
|
+
if (!options.database) return;
|
|
48
69
|
}
|
|
49
70
|
const provision = {
|
|
50
71
|
app,
|
|
51
72
|
adminUrl: config.adminUrl,
|
|
52
73
|
secret: config.dbSecret,
|
|
53
|
-
migrationsFolder
|
|
74
|
+
migrationsFolder: hasMigrations ? migrationsFolder : void 0
|
|
54
75
|
};
|
|
55
76
|
const given = process.env.DATABASE_URL && process.env.WERVT_DB_SCHEMA ? {
|
|
56
77
|
url: process.env.DATABASE_URL,
|
|
@@ -74,10 +95,10 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
74
95
|
});
|
|
75
96
|
const migrate = async () => {
|
|
76
97
|
try {
|
|
77
|
-
const { fresh } = given ? await migrateAs({
|
|
98
|
+
const { fresh } = given ? hasMigrations ? await migrateAs({
|
|
78
99
|
...given,
|
|
79
100
|
migrationsFolder
|
|
80
|
-
}) : await provisionDatabase(provision);
|
|
101
|
+
}) : { fresh: false } : await provisionDatabase(provision);
|
|
81
102
|
logger.success(`Database \`${database}\` is up to date`);
|
|
82
103
|
if (fresh && options.seed !== false) {
|
|
83
104
|
seedPending = true;
|
|
@@ -97,6 +118,7 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
97
118
|
const set = Object.fromEntries(Object.entries(own).filter(([key]) => process.env[key] === void 0));
|
|
98
119
|
Object.assign(process.env, set);
|
|
99
120
|
process.env[OWN_ENV] = JSON.stringify(set);
|
|
121
|
+
if (!hasMigrations) return;
|
|
100
122
|
let timer;
|
|
101
123
|
const watcher = watch(migrationsFolder, { recursive: true }, (_event, file) => {
|
|
102
124
|
if (!file?.endsWith(".sql")) return;
|
|
@@ -155,4 +177,4 @@ async function resolveAppName(rootDir, name) {
|
|
|
155
177
|
}
|
|
156
178
|
return name.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
157
179
|
}
|
|
158
|
-
export { SEED_TASK, resolveAppName, runDevSeed, setupDev, unscheduleSeed };
|
|
180
|
+
export { SEED_TASK, resetDevDatabase, resolveAppName, runDevSeed, setupDev, unscheduleSeed };
|
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