@raymanselltickets/common 1.0.12 → 1.0.14

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.
@@ -3,6 +3,7 @@ export interface OrderCancelledEvent {
3
3
  subject: Subjects.OrderCancelled;
4
4
  data: {
5
5
  id: string;
6
+ version: number;
6
7
  ticket: {
7
8
  id: string;
8
9
  };
@@ -4,6 +4,7 @@ export interface OrderCreatedEvent {
4
4
  subject: Subjects.OrderCreated;
5
5
  data: {
6
6
  id: string;
7
+ version: number;
7
8
  status: OrderStatus;
8
9
  userId: string;
9
10
  expiresAt: string;
@@ -3,6 +3,7 @@ export interface TicketCreatedEvent {
3
3
  subject: Subjects.TicketCreated;
4
4
  data: {
5
5
  id: string;
6
+ version: number;
6
7
  title: string;
7
8
  price: number;
8
9
  userId: string;
@@ -3,6 +3,7 @@ export interface TicketUpdatedEvent {
3
3
  subject: Subjects.TicketUpdated;
4
4
  data: {
5
5
  id: string;
6
+ version: number;
6
7
  title: string;
7
8
  price: number;
8
9
  userId: string;
package/build/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export * from './events/ticket-updated-event.js';
16
16
  export * from './events/types/order-status.js';
17
17
  export * from './events/order-cancelled-event.js';
18
18
  export * from './events/order-created-event.js';
19
+ export * from './util/occ.js';
package/build/index.js CHANGED
@@ -16,3 +16,4 @@ export * from './events/ticket-updated-event.js';
16
16
  export * from './events/types/order-status.js';
17
17
  export * from './events/order-cancelled-event.js';
18
18
  export * from './events/order-created-event.js';
19
+ export * from './util/occ.js';
@@ -0,0 +1,7 @@
1
+ import { Schema } from 'mongoose';
2
+ /**
3
+ * Implement optimistic concurrency control using a schema's version key.
4
+ *
5
+ * @param {mongoose.Schema} schema - A Mongoose schema to be plugged into.
6
+ */
7
+ export declare function versionOCCPlugin(schema: Schema): void;
@@ -0,0 +1,21 @@
1
+ import assert from 'node:assert';
2
+ /**
3
+ * Implement optimistic concurrency control using a schema's version key.
4
+ *
5
+ * @param {mongoose.Schema} schema - A Mongoose schema to be plugged into.
6
+ */
7
+ export function versionOCCPlugin(schema) {
8
+ // Get version key name
9
+ const versionKey = schema.get('versionKey');
10
+ assert(versionKey, 'document schema must have a version key');
11
+ // Add pre-save hook to check version
12
+ schema.pre('save', function () {
13
+ // Condition the save on the versions matching
14
+ this.$where = {
15
+ ...this.$where,
16
+ [versionKey]: this.get(versionKey),
17
+ };
18
+ // Increment the version atomically
19
+ this.increment();
20
+ });
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raymanselltickets/common",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -31,6 +31,7 @@
31
31
  "cookie-session": "^2.1.1",
32
32
  "express": "^5.2.1",
33
33
  "express-validator": "^7.3.1",
34
- "jsonwebtoken": "^9.0.3"
34
+ "jsonwebtoken": "^9.0.3",
35
+ "mongoose": "^9.1.2"
35
36
  }
36
37
  }