diff --git a/.gitignore b/.gitignore index 1968394..d1771b7 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json .DS_Store /test .vscode -.dump \ No newline at end of file +.dump + +/.bun-mariadb \ No newline at end of file diff --git a/README.md b/README.md index 356c514..95405bf 100644 --- a/README.md +++ b/README.md @@ -241,7 +241,6 @@ interface BUN_MARIADB_TableSchemaType { parentTableName?: string; // inherit / merge fields from another table tableNameOld?: string; // rename: old name triggers ALTER TABLE RENAME collation?: "utf8mb4_bin" | "utf8mb4_unicode_520_ci"; - isVector?: boolean; // mark as vector-oriented table } ``` @@ -743,7 +742,6 @@ MariaDB native `VECTOR(n)` columns and `VECTOR INDEX` are supported (MariaDB 11. ```ts { tableName: "documents", - isVector: true, fields: [ { fieldName: "embedding", diff --git a/dist/lib/mariadb/db-sql.js b/dist/lib/mariadb/db-sql.js index 6f02400..be02edf 100644 --- a/dist/lib/mariadb/db-sql.js +++ b/dist/lib/mariadb/db-sql.js @@ -26,7 +26,9 @@ export default async function DbSQL({ sql, values }) { const singleRaw = res.single_res; return { ...res, - success: true, + success: isSelect + ? Boolean(single_res) || Boolean(payload?.[0]) + : true, payload, single_res, debug: { diff --git a/dist/lib/schema/create-table.js b/dist/lib/schema/create-table.js index 771f5c5..e6303ac 100644 --- a/dist/lib/schema/create-table.js +++ b/dist/lib/schema/create-table.js @@ -15,7 +15,7 @@ export default async function createTable({ table, config, }) { if (field.primaryKey && field.fieldName) { primaryKeys.push(field.fieldName); } - if (field.foreignKey && !table.isVector) { + if (field.foreignKey) { foreignKeys.push(buildForeignKeyConstraint(field, table.tableName)); } } diff --git a/dist/lib/schema/recreate-table.d.ts b/dist/lib/schema/recreate-table.d.ts index d411755..c7cedf3 100644 --- a/dist/lib/schema/recreate-table.d.ts +++ b/dist/lib/schema/recreate-table.d.ts @@ -1,7 +1,6 @@ import type { BUN_MARIADB_TableSchemaType, BunMariaDBConfig } from "../../types"; /** - * Full table rebuild. For `isVector` tables this drops and recreates in place - * (preserving rows when possible). For regular tables it uses a temp-table swap. + * Full table rebuild using a temp-table swap (preserving rows when possible). */ export default function recreateTable({ table, config, }: { table: BUN_MARIADB_TableSchemaType; diff --git a/dist/lib/schema/recreate-table.js b/dist/lib/schema/recreate-table.js index 183ecc9..3f6ce0b 100644 --- a/dist/lib/schema/recreate-table.js +++ b/dist/lib/schema/recreate-table.js @@ -13,8 +13,7 @@ async function checkIfTableExists({ tableName, config, }) { return Boolean(rows[0]?.table_exists); } /** - * Full table rebuild. For `isVector` tables this drops and recreates in place - * (preserving rows when possible). For regular tables it uses a temp-table swap. + * Full table rebuild using a temp-table swap (preserving rows when possible). */ export default async function recreateTable({ table, config, }) { const doesTableExist = await checkIfTableExists({ @@ -25,48 +24,6 @@ export default async function recreateTable({ table, config, }) { await createTable({ table, config }); return; } - /** - * Vector tables: drop + recreate + reinsert (MariaDB VECTOR INDEX / dim - * changes are not reliably alterable in place). - */ - if (table.isVector) { - console.log(`Recreating vector table: ${table.tableName}`); - const existingRows = await querySchemaRows({ - query: `SELECT * FROM ${MariaDBQuoteGen(table.tableName)}`, - config, - }); - await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 0`, config }); - try { - await runSchemaQuery({ - query: `DROP TABLE IF EXISTS ${MariaDBQuoteGen(table.tableName)}`, - config, - }); - await createTable({ table, config }); - } - finally { - await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 1`, config }); - } - if (existingRows.length > 0) { - const schemaFieldNames = new Set((table.fields || []) - .map((f) => f.fieldName) - .filter((n) => Boolean(n))); - for (const row of existingRows) { - const columns = Object.keys(row).filter((c) => schemaFieldNames.has(c)); - if (columns.length === 0) - continue; - const placeholders = columns.map(() => "?").join(", "); - const columnList = columns - .map((c) => MariaDBQuoteGen(c)) - .join(", "); - await runSchemaQuery({ - query: `INSERT INTO ${MariaDBQuoteGen(table.tableName)} (${columnList}) VALUES (${placeholders})`, - values: columns.map((c) => row[c] ?? null), - config, - }); - } - } - return; - } const tempTableName = `${table.tableName}_temp_${Date.now()}`; const backupOldTableName = `${table.tableName}_old_${Date.now()}`; const existingColumns = await getTableColumns({ diff --git a/dist/lib/schema/resolve-table.js b/dist/lib/schema/resolve-table.js index f0f9235..b43e604 100644 --- a/dist/lib/schema/resolve-table.js +++ b/dist/lib/schema/resolve-table.js @@ -23,7 +23,6 @@ export default function resolveTable(table, db_schema) { tableName: table.tableName, tableDescription: table.tableDescription || parentTable.tableDescription, collation: table.collation || parentTable.collation, - isVector: table.isVector !== undefined ? table.isVector : parentTable.isVector, fields: Array.from(mergedFieldsMap.values()), indexes: _.uniqBy([...(parentTable.indexes || []), ...(table.indexes || [])], "indexName"), uniqueConstraints: [ diff --git a/dist/lib/schema/sync-foreign-keys.js b/dist/lib/schema/sync-foreign-keys.js index 63c3186..693004c 100644 --- a/dist/lib/schema/sync-foreign-keys.js +++ b/dist/lib/schema/sync-foreign-keys.js @@ -15,9 +15,6 @@ function rulesMatch(live, desired) { ruleIsCascade(live.updateRule) === desired.cascadeUpdate); } export function grabDesiredForeignKeys(table) { - if (table.isVector) { - return []; - } const desired = []; for (const field of table.fields || []) { const fk = field.foreignKey; diff --git a/dist/lib/schema/sync-indexes.js b/dist/lib/schema/sync-indexes.js index a39a8f1..2a58642 100644 --- a/dist/lib/schema/sync-indexes.js +++ b/dist/lib/schema/sync-indexes.js @@ -6,8 +6,6 @@ import { grabDesiredUniqueConstraints } from "./sync-unique-constraints"; function isVectorIndexDef(index, table) { if (index.indexType === "VECTOR") return true; - if (table.isVector) - return true; const firstFieldName = index.indexTableFields?.[0]; if (!firstFieldName) return false; diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index d359b2a..9386ffe 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -74,10 +74,6 @@ export interface BUN_MARIADB_TableSchemaType { */ childTableDbId?: string | number; collation?: (typeof MariaDBCollations)[number]; - /** - * If this is a vector-oriented table (native MariaDB VECTOR columns/indexes) - */ - isVector?: boolean; } /** * Reference object used to link a table to one of its child tables. diff --git a/package.json b/package.json index afccc0d..21f6536 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bun-mariadb", - "version": "1.0.10", + "version": "1.0.11", "description": "Schema-driven MariaDB manager for Bun", "author": "Benjamin Toby", "license": "MIT", diff --git a/src/lib/mariadb/db-sql.ts b/src/lib/mariadb/db-sql.ts index a3403ef..40a99e9 100644 --- a/src/lib/mariadb/db-sql.ts +++ b/src/lib/mariadb/db-sql.ts @@ -39,7 +39,9 @@ export default async function DbSQL< return { ...res, - success: true, + success: isSelect + ? Boolean(single_res) || Boolean(payload?.[0]) + : true, payload, single_res, debug: { diff --git a/src/lib/schema/create-table.ts b/src/lib/schema/create-table.ts index 540cb60..6144c01 100644 --- a/src/lib/schema/create-table.ts +++ b/src/lib/schema/create-table.ts @@ -30,7 +30,7 @@ export default async function createTable({ primaryKeys.push(field.fieldName); } - if (field.foreignKey && !table.isVector) { + if (field.foreignKey) { foreignKeys.push( buildForeignKeyConstraint(field, table.tableName), ); diff --git a/src/lib/schema/recreate-table.ts b/src/lib/schema/recreate-table.ts index 0b120d6..354d79e 100644 --- a/src/lib/schema/recreate-table.ts +++ b/src/lib/schema/recreate-table.ts @@ -26,8 +26,7 @@ async function checkIfTableExists({ } /** - * Full table rebuild. For `isVector` tables this drops and recreates in place - * (preserving rows when possible). For regular tables it uses a temp-table swap. + * Full table rebuild using a temp-table swap (preserving rows when possible). */ export default async function recreateTable({ table, @@ -46,58 +45,6 @@ export default async function recreateTable({ return; } - /** - * Vector tables: drop + recreate + reinsert (MariaDB VECTOR INDEX / dim - * changes are not reliably alterable in place). - */ - if (table.isVector) { - console.log(`Recreating vector table: ${table.tableName}`); - - const existingRows = await querySchemaRows>({ - query: `SELECT * FROM ${MariaDBQuoteGen(table.tableName)}`, - config, - }); - - await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 0`, config }); - try { - await runSchemaQuery({ - query: `DROP TABLE IF EXISTS ${MariaDBQuoteGen(table.tableName)}`, - config, - }); - await createTable({ table, config }); - } finally { - await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 1`, config }); - } - - if (existingRows.length > 0) { - const schemaFieldNames = new Set( - (table.fields || []) - .map((f) => f.fieldName) - .filter((n): n is string => Boolean(n)), - ); - - for (const row of existingRows) { - const columns = Object.keys(row).filter((c) => - schemaFieldNames.has(c), - ); - if (columns.length === 0) continue; - - const placeholders = columns.map(() => "?").join(", "); - const columnList = columns - .map((c) => MariaDBQuoteGen(c)) - .join(", "); - - await runSchemaQuery({ - query: `INSERT INTO ${MariaDBQuoteGen(table.tableName)} (${columnList}) VALUES (${placeholders})`, - values: columns.map((c) => row[c] ?? null), - config, - }); - } - } - - return; - } - const tempTableName = `${table.tableName}_temp_${Date.now()}`; const backupOldTableName = `${table.tableName}_old_${Date.now()}`; const existingColumns = await getTableColumns({ diff --git a/src/lib/schema/resolve-table.ts b/src/lib/schema/resolve-table.ts index 3acc4b9..078d38a 100644 --- a/src/lib/schema/resolve-table.ts +++ b/src/lib/schema/resolve-table.ts @@ -41,8 +41,6 @@ export default function resolveTable( tableName: table.tableName, tableDescription: table.tableDescription || parentTable.tableDescription, collation: table.collation || parentTable.collation, - isVector: - table.isVector !== undefined ? table.isVector : parentTable.isVector, fields: Array.from(mergedFieldsMap.values()), indexes: _.uniqBy( [...(parentTable.indexes || []), ...(table.indexes || [])], diff --git a/src/lib/schema/sync-foreign-keys.ts b/src/lib/schema/sync-foreign-keys.ts index af85347..d075e26 100644 --- a/src/lib/schema/sync-foreign-keys.ts +++ b/src/lib/schema/sync-foreign-keys.ts @@ -57,10 +57,6 @@ function rulesMatch( export function grabDesiredForeignKeys( table: BUN_MARIADB_TableSchemaType, ): DesiredForeignKey[] { - if (table.isVector) { - return []; - } - const desired: DesiredForeignKey[] = []; for (const field of table.fields || []) { diff --git a/src/lib/schema/sync-indexes.ts b/src/lib/schema/sync-indexes.ts index 4f112cf..c95b5e5 100644 --- a/src/lib/schema/sync-indexes.ts +++ b/src/lib/schema/sync-indexes.ts @@ -14,7 +14,6 @@ function isVectorIndexDef( table: BUN_MARIADB_TableSchemaType, ): boolean { if (index.indexType === "VECTOR") return true; - if (table.isVector) return true; const firstFieldName = index.indexTableFields?.[0]; if (!firstFieldName) return false; @@ -97,8 +96,7 @@ async function createIndex({ .map((field) => MariaDBQuoteGen(field)) .join(", "); const typeUpper = index.indexType?.toUpperCase(); - const isSpecialType = - typeUpper === "FULLTEXT" || typeUpper === "SPATIAL"; + const isSpecialType = typeUpper === "FULLTEXT" || typeUpper === "SPATIAL"; const indexPrefix = isSpecialType ? `${typeUpper} ` : ""; const indexSuffix = !isSpecialType && (typeUpper === "BTREE" || typeUpper === "HASH") @@ -199,11 +197,7 @@ export default async function syncIndexes({ const columnsMatch = details.columns.length === schemaColumns.length && details.columns.every((col, idx) => col === schemaColumns[idx]); - const typeMatch = indexTypesMatch( - details.type, - schemaIndex, - table, - ); + const typeMatch = indexTypesMatch(details.type, schemaIndex, table); if (!columnsMatch || !typeMatch) { console.log(`Recreating changed index: ${indexName}`); diff --git a/src/types/index.ts b/src/types/index.ts index aaf7d68..f062077 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -96,10 +96,6 @@ export interface BUN_MARIADB_TableSchemaType { */ childTableDbId?: string | number; collation?: (typeof MariaDBCollations)[number]; - /** - * If this is a vector-oriented table (native MariaDB VECTOR columns/indexes) - */ - isVector?: boolean; } /**