Add MariaDB queue table. Fix Bugs
This commit is contained in:
+21
-1
@@ -1,11 +1,31 @@
|
||||
import _ from "lodash";
|
||||
import { DefaultFields } from "../types";
|
||||
import { DefaultFields, QueueFields, QueueIndexes, } from "../types";
|
||||
const queueFieldNames = new Set(QueueFields.map((f) => f.fieldName));
|
||||
const queueIndexNames = new Set(QueueIndexes.map((i) => i.indexName));
|
||||
export default function ({ dbSchema }) {
|
||||
const finaldbSchema = _.cloneDeep(dbSchema);
|
||||
finaldbSchema.tables = finaldbSchema.tables.map((t) => {
|
||||
const newTable = _.cloneDeep(t);
|
||||
newTable.fields = newTable.fields.filter((f) => !f.fieldName?.match(/^(id|created_at|updated_at)$/));
|
||||
newTable.fields.unshift(...DefaultFields);
|
||||
if (newTable.queue_table) {
|
||||
const reserved = [
|
||||
...newTable.fields
|
||||
.map((f) => f.fieldName)
|
||||
.filter((name) => queueFieldNames.has(name)),
|
||||
...(newTable.indexes || [])
|
||||
.map((i) => i.indexName)
|
||||
.filter((name) => queueIndexNames.has(name)),
|
||||
];
|
||||
if (reserved.length > 0) {
|
||||
throw new Error(`\`${newTable.tableName}\` is a queue table: ${reserved.join(", ")} ${reserved.length > 1 ? "are" : "is"} managed by bun-mariadb and can't be declared in the schema`);
|
||||
}
|
||||
newTable.fields.push(..._.cloneDeep(QueueFields));
|
||||
newTable.indexes = [
|
||||
...(newTable.indexes || []),
|
||||
..._.cloneDeep(QueueIndexes),
|
||||
];
|
||||
}
|
||||
return newTable;
|
||||
});
|
||||
return finaldbSchema;
|
||||
|
||||
Reference in New Issue
Block a user