MCPcopy Index your code
hub / github.com/TechJeeper/Printventory / initializeDatabase

Function initializeDatabase

main.js:1600–1721  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1598
1599// Add this function to initialize the database
1600function initializeDatabase() {
1601 try {
1602 const dbPath = getDatabasePath();
1603 console.log(`Initializing database at ${dbPath}`);
1604
1605 // Create database directory if it doesn't exist
1606 const dbDir = path.dirname(dbPath);
1607 if (!fs.existsSync(dbDir)) {
1608 fs.mkdirSync(dbDir, { recursive: true });
1609 }
1610
1611 // Initialize database
1612 db = new Database(dbPath);
1613
1614 // Enable foreign keys
1615 db.pragma('foreign_keys = ON');
1616
1617 // Create tables in sequence
1618 db.transaction(() => {
1619 // Create models table
1620 db.prepare(`CREATE TABLE IF NOT EXISTS models (
1621 id INTEGER PRIMARY KEY AUTOINCREMENT,
1622 filePath TEXT UNIQUE,
1623 fileName TEXT,
1624 designer TEXT,
1625 source TEXT,
1626 notes TEXT,
1627 printed INTEGER,
1628 thumbnail TEXT,
1629 parentModel TEXT,
1630 hash TEXT,
1631 size INTEGER,
1632 license TEXT,
1633 modifiedDate DATETIME,
1634 dateAdded DATETIME,
1635 isNew INTEGER DEFAULT 1
1636 )`).run();
1637
1638 // Create tags table
1639 db.prepare(`CREATE TABLE IF NOT EXISTS tags (
1640 id INTEGER PRIMARY KEY AUTOINCREMENT,
1641 name TEXT UNIQUE
1642 )`).run();
1643
1644 // Create model_tags table
1645 db.prepare(`CREATE TABLE IF NOT EXISTS model_tags (
1646 model_id INTEGER,
1647 tag_id INTEGER,
1648 FOREIGN KEY(model_id) REFERENCES models(id),
1649 FOREIGN KEY(tag_id) REFERENCES tags(id),
1650 PRIMARY KEY(model_id, tag_id)
1651 )`).run();
1652
1653 // Create settings table
1654 db.prepare(`CREATE TABLE IF NOT EXISTS settings (
1655 key TEXT PRIMARY KEY,
1656 value TEXT
1657 )`).run();

Callers 1

main.jsFile · 0.85

Calls 7

getDatabasePathFunction · 0.85
migrateDateAddedColumnFunction · 0.85
migrateIsNewColumnFunction · 0.85
repairModelTagsTableFunction · 0.85
ensureSlicersTableExistsFunction · 0.85

Tested by

no test coverage detected