resolve-local-event-broker
Version:
The reSolve framework's event broker for applications on a local machine.
114 lines (95 loc) • 4.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _lifecycleErrors = require("./lifecycle-errors");
var _constants = require("../constants");
async function createDatabase({
database: {
runRawQuery,
escapeId
}
}) {
const notificationsTableNameAsId = escapeId(_constants.NOTIFICATIONS_TABLE_NAME);
const subscribersTableNameAsId = escapeId(_constants.SUBSCRIBERS_TABLE_NAME);
const batchesTableNameAsId = escapeId(_constants.BATCHES_TABLE_NAME);
const notificationsSubscriptionIdIndexNameAsId = escapeId(`${_constants.NOTIFICATIONS_TABLE_NAME}-subscriptionId`);
const notificationsBatchIdIndexNameAsId = escapeId(`${_constants.NOTIFICATIONS_TABLE_NAME}-batchId`);
const subscribersEventSubscriberIndexNameAsId = escapeId(`${_constants.SUBSCRIBERS_TABLE_NAME}-eventSubscriber`);
const batchesBatchIdIndexNameAsId = escapeId(`${_constants.BATCHES_TABLE_NAME}-batchId`);
try {
try {
await runRawQuery(`
DELETE FROM ${notificationsTableNameAsId};
COMMIT;
BEGIN IMMEDIATE;
`);
} catch (e) {}
try {
await runRawQuery(`
DELETE FROM ${batchesTableNameAsId};
COMMIT;
BEGIN IMMEDIATE;
`);
} catch (e) {}
await runRawQuery(`
CREATE TABLE IF NOT EXISTS ${notificationsTableNameAsId}(
"insertionId" ${_constants.STRING_SQL_TYPE} NOT NULL,
"subscriptionId" ${_constants.STRING_SQL_TYPE} NOT NULL,
"status" ${_constants.STRING_SQL_TYPE} NOT NULL,
"incomingTimestamp" ${_constants.LONG_INTEGER_SQL_TYPE} NOT NULL,
"processStartTimestamp" ${_constants.LONG_INTEGER_SQL_TYPE},
"processEndTimestamp" ${_constants.LONG_INTEGER_SQL_TYPE},
"heartbeatTimestamp" ${_constants.LONG_INTEGER_SQL_TYPE},
"aggregateIdAndVersion" ${_constants.STRING_SQL_TYPE} NOT NULL,
"xaTransactionId" ${_constants.JSON_SQL_TYPE},
"batchId" ${_constants.STRING_SQL_TYPE} NULL,
PRIMARY KEY("insertionId", "subscriptionId")
);
CREATE TABLE IF NOT EXISTS ${subscribersTableNameAsId}(
"subscriptionId" ${_constants.STRING_SQL_TYPE} NOT NULL,
"eventSubscriber" ${_constants.STRING_SQL_TYPE} NOT NULL,
"status" ${_constants.STRING_SQL_TYPE} NOT NULL,
"deliveryStrategy" ${_constants.STRING_SQL_TYPE} NOT NULL,
"eventTypes" ${_constants.JSON_SQL_TYPE} NOT NULL,
"aggregateIds" ${_constants.JSON_SQL_TYPE} NOT NULL,
"queueStrategy" ${_constants.STRING_SQL_TYPE} NOT NULL,
"maxParallel" ${_constants.LONG_INTEGER_SQL_TYPE} NOT NULL,
"properties" ${_constants.JSON_SQL_TYPE},
"successEvent" ${_constants.JSON_SQL_TYPE},
"failedEvent" ${_constants.JSON_SQL_TYPE},
"errors" ${_constants.JSON_SQL_TYPE},
"cursor" ${_constants.JSON_SQL_TYPE},
PRIMARY KEY("subscriptionId")
);
CREATE TABLE IF NOT EXISTS ${batchesTableNameAsId}(
"batchId" ${_constants.STRING_SQL_TYPE} NOT NULL,
"eventIndex" ${_constants.INTEGER_SQL_TYPE} NOT NULL,
"aggregateIdAndVersion" ${_constants.STRING_SQL_TYPE} NOT NULL,
"threadId" ${_constants.LONG_INTEGER_SQL_TYPE} NOT NULL,
"threadCounter" ${_constants.LONG_INTEGER_SQL_TYPE} NOT NULL,
PRIMARY KEY("batchId", "eventIndex")
);
CREATE INDEX IF NOT EXISTS ${notificationsSubscriptionIdIndexNameAsId}
ON ${notificationsTableNameAsId}("subscriptionId");
CREATE INDEX IF NOT EXISTS ${notificationsBatchIdIndexNameAsId}
ON ${notificationsTableNameAsId}("batchId");
CREATE UNIQUE INDEX IF NOT EXISTS ${subscribersEventSubscriberIndexNameAsId}
ON ${subscribersTableNameAsId}("eventSubscriber");
CREATE INDEX IF NOT EXISTS ${batchesBatchIdIndexNameAsId}
ON ${batchesTableNameAsId}("batchId");
COMMIT;
BEGIN IMMEDIATE;
`);
} catch (error) {
if (error != null && /^SQLITE_ERROR:.*? already exists$/.test(error.message)) {
throw new _lifecycleErrors.ResourceAlreadyExistError(`Double-initialize event-bus database failed`);
} else {
throw error;
}
}
}
var _default = createDatabase;
exports.default = _default;
//# sourceMappingURL=create-database.js.map