notifkit 0.1.11 → 0.1.12
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/README.md +37 -1
- package/dist/{bootstrap-GRXzgPvW.mjs → bootstrap-omGPIBny.mjs} +1 -1
- package/dist/{bulk-BvRfz6Of.mjs → bulk-C4hTcnLu.mjs} +1 -1
- package/dist/index.d.mts +60 -3
- package/dist/index.mjs +2 -2
- package/dist/{main-BENOY7HG.mjs → main-B-AZ-xoN.mjs} +2 -2
- package/dist/{main-D84OX0ux.mjs → main-B5BwjIMu.mjs} +3 -3
- package/dist/{main-HY_TDs61.mjs → main-BU27VjDC.mjs} +2 -2
- package/dist/{main-D6od9N8h.mjs → main-Bes11Ad3.mjs} +2 -2
- package/dist/{main-BpnuFX62.mjs → main-BtnGmyaT.mjs} +2 -2
- package/dist/{main-HH-WwsMM.mjs → main-BxAeC_k6.mjs} +1 -1
- package/dist/{main-BqVH2BHj.mjs → main-DAXO-MiH.mjs} +3 -3
- package/dist/{main-C_5VFIwp.mjs → main-DYGCl7Ja.mjs} +2 -2
- package/dist/{src-CI7FGf3h.mjs → src-CIPTzq9i.mjs} +149 -28
- package/package.json +5 -2
- package/scripts/create-admin.mjs +8 -1
- package/scripts/migrate.mjs +43 -0
- package/src/config/index.ts +11 -0
- package/src/db/index.ts +172 -5
- package/src/server.ts +3 -0
package/README.md
CHANGED
|
@@ -215,7 +215,41 @@ curl -X POST http://localhost:3000/v1/notify \
|
|
|
215
215
|
|
|
216
216
|
### Development vs. production
|
|
217
217
|
|
|
218
|
-
Locally, Docker is the only prerequisite: in development notifkit starts throwaway PostgreSQL and Redis containers for you. In production you need Node 22+, PostgreSQL, and Redis, and you run migrations
|
|
218
|
+
Locally, Docker is the only prerequisite: in development notifkit starts throwaway PostgreSQL and Redis containers for you. In production you need Node 22+, PostgreSQL, and Redis, and you run migrations with the bundled CLI before starting the server:
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
DATABASE_URL=postgres://user:pass@host:5432/db npx notifkit-migrate
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
`notifkit-migrate` reads `DATABASE_URL` and `DB_SCHEMA` (from the environment or `.env`), applies any pending migrations into that schema, and exits non-zero if anything fails, so it can gate a deploy. It is safe to run on every deploy. Do not point `drizzle-kit` at `node_modules/notifkit/drizzle` directly: that ignores `DB_SCHEMA` and always creates the tables in `public`. In the default `public` schema both use the same journal (`drizzle.__drizzle_migrations`), so an install migrated with `drizzle-kit` so far can switch to `notifkit-migrate` as is.
|
|
225
|
+
|
|
226
|
+
### Installing into a custom PostgreSQL schema
|
|
227
|
+
|
|
228
|
+
By default notifkit creates its tables and enum types in `public`. To share a database with an application that already owns `public`, set `DB_SCHEMA` (or `dbOptions.schema` on `NotifkitServer`):
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
DB_SCHEMA=notifications
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Migrations (`notifkit-migrate`, or `autoMigrate` on start-up) then create the schema if it is missing and put every notifkit object into it, and every connection notifkit opens resolves queries against that schema only. After migrating, notifkit checks that every one of its tables exists in that schema and fails with the names of any that do not. The migration journal is kept in the same schema (`notifications.__drizzle_migrations`), so two installs in one database never share it. Pass `runMigrations(db, { schema, migrationsSchema, migrationsTable })` to run migrations yourself.
|
|
235
|
+
|
|
236
|
+
The schema is set as the `search_path` startup parameter. If a connection pooler in front of PostgreSQL rejects that parameter (PgBouncer does unless `search_path` is listed in `track_extra_parameters` or `ignore_startup_parameters`), set it on the role instead: `ALTER ROLE <role> SET search_path = notifications`.
|
|
237
|
+
|
|
238
|
+
> [!WARNING]
|
|
239
|
+
> **Do not set `DB_SCHEMA` on an install that already runs in `public`.** Nothing is moved for you: notifkit creates a fresh, empty set of tables in the new schema and stops looking at `public`. The application then looks empty: admin logins and every project API key stop working, and users, templates and history are gone from its point of view (the data is still in `public`, just no longer read).
|
|
240
|
+
>
|
|
241
|
+
> To move an existing install, stop notifkit and move **every** notifkit table and enum type, plus the migration journal, into the new schema yourself **before** running `notifkit-migrate` with the new `DB_SCHEMA`, for example:
|
|
242
|
+
>
|
|
243
|
+
> ```sql
|
|
244
|
+
> BEGIN;
|
|
245
|
+
> CREATE SCHEMA notifications;
|
|
246
|
+
> ALTER TABLE public.projects SET SCHEMA notifications; -- repeat for every table in notifkitTableNames()
|
|
247
|
+
> ALTER TYPE public.channel SET SCHEMA notifications; -- and for api_key_role, workflow_status
|
|
248
|
+
> ALTER TABLE drizzle.__drizzle_migrations SET SCHEMA notifications;
|
|
249
|
+
> COMMIT;
|
|
250
|
+
> ```
|
|
251
|
+
>
|
|
252
|
+
> Then `notifkit-migrate` finds the journal, applies nothing it has already applied, and confirms that every table is in place.
|
|
219
253
|
|
|
220
254
|
## Running in production
|
|
221
255
|
|
|
@@ -348,6 +382,8 @@ You can also create or update admin users at any time via the CLI:
|
|
|
348
382
|
npx notifkit-create-admin admin@example.com supersecretpassword123
|
|
349
383
|
```
|
|
350
384
|
|
|
385
|
+
Like `notifkit-migrate`, it reads `DATABASE_URL` and `DB_SCHEMA`.
|
|
386
|
+
|
|
351
387
|
In production, the pre-built dashboard bundle in `dashboard/dist` is served automatically. In development, run `npm --prefix dashboard run dev` to start the Vite development server with hot module replacement (the API server automatically proxies `/admin` requests to port 5173).
|
|
352
388
|
|
|
353
389
|
## Documentation
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { G as StreamConsumer, K as StreamProducer, R as RedisClient, W as PendingMessageScanner, Y as createLogger
|
|
1
|
+
import { $t as STREAMS, G as StreamConsumer, K as StreamProducer, Q as createDatabase, R as RedisClient, W as PendingMessageScanner, Y as createLogger } from "./src-CIPTzq9i.mjs";
|
|
2
2
|
//#region src/workers/bootstrap.ts
|
|
3
3
|
/** Logger, shared Redis connection and Postgres pool for one service. */
|
|
4
4
|
function createWorkerRuntime(config, name, loggerName = name) {
|
package/dist/index.d.mts
CHANGED
|
@@ -38,6 +38,7 @@ declare const baseConfigSchema: z.ZodObject<{
|
|
|
38
38
|
QUEUE_MAX_LEN: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
39
39
|
IDEMPOTENCY_TTL_SECONDS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
40
40
|
DB_MAX_CONNECTIONS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
41
|
+
DB_SCHEMA: z.ZodDefault<z.ZodString>;
|
|
41
42
|
LOG_FLUSH_INTERVAL_MS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
42
43
|
LOG_BUFFER_MAX_SIZE: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
43
44
|
SEGMENT_MAX_USERS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
@@ -3888,6 +3889,11 @@ type Sql = postgres.Sql;
|
|
|
3888
3889
|
type Db = PostgresJsDatabase<typeof schema_d_exports>;
|
|
3889
3890
|
interface DatabaseOptions {
|
|
3890
3891
|
url: string;
|
|
3892
|
+
/**
|
|
3893
|
+
* Schema holding notifkit's tables. Defaults to `DB_SCHEMA` (`public`).
|
|
3894
|
+
* Anything else is set as the connection's `search_path`.
|
|
3895
|
+
*/
|
|
3896
|
+
schema?: string;
|
|
3891
3897
|
applicationName?: string;
|
|
3892
3898
|
maxConnections?: number;
|
|
3893
3899
|
idleTimeoutSeconds?: number;
|
|
@@ -3902,8 +3908,57 @@ interface DatabaseClients {
|
|
|
3902
3908
|
* Call once at application startup; pass db into repositories.
|
|
3903
3909
|
* Call sql.end() during graceful shutdown.
|
|
3904
3910
|
*/
|
|
3905
|
-
declare function createDatabase({ url, applicationName, maxConnections, idleTimeoutSeconds, logger }: DatabaseOptions): DatabaseClients;
|
|
3906
|
-
|
|
3911
|
+
declare function createDatabase({ url, schema: dbSchema, applicationName, maxConnections, idleTimeoutSeconds, logger }: DatabaseOptions): DatabaseClients;
|
|
3912
|
+
interface MigrationOptions {
|
|
3913
|
+
/** Schema to create notifkit's objects in. Defaults to `DB_SCHEMA` (`public`). */
|
|
3914
|
+
schema?: string;
|
|
3915
|
+
/**
|
|
3916
|
+
* Schema of drizzle's migration journal table. Defaults to drizzle's own
|
|
3917
|
+
* `drizzle` for `public`, and to `schema` otherwise, so two installs in
|
|
3918
|
+
* different schemas of one database never share a journal.
|
|
3919
|
+
*/
|
|
3920
|
+
migrationsSchema?: string;
|
|
3921
|
+
/** Name of drizzle's migration journal table. Defaults to `__drizzle_migrations`. */
|
|
3922
|
+
migrationsTable?: string;
|
|
3923
|
+
}
|
|
3924
|
+
/**
|
|
3925
|
+
* Retarget one generated migration at `schema`. drizzle-kit qualifies enum
|
|
3926
|
+
* types and foreign-key targets with `"public".` but leaves tables and indexes
|
|
3927
|
+
* unqualified, so both halves have to move: the qualifier is rewritten, and
|
|
3928
|
+
* `SET LOCAL search_path` places the unqualified objects. `SET LOCAL` ends with
|
|
3929
|
+
* the migration transaction and never leaks into a pooled connection.
|
|
3930
|
+
*
|
|
3931
|
+
* Throws if `public` is still mentioned after the rewrite: a migration that
|
|
3932
|
+
* spells the schema some other way would otherwise put objects into `public`
|
|
3933
|
+
* without a word.
|
|
3934
|
+
*/
|
|
3935
|
+
declare function qualifyMigrationSql(source: string, schema: string): string;
|
|
3936
|
+
declare function runMigrations(db: Db, options?: MigrationOptions): Promise<void>;
|
|
3937
|
+
/**
|
|
3938
|
+
* Names of every table notifkit's Drizzle schema defines, sorted. This is the
|
|
3939
|
+
* same object the ORM queries at run time, so the list cannot drift from the
|
|
3940
|
+
* tables notifkit actually uses.
|
|
3941
|
+
*/
|
|
3942
|
+
declare function notifkitTableNames(): string[];
|
|
3943
|
+
/**
|
|
3944
|
+
* Fail if any notifkit table is missing from `targetSchema`. Run after
|
|
3945
|
+
* migrating into a custom schema: if the retargeting ever stops working (the
|
|
3946
|
+
* `search_path` is overridden, a migration is skipped because of a shared
|
|
3947
|
+
* journal, ...), the tables land elsewhere or nowhere, and every runtime query
|
|
3948
|
+
* would fail with "relation does not exist". Only the target schema is checked:
|
|
3949
|
+
* the application sharing the database may well own `users` or `projects` in
|
|
3950
|
+
* `public`, and that is not an error.
|
|
3951
|
+
*/
|
|
3952
|
+
declare function assertTablesInSchema(db: Db, targetSchema: string): Promise<void>;
|
|
3953
|
+
/**
|
|
3954
|
+
* Apply the bundled migrations to the database at `url` and close the
|
|
3955
|
+
* connection, whether or not they succeed. `schema` defaults to `DB_SCHEMA`.
|
|
3956
|
+
* This is what `notifkit-migrate` runs.
|
|
3957
|
+
*/
|
|
3958
|
+
declare function migrateDatabase({ url, schema: dbSchema }: {
|
|
3959
|
+
url: string;
|
|
3960
|
+
schema?: string;
|
|
3961
|
+
}): Promise<string>;
|
|
3907
3962
|
//#endregion
|
|
3908
3963
|
//#region src/idempotency/index.d.ts
|
|
3909
3964
|
interface IdempotencyOptions {
|
|
@@ -5163,6 +5218,8 @@ interface NotifkitOptions {
|
|
|
5163
5218
|
};
|
|
5164
5219
|
dbOptions?: {
|
|
5165
5220
|
maxConnections?: number;
|
|
5221
|
+
/** PostgreSQL schema for notifkit's tables. Same as `DB_SCHEMA`; defaults to `public`. */
|
|
5222
|
+
schema?: string;
|
|
5166
5223
|
};
|
|
5167
5224
|
}
|
|
5168
5225
|
declare class NotifkitServer extends EventEmitter {
|
|
@@ -5179,4 +5236,4 @@ declare class NotifkitServer extends EventEmitter {
|
|
|
5179
5236
|
stop(): Promise<void>;
|
|
5180
5237
|
}
|
|
5181
5238
|
//#endregion
|
|
5182
|
-
export { AI_DEFAULTS, AddContactInput, AddContactSchema, AddUserInput, AddUserSchema, AdminUserRecord, AdminUserRepository, AiConfig, ApiRateLimiter, ApiRateLimiterOptions, AppError, AsyncSemaphore, BaseConfig, BaseWorker, BatchAddContactsInput, BatchAddContactsSchema, BatchItem, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, CircuitBreakerOptions, ConsumerGroup, ContactChannel, ContactChannelSchema, ContactRepository, CreateWorkflowInput, CreateWorkflowSchema, CreatedProjectKeyRecord, DLQMessageRecord, DataLoader, DataLoaderOptions, DatabaseClients, DatabaseOptions, Db, DeliveryOptions, DeliveryOptionsSchema, DeliveryResult, DevicePlatform, DeviceToken, DispatchedTaskPayload, ENRICHED_STREAMS, EscapeMode, EventEnvelope, EventEnvelopeSchema, EventMetadata, EventMetadataSchema, EventPayloadMap, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IdempotencyOptions, IngestEventInput, IngestEventSchema, InlineUser, InlineUserSchema, KnownEventType, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LeaseResult, LockHeldError, LogLevel, Logger, LoggerOptions, NonRetryableError, NotificationAiPendingPayload, NotificationAiPendingPayloadSchema, NotificationCanceledPayload, NotificationCanceledPayloadSchema, NotificationChannel, NotificationChannelSchema, NotificationCreatedPayload, NotificationCreatedPayloadSchema, NotificationDeliveredPayload, NotificationDeliveredPayloadSchema, NotificationDispatchedPayload, NotificationDispatchedPayloadSchema, NotificationEnrichedPayload, NotificationEnrichedPayloadSchema, NotificationFailedPayload, NotificationFailedPayloadSchema, NotificationLogRecord, NotificationPreference, NotificationPriority, NotificationPrioritySchema, NotificationRequestedPayload, NotificationRequestedPayloadSchema, NotificationScheduledPayload, NotificationScheduledPayloadSchema, NotificationSkippedPayload, NotificationSkippedPayloadSchema, NotificationStatus, NotificationStatusSchema, NotificationTarget, NotificationTargetSchema, NotifkitClient, NotifkitClientOptions, NotifkitOptions, NotifkitServer, NotifyRequestInput, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, ParseResult, PendingEntry, PendingMessageScanner, PendingMessageScannerOptions, PreferenceRepository, Preferences, PreferencesSchema, ProcessResult, ProjectKeyRecord, ProjectRecord, ProjectRepository, ProjectSettingsCache, ProjectSettingsCacheOptions, ProjectThrottleSettings, PubSubChannel, QuietHours, QuietHoursSchema, RateLimitConfig, RecipientProfile, RecipientProfileSchema, Redis, RedisClient, RedisClientOptions, type RedisOptions, RenderedContent, RenderedContentSchema, STREAMS, SegmentRepository, Sql, StreamConsumer, StreamConsumerOptions, StreamEvent, StreamEventMetadata, StreamEventMetadataSchema, StreamEventSchema, StreamMessage, StreamName, StreamProducer, StreamProducerOptions, SuppressionRecord, SuspendExecutionError, SyncTemplatesInput, SyncTemplatesSchema, SystemHealthRecord, SystemMetricsRecord, TemplateCache, TemplateInput, TemplateRecord, TemplateRecordResponse, TemplateRepository, TemplateSchema, ThrottleCheckOptions, ThrottleResult, Transport, TriggerWorkflowInput, TriggerWorkflowSchema, TypedEventEnvelope, UnsubscribeClaim, UnsubscribeHeaderOptions, UpdateProjectInput, UpdateProjectSchema, UpdateUserInput, UpdateUserSchema, UserContact, UserContactInput, UserContactInputSchema, UserContactResponse, UserDetailResponse, UserProfile, UserProfileResponse, UserRecord, UserRepository, UserResponse, UserThrottle, UserThrottleOptions, ValidationError, WebhookEvent, WorkerHealth, WorkerOptions, WorkerState, WorkflowContext, WorkflowDefinitionRecord, WorkflowEvent, WorkflowInstanceRecord, WorkflowNotifyInput, WorkflowNotifyPayloadSchema, WorkflowNotifyResult, WorkflowRepository, WorkflowStepContext, WorkflowStepDef, WorkflowStepRecord, WorkflowStepSchema, WorkflowWaiterRecord, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, normaliseTarget, parseConfig, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
|
|
5239
|
+
export { AI_DEFAULTS, AddContactInput, AddContactSchema, AddUserInput, AddUserSchema, AdminUserRecord, AdminUserRepository, AiConfig, ApiRateLimiter, ApiRateLimiterOptions, AppError, AsyncSemaphore, BaseConfig, BaseWorker, BatchAddContactsInput, BatchAddContactsSchema, BatchItem, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, CircuitBreakerOptions, ConsumerGroup, ContactChannel, ContactChannelSchema, ContactRepository, CreateWorkflowInput, CreateWorkflowSchema, CreatedProjectKeyRecord, DLQMessageRecord, DataLoader, DataLoaderOptions, DatabaseClients, DatabaseOptions, Db, DeliveryOptions, DeliveryOptionsSchema, DeliveryResult, DevicePlatform, DeviceToken, DispatchedTaskPayload, ENRICHED_STREAMS, EscapeMode, EventEnvelope, EventEnvelopeSchema, EventMetadata, EventMetadataSchema, EventPayloadMap, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IdempotencyOptions, IngestEventInput, IngestEventSchema, InlineUser, InlineUserSchema, KnownEventType, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LeaseResult, LockHeldError, LogLevel, Logger, LoggerOptions, MigrationOptions, NonRetryableError, NotificationAiPendingPayload, NotificationAiPendingPayloadSchema, NotificationCanceledPayload, NotificationCanceledPayloadSchema, NotificationChannel, NotificationChannelSchema, NotificationCreatedPayload, NotificationCreatedPayloadSchema, NotificationDeliveredPayload, NotificationDeliveredPayloadSchema, NotificationDispatchedPayload, NotificationDispatchedPayloadSchema, NotificationEnrichedPayload, NotificationEnrichedPayloadSchema, NotificationFailedPayload, NotificationFailedPayloadSchema, NotificationLogRecord, NotificationPreference, NotificationPriority, NotificationPrioritySchema, NotificationRequestedPayload, NotificationRequestedPayloadSchema, NotificationScheduledPayload, NotificationScheduledPayloadSchema, NotificationSkippedPayload, NotificationSkippedPayloadSchema, NotificationStatus, NotificationStatusSchema, NotificationTarget, NotificationTargetSchema, NotifkitClient, NotifkitClientOptions, NotifkitOptions, NotifkitServer, NotifyRequestInput, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, ParseResult, PendingEntry, PendingMessageScanner, PendingMessageScannerOptions, PreferenceRepository, Preferences, PreferencesSchema, ProcessResult, ProjectKeyRecord, ProjectRecord, ProjectRepository, ProjectSettingsCache, ProjectSettingsCacheOptions, ProjectThrottleSettings, PubSubChannel, QuietHours, QuietHoursSchema, RateLimitConfig, RecipientProfile, RecipientProfileSchema, Redis, RedisClient, RedisClientOptions, type RedisOptions, RenderedContent, RenderedContentSchema, STREAMS, SegmentRepository, Sql, StreamConsumer, StreamConsumerOptions, StreamEvent, StreamEventMetadata, StreamEventMetadataSchema, StreamEventSchema, StreamMessage, StreamName, StreamProducer, StreamProducerOptions, SuppressionRecord, SuspendExecutionError, SyncTemplatesInput, SyncTemplatesSchema, SystemHealthRecord, SystemMetricsRecord, TemplateCache, TemplateInput, TemplateRecord, TemplateRecordResponse, TemplateRepository, TemplateSchema, ThrottleCheckOptions, ThrottleResult, Transport, TriggerWorkflowInput, TriggerWorkflowSchema, TypedEventEnvelope, UnsubscribeClaim, UnsubscribeHeaderOptions, UpdateProjectInput, UpdateProjectSchema, UpdateUserInput, UpdateUserSchema, UserContact, UserContactInput, UserContactInputSchema, UserContactResponse, UserDetailResponse, UserProfile, UserProfileResponse, UserRecord, UserRepository, UserResponse, UserThrottle, UserThrottleOptions, ValidationError, WebhookEvent, WorkerHealth, WorkerOptions, WorkerState, WorkflowContext, WorkflowDefinitionRecord, WorkflowEvent, WorkflowInstanceRecord, WorkflowNotifyInput, WorkflowNotifyPayloadSchema, WorkflowNotifyResult, WorkflowRepository, WorkflowStepContext, WorkflowStepDef, WorkflowStepRecord, WorkflowStepSchema, WorkflowWaiterRecord, assertTablesInSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, migrateDatabase, normaliseTarget, notifkitTableNames, parseConfig, qualifyMigrationSql, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { $t as
|
|
2
|
-
export { AI_DEFAULTS, AddContactSchema, AddUserSchema, AdminUserRepository, ApiRateLimiter, AppError, AsyncSemaphore, BaseWorker, BatchAddContactsSchema, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, ContactChannelSchema, ContactRepository, CreateWorkflowSchema, DataLoader, DeliveryOptionsSchema, ENRICHED_STREAMS, EventEnvelopeSchema, EventMetadataSchema, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IngestEventSchema, InlineUserSchema, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LockHeldError, NonRetryableError, NotificationAiPendingPayloadSchema, NotificationCanceledPayloadSchema, NotificationChannelSchema, NotificationCreatedPayloadSchema, NotificationDeliveredPayloadSchema, NotificationDispatchedPayloadSchema, NotificationEnrichedPayloadSchema, NotificationFailedPayloadSchema, NotificationPrioritySchema, NotificationRequestedPayloadSchema, NotificationScheduledPayloadSchema, NotificationSkippedPayloadSchema, NotificationStatusSchema, NotificationTargetSchema, NotifkitClient, NotifkitServer, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, PendingMessageScanner, PreferenceRepository, PreferencesSchema, ProjectRepository, ProjectSettingsCache, QuietHoursSchema, RecipientProfileSchema, Redis, RedisClient, RenderedContentSchema, STREAMS, SegmentRepository, StreamConsumer, StreamEventMetadataSchema, StreamEventSchema, StreamProducer, SuspendExecutionError, SyncTemplatesSchema, TemplateCache, TemplateRepository, TemplateSchema, TriggerWorkflowSchema, UpdateProjectSchema, UpdateUserSchema, UserContactInputSchema, UserRepository, UserThrottle, ValidationError, WorkflowNotifyPayloadSchema, WorkflowRepository, WorkflowStepSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, normaliseTarget, parseConfig, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
|
|
1
|
+
import { $ as migrateDatabase, $t as STREAMS, A as LUA_ACQUIRE_LEASE, At as AddUserSchema, B as ApiRateLimiter, Bt as TemplateSchema, C as ContactRepository, Cn as BatchProcessor, Ct as NotificationScheduledPayloadSchema, D as TemplateRepository, Dn as LRUCache, Dt as NotificationRequestedPayloadSchema, E as SegmentRepository, En as normaliseTarget, Et as NotificationCreatedPayloadSchema, F as LUA_SCHEDULER_POLL, Ft as InlineUserSchema, G as StreamConsumer, Gt as WorkflowNotifyPayloadSchema, H as UserThrottle, Ht as UpdateProjectSchema, I as LUA_USER_THROTTLE, It as NotifyRequestSchema, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, Kt as WorkflowStepSchema, L as Redis, Lt as PreferencesSchema, M as LUA_MARK_PROCESSED, Mt as ContactChannelSchema, N as LUA_RELEASE_LOCK, Nt as CreateWorkflowSchema, O as UserRepository, On as globalEmitter, Ot as NotificationTargetSchema, P as LUA_RENEW_LOCK, Pt as IngestEventSchema, Q as createDatabase, Qt as PUBSUB_CHANNELS, R as RedisClient, Rt as QuietHoursSchema, S as AdminUserRepository, Sn as CircuitBreaker, St as RenderedContentSchema, T as ProjectRepository, Tn as getPriorityBucket, Tt as RecipientProfileSchema, U as calculateTokenBatchSize, Ut as UpdateUserSchema, V as ProjectSettingsCache, Vt as TriggerWorkflowSchema, W as PendingMessageScanner, Wt as UserContactInputSchema, X as IdempotencyGuard, Xt as INBOUND_STREAMS, Y as createLogger, Yt as ENRICHED_STREAMS, Z as assertTablesInSchema, Zt as OUTBOUND_STREAMS, _ as TemplateCache, _n as AppError, _t as NotificationSkippedPayloadSchema, a as SuspendExecutionError, an as EventMetadataSchema, b as interpolate, bn as sleep, bt as DeliveryOptionsSchema, c as BaseWorker, cn as NotificationStatusSchema, d as startHealthReporter, dn as getAiConfig, en as EventEnvelopeSchema, et as notifkitTableNames, f as buildUnsubscribeHeaders, fn as loadEnv, g as transportRegistry, gn as setGlobalConfig, gt as NotificationCanceledPayloadSchema, h as registerTransport, hn as setAiConfig, ht as NotificationAiPendingPayloadSchema, i as workflowRegistry, in as registry, j as LUA_LEASE_API_RATE_LIMIT, jt as BatchAddContactsSchema, k as WorkflowRepository, kt as AddContactSchema, l as LockHeldError, ln as AI_DEFAULTS, m as verifyUnsubscribeToken, mn as readBaseConfig, n as NotifkitClient, nn as StreamEventSchema, nt as runMigrations, o as buildStepNotifyPayload, on as NotificationChannelSchema, p as signUnsubscribeToken, pn as parseConfig, q as getMetricsRegistry, qt as buildStreamEvent, r as workflow, rn as EventRegistry, s as resolveStepTarget, sn as NotificationPrioritySchema, t as NotifkitServer, tn as StreamEventMetadataSchema, tt as qualifyMigrationSql, u as NonRetryableError, un as baseConfigSchema, v as escapeHeader, vn as ValidationError, vt as NotificationFailedPayloadSchema, w as PreferenceRepository, wn as AsyncSemaphore, wt as NotificationEnrichedPayloadSchema, x as renderWithTemplate, xn as DataLoader, xt as NotificationDispatchedPayloadSchema, y as escapeHtml, yn as generateId, yt as NotificationDeliveredPayloadSchema, z as registerCustomCommands, zt as SyncTemplatesSchema } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
export { AI_DEFAULTS, AddContactSchema, AddUserSchema, AdminUserRepository, ApiRateLimiter, AppError, AsyncSemaphore, BaseWorker, BatchAddContactsSchema, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, ContactChannelSchema, ContactRepository, CreateWorkflowSchema, DataLoader, DeliveryOptionsSchema, ENRICHED_STREAMS, EventEnvelopeSchema, EventMetadataSchema, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IngestEventSchema, InlineUserSchema, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LockHeldError, NonRetryableError, NotificationAiPendingPayloadSchema, NotificationCanceledPayloadSchema, NotificationChannelSchema, NotificationCreatedPayloadSchema, NotificationDeliveredPayloadSchema, NotificationDispatchedPayloadSchema, NotificationEnrichedPayloadSchema, NotificationFailedPayloadSchema, NotificationPrioritySchema, NotificationRequestedPayloadSchema, NotificationScheduledPayloadSchema, NotificationSkippedPayloadSchema, NotificationStatusSchema, NotificationTargetSchema, NotifkitClient, NotifkitServer, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, PendingMessageScanner, PreferenceRepository, PreferencesSchema, ProjectRepository, ProjectSettingsCache, QuietHoursSchema, RecipientProfileSchema, Redis, RedisClient, RenderedContentSchema, STREAMS, SegmentRepository, StreamConsumer, StreamEventMetadataSchema, StreamEventSchema, StreamProducer, SuspendExecutionError, SyncTemplatesSchema, TemplateCache, TemplateRepository, TemplateSchema, TriggerWorkflowSchema, UpdateProjectSchema, UpdateUserSchema, UserContactInputSchema, UserRepository, UserThrottle, ValidationError, WorkflowNotifyPayloadSchema, WorkflowRepository, WorkflowStepSchema, assertTablesInSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, migrateDatabase, normaliseTarget, notifkitTableNames, parseConfig, qualifyMigrationSql, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
1
|
+
import { $t as STREAMS, D as TemplateRepository, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Tn as getPriorityBucket, X as IdempotencyGuard, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, dn as getAiConfig, fn as loadEnv, in as registry, ln as AI_DEFAULTS, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads, x as renderWithTemplate } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
|
|
3
3
|
import { generateText } from "ai";
|
|
4
4
|
//#region src/services/ai/main.ts
|
|
5
5
|
loadEnv();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { $ as
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
3
|
-
import { n as upsertOutboxProviderIds } from "./bulk-
|
|
1
|
+
import { $t as STREAMS, B as ApiRateLimiter, C as ContactRepository, Cn as BatchProcessor, D as TemplateRepository, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, Sn as CircuitBreaker, Tn as getPriorityBucket, X as IdempotencyGuard, Zt as OUTBOUND_STREAMS, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, fn as loadEnv, g as transportRegistry, in as registry, pn as parseConfig, qt as buildStreamEvent, rt as deliveryOutbox, st as scheduledPayloads, u as NonRetryableError, un as baseConfigSchema, x as renderWithTemplate } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
|
|
3
|
+
import { n as upsertOutboxProviderIds } from "./bulk-C4hTcnLu.mjs";
|
|
4
4
|
import { sql } from "drizzle-orm";
|
|
5
5
|
//#region src/services/delivery/throttle.ts
|
|
6
6
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as ContactRepository, D as TemplateRepository,
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
1
|
+
import { C as ContactRepository, D as TemplateRepository, Jt as CONSUMER_GROUPS, O as UserRepository, On as globalEmitter, Tn as getPriorityBucket, X as IdempotencyGuard, Xt as INBOUND_STREAMS, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, fn as loadEnv, in as registry, mn as readBaseConfig, qt as buildStreamEvent, w as PreferenceRepository } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
//#region src/services/enricher/main.ts
|
|
5
5
|
loadEnv();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as ContactRepository, D as TemplateRepository,
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
1
|
+
import { $t as STREAMS, C as ContactRepository, D as TemplateRepository, En as normaliseTarget, Jt as CONSUMER_GROUPS, O as UserRepository, On as globalEmitter, Tn as getPriorityBucket, c as BaseWorker, ct as suppressions, d as startHealthReporter, fn as loadEnv, in as registry, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { and, asc, eq, gt, inArray, or, sql } from "drizzle-orm";
|
|
5
5
|
//#region src/services/scheduler/send-gate.ts
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
1
|
+
import { $t as STREAMS, C as ContactRepository, D as TemplateRepository, Dn as LRUCache, En as normaliseTarget, H as UserThrottle, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, T as ProjectRepository, Tn as getPriorityBucket, V as ProjectSettingsCache, X as IdempotencyGuard, Yt as ENRICHED_STREAMS, _ as TemplateCache, c as BaseWorker, ct as suppressions, d as startHealthReporter, f as buildUnsubscribeHeaders, fn as loadEnv, in as registry, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads, xn as DataLoader } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { and, eq } from "drizzle-orm";
|
|
5
5
|
//#region src/services/engine/main.ts
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ as
|
|
1
|
+
import { $t as STREAMS, At as AddUserSchema, B as ApiRateLimiter, C as ContactRepository, D as TemplateRepository, Dn as LRUCache, E as SegmentRepository, En as normaliseTarget, It as NotifyRequestSchema, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, Lt as PreferencesSchema, Mt as ContactChannelSchema, Nt as CreateWorkflowSchema, O as UserRepository, On as globalEmitter, Q as createDatabase, Qt as PUBSUB_CHANNELS, R as RedisClient, S as AdminUserRepository, T as ProjectRepository, Tn as getPriorityBucket, Ut as UpdateUserSchema, Vt as TriggerWorkflowSchema, Y as createLogger, at as projectApiKeys, ct as suppressions, dt as workflowDefinitions, fn as loadEnv, g as transportRegistry, it as messageLogs, jt as BatchAddContactsSchema, k as WorkflowRepository, lt as userTopicPreferences, m as verifyUnsubscribeToken, mn as readBaseConfig, ot as projects, q as getMetricsRegistry, qt as buildStreamEvent, rt as deliveryOutbox, st as scheduledPayloads, ut as users, zt as SyncTemplatesSchema } from "./src-CIPTzq9i.mjs";
|
|
2
2
|
import { dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { createHash, randomBytes, randomUUID, scrypt, timingSafeEqual } from "node:crypto";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-
|
|
3
|
-
import { t as insertMessageLogs } from "./bulk-
|
|
1
|
+
import { $t as STREAMS, Jt as CONSUMER_GROUPS, K as StreamProducer, c as BaseWorker, d as startHealthReporter, fn as loadEnv, ft as workflowInstances, mn as readBaseConfig, mt as workflowWaiters, pt as workflowSteps, qt as buildStreamEvent } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-omGPIBny.mjs";
|
|
3
|
+
import { t as insertMessageLogs } from "./bulk-C4hTcnLu.mjs";
|
|
4
4
|
import { and, eq, gt, isNull, or } from "drizzle-orm";
|
|
5
5
|
//#region src/services/events/main.ts
|
|
6
6
|
loadEnv();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-
|
|
1
|
+
import { $t as STREAMS, Jt as CONSUMER_GROUPS, K as StreamProducer, a as SuspendExecutionError, c as BaseWorker, d as startHealthReporter, dt as workflowDefinitions, fn as loadEnv, ft as workflowInstances, i as workflowRegistry, mn as readBaseConfig, mt as workflowWaiters, o as buildStepNotifyPayload, pt as workflowSteps, qt as buildStreamEvent } from "./src-CIPTzq9i.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-omGPIBny.mjs";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { and, eq } from "drizzle-orm";
|
|
5
5
|
//#region src/services/workflow/main.ts
|
|
@@ -6,10 +6,11 @@ import { createHmac, randomUUID, timingSafeEqual } from "node:crypto";
|
|
|
6
6
|
import { EventEmitter } from "node:events";
|
|
7
7
|
import postgres from "postgres";
|
|
8
8
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
9
|
-
import { boolean, check, index, integer, jsonb, pgEnum, pgTable, primaryKey, text, time, timestamp, unique, uuid, varchar } from "drizzle-orm/pg-core";
|
|
10
|
-
import { and, desc, eq, inArray, or, sql } from "drizzle-orm";
|
|
9
|
+
import { PgTable, boolean, check, index, integer, jsonb, pgEnum, pgTable, primaryKey, text, time, timestamp, unique, uuid, varchar } from "drizzle-orm/pg-core";
|
|
10
|
+
import { and, desc, eq, getTableName, inArray, is, or, sql } from "drizzle-orm";
|
|
11
11
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
12
12
|
import { fileURLToPath } from "url";
|
|
13
|
+
import os from "os";
|
|
13
14
|
import path from "path";
|
|
14
15
|
import fs from "fs";
|
|
15
16
|
import pino from "pino";
|
|
@@ -372,6 +373,13 @@ const baseConfigSchema = z.object({
|
|
|
372
373
|
*/
|
|
373
374
|
IDEMPOTENCY_TTL_SECONDS: z.coerce.number().int().min(60).default(3600),
|
|
374
375
|
DB_MAX_CONNECTIONS: z.coerce.number().int().min(1).default(2),
|
|
376
|
+
/**
|
|
377
|
+
* PostgreSQL schema that holds notifkit's tables and enum types. Defaults to
|
|
378
|
+
* `public`. Any other value makes migrations create every object in that
|
|
379
|
+
* schema and sets `search_path` on every connection, so notifkit can share a
|
|
380
|
+
* database with an application that owns `public`.
|
|
381
|
+
*/
|
|
382
|
+
DB_SCHEMA: z.string().trim().regex(/^[a-z_][a-z0-9_]{0,62}$/, "must be a lowercase PostgreSQL identifier").default("public"),
|
|
375
383
|
LOG_FLUSH_INTERVAL_MS: z.coerce.number().int().min(50).default(500),
|
|
376
384
|
LOG_BUFFER_MAX_SIZE: z.coerce.number().int().min(100).default(5e3),
|
|
377
385
|
SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(1e4),
|
|
@@ -1298,14 +1306,16 @@ const adminUsers = pgTable("admin_users", {
|
|
|
1298
1306
|
* Call once at application startup; pass db into repositories.
|
|
1299
1307
|
* Call sql.end() during graceful shutdown.
|
|
1300
1308
|
*/
|
|
1301
|
-
function createDatabase({ url, applicationName = "notifkit", maxConnections, idleTimeoutSeconds = 30, logger }) {
|
|
1309
|
+
function createDatabase({ url, schema: dbSchema, applicationName = "notifkit", maxConnections, idleTimeoutSeconds = 30, logger }) {
|
|
1302
1310
|
const finalMaxConnections = maxConnections ?? readBaseConfig().DB_MAX_CONNECTIONS;
|
|
1311
|
+
const finalSchema = resolveSchema(dbSchema);
|
|
1303
1312
|
const sql = postgres(url, {
|
|
1304
1313
|
max: finalMaxConnections,
|
|
1305
1314
|
idle_timeout: idleTimeoutSeconds,
|
|
1306
1315
|
connection: {
|
|
1307
1316
|
application_name: applicationName,
|
|
1308
|
-
statement_timeout: 1e4
|
|
1317
|
+
statement_timeout: 1e4,
|
|
1318
|
+
...finalSchema === DEFAULT_SCHEMA ? {} : { search_path: quoteIdentifier(finalSchema) }
|
|
1309
1319
|
},
|
|
1310
1320
|
onnotice: (notice) => {
|
|
1311
1321
|
logger?.debug({ notice }, "postgres notice");
|
|
@@ -1316,12 +1326,122 @@ function createDatabase({ url, applicationName = "notifkit", maxConnections, idl
|
|
|
1316
1326
|
db: drizzle(sql, { schema: schema_exports })
|
|
1317
1327
|
};
|
|
1318
1328
|
}
|
|
1319
|
-
|
|
1329
|
+
const DEFAULT_SCHEMA = "public";
|
|
1330
|
+
const SCHEMA_NAME = /^[a-z_][a-z0-9_]{0,62}$/;
|
|
1331
|
+
const STATEMENT_BREAKPOINT = "--> statement-breakpoint";
|
|
1332
|
+
function resolveSchema(schema) {
|
|
1333
|
+
const name = schema ?? readBaseConfig().DB_SCHEMA;
|
|
1334
|
+
if (!SCHEMA_NAME.test(name)) throw new Error(`Invalid database schema "${name}": must be a lowercase PostgreSQL identifier`);
|
|
1335
|
+
return name;
|
|
1336
|
+
}
|
|
1337
|
+
function quoteIdentifier(name) {
|
|
1338
|
+
return `"${name}"`;
|
|
1339
|
+
}
|
|
1340
|
+
function packagedMigrationsFolder() {
|
|
1320
1341
|
const __filename = fileURLToPath(import.meta.url);
|
|
1321
1342
|
const __dirname = path.dirname(__filename);
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1343
|
+
const folder = path.resolve(__dirname, "../../drizzle");
|
|
1344
|
+
return fs.existsSync(folder) ? folder : path.resolve(__dirname, "../drizzle");
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* Retarget one generated migration at `schema`. drizzle-kit qualifies enum
|
|
1348
|
+
* types and foreign-key targets with `"public".` but leaves tables and indexes
|
|
1349
|
+
* unqualified, so both halves have to move: the qualifier is rewritten, and
|
|
1350
|
+
* `SET LOCAL search_path` places the unqualified objects. `SET LOCAL` ends with
|
|
1351
|
+
* the migration transaction and never leaks into a pooled connection.
|
|
1352
|
+
*
|
|
1353
|
+
* Throws if `public` is still mentioned after the rewrite: a migration that
|
|
1354
|
+
* spells the schema some other way would otherwise put objects into `public`
|
|
1355
|
+
* without a word.
|
|
1356
|
+
*/
|
|
1357
|
+
function qualifyMigrationSql(source, schema) {
|
|
1358
|
+
const rewritten = source.replaceAll(`"${DEFAULT_SCHEMA}".`, `${quoteIdentifier(schema)}.`);
|
|
1359
|
+
if (/\bpublic\b/i.test(rewritten)) throw new Error(`Migration still references the public schema after retargeting it to "${schema}"`);
|
|
1360
|
+
return `SET LOCAL search_path TO ${quoteIdentifier(schema)};\n${STATEMENT_BREAKPOINT}\n${rewritten}`;
|
|
1361
|
+
}
|
|
1362
|
+
/** Copy the journal and every migration it lists into `target`, retargeted at `schema`. */
|
|
1363
|
+
function writeSchemaMigrations(source, target, schema) {
|
|
1364
|
+
const journalPath = path.join(source, "meta", "_journal.json");
|
|
1365
|
+
const journal = JSON.parse(fs.readFileSync(journalPath, "utf8"));
|
|
1366
|
+
fs.mkdirSync(path.join(target, "meta"));
|
|
1367
|
+
fs.copyFileSync(journalPath, path.join(target, "meta", "_journal.json"));
|
|
1368
|
+
for (const { tag } of journal.entries) {
|
|
1369
|
+
const file = `${tag}.sql`;
|
|
1370
|
+
const migration = fs.readFileSync(path.join(source, file), "utf8");
|
|
1371
|
+
fs.writeFileSync(path.join(target, file), qualifyMigrationSql(migration, schema));
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
async function runMigrations(db, options = {}) {
|
|
1375
|
+
const migrationsFolder = packagedMigrationsFolder();
|
|
1376
|
+
const schema = resolveSchema(options.schema);
|
|
1377
|
+
const { migrationsTable } = options;
|
|
1378
|
+
if (schema === DEFAULT_SCHEMA) {
|
|
1379
|
+
await migrate(db, {
|
|
1380
|
+
migrationsFolder,
|
|
1381
|
+
migrationsTable,
|
|
1382
|
+
migrationsSchema: options.migrationsSchema
|
|
1383
|
+
});
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
await db.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(schema)}`);
|
|
1387
|
+
const retargeted = fs.mkdtempSync(path.join(os.tmpdir(), "notifkit-migrations-"));
|
|
1388
|
+
try {
|
|
1389
|
+
writeSchemaMigrations(migrationsFolder, retargeted, schema);
|
|
1390
|
+
await migrate(db, {
|
|
1391
|
+
migrationsFolder: retargeted,
|
|
1392
|
+
migrationsTable,
|
|
1393
|
+
migrationsSchema: options.migrationsSchema ?? schema
|
|
1394
|
+
});
|
|
1395
|
+
} finally {
|
|
1396
|
+
fs.rmSync(retargeted, {
|
|
1397
|
+
recursive: true,
|
|
1398
|
+
force: true
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
await assertTablesInSchema(db, schema);
|
|
1402
|
+
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Names of every table notifkit's Drizzle schema defines, sorted. This is the
|
|
1405
|
+
* same object the ORM queries at run time, so the list cannot drift from the
|
|
1406
|
+
* tables notifkit actually uses.
|
|
1407
|
+
*/
|
|
1408
|
+
function notifkitTableNames() {
|
|
1409
|
+
return Object.values(schema_exports).filter((value) => is(value, PgTable)).map((table) => getTableName(table)).sort();
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1412
|
+
* Fail if any notifkit table is missing from `targetSchema`. Run after
|
|
1413
|
+
* migrating into a custom schema: if the retargeting ever stops working (the
|
|
1414
|
+
* `search_path` is overridden, a migration is skipped because of a shared
|
|
1415
|
+
* journal, ...), the tables land elsewhere or nowhere, and every runtime query
|
|
1416
|
+
* would fail with "relation does not exist". Only the target schema is checked:
|
|
1417
|
+
* the application sharing the database may well own `users` or `projects` in
|
|
1418
|
+
* `public`, and that is not an error.
|
|
1419
|
+
*/
|
|
1420
|
+
async function assertTablesInSchema(db, targetSchema) {
|
|
1421
|
+
const rows = await db.execute(sql`SELECT table_name FROM information_schema.tables WHERE table_schema = ${targetSchema}`);
|
|
1422
|
+
const present = new Set(rows.map((row) => row.table_name));
|
|
1423
|
+
const missing = notifkitTableNames().filter((name) => !present.has(name));
|
|
1424
|
+
if (missing.length > 0) throw new Error(`Migrations finished, but schema "${targetSchema}" is missing notifkit tables: ${missing.join(", ")}`);
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Apply the bundled migrations to the database at `url` and close the
|
|
1428
|
+
* connection, whether or not they succeed. `schema` defaults to `DB_SCHEMA`.
|
|
1429
|
+
* This is what `notifkit-migrate` runs.
|
|
1430
|
+
*/
|
|
1431
|
+
async function migrateDatabase({ url, schema: dbSchema }) {
|
|
1432
|
+
const targetSchema = resolveSchema(dbSchema);
|
|
1433
|
+
const { db, sql } = createDatabase({
|
|
1434
|
+
url,
|
|
1435
|
+
schema: targetSchema,
|
|
1436
|
+
applicationName: "notifkit-migrate",
|
|
1437
|
+
maxConnections: 1
|
|
1438
|
+
});
|
|
1439
|
+
try {
|
|
1440
|
+
await runMigrations(db, { schema: targetSchema });
|
|
1441
|
+
} finally {
|
|
1442
|
+
await sql.end();
|
|
1443
|
+
}
|
|
1444
|
+
return targetSchema;
|
|
1325
1445
|
}
|
|
1326
1446
|
//#endregion
|
|
1327
1447
|
//#region src/idempotency/index.ts
|
|
@@ -4006,6 +4126,7 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
4006
4126
|
if (this.options.deliveryConcurrency) process.env.DELIVERY_CONCURRENCY = String(this.options.deliveryConcurrency);
|
|
4007
4127
|
if (this.options.redisOptions?.maxQueueLength) process.env.QUEUE_MAX_LEN = String(this.options.redisOptions.maxQueueLength);
|
|
4008
4128
|
if (this.options.dbOptions?.maxConnections) process.env.DB_MAX_CONNECTIONS = String(this.options.dbOptions.maxConnections);
|
|
4129
|
+
if (this.options.dbOptions?.schema) process.env.DB_SCHEMA = this.options.dbOptions.schema;
|
|
4009
4130
|
const isProduction = process.env.NODE_ENV === "production";
|
|
4010
4131
|
if (!this.options.redisUrl) if (process.env.REDIS_URL) this.options.redisUrl = process.env.REDIS_URL;
|
|
4011
4132
|
else if (!isProduction) {
|
|
@@ -4048,35 +4169,35 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
4048
4169
|
}
|
|
4049
4170
|
const startupPromises = [];
|
|
4050
4171
|
if (services.includes("api")) {
|
|
4051
|
-
const { startApiServer } = await import("./main-
|
|
4172
|
+
const { startApiServer } = await import("./main-BxAeC_k6.mjs");
|
|
4052
4173
|
startupPromises.push(startApiServer());
|
|
4053
4174
|
}
|
|
4054
4175
|
if (services.includes("delivery")) {
|
|
4055
|
-
const { startDeliveryWorker } = await import("./main-
|
|
4176
|
+
const { startDeliveryWorker } = await import("./main-B5BwjIMu.mjs");
|
|
4056
4177
|
startupPromises.push(startDeliveryWorker());
|
|
4057
4178
|
}
|
|
4058
4179
|
if (services.includes("engine")) {
|
|
4059
|
-
const { startEngineWorker } = await import("./main-
|
|
4180
|
+
const { startEngineWorker } = await import("./main-BtnGmyaT.mjs");
|
|
4060
4181
|
startupPromises.push(startEngineWorker());
|
|
4061
4182
|
}
|
|
4062
4183
|
if (services.includes("enricher")) {
|
|
4063
|
-
const { startEnricherWorker } = await import("./main-
|
|
4184
|
+
const { startEnricherWorker } = await import("./main-BU27VjDC.mjs");
|
|
4064
4185
|
startupPromises.push(startEnricherWorker());
|
|
4065
4186
|
}
|
|
4066
4187
|
if (services.includes("scheduler")) {
|
|
4067
|
-
const { startSchedulerWorker } = await import("./main-
|
|
4188
|
+
const { startSchedulerWorker } = await import("./main-Bes11Ad3.mjs");
|
|
4068
4189
|
startupPromises.push(startSchedulerWorker());
|
|
4069
4190
|
}
|
|
4070
4191
|
if (services.includes("ai")) {
|
|
4071
|
-
const { startAiWorker } = await import("./main-
|
|
4192
|
+
const { startAiWorker } = await import("./main-B-AZ-xoN.mjs");
|
|
4072
4193
|
startupPromises.push(startAiWorker());
|
|
4073
4194
|
}
|
|
4074
4195
|
if (services.includes("workflow")) {
|
|
4075
|
-
const { startWorkflowWorker } = await import("./main-
|
|
4196
|
+
const { startWorkflowWorker } = await import("./main-DYGCl7Ja.mjs");
|
|
4076
4197
|
startupPromises.push(startWorkflowWorker());
|
|
4077
4198
|
}
|
|
4078
4199
|
if (services.includes("events")) {
|
|
4079
|
-
const { startEventWorker } = await import("./main-
|
|
4200
|
+
const { startEventWorker } = await import("./main-DAXO-MiH.mjs");
|
|
4080
4201
|
startupPromises.push(startEventWorker());
|
|
4081
4202
|
}
|
|
4082
4203
|
const handleSignal = async (signal) => {
|
|
@@ -4098,9 +4219,9 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
4098
4219
|
await Promise.all(startupPromises);
|
|
4099
4220
|
if (readBaseConfig().PIPELINE_FUSED && services.includes("enricher") && services.includes("engine") && services.includes("delivery")) {
|
|
4100
4221
|
const [{ getEnricherWorker }, { getEngineWorker }, { getDeliveryWorker }] = await Promise.all([
|
|
4101
|
-
import("./main-
|
|
4102
|
-
import("./main-
|
|
4103
|
-
import("./main-
|
|
4222
|
+
import("./main-BU27VjDC.mjs"),
|
|
4223
|
+
import("./main-BtnGmyaT.mjs"),
|
|
4224
|
+
import("./main-B5BwjIMu.mjs")
|
|
4104
4225
|
]);
|
|
4105
4226
|
const enricher = getEnricherWorker();
|
|
4106
4227
|
const engine = getEngineWorker();
|
|
@@ -4131,14 +4252,14 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
4131
4252
|
"events"
|
|
4132
4253
|
] : this.options.services;
|
|
4133
4254
|
const shutdownOrder = [
|
|
4134
|
-
["api", async () => (await import("./main-
|
|
4135
|
-
["workflow", async () => (await import("./main-
|
|
4136
|
-
["enricher", async () => (await import("./main-
|
|
4137
|
-
["ai", async () => (await import("./main-
|
|
4138
|
-
["engine", async () => (await import("./main-
|
|
4139
|
-
["scheduler", async () => (await import("./main-
|
|
4140
|
-
["delivery", async () => (await import("./main-
|
|
4141
|
-
["events", async () => (await import("./main-
|
|
4255
|
+
["api", async () => (await import("./main-BxAeC_k6.mjs")).stopApiServer()],
|
|
4256
|
+
["workflow", async () => (await import("./main-DYGCl7Ja.mjs")).stopWorkflowWorker()],
|
|
4257
|
+
["enricher", async () => (await import("./main-BU27VjDC.mjs")).stopEnricherWorker()],
|
|
4258
|
+
["ai", async () => (await import("./main-B-AZ-xoN.mjs")).stopAiWorker()],
|
|
4259
|
+
["engine", async () => (await import("./main-BtnGmyaT.mjs")).stopEngineWorker()],
|
|
4260
|
+
["scheduler", async () => (await import("./main-Bes11Ad3.mjs")).stopSchedulerWorker()],
|
|
4261
|
+
["delivery", async () => (await import("./main-B5BwjIMu.mjs")).stopDeliveryWorker()],
|
|
4262
|
+
["events", async () => (await import("./main-DAXO-MiH.mjs")).stopEventWorker()]
|
|
4142
4263
|
];
|
|
4143
4264
|
for (const [name, stop] of shutdownOrder) if (services.includes(name)) await stop();
|
|
4144
4265
|
if (this.pgContainer) {
|
|
@@ -4152,4 +4273,4 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
4152
4273
|
}
|
|
4153
4274
|
};
|
|
4154
4275
|
//#endregion
|
|
4155
|
-
export {
|
|
4276
|
+
export { migrateDatabase as $, STREAMS as $t, LUA_ACQUIRE_LEASE as A, AddUserSchema as At, ApiRateLimiter as B, TemplateSchema as Bt, ContactRepository as C, BatchProcessor as Cn, NotificationScheduledPayloadSchema as Ct, TemplateRepository as D, LRUCache as Dn, NotificationRequestedPayloadSchema as Dt, SegmentRepository as E, normaliseTarget as En, NotificationCreatedPayloadSchema as Et, LUA_SCHEDULER_POLL as F, InlineUserSchema as Ft, StreamConsumer as G, WorkflowNotifyPayloadSchema as Gt, UserThrottle as H, UpdateProjectSchema as Ht, LUA_USER_THROTTLE as I, NotifyRequestSchema as It, metrics as J, CONSUMER_GROUPS as Jt, StreamProducer as K, WorkflowStepSchema as Kt, Redis as L, PreferencesSchema as Lt, LUA_MARK_PROCESSED as M, ContactChannelSchema as Mt, LUA_RELEASE_LOCK as N, CreateWorkflowSchema as Nt, UserRepository as O, globalEmitter as On, NotificationTargetSchema as Ot, LUA_RENEW_LOCK as P, IngestEventSchema as Pt, createDatabase as Q, PUBSUB_CHANNELS as Qt, RedisClient as R, QuietHoursSchema as Rt, AdminUserRepository as S, CircuitBreaker as Sn, RenderedContentSchema as St, ProjectRepository as T, getPriorityBucket as Tn, RecipientProfileSchema as Tt, calculateTokenBatchSize as U, UpdateUserSchema as Ut, ProjectSettingsCache as V, TriggerWorkflowSchema as Vt, PendingMessageScanner as W, UserContactInputSchema as Wt, IdempotencyGuard as X, INBOUND_STREAMS as Xt, createLogger as Y, ENRICHED_STREAMS as Yt, assertTablesInSchema as Z, OUTBOUND_STREAMS as Zt, TemplateCache as _, AppError as _n, NotificationSkippedPayloadSchema as _t, SuspendExecutionError as a, EventMetadataSchema as an, projectApiKeys as at, interpolate as b, sleep as bn, DeliveryOptionsSchema as bt, BaseWorker as c, NotificationStatusSchema as cn, suppressions as ct, startHealthReporter as d, getAiConfig as dn, workflowDefinitions as dt, EventEnvelopeSchema as en, notifkitTableNames as et, buildUnsubscribeHeaders as f, loadEnv as fn, workflowInstances as ft, transportRegistry as g, setGlobalConfig as gn, NotificationCanceledPayloadSchema as gt, registerTransport as h, setAiConfig as hn, NotificationAiPendingPayloadSchema as ht, workflowRegistry as i, registry as in, messageLogs as it, LUA_LEASE_API_RATE_LIMIT as j, BatchAddContactsSchema as jt, WorkflowRepository as k, AddContactSchema as kt, LockHeldError as l, AI_DEFAULTS as ln, userTopicPreferences as lt, verifyUnsubscribeToken as m, readBaseConfig as mn, workflowWaiters as mt, NotifkitClient as n, StreamEventSchema as nn, runMigrations as nt, buildStepNotifyPayload as o, NotificationChannelSchema as on, projects as ot, signUnsubscribeToken as p, parseConfig as pn, workflowSteps as pt, getMetricsRegistry as q, buildStreamEvent as qt, workflow as r, EventRegistry as rn, deliveryOutbox as rt, resolveStepTarget as s, NotificationPrioritySchema as sn, scheduledPayloads as st, NotifkitServer as t, StreamEventMetadataSchema as tn, qualifyMigrationSql as tt, NonRetryableError as u, baseConfigSchema as un, users as ut, escapeHeader as v, ValidationError as vn, NotificationFailedPayloadSchema as vt, PreferenceRepository as w, AsyncSemaphore as wn, NotificationEnrichedPayloadSchema as wt, renderWithTemplate as x, DataLoader as xn, NotificationDispatchedPayloadSchema as xt, escapeHtml as y, generateId as yn, NotificationDeliveredPayloadSchema as yt, registerCustomCommands as z, SyncTemplatesSchema as zt };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "notifkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Self-hosted notification infrastructure. One call delivers to email, SMS, push, and webhook — routed by preference, quiet hours, and consent.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "devkitshq",
|
|
@@ -51,18 +51,21 @@
|
|
|
51
51
|
"dashboard/dist",
|
|
52
52
|
"scripts/create-project.mjs",
|
|
53
53
|
"scripts/create-admin.mjs",
|
|
54
|
+
"scripts/migrate.mjs",
|
|
54
55
|
"README.md",
|
|
55
56
|
"LICENSE"
|
|
56
57
|
],
|
|
57
58
|
"bin": {
|
|
58
59
|
"notifkit-create-project": "./scripts/create-project.mjs",
|
|
59
|
-
"notifkit-create-admin": "./scripts/create-admin.mjs"
|
|
60
|
+
"notifkit-create-admin": "./scripts/create-admin.mjs",
|
|
61
|
+
"notifkit-migrate": "./scripts/migrate.mjs"
|
|
60
62
|
},
|
|
61
63
|
"scripts": {
|
|
62
64
|
"build": "tsdown",
|
|
63
65
|
"dev": "tsdown --watch",
|
|
64
66
|
"create-project": "node scripts/create-project.mjs",
|
|
65
67
|
"create-admin": "node scripts/create-admin.mjs",
|
|
68
|
+
"migrate": "node scripts/migrate.mjs",
|
|
66
69
|
"build:dashboard": "npm --prefix dashboard run build",
|
|
67
70
|
"test": "vitest run --coverage",
|
|
68
71
|
"test:watch": "vitest",
|
package/scripts/create-admin.mjs
CHANGED
|
@@ -36,7 +36,14 @@ if (!email || !password) {
|
|
|
36
36
|
|
|
37
37
|
const databaseUrl =
|
|
38
38
|
process.env.DATABASE_URL || "postgres://platform:platform@localhost:5432/notifkit";
|
|
39
|
-
const
|
|
39
|
+
const schema = (process.env.DB_SCHEMA || "public").trim();
|
|
40
|
+
if (!/^[a-z_][a-z0-9_]{0,62}$/.test(schema)) {
|
|
41
|
+
console.error(`Invalid DB_SCHEMA "${schema}": must be a lowercase PostgreSQL identifier.`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
const sql = postgres(databaseUrl, {
|
|
45
|
+
connection: schema === "public" ? {} : { search_path: `"${schema}"` },
|
|
46
|
+
});
|
|
40
47
|
|
|
41
48
|
try {
|
|
42
49
|
const passwordHash = await hashPassword(password);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Apply notifkit's database migrations.
|
|
4
|
+
*
|
|
5
|
+
* Reads DATABASE_URL and DB_SCHEMA (from the environment or .env) and runs the
|
|
6
|
+
* same migrations NotifkitServer runs on start-up with autoMigrate, into the
|
|
7
|
+
* same schema. Safe to run repeatedly: already-applied migrations are skipped.
|
|
8
|
+
* Exits non-zero if anything fails.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* DATABASE_URL=postgres://user:pass@host:5432/db npx notifkit-migrate
|
|
12
|
+
* DATABASE_URL=... DB_SCHEMA=notifications npx notifkit-migrate
|
|
13
|
+
* node scripts/migrate.mjs
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { config } from "dotenv";
|
|
17
|
+
import { resolve } from "node:path";
|
|
18
|
+
|
|
19
|
+
config({ path: resolve(process.cwd(), ".env") });
|
|
20
|
+
|
|
21
|
+
if (!process.env.DATABASE_URL) {
|
|
22
|
+
console.error("Usage: DATABASE_URL=postgres://... [DB_SCHEMA=schema] npx notifkit-migrate");
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
// Imported after .env is loaded: the package reads DB_SCHEMA from the environment.
|
|
28
|
+
const { migrateDatabase } = await import("../dist/index.mjs");
|
|
29
|
+
const schema = await migrateDatabase({ url: process.env.DATABASE_URL });
|
|
30
|
+
console.log(`Migrations applied to schema "${schema}".`);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
console.error(`Failed to run migrations: ${describe(err)}`);
|
|
33
|
+
process.exitCode = 1;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** drizzle wraps driver errors as "Failed query: ..."; the cause says what actually went wrong. */
|
|
37
|
+
function describe(err) {
|
|
38
|
+
const parts = [err.message];
|
|
39
|
+
if (err.fields) parts.push(JSON.stringify(err.fields));
|
|
40
|
+
const cause = err.cause;
|
|
41
|
+
if (cause) parts.push(`cause: ${cause.message || cause.code || String(cause)}`);
|
|
42
|
+
return parts.join("\n ");
|
|
43
|
+
}
|
package/src/config/index.ts
CHANGED
|
@@ -69,6 +69,17 @@ export const baseConfigSchema = z.object({
|
|
|
69
69
|
*/
|
|
70
70
|
IDEMPOTENCY_TTL_SECONDS: z.coerce.number().int().min(60).default(3600),
|
|
71
71
|
DB_MAX_CONNECTIONS: z.coerce.number().int().min(1).default(2),
|
|
72
|
+
/**
|
|
73
|
+
* PostgreSQL schema that holds notifkit's tables and enum types. Defaults to
|
|
74
|
+
* `public`. Any other value makes migrations create every object in that
|
|
75
|
+
* schema and sets `search_path` on every connection, so notifkit can share a
|
|
76
|
+
* database with an application that owns `public`.
|
|
77
|
+
*/
|
|
78
|
+
DB_SCHEMA: z
|
|
79
|
+
.string()
|
|
80
|
+
.trim()
|
|
81
|
+
.regex(/^[a-z_][a-z0-9_]{0,62}$/, "must be a lowercase PostgreSQL identifier")
|
|
82
|
+
.default("public"),
|
|
72
83
|
LOG_FLUSH_INTERVAL_MS: z.coerce.number().int().min(50).default(500),
|
|
73
84
|
LOG_BUFFER_MAX_SIZE: z.coerce.number().int().min(100).default(5000),
|
|
74
85
|
SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(10000),
|
package/src/db/index.ts
CHANGED
|
@@ -9,6 +9,11 @@ export type Db = PostgresJsDatabase<typeof schema>;
|
|
|
9
9
|
|
|
10
10
|
export interface DatabaseOptions {
|
|
11
11
|
url: string;
|
|
12
|
+
/**
|
|
13
|
+
* Schema holding notifkit's tables. Defaults to `DB_SCHEMA` (`public`).
|
|
14
|
+
* Anything else is set as the connection's `search_path`.
|
|
15
|
+
*/
|
|
16
|
+
schema?: string;
|
|
12
17
|
applicationName?: string;
|
|
13
18
|
maxConnections?: number;
|
|
14
19
|
idleTimeoutSeconds?: number;
|
|
@@ -27,12 +32,14 @@ export interface DatabaseClients {
|
|
|
27
32
|
*/
|
|
28
33
|
export function createDatabase({
|
|
29
34
|
url,
|
|
35
|
+
schema: dbSchema,
|
|
30
36
|
applicationName = "notifkit",
|
|
31
37
|
maxConnections,
|
|
32
38
|
idleTimeoutSeconds = 30,
|
|
33
39
|
logger,
|
|
34
40
|
}: DatabaseOptions): DatabaseClients {
|
|
35
41
|
const finalMaxConnections = maxConnections ?? readBaseConfig().DB_MAX_CONNECTIONS;
|
|
42
|
+
const finalSchema = resolveSchema(dbSchema);
|
|
36
43
|
|
|
37
44
|
const sql = postgres(url, {
|
|
38
45
|
max: finalMaxConnections,
|
|
@@ -40,6 +47,9 @@ export function createDatabase({
|
|
|
40
47
|
connection: {
|
|
41
48
|
application_name: applicationName,
|
|
42
49
|
statement_timeout: 10000 as any, // prevent TS issues with postgres.js types
|
|
50
|
+
// Unqualified queries resolve against this schema only, so a same-named
|
|
51
|
+
// table in `public` can never be picked up by mistake.
|
|
52
|
+
...(finalSchema === DEFAULT_SCHEMA ? {} : { search_path: quoteIdentifier(finalSchema) }),
|
|
43
53
|
},
|
|
44
54
|
onnotice: (notice) => {
|
|
45
55
|
logger?.debug({ notice }, "postgres notice");
|
|
@@ -52,18 +62,175 @@ export function createDatabase({
|
|
|
52
62
|
}
|
|
53
63
|
|
|
54
64
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
65
|
+
import { getTableName, is, sql as drizzleSql } from "drizzle-orm";
|
|
66
|
+
import { PgTable } from "drizzle-orm/pg-core";
|
|
55
67
|
import { fileURLToPath } from "url";
|
|
68
|
+
import os from "os";
|
|
56
69
|
import path from "path";
|
|
57
70
|
import fs from "fs";
|
|
58
71
|
|
|
59
|
-
|
|
72
|
+
const DEFAULT_SCHEMA = "public";
|
|
73
|
+
const SCHEMA_NAME = /^[a-z_][a-z0-9_]{0,62}$/;
|
|
74
|
+
const STATEMENT_BREAKPOINT = "--> statement-breakpoint";
|
|
75
|
+
|
|
76
|
+
export interface MigrationOptions {
|
|
77
|
+
/** Schema to create notifkit's objects in. Defaults to `DB_SCHEMA` (`public`). */
|
|
78
|
+
schema?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Schema of drizzle's migration journal table. Defaults to drizzle's own
|
|
81
|
+
* `drizzle` for `public`, and to `schema` otherwise, so two installs in
|
|
82
|
+
* different schemas of one database never share a journal.
|
|
83
|
+
*/
|
|
84
|
+
migrationsSchema?: string;
|
|
85
|
+
/** Name of drizzle's migration journal table. Defaults to `__drizzle_migrations`. */
|
|
86
|
+
migrationsTable?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function resolveSchema(schema?: string): string {
|
|
90
|
+
const name = schema ?? readBaseConfig().DB_SCHEMA;
|
|
91
|
+
if (!SCHEMA_NAME.test(name)) {
|
|
92
|
+
throw new Error(`Invalid database schema "${name}": must be a lowercase PostgreSQL identifier`);
|
|
93
|
+
}
|
|
94
|
+
return name;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function quoteIdentifier(name: string): string {
|
|
98
|
+
return `"${name}"`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function packagedMigrationsFolder(): string {
|
|
60
102
|
const __filename = fileURLToPath(import.meta.url);
|
|
61
103
|
const __dirname = path.dirname(__filename);
|
|
62
104
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
105
|
+
const folder = path.resolve(__dirname, "../../drizzle");
|
|
106
|
+
return fs.existsSync(folder) ? folder : path.resolve(__dirname, "../drizzle");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Retarget one generated migration at `schema`. drizzle-kit qualifies enum
|
|
111
|
+
* types and foreign-key targets with `"public".` but leaves tables and indexes
|
|
112
|
+
* unqualified, so both halves have to move: the qualifier is rewritten, and
|
|
113
|
+
* `SET LOCAL search_path` places the unqualified objects. `SET LOCAL` ends with
|
|
114
|
+
* the migration transaction and never leaks into a pooled connection.
|
|
115
|
+
*
|
|
116
|
+
* Throws if `public` is still mentioned after the rewrite: a migration that
|
|
117
|
+
* spells the schema some other way would otherwise put objects into `public`
|
|
118
|
+
* without a word.
|
|
119
|
+
*/
|
|
120
|
+
export function qualifyMigrationSql(source: string, schema: string): string {
|
|
121
|
+
const rewritten = source.replaceAll(`"${DEFAULT_SCHEMA}".`, `${quoteIdentifier(schema)}.`);
|
|
122
|
+
if (/\bpublic\b/i.test(rewritten)) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`Migration still references the public schema after retargeting it to "${schema}"`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return `SET LOCAL search_path TO ${quoteIdentifier(schema)};\n${STATEMENT_BREAKPOINT}\n${rewritten}`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Copy the journal and every migration it lists into `target`, retargeted at `schema`. */
|
|
131
|
+
function writeSchemaMigrations(source: string, target: string, schema: string): void {
|
|
132
|
+
const journalPath = path.join(source, "meta", "_journal.json");
|
|
133
|
+
const journal = JSON.parse(fs.readFileSync(journalPath, "utf8")) as {
|
|
134
|
+
entries: { tag: string }[];
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
fs.mkdirSync(path.join(target, "meta"));
|
|
138
|
+
fs.copyFileSync(journalPath, path.join(target, "meta", "_journal.json"));
|
|
139
|
+
for (const { tag } of journal.entries) {
|
|
140
|
+
const file = `${tag}.sql`;
|
|
141
|
+
const migration = fs.readFileSync(path.join(source, file), "utf8");
|
|
142
|
+
fs.writeFileSync(path.join(target, file), qualifyMigrationSql(migration, schema));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export async function runMigrations(db: Db, options: MigrationOptions = {}) {
|
|
147
|
+
const migrationsFolder = packagedMigrationsFolder();
|
|
148
|
+
const schema = resolveSchema(options.schema);
|
|
149
|
+
const { migrationsTable } = options;
|
|
150
|
+
|
|
151
|
+
if (schema === DEFAULT_SCHEMA) {
|
|
152
|
+
await migrate(db, {
|
|
153
|
+
migrationsFolder,
|
|
154
|
+
migrationsTable,
|
|
155
|
+
migrationsSchema: options.migrationsSchema,
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
66
158
|
}
|
|
67
159
|
|
|
68
|
-
await
|
|
160
|
+
await db.execute(drizzleSql`CREATE SCHEMA IF NOT EXISTS ${drizzleSql.identifier(schema)}`);
|
|
161
|
+
|
|
162
|
+
const retargeted = fs.mkdtempSync(path.join(os.tmpdir(), "notifkit-migrations-"));
|
|
163
|
+
try {
|
|
164
|
+
writeSchemaMigrations(migrationsFolder, retargeted, schema);
|
|
165
|
+
await migrate(db, {
|
|
166
|
+
migrationsFolder: retargeted,
|
|
167
|
+
migrationsTable,
|
|
168
|
+
migrationsSchema: options.migrationsSchema ?? schema,
|
|
169
|
+
});
|
|
170
|
+
} finally {
|
|
171
|
+
fs.rmSync(retargeted, { recursive: true, force: true });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
await assertTablesInSchema(db, schema);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Names of every table notifkit's Drizzle schema defines, sorted. This is the
|
|
179
|
+
* same object the ORM queries at run time, so the list cannot drift from the
|
|
180
|
+
* tables notifkit actually uses.
|
|
181
|
+
*/
|
|
182
|
+
export function notifkitTableNames(): string[] {
|
|
183
|
+
return Object.values(schema as Record<string, unknown>)
|
|
184
|
+
.filter((value): value is PgTable => is(value, PgTable))
|
|
185
|
+
.map((table) => getTableName(table))
|
|
186
|
+
.sort();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Fail if any notifkit table is missing from `targetSchema`. Run after
|
|
191
|
+
* migrating into a custom schema: if the retargeting ever stops working (the
|
|
192
|
+
* `search_path` is overridden, a migration is skipped because of a shared
|
|
193
|
+
* journal, ...), the tables land elsewhere or nowhere, and every runtime query
|
|
194
|
+
* would fail with "relation does not exist". Only the target schema is checked:
|
|
195
|
+
* the application sharing the database may well own `users` or `projects` in
|
|
196
|
+
* `public`, and that is not an error.
|
|
197
|
+
*/
|
|
198
|
+
export async function assertTablesInSchema(db: Db, targetSchema: string): Promise<void> {
|
|
199
|
+
const rows = await db.execute<{ table_name: string }>(
|
|
200
|
+
drizzleSql`SELECT table_name FROM information_schema.tables WHERE table_schema = ${targetSchema}`,
|
|
201
|
+
);
|
|
202
|
+
const present = new Set(rows.map((row) => row.table_name));
|
|
203
|
+
const missing = notifkitTableNames().filter((name) => !present.has(name));
|
|
204
|
+
if (missing.length > 0) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Migrations finished, but schema "${targetSchema}" is missing notifkit tables: ${missing.join(", ")}`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Apply the bundled migrations to the database at `url` and close the
|
|
213
|
+
* connection, whether or not they succeed. `schema` defaults to `DB_SCHEMA`.
|
|
214
|
+
* This is what `notifkit-migrate` runs.
|
|
215
|
+
*/
|
|
216
|
+
export async function migrateDatabase({
|
|
217
|
+
url,
|
|
218
|
+
schema: dbSchema,
|
|
219
|
+
}: {
|
|
220
|
+
url: string;
|
|
221
|
+
schema?: string;
|
|
222
|
+
}): Promise<string> {
|
|
223
|
+
const targetSchema = resolveSchema(dbSchema);
|
|
224
|
+
const { db, sql } = createDatabase({
|
|
225
|
+
url,
|
|
226
|
+
schema: targetSchema,
|
|
227
|
+
applicationName: "notifkit-migrate",
|
|
228
|
+
maxConnections: 1,
|
|
229
|
+
});
|
|
230
|
+
try {
|
|
231
|
+
await runMigrations(db, { schema: targetSchema });
|
|
232
|
+
} finally {
|
|
233
|
+
await sql.end();
|
|
234
|
+
}
|
|
235
|
+
return targetSchema;
|
|
69
236
|
}
|
package/src/server.ts
CHANGED
|
@@ -41,6 +41,8 @@ export interface NotifkitOptions {
|
|
|
41
41
|
};
|
|
42
42
|
dbOptions?: {
|
|
43
43
|
maxConnections?: number;
|
|
44
|
+
/** PostgreSQL schema for notifkit's tables. Same as `DB_SCHEMA`; defaults to `public`. */
|
|
45
|
+
schema?: string;
|
|
44
46
|
};
|
|
45
47
|
}
|
|
46
48
|
|
|
@@ -102,6 +104,7 @@ export class NotifkitServer extends EventEmitter {
|
|
|
102
104
|
process.env.QUEUE_MAX_LEN = String(this.options.redisOptions.maxQueueLength);
|
|
103
105
|
if (this.options.dbOptions?.maxConnections)
|
|
104
106
|
process.env.DB_MAX_CONNECTIONS = String(this.options.dbOptions.maxConnections);
|
|
107
|
+
if (this.options.dbOptions?.schema) process.env.DB_SCHEMA = this.options.dbOptions.schema;
|
|
105
108
|
|
|
106
109
|
const isProduction = process.env.NODE_ENV === "production";
|
|
107
110
|
|