Skip to content

Commit db5e266

Browse files
authored
fix(appkit): query cache generation (#37)
1 parent 086faac commit db5e266

File tree

6 files changed

+38
-58
lines changed

6 files changed

+38
-58
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ coverage
99
*.tsbuildinfo
1010

1111
.turbo
12-
13-
# AppKit type generation cache
14-
.appkit-types-cache.json

apps/dev-playground/config/queries/.appkit-types-cache.json

Lines changed: 0 additions & 41 deletions
This file was deleted.

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",
7-
"dev": "docusaurus start",
7+
"dev": "docusaurus start --no-open",
88
"build": "docusaurus build",
99
"swizzle": "docusaurus swizzle",
1010
"deploy": "docusaurus deploy",

packages/appkit/src/type-generator/cache.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ interface Cache {
2424

2525
export const CACHE_VERSION = "1";
2626
const CACHE_FILE = ".appkit-types-cache.json";
27+
const CACHE_DIR = path.join(
28+
process.cwd(),
29+
"node_modules",
30+
".databricks",
31+
"appkit",
32+
);
2733

2834
/**
2935
* Hash the SQL query
@@ -38,12 +44,15 @@ export function hashSQL(sql: string): string {
3844
/**
3945
* Load the cache from the file system
4046
* If the cache is not found, run the query explain
41-
* @param cacheDir - the directory to load the cache from
4247
* @returns - the cache
4348
*/
44-
export function loadCache(cacheDir: string): Cache {
45-
const cachePath = path.join(cacheDir, CACHE_FILE);
49+
export function loadCache(): Cache {
50+
const cachePath = path.join(CACHE_DIR, CACHE_FILE);
4651
try {
52+
if (!fs.existsSync(CACHE_DIR)) {
53+
fs.mkdirSync(CACHE_DIR, { recursive: true });
54+
}
55+
4756
if (fs.existsSync(cachePath)) {
4857
const cache = JSON.parse(fs.readFileSync(cachePath, "utf8")) as Cache;
4958
if (cache.version === CACHE_VERSION) {
@@ -59,10 +68,9 @@ export function loadCache(cacheDir: string): Cache {
5968
/**
6069
* Save the cache to the file system
6170
* The cache is saved as a JSON file, it is used to avoid running the query explain multiple times
62-
* @param cacheDir - the directory to save the cache to
6371
* @param cache - cache object to save
6472
*/
65-
export function saveCache(cacheDir: string, cache: Cache): void {
66-
const cachePath = path.join(cacheDir, CACHE_FILE);
73+
export function saveCache(cache: Cache): void {
74+
const cachePath = path.join(CACHE_DIR, CACHE_FILE);
6775
fs.writeFileSync(cachePath, JSON.stringify(cache, null, 2), "utf8");
6876
}

packages/appkit/src/type-generator/query-registry.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ export async function generateQueriesFromDescribe(
124124
console.log(` Found ${queryFiles.length} SQL queries\n`);
125125

126126
// load cache
127-
const cache = noCache
128-
? { version: CACHE_VERSION, queries: {} }
129-
: loadCache(queryFolder);
127+
const cache = noCache ? { version: CACHE_VERSION, queries: {} } : loadCache();
130128

131129
const client = new WorkspaceClient({});
132130
const querySchemas: QuerySchema[] = [];
@@ -191,7 +189,7 @@ export async function generateQueriesFromDescribe(
191189
}
192190

193191
// save cache
194-
saveCache(queryFolder, cache);
192+
saveCache(cache);
195193

196194
// log warning if there are failed queries
197195
if (failedQueries.length > 0) {

packages/appkit/src/type-generator/vite-plugin.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "node:path";
22
import type { Plugin } from "vite";
3+
import fs from "node:fs";
34
import { generateFromEntryPoint } from "./index";
45

56
/**
@@ -52,12 +53,29 @@ export function appKitTypesPlugin(options?: AppKitTypesPluginOptions): Plugin {
5253
return {
5354
name: "appkit-types",
5455

56+
apply() {
57+
const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || "";
58+
59+
if (!warehouseId) {
60+
console.warn(
61+
"[AppKit] Warehouse ID not found. Skipping type generation.",
62+
);
63+
return false;
64+
}
65+
66+
if (!fs.existsSync(path.join(process.cwd(), "config", "queries"))) {
67+
return false;
68+
}
69+
70+
return true;
71+
},
72+
5573
configResolved(config) {
5674
root = config.root;
5775
outFile = path.resolve(root, options?.outFile ?? "src/appKitTypes.d.ts");
58-
watchFolders = (options?.watchFolders ?? ["../config/queries"]).map(
59-
(folder) => path.resolve(root, folder),
60-
);
76+
watchFolders = options?.watchFolders ?? [
77+
path.join(process.cwd(), "config", "queries"),
78+
];
6179
},
6280

6381
buildStart() {

0 commit comments

Comments
 (0)