@npio/cli
Version:
A free visual website editor, powered with your own SolidJS components.
41 lines (31 loc) • 1.09 kB
text/typescript
import prismaInternals from "@prisma/internals";
import { readFile, readdir } from "fs/promises";
import { getSchemaRootDir } from "nitropage/internals";
import { join } from "path";
const datasourceRegex = /datasource \S+ {/;
const getSchemaPath = async () => {
const rootDir = getSchemaRootDir();
const items = await readdir(rootDir);
for (const item of items) {
if (!item.endsWith(".prisma")) {
continue;
}
const itemPath = join(rootDir, item);
const contents = await readFile(itemPath, "utf-8");
if (contents.match(datasourceRegex)) {
return itemPath;
}
}
throw new Error("Unable to find prisma schema file");
};
export const getDatabaseProvider = async () => {
const { getConfig: getPrismaConfig } = prismaInternals;
const schemaPath = await getSchemaPath();
let mainSchema = await readFile(schemaPath, { encoding: "utf8" });
const schemaConfig = await getPrismaConfig({
datamodel: mainSchema,
ignoreEnvVarErrors: true,
});
const provider = schemaConfig.datasources[0]?.provider || "sqlite";
return provider;
};