-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (74 loc) · 2.25 KB
/
server.js
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
import express from "express";
import ViteExpress from "vite-express";
import { attachWebsocketServer } from "@vlcn.io/ws-server";
import * as http from "http";
import { createIssues } from "./createIssues.js";
const PORT = parseInt(process.env.PORT || "8080");
const app = express();
const server = http.createServer(app);
const wsConfig = {
dbFolder: "./dbs",
schemaFolder: "./src/domain",
pathPattern: /\/sync/,
};
// Change this to return something we can listen to
// for when dbs are created and then use those DBs.
const dbCache = attachWebsocketServer(server, wsConfig);
// Set up our `linear` db on the backend
// We could have any number of DBs we want
// E.g., 1 per client, 1 per workspace, whatever.
// We currently just set up 1 single DB.
// The "room" set in the client identifies the DB used on the server.
// If the client sets the room to "linear" then it will connect to the
// "linear" DB on the server.
dbCache.use("linear", "Schema.sql", (wrapper) => {
// getDB_unsafe is only unsafe under LiteFS deployments
const db = wrapper.getDB();
seedDB(db);
});
server.listen(PORT, () =>
console.log("info", `listening on http://localhost:${PORT}!`)
);
ViteExpress.bind(app, server);
/**
*
* @param {import("better-sqlite3").Database} db
* @returns
*/
async function seedDB(db) {
const existing = db.prepare(`SELECT * FROM issue`).all();
if (existing.length > 0) {
console.log("db already seeded");
return;
}
console.log("Seeding DB");
const createIssueStmt = db.prepare(
`INSERT INTO issue
(id, title, creator, priority, status, created, modified, kanbanorder)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?)`
);
const createDescriptionStmt = db.prepare(
`INSERT INTO description (id, body) VALUES (?, ?)`
);
db.transaction(() => {
let i = 0;
for (const [issue, description] of createIssues(7000)) {
if (++i % 1000 === 0) {
console.log("Seeded", i, "issues");
}
createIssueStmt.run(
issue.id,
issue.title,
issue.creator,
issue.priority,
issue.status,
issue.created,
issue.modified,
issue.kanbanorder
);
createDescriptionStmt.run(description.id, description.body);
}
})();
console.log("Done seeding DB");
}