@rsdk/kafka.producer.outbox.knex 1.0.13
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/CHANGELOG.md +24 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/knex-outbox.producer.d.ts +5 -0
- package/dist/knex-outbox.producer.js +42 -0
- package/dist/knex-outbox.producer.js.map +1 -0
- package/dist/knex.producer.d.ts +10 -0
- package/dist/knex.producer.js +46 -0
- package/dist/knex.producer.js.map +1 -0
- package/dist/metadata/autodoc.resolver.d.ts +8 -0
- package/dist/metadata/autodoc.resolver.js +23 -0
- package/dist/metadata/autodoc.resolver.js.map +1 -0
- package/dist/metadata/constants.d.ts +1 -0
- package/dist/metadata/constants.js +5 -0
- package/dist/metadata/constants.js.map +1 -0
- package/dist/metadata/types.d.ts +5 -0
- package/dist/metadata/types.js +3 -0
- package/dist/metadata/types.js.map +1 -0
- package/package.json +30 -0
- package/src/index.ts +1 -0
- package/src/knex-outbox.producer.ts +51 -0
- package/src/knex.producer.ts +28 -0
- package/src/metadata/autodoc.resolver.ts +36 -0
- package/src/metadata/constants.ts +1 -0
- package/src/metadata/types.ts +6 -0
- package/tsconfig.json +9 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## 1.0.13 (2023-06-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @rsdk/kafka.producer.outbox.knex
|
|
9
|
+
|
|
10
|
+
## 1.0.12 (2023-05-23)
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* remove private dependencies from `peerDependencies` section ([0bce6ff](https://github.com/R-Vision/rsdk/commit/0bce6ffb5b699e2ed1dc9aca77cbdbf085267ff7))
|
|
15
|
+
|
|
16
|
+
## 1.0.11 (2023-05-23)
|
|
17
|
+
|
|
18
|
+
**Note:** Version bump only for package @rsdk/kafka.producer.outbox.knex
|
|
19
|
+
|
|
20
|
+
## 1.0.10 (2023-05-22)
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* add "access: public" to new packages ([04d5983](https://github.com/R-Vision/rsdk/commit/04d5983cfa3e97c5d8219e07208a0499a8c6bc83))
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { KnexOutboxProducer } from './knex-outbox.producer';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KnexOutboxProducer = void 0;
|
|
4
|
+
var knex_outbox_producer_1 = require("./knex-outbox.producer");
|
|
5
|
+
Object.defineProperty(exports, "KnexOutboxProducer", { enumerable: true, get: function () { return knex_outbox_producer_1.KnexOutboxProducer; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+DAA4D;AAAnD,0HAAA,kBAAkB,OAAA"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.KnexOutboxProducer = void 0;
|
|
7
|
+
const autodoc_protocol_1 = require("@rsdk/autodoc.protocol");
|
|
8
|
+
const db_knex_1 = require("@rsdk/db.knex");
|
|
9
|
+
const metadata_1 = require("@rsdk/metadata");
|
|
10
|
+
const omit_1 = __importDefault(require("lodash/omit"));
|
|
11
|
+
const autodoc_resolver_1 = require("./metadata/autodoc.resolver");
|
|
12
|
+
const constants_1 = require("./metadata/constants");
|
|
13
|
+
const knex_producer_1 = require("./knex.producer");
|
|
14
|
+
class KnexOutboxProducer {
|
|
15
|
+
static forEvent(eventType) {
|
|
16
|
+
const eventTypes = Array.isArray(eventType) ? eventType : [eventType];
|
|
17
|
+
const providers = eventTypes.map((eventType) => ({
|
|
18
|
+
inject: [db_knex_1.KNEX_INJECTION_TOKEN],
|
|
19
|
+
provide: eventType.$type,
|
|
20
|
+
useFactory: (knex) => {
|
|
21
|
+
return new knex_producer_1.KnexProducer(eventType, knex);
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
const moduleDef = {
|
|
25
|
+
providers,
|
|
26
|
+
module: KnexOutboxProducer,
|
|
27
|
+
exports: providers,
|
|
28
|
+
};
|
|
29
|
+
const rsdkMetadata = new metadata_1.RsdkMetadata(moduleDef, constants_1.KNEX_OUTBOX_RSDK_METADATA_SCOPE);
|
|
30
|
+
for (const eventType of eventTypes) {
|
|
31
|
+
rsdkMetadata.add({
|
|
32
|
+
eventType,
|
|
33
|
+
// Потому что https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#:~:text=If%20the%20value%20has%20a%20toJSON()%20method%2C%20it%27s%20responsible%20to%20define%20what%20data%20will%20be%20serialized.
|
|
34
|
+
eventData: (0, omit_1.default)(eventType, 'toJSON'),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
autodoc_protocol_1.AutodocMetadata.defineResolver(moduleDef, constants_1.KNEX_OUTBOX_RSDK_METADATA_SCOPE, autodoc_resolver_1.knexOutboxAutodocResolver);
|
|
38
|
+
return moduleDef;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.KnexOutboxProducer = KnexOutboxProducer;
|
|
42
|
+
//# sourceMappingURL=knex-outbox.producer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knex-outbox.producer.js","sourceRoot":"","sources":["../src/knex-outbox.producer.ts"],"names":[],"mappings":";;;;;;AACA,6DAAyD;AACzD,2CAAqD;AAErD,6CAA8C;AAE9C,uDAA+B;AAE/B,kEAAwE;AACxE,oDAAuE;AAEvE,mDAA+C;AAE/C,MAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,SAAkC;QAChD,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACtE,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,SAAoB,EAAE,EAAE,CAAC,CAAC;YAC1D,MAAM,EAAE,CAAC,8BAAoB,CAAC;YAC9B,OAAO,EAAE,SAAS,CAAC,KAAK;YACxB,UAAU,EAAE,CAAC,IAAU,EAA2B,EAAE;gBAClD,OAAO,IAAI,4BAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAC3C,CAAC;SACF,CAAC,CAAC,CAAC;QAEJ,MAAM,SAAS,GAAG;YAChB,SAAS;YACT,MAAM,EAAE,kBAAkB;YAC1B,OAAO,EAAE,SAAS;SACnB,CAAC;QAEF,MAAM,YAAY,GAAG,IAAI,uBAAY,CACnC,SAAS,EACT,2CAA+B,CAChC,CAAC;QAEF,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE;YAClC,YAAY,CAAC,GAAG,CAAC;gBACf,SAAS;gBACT,oPAAoP;gBACpP,SAAS,EAAE,IAAA,cAAI,EAAC,SAAS,EAAE,QAAQ,CAAC;aACrC,CAAC,CAAC;SACJ;QAED,kCAAe,CAAC,cAAc,CAC5B,SAAS,EACT,2CAA+B,EAC/B,4CAAyB,CAC1B,CAAC;QACF,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AArCD,gDAqCC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EventType } from '@rsdk/kafka.common';
|
|
2
|
+
import type { KafkaProducer, PublishOptions } from '@rsdk/kafka.producer';
|
|
3
|
+
import { BaseProducer } from '@rsdk/kafka.producer';
|
|
4
|
+
import type { Knex } from 'knex';
|
|
5
|
+
export declare class KnexProducer<T extends EventType> extends BaseProducer<T> implements KafkaProducer<T> {
|
|
6
|
+
readonly protoType: T;
|
|
7
|
+
private readonly knex;
|
|
8
|
+
constructor(protoType: T, knex: Knex);
|
|
9
|
+
publish(payload: T, options?: PublishOptions): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.KnexProducer = void 0;
|
|
27
|
+
const crypto = __importStar(require("crypto"));
|
|
28
|
+
const kafka_producer_1 = require("@rsdk/kafka.producer");
|
|
29
|
+
class KnexProducer extends kafka_producer_1.BaseProducer {
|
|
30
|
+
protoType;
|
|
31
|
+
knex;
|
|
32
|
+
constructor(protoType, knex) {
|
|
33
|
+
super(protoType);
|
|
34
|
+
this.protoType = protoType;
|
|
35
|
+
this.knex = knex;
|
|
36
|
+
}
|
|
37
|
+
async publish(payload, options) {
|
|
38
|
+
const value = this.parsePayload(payload, options);
|
|
39
|
+
await this.knex('outbox').insert({
|
|
40
|
+
...value,
|
|
41
|
+
id: crypto.randomUUID(),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.KnexProducer = KnexProducer;
|
|
46
|
+
//# sourceMappingURL=knex.producer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"knex.producer.js","sourceRoot":"","sources":["../src/knex.producer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAiC;AAQjC,yDAAoD;AAGpD,MAAa,YACX,SAAQ,6BAAe;IAGF;IAA+B;IAApD,YAAqB,SAAY,EAAmB,IAAU;QAC5D,KAAK,CAAC,SAAS,CAAC,CAAC;QADE,cAAS,GAAT,SAAS,CAAG;QAAmB,SAAI,GAAJ,IAAI,CAAM;IAE9D,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,OAAU,EAAE,OAAwB;QAChD,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAElD,MAAM,IAAI,CAAC,IAAI,CAAc,QAAQ,CAAC,CAAC,MAAM,CAAC;YAC5C,GAAG,KAAK;YACR,EAAE,EAAE,MAAM,CAAC,UAAU,EAAE;SACxB,CAAC,CAAC;IACL,CAAC;CACF;AAhBD,oCAgBC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DocumentResolver } from '@rsdk/autodoc.protocol';
|
|
2
|
+
import { Composite } from '@rsdk/autodoc.protocol';
|
|
3
|
+
import type { RsdkMetadataProvider } from '@rsdk/metadata';
|
|
4
|
+
declare class KnexOutboxAutodocResolver implements DocumentResolver {
|
|
5
|
+
getNode(rsdkMetadataProvider: RsdkMetadataProvider): Composite | null;
|
|
6
|
+
}
|
|
7
|
+
export declare const knexOutboxAutodocResolver: KnexOutboxAutodocResolver;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.knexOutboxAutodocResolver = void 0;
|
|
4
|
+
const autodoc_protocol_1 = require("@rsdk/autodoc.protocol");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
class KnexOutboxAutodocResolver {
|
|
7
|
+
getNode(rsdkMetadataProvider) {
|
|
8
|
+
const resources = rsdkMetadataProvider.get(constants_1.KNEX_OUTBOX_RSDK_METADATA_SCOPE);
|
|
9
|
+
if (!resources) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return new autodoc_protocol_1.Composite(new autodoc_protocol_1.Table(resources.map((resource) => {
|
|
13
|
+
const { $type, $group, $partitionKeyField } = resource.value.eventType;
|
|
14
|
+
return {
|
|
15
|
+
Type: $type,
|
|
16
|
+
Group: $group,
|
|
17
|
+
PartitionKeyField: $partitionKeyField ?? '-',
|
|
18
|
+
};
|
|
19
|
+
})), 'Kafka knex outbox');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.knexOutboxAutodocResolver = new KnexOutboxAutodocResolver();
|
|
23
|
+
//# sourceMappingURL=autodoc.resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autodoc.resolver.js","sourceRoot":"","sources":["../../src/metadata/autodoc.resolver.ts"],"names":[],"mappings":";;;AACA,6DAA0D;AAG1D,2CAA8D;AAG9D,MAAM,yBAAyB;IAC7B,OAAO,CAAC,oBAA0C;QAChD,MAAM,SAAS,GAAG,oBAAoB,CAAC,GAAG,CACxC,2CAA+B,CAChC,CAAC;QAEF,IAAI,CAAC,SAAS,EAAE;YACd,OAAO,IAAI,CAAC;SACb;QAED,OAAO,IAAI,4BAAS,CAClB,IAAI,wBAAK,CACP,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;YACzB,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,GACzC,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC;YAE3B,OAAO;gBACL,IAAI,EAAE,KAAK;gBACX,KAAK,EAAE,MAAM;gBACb,iBAAiB,EAAE,kBAAkB,IAAI,GAAG;aAC7C,CAAC;QACJ,CAAC,CAAC,CACH,EACD,mBAAmB,CACpB,CAAC;IACJ,CAAC;CACF;AAEY,QAAA,yBAAyB,GAAG,IAAI,yBAAyB,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const KNEX_OUTBOX_RSDK_METADATA_SCOPE = "kafka-producer-direct";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/metadata/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,+BAA+B,GAAG,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/metadata/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsdk/kafka.producer.outbox.knex",
|
|
3
|
+
"version": "1.0.13",
|
|
4
|
+
"description": "Producing messages to outbox table (on top of knex)",
|
|
5
|
+
"license": "Apache License 2.0",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"url": "https://github.com/R-Vision/rsdk"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"@nestjs/common": "^9.0.0",
|
|
15
|
+
"@nestjs/microservices": "^9.0.0",
|
|
16
|
+
"@rsdk/autodoc.protocol": "^1.0.13",
|
|
17
|
+
"@rsdk/common": "^1.0.13",
|
|
18
|
+
"@rsdk/core": "^1.0.13",
|
|
19
|
+
"@rsdk/db.knex": "^1.0.13",
|
|
20
|
+
"@rsdk/kafka.common": "^1.0.13",
|
|
21
|
+
"@rsdk/kafka.producer": "^1.0.13",
|
|
22
|
+
"@rsdk/logging": "^1.0.13",
|
|
23
|
+
"@rsdk/metadata": "^1.0.13",
|
|
24
|
+
"knex": "^2.4.2"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"lodash": "^4.17.21"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "1f40e539993d341b239f78bf2f8b29b44fae9c1e"
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { KnexOutboxProducer } from './knex-outbox.producer';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { AutodocMetadata } from '@rsdk/autodoc.protocol';
|
|
3
|
+
import { KNEX_INJECTION_TOKEN } from '@rsdk/db.knex';
|
|
4
|
+
import type { EventType } from '@rsdk/kafka.common';
|
|
5
|
+
import { RsdkMetadata } from '@rsdk/metadata';
|
|
6
|
+
import type { Knex } from 'knex';
|
|
7
|
+
import omit from 'lodash/omit';
|
|
8
|
+
|
|
9
|
+
import { knexOutboxAutodocResolver } from './metadata/autodoc.resolver';
|
|
10
|
+
import { KNEX_OUTBOX_RSDK_METADATA_SCOPE } from './metadata/constants';
|
|
11
|
+
import type { KnexOutboxMetadata } from './metadata/types';
|
|
12
|
+
import { KnexProducer } from './knex.producer';
|
|
13
|
+
|
|
14
|
+
export class KnexOutboxProducer {
|
|
15
|
+
static forEvent(eventType: EventType | EventType[]): DynamicModule {
|
|
16
|
+
const eventTypes = Array.isArray(eventType) ? eventType : [eventType];
|
|
17
|
+
const providers = eventTypes.map((eventType: EventType) => ({
|
|
18
|
+
inject: [KNEX_INJECTION_TOKEN],
|
|
19
|
+
provide: eventType.$type,
|
|
20
|
+
useFactory: (knex: Knex): KnexProducer<EventType> => {
|
|
21
|
+
return new KnexProducer(eventType, knex);
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
const moduleDef = {
|
|
26
|
+
providers,
|
|
27
|
+
module: KnexOutboxProducer,
|
|
28
|
+
exports: providers,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const rsdkMetadata = new RsdkMetadata<KnexOutboxMetadata>(
|
|
32
|
+
moduleDef,
|
|
33
|
+
KNEX_OUTBOX_RSDK_METADATA_SCOPE,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
for (const eventType of eventTypes) {
|
|
37
|
+
rsdkMetadata.add({
|
|
38
|
+
eventType,
|
|
39
|
+
// Потому что https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#:~:text=If%20the%20value%20has%20a%20toJSON()%20method%2C%20it%27s%20responsible%20to%20define%20what%20data%20will%20be%20serialized.
|
|
40
|
+
eventData: omit(eventType, 'toJSON'),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
AutodocMetadata.defineResolver(
|
|
45
|
+
moduleDef,
|
|
46
|
+
KNEX_OUTBOX_RSDK_METADATA_SCOPE,
|
|
47
|
+
knexOutboxAutodocResolver,
|
|
48
|
+
);
|
|
49
|
+
return moduleDef;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
import type { EventType } from '@rsdk/kafka.common';
|
|
4
|
+
import type {
|
|
5
|
+
KafkaProducer,
|
|
6
|
+
OutboxTable,
|
|
7
|
+
PublishOptions,
|
|
8
|
+
} from '@rsdk/kafka.producer';
|
|
9
|
+
import { BaseProducer } from '@rsdk/kafka.producer';
|
|
10
|
+
import type { Knex } from 'knex';
|
|
11
|
+
|
|
12
|
+
export class KnexProducer<T extends EventType>
|
|
13
|
+
extends BaseProducer<T>
|
|
14
|
+
implements KafkaProducer<T>
|
|
15
|
+
{
|
|
16
|
+
constructor(readonly protoType: T, private readonly knex: Knex) {
|
|
17
|
+
super(protoType);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async publish(payload: T, options?: PublishOptions): Promise<void> {
|
|
21
|
+
const value = this.parsePayload(payload, options);
|
|
22
|
+
|
|
23
|
+
await this.knex<OutboxTable>('outbox').insert({
|
|
24
|
+
...value,
|
|
25
|
+
id: crypto.randomUUID(),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { DocumentResolver } from '@rsdk/autodoc.protocol';
|
|
2
|
+
import { Composite, Table } from '@rsdk/autodoc.protocol';
|
|
3
|
+
import type { RsdkMetadataProvider } from '@rsdk/metadata';
|
|
4
|
+
|
|
5
|
+
import { KNEX_OUTBOX_RSDK_METADATA_SCOPE } from './constants';
|
|
6
|
+
import type { KnexOutboxMetadata } from './types';
|
|
7
|
+
|
|
8
|
+
class KnexOutboxAutodocResolver implements DocumentResolver {
|
|
9
|
+
getNode(rsdkMetadataProvider: RsdkMetadataProvider): Composite | null {
|
|
10
|
+
const resources = rsdkMetadataProvider.get<KnexOutboxMetadata>(
|
|
11
|
+
KNEX_OUTBOX_RSDK_METADATA_SCOPE,
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
if (!resources) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return new Composite(
|
|
19
|
+
new Table(
|
|
20
|
+
resources.map((resource) => {
|
|
21
|
+
const { $type, $group, $partitionKeyField } =
|
|
22
|
+
resource.value.eventType;
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
Type: $type,
|
|
26
|
+
Group: $group,
|
|
27
|
+
PartitionKeyField: $partitionKeyField ?? '-',
|
|
28
|
+
};
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
'Kafka knex outbox',
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const knexOutboxAutodocResolver = new KnexOutboxAutodocResolver();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const KNEX_OUTBOX_RSDK_METADATA_SCOPE = 'kafka-producer-direct';
|