@dudousxd/nestjs-durable-store-drizzle 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ __export(src_exports, {
29
29
  durableSchedules: () => durableSchedules,
30
30
  durableSchema: () => durableSchema,
31
31
  runAttributes: () => runAttributes,
32
+ signalIds: () => signalIds,
32
33
  signalWaiters: () => signalWaiters,
33
34
  stepCheckpoints: () => stepCheckpoints,
34
35
  workflowRuns: () => workflowRuns
@@ -208,6 +209,20 @@ var bufferedEvents = (0, import_sqlite_core.sqliteTable)("durable_buffered_event
208
209
  }),
209
210
  publishedAt: (0, import_sqlite_core.integer)("published_at").notNull()
210
211
  });
212
+ var signalIds = (0, import_sqlite_core.sqliteTable)("durable_signal_ids", {
213
+ token: (0, import_sqlite_core.text)("token").notNull(),
214
+ signalId: (0, import_sqlite_core.text)("signal_id").notNull(),
215
+ expiresAt: (0, import_sqlite_core.integer)("expires_at").notNull()
216
+ }, (t) => [
217
+ (0, import_sqlite_core.primaryKey)({
218
+ columns: [
219
+ t.token,
220
+ t.signalId
221
+ ]
222
+ }),
223
+ // The sweep's "expired by T" scan.
224
+ (0, import_sqlite_core.index)("durable_signal_ids_expires_at_idx").on(t.expiresAt)
225
+ ]);
211
226
  var durableSchedules = (0, import_sqlite_core.sqliteTable)(
212
227
  "durable_schedules",
213
228
  {
@@ -242,6 +257,7 @@ var durableSchema = {
242
257
  signalWaiters,
243
258
  bufferedSignals,
244
259
  bufferedEvents,
260
+ signalIds,
245
261
  durableSchedules
246
262
  };
247
263
  function durableManagedTables() {
@@ -977,6 +993,51 @@ var DrizzleStateStore = class {
977
993
  payload: row.payload ?? void 0
978
994
  };
979
995
  }
996
+ async takeOldestBufferedSignal(tokens) {
997
+ if (tokens.length === 0) return null;
998
+ const oldest = this.db.select({
999
+ id: bufferedSignals.id
1000
+ }).from(bufferedSignals).where((0, import_drizzle_orm2.inArray)(bufferedSignals.token, [
1001
+ ...new Set(tokens)
1002
+ ])).orderBy((0, import_drizzle_orm2.asc)(bufferedSignals.id)).limit(1);
1003
+ const rows = await this.db.delete(bufferedSignals).where((0, import_drizzle_orm2.inArray)(bufferedSignals.id, oldest)).returning({
1004
+ token: bufferedSignals.token,
1005
+ payload: bufferedSignals.payload
1006
+ });
1007
+ const row = rows[0];
1008
+ return row ? {
1009
+ token: row.token,
1010
+ payload: row.payload ?? void 0
1011
+ } : null;
1012
+ }
1013
+ async claimSignalId(token, id, expiresAt, nowMs) {
1014
+ const rows = await this.db.insert(signalIds).values({
1015
+ token,
1016
+ signalId: id,
1017
+ expiresAt
1018
+ }).onConflictDoUpdate({
1019
+ target: [
1020
+ signalIds.token,
1021
+ signalIds.signalId
1022
+ ],
1023
+ set: {
1024
+ expiresAt
1025
+ },
1026
+ setWhere: (0, import_drizzle_orm2.lte)(signalIds.expiresAt, nowMs)
1027
+ }).returning({
1028
+ token: signalIds.token
1029
+ });
1030
+ return rows.length > 0;
1031
+ }
1032
+ async releaseSignalId(token, id) {
1033
+ await this.db.delete(signalIds).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(signalIds.token, token), (0, import_drizzle_orm2.eq)(signalIds.signalId, id)));
1034
+ }
1035
+ async deleteExpiredSignalIds(nowMs, limit) {
1036
+ const rows = await this.db.delete(signalIds).where(import_drizzle_orm2.sql`(${signalIds.token}, ${signalIds.signalId}) in (select ${signalIds.token}, ${signalIds.signalId} from ${signalIds} where ${signalIds.expiresAt} <= ${nowMs} limit ${limit})`).returning({
1037
+ token: signalIds.token
1038
+ });
1039
+ return rows.length;
1040
+ }
980
1041
  async listBufferedSignals(tokenPrefix, afterId, limit) {
981
1042
  const rows = await this.db.select().from(bufferedSignals).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.like)(bufferedSignals.token, `${tokenPrefix}%`), afterId !== void 0 ? (0, import_drizzle_orm2.gt)(bufferedSignals.id, Number(afterId)) : void 0)).orderBy((0, import_drizzle_orm2.asc)(bufferedSignals.id)).limit(limit);
982
1043
  return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
@@ -1251,6 +1312,7 @@ __name(excludedSet, "excludedSet");
1251
1312
  durableSchedules,
1252
1313
  durableSchema,
1253
1314
  runAttributes,
1315
+ signalIds,
1254
1316
  signalWaiters,
1255
1317
  stepCheckpoints,
1256
1318
  workflowRuns