@directus/api
Version:
Directus is a real-time API and App dashboard for managing SQL database content
68 lines (66 loc) • 2.5 kB
JavaScript
import { getDefaultIndexName } from "../../../../utils/get-default-index-name.js";
import { SchemaHelper } from "../types.js";
import { prepQueryParams } from "../utils/prep-query-params.js";
//#region src/database/helpers/schema/dialects/mssql.ts
const triggerCache = /* @__PURE__ */ new Map();
var SchemaHelperMSSQL = class extends SchemaHelper {
generateIndexName(type, collection, fields) {
return getDefaultIndexName(type, collection, fields, { maxLength: 128 });
}
async hasTriggers(collection) {
const cached = triggerCache.get(collection);
if (cached !== void 0) return cached;
const result = await this.knex.raw(`SELECT TOP 1 1 AS has_trigger FROM sys.triggers WHERE parent_id = OBJECT_ID(?)`, [collection]);
const hasTriggers = Array.isArray(result) && result.length > 0;
triggerCache.set(collection, hasTriggers);
return hasTriggers;
}
applyLimit(rootQuery, limit) {
if (limit === -1) rootQuery.limit(Number.MAX_SAFE_INTEGER);
else rootQuery.limit(limit);
}
applyOffset(rootQuery, offset) {
rootQuery.offset(offset);
rootQuery.orderBy(1);
}
formatUUID(uuid) {
return uuid.toUpperCase();
}
async getDatabaseSize() {
try {
const result = await this.knex.raw("SELECT SUM(size) * 8192 AS size FROM sys.database_files;");
return result[0]?.["size"] ? Number(result[0]?.["size"]) : null;
} catch {
return null;
}
}
prepQueryParams(queryParams) {
return prepQueryParams(queryParams, { format: (index) => `@p${index}` });
}
addInnerSortFieldsToGroupBy(groupByFields, sortRecords, _hasRelationalSort) {
if (sortRecords.length > 0) groupByFields.push(...sortRecords.map(({ column }) => column));
}
getColumnNameMaxLength() {
return 128;
}
getTableNameMaxLength() {
return 128;
}
async createIndex(collection, field, options = {}) {
const isUnique = Boolean(options.unique);
const constraintName = this.generateIndexName(isUnique ? "unique" : "index", collection, field);
const edition = await this.knex.raw(`SELECT SERVERPROPERTY('edition') AS edition`).then((data) => data?.[0]?.["edition"]);
if (options.attemptConcurrentIndex && typeof edition === "string" && edition.startsWith("Enterprise")) return this.knex.raw(`CREATE ${isUnique ? "UNIQUE " : ""}INDEX ?? ON ?? (??) WITH (ONLINE = ON)`, [
constraintName,
collection,
field
]);
return this.knex.raw(`CREATE ${isUnique ? "UNIQUE " : ""}INDEX ?? ON ?? (??)`, [
constraintName,
collection,
field
]);
}
};
//#endregion
export { SchemaHelperMSSQL };