@unchainedshop/core-events 3.0.0-alpha1 → 3.0.0-alpha3

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.
@@ -1,5 +1,5 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
- import { TimestampFields } from '@unchainedshop/types/common.js';
2
+ import type { TimestampFields } from '@unchainedshop/mongodb';
3
3
  export type Event = {
4
4
  _id?: string;
5
5
  type: string;
@@ -7,3 +7,4 @@ export type Event = {
7
7
  payload?: Record<string, unknown>;
8
8
  } & TimestampFields;
9
9
  export declare const EventsCollection: (db: mongodb.Db) => Promise<mongodb.Collection<Event>>;
10
+ //# sourceMappingURL=EventsCollection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventsCollection.d.ts","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAI9D,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,GAAG,eAAe,CAAC;AAEpB,eAAO,MAAM,gBAAgB,OAAc,OAAO,CAAC,EAAE,uCAsBpD,CAAC"}
@@ -1,2 +1,3 @@
1
1
  export type { Event } from './db/EventsCollection.js';
2
- export { configureEventsModule } from './module/configureEventsModule.js';
2
+ export * from './module/configureEventsModule.js';
3
+ //# sourceMappingURL=events-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events-index.d.ts","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AAEtD,cAAc,mCAAmC,CAAC"}
@@ -1,2 +1,2 @@
1
- export { configureEventsModule } from './module/configureEventsModule.js';
1
+ export * from './module/configureEventsModule.js';
2
2
  //# sourceMappingURL=events-index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"events-index.js","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC"}
1
+ {"version":3,"file":"events-index.js","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAEA,cAAc,mCAAmC,CAAC"}
@@ -1,3 +1,5 @@
1
- import { ModuleMutations } from '@unchainedshop/types/core.js';
2
- import { Event } from '../db/EventsCollection.js';
3
- export declare const configureEventHistoryAdapter: (mutations: ModuleMutations<Event>) => void;
1
+ import { RawPayloadType } from '@unchainedshop/events/EventDirector.js';
2
+ export declare const configureEventHistoryAdapter: (createFn: ({ type, payload, context }: RawPayloadType<any> & {
3
+ type: string;
4
+ }) => Promise<unknown>) => void;
5
+ //# sourceMappingURL=configureEventHistoryAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"configureEventHistoryAdapter.d.ts","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAExE,eAAO,MAAM,4BAA4B,aAC7B,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,cAAc,CAAC,GAAG,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,OAAO,CAAC,SAiBnG,CAAC"}
@@ -1,12 +1,12 @@
1
1
  import { getEmitHistoryAdapter, setEmitHistoryAdapter } from '@unchainedshop/events';
2
- export const configureEventHistoryAdapter = (mutations) => {
2
+ export const configureEventHistoryAdapter = (createFn) => {
3
3
  if (!getEmitHistoryAdapter()) {
4
4
  const adapter = {
5
5
  subscribe: () => {
6
6
  // Do nothing
7
7
  },
8
8
  publish: async (eventName, { payload, context = {} }) => {
9
- await mutations.create({
9
+ await createFn({
10
10
  type: eventName,
11
11
  payload,
12
12
  context,
@@ -1 +1 @@
1
- {"version":3,"file":"configureEventHistoryAdapter.js","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAGlG,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,SAAiC,EAAE,EAAE;IAChF,IAAI,CAAC,qBAAqB,EAAE,EAAE,CAAC;QAC7B,MAAM,OAAO,GAAgB;YAC3B,SAAS,EAAE,GAAG,EAAE;gBACd,aAAa;YACf,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,EAAE,EAAE,EAAE;gBACtD,MAAM,SAAS,CAAC,MAAM,CAAC;oBACrB,IAAI,EAAE,SAAS;oBACf,OAAO;oBACP,OAAO;iBACR,CAAC,CAAC;YACL,CAAC;SACF,CAAC;QACF,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;AACH,CAAC,CAAC"}
1
+ {"version":3,"file":"configureEventHistoryAdapter.js","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAGlG,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,QAAkG,EAClG,EAAE;IACF,IAAI,CAAC,qBAAqB,EAAE,EAAE,CAAC;QAC7B,MAAM,OAAO,GAAgB;YAC3B,SAAS,EAAE,GAAG,EAAE;gBACd,aAAa;YACf,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,EAAE,EAAE,EAAE;gBACtD,MAAM,QAAQ,CAAC;oBACb,IAAI,EAAE,SAAS;oBACf,OAAO;oBACP,OAAO;iBACR,CAAC,CAAC;YACL,CAAC;SACF,CAAC;QACF,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;AACH,CAAC,CAAC"}
@@ -1,7 +1,11 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
- import { SortOption } from '@unchainedshop/types/api.js';
3
- import { ModuleCreateMutation, ModuleInput } from '@unchainedshop/types/core.js';
2
+ import { SortOption } from '@unchainedshop/utils';
3
+ import { ModuleCreateMutation, ModuleInput } from '@unchainedshop/core';
4
4
  import { Event } from '../db/EventsCollection.js';
5
+ export type EventReport = {
6
+ emitCount: number;
7
+ type: string;
8
+ };
5
9
  export type EventQuery = {
6
10
  types?: Array<string>;
7
11
  queryString?: string;
@@ -23,5 +27,11 @@ export interface EventsModule extends ModuleCreateMutation<Event> {
23
27
  }, options?: mongodb.FindOptions) => Promise<Array<Event>>;
24
28
  type: (event: Event) => string;
25
29
  count: (query: EventQuery) => Promise<number>;
30
+ getReport: (params?: {
31
+ from?: Date;
32
+ to?: Date;
33
+ types?: string[];
34
+ }) => Promise<EventReport[]>;
26
35
  }
27
36
  export declare const configureEventsModule: ({ db, }: ModuleInput<Record<string, never>>) => Promise<EventsModule>;
37
+ //# sourceMappingURL=configureEventsModule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"configureEventsModule.d.ts","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAItD,OAAO,EAAiB,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAoB,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAGpE,MAAM,MAAM,WAAW,GAAG;IACxB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YAE/B,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,KAAG,OAAO,CAAC,YAAY,CAoG3D,CAAC"}
@@ -1,8 +1,7 @@
1
- import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/mongodb';
1
+ import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
2
2
  import { getRegisteredEvents } from '@unchainedshop/events';
3
- import { SortDirection } from '@unchainedshop/types/api.js';
3
+ import { SortDirection } from '@unchainedshop/utils';
4
4
  import { EventsCollection } from '../db/EventsCollection.js';
5
- import { EventsSchema } from '../db/EventsSchema.js';
6
5
  import { configureEventHistoryAdapter } from './configureEventHistoryAdapter.js';
7
6
  export const buildFindSelector = ({ types, queryString, created }) => {
8
7
  const selector = {};
@@ -16,12 +15,17 @@ export const buildFindSelector = ({ types, queryString, created }) => {
16
15
  };
17
16
  export const configureEventsModule = async ({ db, }) => {
18
17
  const Events = await EventsCollection(db);
19
- const mutations = generateDbMutations(Events, EventsSchema, {
20
- hasCreateOnly: true,
21
- });
22
- configureEventHistoryAdapter(mutations);
18
+ const create = async (doc) => {
19
+ const result = await Events.insertOne({
20
+ _id: generateDbObjectId(),
21
+ created: new Date(),
22
+ ...doc,
23
+ });
24
+ return result.insertedId;
25
+ };
26
+ configureEventHistoryAdapter(create);
23
27
  return {
24
- ...mutations,
28
+ create,
25
29
  findEvent: async ({ eventId, ...rest }, options) => {
26
30
  const selector = eventId ? generateDbFilterById(eventId) : rest;
27
31
  return Events.findOne(selector, options);
@@ -44,6 +48,58 @@ export const configureEventsModule = async ({ db, }) => {
44
48
  const count = await Events.countDocuments(buildFindSelector(query));
45
49
  return count;
46
50
  },
51
+ getReport: async ({ from, to, types } = { from: null, to: null, types: null }) => {
52
+ const pipeline = [];
53
+ const matchConditions = [];
54
+ // build date filter based on provided values it can be a range if both to and from is supplied
55
+ // a upper or lowe limit if either from or to is provided
56
+ // or all if none is provided
57
+ if (from || to) {
58
+ const dateConditions = [];
59
+ if (from) {
60
+ const fromDate = new Date(from);
61
+ dateConditions.push({
62
+ $or: [{ created: { $gte: fromDate } }, { updated: { $gte: fromDate } }],
63
+ });
64
+ }
65
+ if (to) {
66
+ const toDate = new Date(to);
67
+ dateConditions.push({
68
+ $or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
69
+ });
70
+ }
71
+ if (dateConditions.length > 0) {
72
+ matchConditions.push({ $and: dateConditions });
73
+ }
74
+ }
75
+ // build types filter if type is provided or ignore types if it is not provided
76
+ if (types && Array.isArray(types) && types.length) {
77
+ matchConditions.push({ type: { $in: types } });
78
+ }
79
+ if (matchConditions.length > 0) {
80
+ pipeline.push({
81
+ $match: {
82
+ $and: matchConditions,
83
+ },
84
+ });
85
+ }
86
+ pipeline.push(...[
87
+ {
88
+ $group: {
89
+ _id: '$type',
90
+ emitCount: { $sum: 1 },
91
+ },
92
+ },
93
+ {
94
+ $project: {
95
+ _id: 0,
96
+ type: '$_id',
97
+ emitCount: 1,
98
+ },
99
+ },
100
+ ]);
101
+ return Events.aggregate(pipeline).toArray();
102
+ },
47
103
  };
48
104
  };
49
105
  //# sourceMappingURL=configureEventsModule.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"configureEventsModule.js","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAc,MAAM,6BAA6B,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAS,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AAQjF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,EAAE,KAAK,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AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/core-events",
3
- "version": "3.0.0-alpha1",
3
+ "version": "3.0.0-alpha3",
4
4
  "main": "lib/events-index.js",
5
5
  "exports": {
6
6
  ".": "./lib/events-index.js",
@@ -10,7 +10,7 @@
10
10
  "type": "module",
11
11
  "scripts": {
12
12
  "clean": "rm -rf lib",
13
- "build": "npm run clean && tsc",
13
+ "prepublishOnly": "npm run clean && tsc",
14
14
  "watch": "tsc -w",
15
15
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --forceExit",
16
16
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --detectOpenHandles --forceExit"
@@ -31,15 +31,13 @@
31
31
  },
32
32
  "homepage": "https://github.com/unchainedshop/unchained#readme",
33
33
  "dependencies": {
34
- "@unchainedshop/events": "^3.0.0-alpha1",
35
- "@unchainedshop/utils": "^3.0.0-alpha1",
36
- "simpl-schema": "^3.4.6"
34
+ "@unchainedshop/events": "^3.0.0-alpha3",
35
+ "@unchainedshop/utils": "^3.0.0-alpha3"
37
36
  },
38
37
  "devDependencies": {
39
- "@types/node": "^20.14.2",
40
- "@unchainedshop/types": "^3.0.0-alpha1",
38
+ "@types/node": "^22.5.5",
41
39
  "jest": "^29.7.0",
42
- "ts-jest": "^29.1.4",
43
- "typescript": "^5.4.5"
40
+ "ts-jest": "^29.2.5",
41
+ "typescript": "^5.6.2"
44
42
  }
45
43
  }
@@ -1,6 +1,6 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
2
  import { buildDbIndexes } from '@unchainedshop/mongodb';
3
- import { TimestampFields } from '@unchainedshop/types/common.js';
3
+ import type { TimestampFields } from '@unchainedshop/mongodb';
4
4
 
5
5
  const TWO_DAYS_SEC = 172800;
6
6
 
@@ -1,3 +1,3 @@
1
1
  export type { Event } from './db/EventsCollection.js';
2
2
 
3
- export { configureEventsModule } from './module/configureEventsModule.js';
3
+ export * from './module/configureEventsModule.js';
@@ -1,15 +1,16 @@
1
- import { ModuleMutations } from '@unchainedshop/types/core.js';
2
1
  import { getEmitHistoryAdapter, setEmitHistoryAdapter, EmitAdapter } from '@unchainedshop/events';
3
- import { Event } from '../db/EventsCollection.js';
2
+ import { RawPayloadType } from '@unchainedshop/events/EventDirector.js';
4
3
 
5
- export const configureEventHistoryAdapter = (mutations: ModuleMutations<Event>) => {
4
+ export const configureEventHistoryAdapter = (
5
+ createFn: ({ type, payload, context }: RawPayloadType<any> & { type: string }) => Promise<unknown>,
6
+ ) => {
6
7
  if (!getEmitHistoryAdapter()) {
7
8
  const adapter: EmitAdapter = {
8
9
  subscribe: () => {
9
10
  // Do nothing
10
11
  },
11
12
  publish: async (eventName, { payload, context = {} }) => {
12
- await mutations.create({
13
+ await createFn({
13
14
  type: eventName,
14
15
  payload,
15
16
  context,
@@ -1,13 +1,17 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
2
 
3
- import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/mongodb';
3
+ import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
4
4
  import { getRegisteredEvents } from '@unchainedshop/events';
5
- import { SortDirection, SortOption } from '@unchainedshop/types/api.js';
6
- import { ModuleCreateMutation, ModuleInput, ModuleMutations } from '@unchainedshop/types/core.js';
5
+ import { SortDirection, SortOption } from '@unchainedshop/utils';
6
+ import { ModuleCreateMutation, ModuleInput } from '@unchainedshop/core';
7
7
  import { EventsCollection, Event } from '../db/EventsCollection.js';
8
- import { EventsSchema } from '../db/EventsSchema.js';
9
8
  import { configureEventHistoryAdapter } from './configureEventHistoryAdapter.js';
10
9
 
10
+ export type EventReport = {
11
+ emitCount: number;
12
+ type: string;
13
+ };
14
+
11
15
  export type EventQuery = {
12
16
  types?: Array<string>;
13
17
  queryString?: string;
@@ -41,6 +45,7 @@ export interface EventsModule extends ModuleCreateMutation<Event> {
41
45
  type: (event: Event) => string;
42
46
 
43
47
  count: (query: EventQuery) => Promise<number>;
48
+ getReport: (params?: { from?: Date; to?: Date; types?: string[] }) => Promise<EventReport[]>;
44
49
  }
45
50
 
46
51
  export const configureEventsModule = async ({
@@ -48,14 +53,20 @@ export const configureEventsModule = async ({
48
53
  }: ModuleInput<Record<string, never>>): Promise<EventsModule> => {
49
54
  const Events = await EventsCollection(db);
50
55
 
51
- const mutations = generateDbMutations<Event>(Events, EventsSchema, {
52
- hasCreateOnly: true,
53
- }) as ModuleMutations<Event>;
56
+ const create = async (doc) => {
57
+ const result = await Events.insertOne({
58
+ _id: generateDbObjectId(),
59
+ created: new Date(),
60
+ ...doc,
61
+ });
62
+ return result.insertedId as string;
63
+ };
54
64
 
55
- configureEventHistoryAdapter(mutations);
65
+ configureEventHistoryAdapter(create);
56
66
 
57
67
  return {
58
- ...mutations,
68
+ create,
69
+
59
70
  findEvent: async ({ eventId, ...rest }, options) => {
60
71
  const selector = eventId ? generateDbFilterById<Event>(eventId) : rest;
61
72
  return Events.findOne(selector, options);
@@ -81,5 +92,62 @@ export const configureEventsModule = async ({
81
92
  const count = await Events.countDocuments(buildFindSelector(query));
82
93
  return count;
83
94
  },
95
+
96
+ getReport: async ({ from, to, types } = { from: null, to: null, types: null }) => {
97
+ const pipeline = [];
98
+ const matchConditions = [];
99
+ // build date filter based on provided values it can be a range if both to and from is supplied
100
+ // a upper or lowe limit if either from or to is provided
101
+ // or all if none is provided
102
+ if (from || to) {
103
+ const dateConditions = [];
104
+ if (from) {
105
+ const fromDate = new Date(from);
106
+ dateConditions.push({
107
+ $or: [{ created: { $gte: fromDate } }, { updated: { $gte: fromDate } }],
108
+ });
109
+ }
110
+ if (to) {
111
+ const toDate = new Date(to);
112
+ dateConditions.push({
113
+ $or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
114
+ });
115
+ }
116
+ if (dateConditions.length > 0) {
117
+ matchConditions.push({ $and: dateConditions });
118
+ }
119
+ }
120
+ // build types filter if type is provided or ignore types if it is not provided
121
+ if (types && Array.isArray(types) && types.length) {
122
+ matchConditions.push({ type: { $in: types } });
123
+ }
124
+ if (matchConditions.length > 0) {
125
+ pipeline.push({
126
+ $match: {
127
+ $and: matchConditions,
128
+ },
129
+ });
130
+ }
131
+
132
+ pipeline.push(
133
+ ...[
134
+ {
135
+ $group: {
136
+ _id: '$type',
137
+ emitCount: { $sum: 1 },
138
+ },
139
+ },
140
+ {
141
+ $project: {
142
+ _id: 0,
143
+ type: '$_id',
144
+ emitCount: 1,
145
+ },
146
+ },
147
+ ],
148
+ );
149
+
150
+ return Events.aggregate(pipeline).toArray() as Promise<EventReport[]>;
151
+ },
84
152
  };
85
153
  };
@@ -1,2 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export declare const EventsSchema: SimpleSchema;
@@ -1,9 +0,0 @@
1
- import { Schemas } from '@unchainedshop/utils';
2
- import SimpleSchema from 'simpl-schema';
3
- export const EventsSchema = new SimpleSchema({
4
- type: { type: String, required: true },
5
- payload: { type: Object, blackbox: true },
6
- context: { type: Object, blackbox: true },
7
- ...Schemas.timestampFields,
8
- }, { requiredByDefault: false });
9
- //# sourceMappingURL=EventsSchema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EventsSchema.js","sourceRoot":"","sources":["../../src/db/EventsSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,YAAY,CAC1C;IACE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACtC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACzC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACzC,GAAG,OAAO,CAAC,eAAe;CAC3B,EACD,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAC7B,CAAC"}
@@ -1,12 +0,0 @@
1
- import { Schemas } from '@unchainedshop/utils';
2
- import SimpleSchema from 'simpl-schema';
3
-
4
- export const EventsSchema = new SimpleSchema(
5
- {
6
- type: { type: String, required: true },
7
- payload: { type: Object, blackbox: true },
8
- context: { type: Object, blackbox: true },
9
- ...Schemas.timestampFields,
10
- },
11
- { requiredByDefault: false },
12
- );