Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ coverage
*.tsbuildinfo

.turbo

# AppKit type generation cache
.appkit-types-cache.json
41 changes: 0 additions & 41 deletions apps/dev-playground/config/queries/.appkit-types-cache.json

This file was deleted.

2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"docusaurus": "docusaurus",
"dev": "docusaurus start",
"dev": "docusaurus start --no-open",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
Expand Down
20 changes: 14 additions & 6 deletions packages/appkit/src/type-generator/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ interface Cache {

export const CACHE_VERSION = "1";
const CACHE_FILE = ".appkit-types-cache.json";
const CACHE_DIR = path.join(
process.cwd(),
"node_modules",
".databricks",
"appkit",
);

/**
* Hash the SQL query
Expand All @@ -38,12 +44,15 @@ export function hashSQL(sql: string): string {
/**
* Load the cache from the file system
* If the cache is not found, run the query explain
* @param cacheDir - the directory to load the cache from
* @returns - the cache
*/
export function loadCache(cacheDir: string): Cache {
const cachePath = path.join(cacheDir, CACHE_FILE);
export function loadCache(): Cache {
const cachePath = path.join(CACHE_DIR, CACHE_FILE);
try {
if (!fs.existsSync(CACHE_DIR)) {
fs.mkdirSync(CACHE_DIR, { recursive: true });
}

if (fs.existsSync(cachePath)) {
const cache = JSON.parse(fs.readFileSync(cachePath, "utf8")) as Cache;
if (cache.version === CACHE_VERSION) {
Expand All @@ -59,10 +68,9 @@ export function loadCache(cacheDir: string): Cache {
/**
* Save the cache to the file system
* The cache is saved as a JSON file, it is used to avoid running the query explain multiple times
* @param cacheDir - the directory to save the cache to
* @param cache - cache object to save
*/
export function saveCache(cacheDir: string, cache: Cache): void {
const cachePath = path.join(cacheDir, CACHE_FILE);
export function saveCache(cache: Cache): void {
const cachePath = path.join(CACHE_DIR, CACHE_FILE);
fs.writeFileSync(cachePath, JSON.stringify(cache, null, 2), "utf8");
}
6 changes: 2 additions & 4 deletions packages/appkit/src/type-generator/query-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ export async function generateQueriesFromDescribe(
console.log(` Found ${queryFiles.length} SQL queries\n`);

// load cache
const cache = noCache
? { version: CACHE_VERSION, queries: {} }
: loadCache(queryFolder);
const cache = noCache ? { version: CACHE_VERSION, queries: {} } : loadCache();

const client = new WorkspaceClient({});
const querySchemas: QuerySchema[] = [];
Expand Down Expand Up @@ -191,7 +189,7 @@ export async function generateQueriesFromDescribe(
}

// save cache
saveCache(queryFolder, cache);
saveCache(cache);

// log warning if there are failed queries
if (failedQueries.length > 0) {
Expand Down
24 changes: 21 additions & 3 deletions packages/appkit/src/type-generator/vite-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "node:path";
import type { Plugin } from "vite";
import fs from "node:fs";
import { generateFromEntryPoint } from "./index";

/**
Expand Down Expand Up @@ -52,12 +53,29 @@ export function appKitTypesPlugin(options?: AppKitTypesPluginOptions): Plugin {
return {
name: "appkit-types",

apply() {
const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || "";

if (!warehouseId) {
console.warn(
"[AppKit] Warehouse ID not found. Skipping type generation.",
);
return false;
}

if (!fs.existsSync(path.join(process.cwd(), "config", "queries"))) {
return false;
}

return true;
},

configResolved(config) {
root = config.root;
outFile = path.resolve(root, options?.outFile ?? "src/appKitTypes.d.ts");
watchFolders = (options?.watchFolders ?? ["../config/queries"]).map(
(folder) => path.resolve(root, folder),
);
watchFolders = options?.watchFolders ?? [
path.join(process.cwd(), "config", "queries"),
];
},

buildStart() {
Expand Down