All files / plainweb/bin admin.ts

0% Statements 0/62
0% Branches 0/1
0% Functions 0/1
0% Lines 0/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                                                                       
#!/usr/bin/env -S pnpm dlx tsx
 
import BetterSqlite3Database, { type Database } from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import express from "express";
import { log, printRoutes, unstable_admin } from "../src";
 
function migrate(connection: Database) {
  const run = `
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS order_items;
 
CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    email TEXT UNIQUE NOT NULL,
    name TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
 
CREATE TABLE orders (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    status TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
 
CREATE TABLE products (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    description TEXT
);
 
CREATE TABLE order_items (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    order_id INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    quantity INTEGER NOT NULL,
    unit_price DECIMAL(10, 2) NOT NULL,
    subtotal DECIMAL(10, 2) NOT NULL,
    discount DECIMAL(10, 2) DEFAULT 0,
    tax DECIMAL(10, 2) DEFAULT 0,
    total DECIMAL(10, 2) NOT NULL,
    notes TEXT
);`;
  connection.exec(run);
}
 
function seed(connection: Database) {
  const run = `
INSERT INTO users (email, name) VALUES
    ${Array(30)
      .fill(0)
      .map((_, i) => `('user${i + 1}@example.com', 'User ${i + 1}')`)
      .join(",\n    ")};
 
INSERT INTO products (name, price, description) VALUES
    ${Array(25)
      .fill(0)
      .map(
        (_, i) =>
          `('Product ${i + 1}', ${(Math.random() * 100).toFixed(2)}, 'Description for Product ${i + 1} with a very long description that should be truncated')`,
      )
      .join(",\n    ")};
 
INSERT INTO orders (user_id, status) VALUES
    ${Array(35)
      .fill(0)
      .map(
        () =>
          `(${Math.floor(Math.random() * 30) + 1}, '${["pending", "completed", "shipped", "cancelled"][Math.floor(Math.random() * 4)]}')`,
      )
      .join(",\n    ")};
 
INSERT INTO order_items (order_id, product_id, quantity, unit_price, subtotal, discount, tax, total, notes) VALUES
    ${Array(40)
      .fill(0)
      .map(() => {
        const quantity = Math.floor(Math.random() * 5) + 1;
        const unitPrice = Number.parseFloat((Math.random() * 100).toFixed(2));
        const subtotal = quantity * unitPrice;
        const discount = Number.parseFloat((Math.random() * 10).toFixed(2));
        const tax = Number.parseFloat((subtotal * 0.1).toFixed(2));
        const total = subtotal - discount + tax;
        return `(${Math.floor(Math.random() * 35) + 1}, ${Math.floor(Math.random() * 25) + 1}, ${quantity}, ${unitPrice}, ${subtotal}, ${discount}, ${tax}, ${total}, 'Note for item ${Math.floor(Math.random() * 1000)}')`;
      })
      .join(",\n    ")};
  `;
  connection.exec(run);
}
 
async function start() {
  process.env.BIN_ADMIN_TESTING = "1";
  log.info("Starting server...");
  const connection = new BetterSqlite3Database(":memory:");
  const database = drizzle(connection);
  connection.pragma("journal_mode = WAL");
  migrate(connection);
  seed(connection);
  const app = express();
  app.use(express.urlencoded({ extended: true }));
  app.use("/admin", await unstable_admin({ database, path: "/admin" }));
  app.use("/public", express.static(`${process.cwd()}`));
  app.listen(3000);
  // TODO config
  // printRoutes(app);
  app.use("/", (req, res) => {
    res.redirect("/admin/database");
  });
  log.info("http://localhost:3000/admin/database");
}
 
void start();