fix: remove nested .git folders, re-add as normal directories
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { createServer } from "node:http";
|
||||
import { Server as SocketIOServer } from "socket.io";
|
||||
import fs from "fs";
|
||||
|
||||
export const startServer = (port = Number(Bun.env.PORT ?? 3000)) => {
|
||||
const requiredPsk = Bun.env.PSK;
|
||||
if (!requiredPsk) {
|
||||
throw new Error("PSK is not set.");
|
||||
}
|
||||
|
||||
const httpServer = createServer((_, response) => {
|
||||
response.writeHead(200, { "content-type": "text/plain" });
|
||||
response.end("Socket.IO server is running");
|
||||
});
|
||||
|
||||
const io = new SocketIOServer(httpServer, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
},
|
||||
});
|
||||
|
||||
io.use((socket, next) => {
|
||||
const headerPsk = socket.handshake.headers["x-psk"];
|
||||
const queryPsk = socket.handshake.query.psk;
|
||||
const authPsk = socket.handshake.auth?.psk;
|
||||
|
||||
const providedPsk =
|
||||
(typeof authPsk === "string" && authPsk) ||
|
||||
(Array.isArray(headerPsk) ? headerPsk[0] : headerPsk) ||
|
||||
(Array.isArray(queryPsk) ? queryPsk[0] : queryPsk);
|
||||
|
||||
if (providedPsk !== requiredPsk) {
|
||||
return next(new Error("Unauthorized"));
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
console.log(`Socket connected: ${socket.id}`);
|
||||
|
||||
fs.readdirSync("./src/collectors").forEach((file) => {
|
||||
if (file.endsWith(".ts")) {
|
||||
const collectorName = file.replace(".ts", "");
|
||||
socket.on(collectorName, async (opts, callback) => {
|
||||
try {
|
||||
const collector = (await import(`../collectors/${file}`)).default;
|
||||
const data = await collector(opts);
|
||||
callback({ success: true, data });
|
||||
} catch (error: Error | unknown) {
|
||||
const err = error as any;
|
||||
console.error(`❌ Error in ${collectorName}:\n${err.stack}`);
|
||||
callback({ success: false, error: err.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log(`Socket disconnected: ${socket.id}`);
|
||||
});
|
||||
});
|
||||
|
||||
httpServer.listen(port, () => {
|
||||
console.log(`Socket.IO server listening on port ${port}`);
|
||||
});
|
||||
|
||||
return { io, httpServer };
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
import { prisma } from "../constants";
|
||||
import { Prisma } from "../../generated/prisma/client";
|
||||
import {
|
||||
buildCollectorFindManyArgs,
|
||||
CollectorQueryOptions,
|
||||
} from "../helper/collectorQuery";
|
||||
|
||||
type CompanyCollectorOpts = CollectorQueryOptions<
|
||||
Prisma.CompanySelect,
|
||||
Prisma.CompanyInclude
|
||||
>;
|
||||
|
||||
export default async (opts?: CompanyCollectorOpts) => {
|
||||
const args: Prisma.CompanyFindManyArgs = buildCollectorFindManyArgs<
|
||||
Prisma.CompanySelect,
|
||||
Prisma.CompanyInclude
|
||||
>(opts);
|
||||
|
||||
const data = await prisma.company.findMany(args);
|
||||
|
||||
return data;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
import { prisma } from "../constants";
|
||||
import { Prisma } from "../../generated/prisma/client";
|
||||
import {
|
||||
buildCollectorFindManyArgs,
|
||||
CollectorQueryOptions,
|
||||
} from "../helper/collectorQuery";
|
||||
|
||||
type MemberCollectorOpts = CollectorQueryOptions<
|
||||
Prisma.MemberSelect,
|
||||
Prisma.MemberInclude
|
||||
>;
|
||||
|
||||
export default async (opts?: MemberCollectorOpts) => {
|
||||
const args: Prisma.MemberFindManyArgs = buildCollectorFindManyArgs<
|
||||
Prisma.MemberSelect,
|
||||
Prisma.MemberInclude
|
||||
>(opts);
|
||||
|
||||
const data = await prisma.member.findMany(args);
|
||||
|
||||
return data;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
import { prisma } from "../constants";
|
||||
import { Prisma } from "../../generated/prisma/client";
|
||||
import {
|
||||
buildCollectorFindManyArgs,
|
||||
CollectorQueryOptions,
|
||||
} from "../helper/collectorQuery";
|
||||
|
||||
type OpportunityCollectorOpts = CollectorQueryOptions<
|
||||
Prisma.OpportunitySelect,
|
||||
Prisma.OpportunityInclude
|
||||
>;
|
||||
|
||||
export default async (opts?: OpportunityCollectorOpts) => {
|
||||
const args: Prisma.OpportunityFindManyArgs = buildCollectorFindManyArgs<
|
||||
Prisma.OpportunitySelect,
|
||||
Prisma.OpportunityInclude
|
||||
>(opts);
|
||||
|
||||
const data = await prisma.opportunity.findMany(args);
|
||||
|
||||
return data;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
import { prisma } from "../constants";
|
||||
import { Prisma } from "../../generated/prisma/client";
|
||||
import {
|
||||
buildCollectorFindManyArgs,
|
||||
CollectorQueryOptions,
|
||||
} from "../helper/collectorQuery";
|
||||
|
||||
type ProductCollectorOpts = CollectorQueryOptions<
|
||||
Prisma.ProductCatalogSelect,
|
||||
Prisma.ProductCatalogInclude
|
||||
>;
|
||||
|
||||
export default async (opts?: ProductCollectorOpts) => {
|
||||
const args: Prisma.ProductCatalogFindManyArgs = buildCollectorFindManyArgs<
|
||||
Prisma.ProductCatalogSelect,
|
||||
Prisma.ProductCatalogInclude
|
||||
>(opts);
|
||||
|
||||
const data = await prisma.productCatalog.findMany(args);
|
||||
|
||||
return data;
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import { PrismaMssql } from "@prisma/adapter-mssql";
|
||||
import { PrismaClient } from "../generated/prisma/client";
|
||||
|
||||
const databaseUrl = Bun.env.DATABASE_URL;
|
||||
|
||||
if (!databaseUrl) {
|
||||
throw new Error("DATABASE_URL is not set.");
|
||||
}
|
||||
|
||||
const adapter = new PrismaMssql(databaseUrl, { schema: "dbo" });
|
||||
|
||||
export const prisma = new PrismaClient({ adapter });
|
||||
|
||||
// Global error handler for unhandled Prisma promise rejections
|
||||
process.on("unhandledRejection", (reason, promise) => {
|
||||
if (reason && typeof reason === "object" && "code" in reason) {
|
||||
console.error("Unhandled Prisma Rejection:", reason);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { PrismaClient } from "../generated/prisma/client";
|
||||
import { PrismaMssql } from "@prisma/adapter-mssql";
|
||||
import { writeFileSync } from "fs";
|
||||
|
||||
const databaseUrl = Bun.env.DATABASE_URL;
|
||||
|
||||
if (!databaseUrl) {
|
||||
throw new Error("DATABASE_URL is not set.");
|
||||
}
|
||||
|
||||
const adapter = new PrismaMssql(databaseUrl, { schema: "dbo" });
|
||||
const prisma = new PrismaClient({ adapter });
|
||||
|
||||
async function exportProducts() {
|
||||
console.log("Fetching products with relations...");
|
||||
|
||||
const products = await prisma.productCatalog.findMany({
|
||||
where: {
|
||||
inactiveFlag: false,
|
||||
},
|
||||
include: {
|
||||
subcategory: {
|
||||
include: {
|
||||
category: true,
|
||||
},
|
||||
},
|
||||
manufacturer: true,
|
||||
inventory: true,
|
||||
itemVendors: true,
|
||||
},
|
||||
take: 100,
|
||||
});
|
||||
|
||||
const jsonData = JSON.stringify(products, null, 2);
|
||||
|
||||
writeFileSync("products-with-relations.json", jsonData);
|
||||
|
||||
console.log(
|
||||
`Exported ${products.length} products to products-with-relations.json`,
|
||||
);
|
||||
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
|
||||
exportProducts().catch(console.error);
|
||||
@@ -0,0 +1,54 @@
|
||||
export type CollectorQueryOptions<
|
||||
SelectShape extends object,
|
||||
IncludeShape extends object,
|
||||
> = {
|
||||
select?: (keyof SelectShape)[];
|
||||
include?: (keyof IncludeShape)[];
|
||||
};
|
||||
|
||||
const normalizeCollectorQuery = <
|
||||
SelectShape extends object,
|
||||
IncludeShape extends object,
|
||||
>(
|
||||
opts?: CollectorQueryOptions<SelectShape, IncludeShape>,
|
||||
): CollectorQueryOptions<SelectShape, IncludeShape> => {
|
||||
if (!opts) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return opts;
|
||||
};
|
||||
|
||||
export const buildCollectorFindManyArgs = <
|
||||
SelectShape extends object,
|
||||
IncludeShape extends object,
|
||||
>(
|
||||
opts?: CollectorQueryOptions<SelectShape, IncludeShape>,
|
||||
) => {
|
||||
const { select: selectedKeys, include: includedKeys } =
|
||||
normalizeCollectorQuery(opts);
|
||||
|
||||
if (selectedKeys?.length && includedKeys?.length) {
|
||||
throw new Error("Use either opts.select or opts.include, not both.");
|
||||
}
|
||||
|
||||
if (selectedKeys?.length) {
|
||||
const select: Partial<Record<keyof SelectShape, boolean>> = {};
|
||||
selectedKeys.forEach((key) => {
|
||||
select[key] = true;
|
||||
});
|
||||
|
||||
return { select };
|
||||
}
|
||||
|
||||
if (includedKeys?.length) {
|
||||
const include: Partial<Record<keyof IncludeShape, boolean>> = {};
|
||||
includedKeys.forEach((key) => {
|
||||
include[key] = true;
|
||||
});
|
||||
|
||||
return { include };
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
import { startServer } from "./api/server";
|
||||
|
||||
startServer();
|
||||
Reference in New Issue
Block a user