PHP Classes

File: resources/js/table-export.js

Recommend this page to a friend!
  Packages of Alberto Arena   Laravel Truss   resources/js/table-export.js   Download  
File: resources/js/table-export.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Laravel Truss
View a diagram of a Laravel application models
Author: By
Last change:
Date: 9 days ago
Size: 1,345 bytes
 

Contents

Class file image Download
// Pure serializers for a single table's structure. Structure only, never row // data: they operate on the snapshot the browser already holds. Used by the // per-table export menu in truss.js and unit-tested in isolation. const CSV_HEADER = ['name', 'type', 'nullable', 'default', 'key']; function csvCell(value) { const s = value == null ? '' : String(value); return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s; } // The full structure, pretty-printed. The table object already is the // structure (columns, primary key, indexes, foreign keys), so this is a // stable, copy-pasteable dump of exactly what Truss knows. export function toJson(table) { return JSON.stringify(table, null, 2); } // A flat, spreadsheet-friendly view of the columns, with a derived key column // (PK / FK) mirroring the badges shown in the diagram. export function toCsv(table) { const pk = new Set(table.primary_key ?? []); const fk = new Set((table.foreign_keys ?? []).flatMap((f) => f.columns)); const rows = [CSV_HEADER]; for (const c of table.columns ?? []) { const key = [pk.has(c.name) ? 'PK' : null, fk.has(c.name) ? 'FK' : null] .filter(Boolean) .join(', '); rows.push([c.name, c.type, c.nullable ? 'true' : 'false', c.default ?? '', key]); } return rows.map((r) => r.map(csvCell).join(',')).join('\n'); }