@unchainedshop/core-events 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-events",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"main": "lib/events-index.js",
|
|
5
5
|
"types": "lib/events-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -27,14 +27,17 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@unchainedshop/
|
|
31
|
-
"@unchainedshop/utils": "latest",
|
|
30
|
+
"@unchainedshop/utils": "1.1.3",
|
|
32
31
|
"simpl-schema": "^1.12.2"
|
|
33
32
|
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@unchainedshop/events": "1.1.3"
|
|
35
|
+
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"@types/chai": "^4.3.1",
|
|
36
38
|
"@types/mocha": "^9.1.1",
|
|
37
|
-
"@unchainedshop/
|
|
39
|
+
"@unchainedshop/events": "1.1.3",
|
|
40
|
+
"@unchainedshop/types": "^1.1.2",
|
|
38
41
|
"chai": "^4.3.6",
|
|
39
42
|
"cross-env": "^7.0.3",
|
|
40
43
|
"isomorphic-unfetch": "^3.1.0",
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Db } from '@unchainedshop/types/common';
|
|
2
|
+
import { Event } from '@unchainedshop/types/events';
|
|
3
|
+
import { buildDbIndexes } from '@unchainedshop/utils';
|
|
4
|
+
|
|
5
|
+
const TWO_DAYS_SEC = 172800;
|
|
6
|
+
|
|
7
|
+
export const EventsCollection = async (db: Db) => {
|
|
8
|
+
const Events = db.collection<Event>('events');
|
|
9
|
+
|
|
10
|
+
await buildDbIndexes(Events, [
|
|
11
|
+
{
|
|
12
|
+
index: { created: -1 },
|
|
13
|
+
options: { expireAfterSeconds: TWO_DAYS_SEC, name: 'created' },
|
|
14
|
+
},
|
|
15
|
+
{ index: { type: 1 }, options: { name: 'type' } },
|
|
16
|
+
{
|
|
17
|
+
index: { _id: 'text', type: 'text' },
|
|
18
|
+
options: {
|
|
19
|
+
weights: {
|
|
20
|
+
_id: 8,
|
|
21
|
+
type: 4,
|
|
22
|
+
},
|
|
23
|
+
name: 'events_fulltext_search',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
return Events;
|
|
29
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { configureEventsModule } from './module/configureEventsModule';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ModuleMutations } from '@unchainedshop/types/common';
|
|
2
|
+
import { Event, EmitAdapter } from '@unchainedshop/types/events';
|
|
3
|
+
import { getEmitHistoryAdapter, setEmitHistoryAdapter } from '@unchainedshop/events';
|
|
4
|
+
|
|
5
|
+
export const configureEventHistoryAdapter = (mutations: ModuleMutations<Event>) => {
|
|
6
|
+
if (!getEmitHistoryAdapter()) {
|
|
7
|
+
const adapter: EmitAdapter = {
|
|
8
|
+
subscribe: () => {
|
|
9
|
+
// Do nothing
|
|
10
|
+
},
|
|
11
|
+
publish: async (eventName, { payload, context = {} }) => {
|
|
12
|
+
await mutations.create({
|
|
13
|
+
type: eventName,
|
|
14
|
+
payload,
|
|
15
|
+
context,
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
setEmitHistoryAdapter(adapter);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/utils';
|
|
2
|
+
import { ModuleInput, ModuleMutations, Filter } from '@unchainedshop/types/common';
|
|
3
|
+
import { Event, EventQuery, EventsModule } from '@unchainedshop/types/events';
|
|
4
|
+
import { getRegisteredEvents } from '@unchainedshop/events';
|
|
5
|
+
import { EventsCollection } from '../db/EventsCollection';
|
|
6
|
+
import { EventsSchema } from '../db/EventsSchema';
|
|
7
|
+
import { configureEventHistoryAdapter } from './configureEventHistoryAdapter';
|
|
8
|
+
|
|
9
|
+
const buildFindSelector = ({ types, queryString, created }: EventQuery) => {
|
|
10
|
+
const selector: { type?: any; $text?: any; created?: any } = {};
|
|
11
|
+
|
|
12
|
+
if (types && Array.isArray(types)) selector.type = { $in: types };
|
|
13
|
+
if (queryString) selector.$text = { $search: queryString };
|
|
14
|
+
if (created) selector.created = { $gte: created };
|
|
15
|
+
return selector;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const configureEventsModule = async ({
|
|
19
|
+
db,
|
|
20
|
+
}: ModuleInput<Record<string, never>>): Promise<EventsModule> => {
|
|
21
|
+
const Events = await EventsCollection(db);
|
|
22
|
+
|
|
23
|
+
const mutations = generateDbMutations<Event>(Events, EventsSchema, {
|
|
24
|
+
hasCreateOnly: true,
|
|
25
|
+
}) as ModuleMutations<Event>;
|
|
26
|
+
|
|
27
|
+
configureEventHistoryAdapter(mutations);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
...mutations,
|
|
31
|
+
findEvent: async ({ eventId, ...rest }, options) => {
|
|
32
|
+
const selector = eventId ? generateDbFilterById(eventId) : rest;
|
|
33
|
+
return Events.findOne(selector as unknown as Filter<Event>, options);
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
findEvents: async ({ limit, offset, sort, ...query }) => {
|
|
37
|
+
return Events.find(buildFindSelector(query), {
|
|
38
|
+
skip: offset,
|
|
39
|
+
limit,
|
|
40
|
+
sort: buildSortOptions(sort || [{ key: 'created', value: 'DESC' }]),
|
|
41
|
+
}).toArray();
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
type: (event) => {
|
|
45
|
+
if (getRegisteredEvents().includes(event.type)) {
|
|
46
|
+
return event.type;
|
|
47
|
+
}
|
|
48
|
+
return 'UNKNOWN';
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
count: async (query) => {
|
|
52
|
+
const count = await Events.countDocuments(buildFindSelector(query));
|
|
53
|
+
return count;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|