@unchainedshop/core-events 1.1.3
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 +3 -0
- package/lib/db/EventsCollection.d.ts +3 -0
- package/lib/db/EventsCollection.js +24 -0
- package/lib/db/EventsCollection.js.map +1 -0
- package/lib/db/EventsSchema.d.ts +1 -0
- package/lib/db/EventsSchema.js +9 -0
- package/lib/db/EventsSchema.js.map +1 -0
- package/lib/events-index.d.ts +1 -0
- package/lib/events-index.js +2 -0
- package/lib/events-index.js.map +1 -0
- package/lib/module/configureEventHistoryAdapter.d.ts +3 -0
- package/lib/module/configureEventHistoryAdapter.js +19 -0
- package/lib/module/configureEventHistoryAdapter.js.map +1 -0
- package/lib/module/configureEventsModule.d.ts +3 -0
- package/lib/module/configureEventsModule.js +47 -0
- package/lib/module/configureEventsModule.js.map +1 -0
- package/package.json +44 -0
- package/tests/events-index.test.ts +33 -0
- package/tsconfig.build.json +26 -0
- package/tsconfig.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { buildDbIndexes } from '@unchainedshop/utils';
|
|
2
|
+
const TWO_DAYS_SEC = 172800;
|
|
3
|
+
export const EventsCollection = async (db) => {
|
|
4
|
+
const Events = db.collection('events');
|
|
5
|
+
await buildDbIndexes(Events, [
|
|
6
|
+
{
|
|
7
|
+
index: { created: -1 },
|
|
8
|
+
options: { expireAfterSeconds: TWO_DAYS_SEC, name: 'created' },
|
|
9
|
+
},
|
|
10
|
+
{ index: { type: 1 }, options: { name: 'type' } },
|
|
11
|
+
{
|
|
12
|
+
index: { _id: 'text', type: 'text' },
|
|
13
|
+
options: {
|
|
14
|
+
weights: {
|
|
15
|
+
_id: 8,
|
|
16
|
+
type: 4,
|
|
17
|
+
},
|
|
18
|
+
name: 'events_fulltext_search',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
]);
|
|
22
|
+
return Events;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=EventsCollection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventsCollection.js","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,MAAM,YAAY,GAAG,MAAM,CAAC;AAE5B,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,EAAE,EAAM,EAAE,EAAE;IAC/C,MAAM,MAAM,GAAG,EAAE,CAAC,UAAU,CAAQ,QAAQ,CAAC,CAAC;IAE9C,MAAM,cAAc,CAAC,MAAM,EAAE;QAC3B;YACE,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE;YACtB,OAAO,EAAE,EAAE,kBAAkB,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE;SAC/D;QACD,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QACjD;YACE,KAAK,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE;YACpC,OAAO,EAAE;gBACP,OAAO,EAAE;oBACP,GAAG,EAAE,CAAC;oBACN,IAAI,EAAE,CAAC;iBACR;gBACD,IAAI,EAAE,wBAAwB;aAC/B;SACF;KACF,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const EventsSchema: any;
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventsSchema.js","sourceRoot":"","sources":["../../src/db/EventsSchema.js"],"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { configureEventsModule } from './module/configureEventsModule';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events-index.js","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getEmitHistoryAdapter, setEmitHistoryAdapter } from '@unchainedshop/events';
|
|
2
|
+
export const configureEventHistoryAdapter = (mutations) => {
|
|
3
|
+
if (!getEmitHistoryAdapter()) {
|
|
4
|
+
const adapter = {
|
|
5
|
+
subscribe: () => {
|
|
6
|
+
// Do nothing
|
|
7
|
+
},
|
|
8
|
+
publish: async (eventName, { payload, context = {} }) => {
|
|
9
|
+
await mutations.create({
|
|
10
|
+
type: eventName,
|
|
11
|
+
payload,
|
|
12
|
+
context,
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
setEmitHistoryAdapter(adapter);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=configureEventHistoryAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configureEventHistoryAdapter.js","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAErF,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,SAAiC,EAAE,EAAE;IAChF,IAAI,CAAC,qBAAqB,EAAE,EAAE;QAC5B,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;KAChC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/utils';
|
|
2
|
+
import { getRegisteredEvents } from '@unchainedshop/events';
|
|
3
|
+
import { EventsCollection } from '../db/EventsCollection';
|
|
4
|
+
import { EventsSchema } from '../db/EventsSchema';
|
|
5
|
+
import { configureEventHistoryAdapter } from './configureEventHistoryAdapter';
|
|
6
|
+
const buildFindSelector = ({ types, queryString, created }) => {
|
|
7
|
+
const selector = {};
|
|
8
|
+
if (types && Array.isArray(types))
|
|
9
|
+
selector.type = { $in: types };
|
|
10
|
+
if (queryString)
|
|
11
|
+
selector.$text = { $search: queryString };
|
|
12
|
+
if (created)
|
|
13
|
+
selector.created = { $gte: created };
|
|
14
|
+
return selector;
|
|
15
|
+
};
|
|
16
|
+
export const configureEventsModule = async ({ db, }) => {
|
|
17
|
+
const Events = await EventsCollection(db);
|
|
18
|
+
const mutations = generateDbMutations(Events, EventsSchema, {
|
|
19
|
+
hasCreateOnly: true,
|
|
20
|
+
});
|
|
21
|
+
configureEventHistoryAdapter(mutations);
|
|
22
|
+
return {
|
|
23
|
+
...mutations,
|
|
24
|
+
findEvent: async ({ eventId, ...rest }, options) => {
|
|
25
|
+
const selector = eventId ? generateDbFilterById(eventId) : rest;
|
|
26
|
+
return Events.findOne(selector, options);
|
|
27
|
+
},
|
|
28
|
+
findEvents: async ({ limit, offset, sort, ...query }) => {
|
|
29
|
+
return Events.find(buildFindSelector(query), {
|
|
30
|
+
skip: offset,
|
|
31
|
+
limit,
|
|
32
|
+
sort: buildSortOptions(sort || [{ key: 'created', value: 'DESC' }]),
|
|
33
|
+
}).toArray();
|
|
34
|
+
},
|
|
35
|
+
type: (event) => {
|
|
36
|
+
if (getRegisteredEvents().includes(event.type)) {
|
|
37
|
+
return event.type;
|
|
38
|
+
}
|
|
39
|
+
return 'UNKNOWN';
|
|
40
|
+
},
|
|
41
|
+
count: async (query) => {
|
|
42
|
+
const count = await Events.countDocuments(buildFindSelector(query));
|
|
43
|
+
return count;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=configureEventsModule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configureEventsModule.js","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGnG,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CA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,OAAO,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE;gBAC3C,IAAI,EAAE,MAAM;gBACZ,KAAK;gBACL,IAAI,EAAE,gBAAgB,CAAC,IAAI,IAAI,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;aACpE,CAAC,CAAC,OAAO,EAAE,CAAC;QACf,CAAC;QAED,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YACd,IAAI,mBAAmB,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;gBAC9C,OAAO,KAAK,CAAC,IAAI,CAAC;aACnB;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;YACrB,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC;YACpE,OAAO,KAAK,CAAC;QACf,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unchainedshop/core-events",
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"main": "lib/events-index.js",
|
|
5
|
+
"types": "lib/events-index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rm -rf lib",
|
|
9
|
+
"build": "npm run clean && tsc -p tsconfig.build.json",
|
|
10
|
+
"watch": "tsc --watch",
|
|
11
|
+
"link:core": "npm link @unchainedshop/types",
|
|
12
|
+
"test": "cross-env METEOR_PACKAGE_DIRS=../ TEST_CLIENT=0 TEST_WATCH=1 meteor test-packages ./ --driver-package meteortesting:mocha --port 4212"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/unchainedshop/unchained.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"unchained",
|
|
20
|
+
"ecommerce",
|
|
21
|
+
"core"
|
|
22
|
+
],
|
|
23
|
+
"author": "Joël Meiller",
|
|
24
|
+
"license": "EUPL-1.2",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/unchainedshop/unchained/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@unchainedshop/events": "latest",
|
|
31
|
+
"@unchainedshop/utils": "latest",
|
|
32
|
+
"simpl-schema": "^1.12.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/chai": "^4.3.1",
|
|
36
|
+
"@types/mocha": "^9.1.1",
|
|
37
|
+
"@unchainedshop/types": "latest",
|
|
38
|
+
"chai": "^4.3.6",
|
|
39
|
+
"cross-env": "^7.0.3",
|
|
40
|
+
"isomorphic-unfetch": "^3.1.0",
|
|
41
|
+
"mongodb": "^4.4.1",
|
|
42
|
+
"typescript": "^4.7.4"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { assert } from 'chai';
|
|
2
|
+
import { initDb } from 'meteor/unchained:mongodb';
|
|
3
|
+
import { configureEventsModule } from '@unchainedshop/events';
|
|
4
|
+
import { emit, registerEvents, getEmitHistoryAdapter } from '@unchainedshop/events';
|
|
5
|
+
|
|
6
|
+
describe('Test exports', () => {
|
|
7
|
+
let module;
|
|
8
|
+
before(async () => {
|
|
9
|
+
const db = await initDb();
|
|
10
|
+
module = await configureEventsModule({ db });
|
|
11
|
+
|
|
12
|
+
registerEvents(['TEST EVENT']);
|
|
13
|
+
});
|
|
14
|
+
it('Configure Events', () => {
|
|
15
|
+
assert.isDefined(configureEventsModule);
|
|
16
|
+
assert.ok(module);
|
|
17
|
+
assert.isFunction(module.findEvent);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('Test event history adapter', async () => {
|
|
21
|
+
assert.ok(getEmitHistoryAdapter());
|
|
22
|
+
|
|
23
|
+
emit('TEST EVENT', { orderId: 'Order1234' });
|
|
24
|
+
|
|
25
|
+
const events = await module.findEvents({
|
|
26
|
+
limit: 10,
|
|
27
|
+
offset: 0,
|
|
28
|
+
type: 'TEST EVENT',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
assert.isAtLeast(events.length, 1)
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"allowSyntheticDefaultImports": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"experimentalDecorators": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"lib": ["esnext"],
|
|
10
|
+
"module": "esnext",
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"noUnusedLocals": false,
|
|
14
|
+
"outDir": "lib",
|
|
15
|
+
"preserveWatchOutput": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"sourceMap": true,
|
|
18
|
+
"target": "esnext",
|
|
19
|
+
"types": ["node"],
|
|
20
|
+
"baseUrl": ".", // This must be specified if "paths" is.
|
|
21
|
+
"paths": {
|
|
22
|
+
"meteor/unchained:*": ["node_modules/@unchainedshop/types/index.d.ts"]
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"include": ["src"]
|
|
26
|
+
}
|