on-zero 0.4.3 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli.cjs +46 -41
- package/dist/cjs/cli.native.js +48 -43
- package/dist/cjs/cli.native.js.map +1 -1
- package/dist/cjs/constants.cjs +14 -12
- package/dist/cjs/constants.native.js +14 -12
- package/dist/cjs/constants.native.js.map +1 -1
- package/dist/cjs/createPermissions.cjs +66 -34
- package/dist/cjs/createPermissions.native.js +143 -87
- package/dist/cjs/createPermissions.native.js.map +1 -1
- package/dist/cjs/createSchemaFromDrizzle.cjs +15 -13
- package/dist/cjs/createSchemaFromDrizzle.native.js +15 -13
- package/dist/cjs/createSchemaFromDrizzle.native.js.map +1 -1
- package/dist/cjs/createUseQuery.cjs +48 -35
- package/dist/cjs/createUseQuery.native.js +52 -38
- package/dist/cjs/createUseQuery.native.js.map +1 -1
- package/dist/cjs/createZeroClient.cjs +177 -135
- package/dist/cjs/createZeroClient.native.js +246 -195
- package/dist/cjs/createZeroClient.native.js.map +1 -1
- package/dist/cjs/createZeroServer.cjs +175 -129
- package/dist/cjs/createZeroServer.native.js +170 -138
- package/dist/cjs/createZeroServer.native.js.map +1 -1
- package/dist/cjs/generate-helpers.cjs +122 -88
- package/dist/cjs/generate-helpers.native.js +209 -174
- package/dist/cjs/generate-helpers.native.js.map +1 -1
- package/dist/cjs/generate-lite.cjs +90 -58
- package/dist/cjs/generate-lite.native.js +200 -140
- package/dist/cjs/generate-lite.native.js.map +1 -1
- package/dist/cjs/generate-lite.test.cjs +229 -192
- package/dist/cjs/generate-lite.test.native.js +229 -194
- package/dist/cjs/generate-lite.test.native.js.map +1 -1
- package/dist/cjs/generate.cjs +455 -291
- package/dist/cjs/generate.native.js +747 -565
- package/dist/cjs/generate.native.js.map +1 -1
- package/dist/cjs/generate.test.cjs +170 -106
- package/dist/cjs/generate.test.native.js +171 -107
- package/dist/cjs/generate.test.native.js.map +1 -1
- package/dist/cjs/helpers/batchQuery.cjs +29 -18
- package/dist/cjs/helpers/batchQuery.native.js +36 -21
- package/dist/cjs/helpers/batchQuery.native.js.map +1 -1
- package/dist/cjs/helpers/clearZeroClientData.cjs +30 -21
- package/dist/cjs/helpers/clearZeroClientData.native.js +42 -33
- package/dist/cjs/helpers/clearZeroClientData.native.js.map +1 -1
- package/dist/cjs/helpers/createMutators.cjs +80 -42
- package/dist/cjs/helpers/createMutators.native.js +139 -87
- package/dist/cjs/helpers/createMutators.native.js.map +1 -1
- package/dist/cjs/helpers/didRunPermissionCheck.cjs +19 -13
- package/dist/cjs/helpers/didRunPermissionCheck.native.js +22 -20
- package/dist/cjs/helpers/didRunPermissionCheck.native.js.map +1 -1
- package/dist/cjs/helpers/ensureLoggedIn.cjs +16 -13
- package/dist/cjs/helpers/ensureLoggedIn.native.js +21 -18
- package/dist/cjs/helpers/ensureLoggedIn.native.js.map +1 -1
- package/dist/cjs/helpers/getAuth.cjs +25 -15
- package/dist/cjs/helpers/getAuth.native.js +25 -15
- package/dist/cjs/helpers/getAuth.native.js.map +1 -1
- package/dist/cjs/helpers/mutatorContext.cjs +21 -14
- package/dist/cjs/helpers/mutatorContext.native.js +21 -15
- package/dist/cjs/helpers/mutatorContext.native.js.map +1 -1
- package/dist/cjs/helpers/prettyFormatZeroQuery.cjs +126 -81
- package/dist/cjs/helpers/prettyFormatZeroQuery.native.js +135 -92
- package/dist/cjs/helpers/prettyFormatZeroQuery.native.js.map +1 -1
- package/dist/cjs/helpers/queryContext.cjs +12 -10
- package/dist/cjs/helpers/queryContext.native.js +16 -13
- package/dist/cjs/helpers/queryContext.native.js.map +1 -1
- package/dist/cjs/helpers/showZeroClientError.cjs +24 -19
- package/dist/cjs/helpers/showZeroClientError.native.js +38 -34
- package/dist/cjs/helpers/showZeroClientError.native.js.map +1 -1
- package/dist/cjs/helpers/useZeroDebug.cjs +82 -49
- package/dist/cjs/helpers/useZeroDebug.native.js +83 -55
- package/dist/cjs/helpers/useZeroDebug.native.js.map +1 -1
- package/dist/cjs/index.cjs +17 -15
- package/dist/cjs/index.native.js +17 -15
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/modelRegistry.cjs +12 -10
- package/dist/cjs/modelRegistry.native.js +12 -10
- package/dist/cjs/modelRegistry.native.js.map +1 -1
- package/dist/cjs/mutations.cjs +66 -42
- package/dist/cjs/mutations.native.js +68 -46
- package/dist/cjs/mutations.native.js.map +1 -1
- package/dist/cjs/queryRegistry.cjs +13 -10
- package/dist/cjs/queryRegistry.native.js +14 -11
- package/dist/cjs/queryRegistry.native.js.map +1 -1
- package/dist/cjs/resolveQuery.cjs +22 -18
- package/dist/cjs/resolveQuery.native.js +32 -28
- package/dist/cjs/resolveQuery.native.js.map +1 -1
- package/dist/cjs/run.cjs +35 -28
- package/dist/cjs/run.native.js +35 -29
- package/dist/cjs/run.native.js.map +1 -1
- package/dist/cjs/server.cjs +7 -5
- package/dist/cjs/serverWhere.cjs +13 -11
- package/dist/cjs/serverWhere.native.js +13 -11
- package/dist/cjs/serverWhere.native.js.map +1 -1
- package/dist/cjs/serverWhere.test.cjs +83 -55
- package/dist/cjs/serverWhere.test.native.js +72 -50
- package/dist/cjs/serverWhere.test.native.js.map +1 -1
- package/dist/cjs/state.cjs +49 -44
- package/dist/cjs/state.native.js +56 -53
- package/dist/cjs/state.native.js.map +1 -1
- package/dist/cjs/types.cjs +7 -5
- package/dist/cjs/types.native.js +7 -5
- package/dist/cjs/types.native.js.map +1 -1
- package/dist/cjs/usePermission.test.cjs +58 -45
- package/dist/cjs/usePermission.test.native.js +59 -48
- package/dist/cjs/usePermission.test.native.js.map +1 -1
- package/dist/cjs/vite-plugin.cjs +41 -21
- package/dist/cjs/vite-plugin.native.js +61 -47
- package/dist/cjs/vite-plugin.native.js.map +1 -1
- package/dist/cjs/where.cjs +39 -24
- package/dist/cjs/where.native.js +44 -29
- package/dist/cjs/where.native.js.map +1 -1
- package/dist/cjs/zeroRunner.cjs +18 -12
- package/dist/cjs/zeroRunner.native.js +22 -16
- package/dist/cjs/zeroRunner.native.js.map +1 -1
- package/dist/cjs/zql.cjs +14 -11
- package/dist/cjs/zql.native.js +19 -17
- package/dist/cjs/zql.native.js.map +1 -1
- package/dist/esm/cli.mjs +43 -38
- package/dist/esm/cli.mjs.map +1 -1
- package/dist/esm/cli.native.js +44 -39
- package/dist/esm/cli.native.js.map +1 -1
- package/dist/esm/constants.mjs +2 -2
- package/dist/esm/constants.mjs.map +1 -1
- package/dist/esm/constants.native.js +2 -2
- package/dist/esm/constants.native.js.map +1 -1
- package/dist/esm/createPermissions.mjs +48 -18
- package/dist/esm/createPermissions.mjs.map +1 -1
- package/dist/esm/createPermissions.native.js +125 -71
- package/dist/esm/createPermissions.native.js.map +1 -1
- package/dist/esm/createUseQuery.mjs +32 -21
- package/dist/esm/createUseQuery.mjs.map +1 -1
- package/dist/esm/createUseQuery.native.js +36 -24
- package/dist/esm/createUseQuery.native.js.map +1 -1
- package/dist/esm/createZeroClient.mjs +148 -108
- package/dist/esm/createZeroClient.mjs.map +1 -1
- package/dist/esm/createZeroClient.native.js +217 -168
- package/dist/esm/createZeroClient.native.js.map +1 -1
- package/dist/esm/createZeroServer.mjs +148 -104
- package/dist/esm/createZeroServer.mjs.map +1 -1
- package/dist/esm/createZeroServer.native.js +143 -113
- package/dist/esm/createZeroServer.native.js.map +1 -1
- package/dist/esm/generate-helpers.mjs +110 -78
- package/dist/esm/generate-helpers.mjs.map +1 -1
- package/dist/esm/generate-helpers.native.js +197 -164
- package/dist/esm/generate-helpers.native.js.map +1 -1
- package/dist/esm/generate-lite.mjs +78 -48
- package/dist/esm/generate-lite.mjs.map +1 -1
- package/dist/esm/generate-lite.native.js +188 -130
- package/dist/esm/generate-lite.native.js.map +1 -1
- package/dist/esm/generate-lite.test.mjs +227 -190
- package/dist/esm/generate-lite.test.mjs.map +1 -1
- package/dist/esm/generate-lite.test.native.js +227 -192
- package/dist/esm/generate-lite.test.native.js.map +1 -1
- package/dist/esm/generate.mjs +427 -265
- package/dist/esm/generate.mjs.map +1 -1
- package/dist/esm/generate.native.js +718 -538
- package/dist/esm/generate.native.js.map +1 -1
- package/dist/esm/generate.test.mjs +165 -101
- package/dist/esm/generate.test.mjs.map +1 -1
- package/dist/esm/generate.test.native.js +165 -101
- package/dist/esm/generate.test.native.js.map +1 -1
- package/dist/esm/helpers/batchQuery.mjs +17 -8
- package/dist/esm/helpers/batchQuery.mjs.map +1 -1
- package/dist/esm/helpers/batchQuery.native.js +24 -11
- package/dist/esm/helpers/batchQuery.native.js.map +1 -1
- package/dist/esm/helpers/clearZeroClientData.mjs +18 -11
- package/dist/esm/helpers/clearZeroClientData.mjs.map +1 -1
- package/dist/esm/helpers/clearZeroClientData.native.js +30 -23
- package/dist/esm/helpers/clearZeroClientData.native.js.map +1 -1
- package/dist/esm/helpers/createMutators.mjs +53 -17
- package/dist/esm/helpers/createMutators.mjs.map +1 -1
- package/dist/esm/helpers/createMutators.native.js +112 -62
- package/dist/esm/helpers/createMutators.native.js.map +1 -1
- package/dist/esm/helpers/didRunPermissionCheck.mjs +7 -3
- package/dist/esm/helpers/didRunPermissionCheck.mjs.map +1 -1
- package/dist/esm/helpers/didRunPermissionCheck.native.js +9 -9
- package/dist/esm/helpers/didRunPermissionCheck.native.js.map +1 -1
- package/dist/esm/helpers/ensureLoggedIn.mjs +2 -1
- package/dist/esm/helpers/ensureLoggedIn.mjs.map +1 -1
- package/dist/esm/helpers/ensureLoggedIn.native.js +2 -1
- package/dist/esm/helpers/ensureLoggedIn.native.js.map +1 -1
- package/dist/esm/helpers/getAuth.mjs +10 -2
- package/dist/esm/helpers/getAuth.mjs.map +1 -1
- package/dist/esm/helpers/getAuth.native.js +10 -2
- package/dist/esm/helpers/getAuth.native.js.map +1 -1
- package/dist/esm/helpers/mutatorContext.mjs +9 -4
- package/dist/esm/helpers/mutatorContext.mjs.map +1 -1
- package/dist/esm/helpers/mutatorContext.native.js +8 -4
- package/dist/esm/helpers/mutatorContext.native.js.map +1 -1
- package/dist/esm/helpers/prettyFormatZeroQuery.mjs +114 -71
- package/dist/esm/helpers/prettyFormatZeroQuery.mjs.map +1 -1
- package/dist/esm/helpers/prettyFormatZeroQuery.native.js +121 -80
- package/dist/esm/helpers/prettyFormatZeroQuery.native.js.map +1 -1
- package/dist/esm/helpers/queryContext.native.js +2 -1
- package/dist/esm/helpers/queryContext.native.js.map +1 -1
- package/dist/esm/helpers/showZeroClientError.mjs +12 -9
- package/dist/esm/helpers/showZeroClientError.mjs.map +1 -1
- package/dist/esm/helpers/showZeroClientError.native.js +25 -23
- package/dist/esm/helpers/showZeroClientError.native.js.map +1 -1
- package/dist/esm/helpers/useZeroDebug.mjs +67 -36
- package/dist/esm/helpers/useZeroDebug.mjs.map +1 -1
- package/dist/esm/helpers/useZeroDebug.native.js +68 -42
- package/dist/esm/helpers/useZeroDebug.native.js.map +1 -1
- package/dist/esm/mutations.mjs +51 -29
- package/dist/esm/mutations.mjs.map +1 -1
- package/dist/esm/mutations.native.js +53 -33
- package/dist/esm/mutations.native.js.map +1 -1
- package/dist/esm/queryRegistry.mjs +1 -0
- package/dist/esm/queryRegistry.mjs.map +1 -1
- package/dist/esm/queryRegistry.native.js +2 -1
- package/dist/esm/queryRegistry.native.js.map +1 -1
- package/dist/esm/resolveQuery.mjs +10 -8
- package/dist/esm/resolveQuery.mjs.map +1 -1
- package/dist/esm/resolveQuery.native.js +20 -18
- package/dist/esm/resolveQuery.native.js.map +1 -1
- package/dist/esm/run.mjs +21 -16
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/run.native.js +20 -16
- package/dist/esm/run.native.js.map +1 -1
- package/dist/esm/serverWhere.mjs +1 -1
- package/dist/esm/serverWhere.mjs.map +1 -1
- package/dist/esm/serverWhere.native.js +1 -1
- package/dist/esm/serverWhere.native.js.map +1 -1
- package/dist/esm/serverWhere.test.mjs +79 -51
- package/dist/esm/serverWhere.test.mjs.map +1 -1
- package/dist/esm/serverWhere.test.native.js +68 -46
- package/dist/esm/serverWhere.test.native.js.map +1 -1
- package/dist/esm/state.mjs +35 -32
- package/dist/esm/state.mjs.map +1 -1
- package/dist/esm/state.native.js +41 -40
- package/dist/esm/state.native.js.map +1 -1
- package/dist/esm/usePermission.test.mjs +53 -40
- package/dist/esm/usePermission.test.mjs.map +1 -1
- package/dist/esm/usePermission.test.native.js +54 -43
- package/dist/esm/usePermission.test.native.js.map +1 -1
- package/dist/esm/vite-plugin.mjs +27 -9
- package/dist/esm/vite-plugin.mjs.map +1 -1
- package/dist/esm/vite-plugin.native.js +47 -35
- package/dist/esm/vite-plugin.native.js.map +1 -1
- package/dist/esm/where.mjs +24 -11
- package/dist/esm/where.mjs.map +1 -1
- package/dist/esm/where.native.js +29 -16
- package/dist/esm/where.native.js.map +1 -1
- package/dist/esm/zeroRunner.mjs +6 -2
- package/dist/esm/zeroRunner.mjs.map +1 -1
- package/dist/esm/zeroRunner.native.js +8 -4
- package/dist/esm/zeroRunner.native.js.map +1 -1
- package/dist/esm/zql.mjs +2 -1
- package/dist/esm/zql.mjs.map +1 -1
- package/dist/esm/zql.native.js.map +1 -1
- package/package.json +3 -3
package/dist/cjs/generate.cjs
CHANGED
|
@@ -2,33 +2,35 @@ var __create = Object.create;
|
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf
|
|
6
|
-
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
7
|
var __export = (target, all) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
15
16
|
get: () => from[key],
|
|
16
17
|
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
20
22
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
+
value: true
|
|
33
|
+
}), mod);
|
|
32
34
|
var generate_exports = {};
|
|
33
35
|
__export(generate_exports, {
|
|
34
36
|
generate: () => generate,
|
|
@@ -36,22 +38,25 @@ __export(generate_exports, {
|
|
|
36
38
|
watch: () => watch
|
|
37
39
|
});
|
|
38
40
|
module.exports = __toCommonJS(generate_exports);
|
|
39
|
-
var import_node_crypto = require("node:crypto")
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
var import_node_crypto = require("node:crypto");
|
|
42
|
+
var import_node_fs = require("node:fs");
|
|
43
|
+
var import_node_path = require("node:path");
|
|
44
|
+
var import_generate_helpers = require("./generate-helpers.cjs");
|
|
43
45
|
const hash = s => (0, import_node_crypto.createHash)("sha256").update(s).digest("hex");
|
|
44
|
-
let generateCache = {}
|
|
45
|
-
|
|
46
|
+
let generateCache = {};
|
|
47
|
+
let generateCachePath = "";
|
|
46
48
|
function getCacheDir() {
|
|
47
49
|
let dir = process.cwd();
|
|
48
|
-
|
|
50
|
+
while (dir !== "/") {
|
|
49
51
|
const nm = (0, import_node_path.resolve)(dir, "node_modules");
|
|
50
52
|
if ((0, import_node_fs.existsSync)(nm)) {
|
|
51
53
|
const cacheDir = (0, import_node_path.resolve)(nm, ".on-zero");
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
if (!(0, import_node_fs.existsSync)(cacheDir)) {
|
|
55
|
+
(0, import_node_fs.mkdirSync)(cacheDir, {
|
|
56
|
+
recursive: true
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return cacheDir;
|
|
55
60
|
}
|
|
56
61
|
dir = (0, import_node_path.resolve)(dir, "..");
|
|
57
62
|
}
|
|
@@ -59,22 +64,28 @@ function getCacheDir() {
|
|
|
59
64
|
}
|
|
60
65
|
function loadCache() {
|
|
61
66
|
const cacheDir = getCacheDir();
|
|
62
|
-
if (cacheDir)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
67
|
+
if (!cacheDir) return;
|
|
68
|
+
generateCachePath = (0, import_node_path.resolve)(cacheDir, "generate-cache.json");
|
|
69
|
+
try {
|
|
70
|
+
generateCache = JSON.parse((0, import_node_fs.readFileSync)(generateCachePath, "utf-8"));
|
|
71
|
+
} catch {
|
|
72
|
+
generateCache = {};
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
function saveCache() {
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
if (generateCachePath) {
|
|
77
|
+
(0, import_node_fs.writeFileSync)(generateCachePath, JSON.stringify(generateCache) + "\n", "utf-8");
|
|
78
|
+
}
|
|
74
79
|
}
|
|
75
80
|
function writeFileIfChanged(filePath, content) {
|
|
76
81
|
const contentHash = hash(content);
|
|
77
|
-
|
|
82
|
+
const cachedHash = generateCache[filePath];
|
|
83
|
+
if (cachedHash === contentHash && (0, import_node_fs.existsSync)(filePath)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
(0, import_node_fs.writeFileSync)(filePath, content, "utf-8");
|
|
87
|
+
generateCache[filePath] = contentHash;
|
|
88
|
+
return true;
|
|
78
89
|
}
|
|
79
90
|
function createTypeResolver(ts, files, dir) {
|
|
80
91
|
const configPath = ts.findConfigFile(dir, ts.sys.fileExists, "tsconfig.json");
|
|
@@ -82,9 +93,9 @@ function createTypeResolver(ts, files, dir) {
|
|
|
82
93
|
target: ts.ScriptTarget.Latest,
|
|
83
94
|
module: ts.ModuleKind.ESNext,
|
|
84
95
|
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
85
|
-
strict:
|
|
86
|
-
skipLibCheck:
|
|
87
|
-
noEmit:
|
|
96
|
+
strict: false,
|
|
97
|
+
skipLibCheck: true,
|
|
98
|
+
noEmit: true
|
|
88
99
|
};
|
|
89
100
|
if (configPath) {
|
|
90
101
|
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
@@ -97,15 +108,22 @@ function createTypeResolver(ts, files, dir) {
|
|
|
97
108
|
}
|
|
98
109
|
}
|
|
99
110
|
const fileMap = /* @__PURE__ */new Map();
|
|
100
|
-
for (const f of files)
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
for (const f of files) {
|
|
112
|
+
fileMap.set(f.path, f.content);
|
|
113
|
+
}
|
|
114
|
+
const host = ts.createCompilerHost(compilerOptions);
|
|
115
|
+
const originalGetSourceFile = host.getSourceFile.bind(host);
|
|
103
116
|
host.getSourceFile = (fileName, languageVersion, onError) => {
|
|
104
117
|
const content = fileMap.get(fileName);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
118
|
+
if (content !== void 0) {
|
|
119
|
+
return ts.createSourceFile(fileName, content, languageVersion, true);
|
|
120
|
+
}
|
|
121
|
+
return originalGetSourceFile(fileName, languageVersion, onError);
|
|
122
|
+
};
|
|
123
|
+
host.fileExists = fileName => fileMap.has(fileName) || ts.sys.fileExists(fileName);
|
|
124
|
+
host.readFile = fileName => fileMap.get(fileName) ?? ts.sys.readFile(fileName);
|
|
125
|
+
const program = ts.createProgram(files.map(f => f.path), compilerOptions, host);
|
|
126
|
+
const checker = program.getTypeChecker();
|
|
109
127
|
return {
|
|
110
128
|
program,
|
|
111
129
|
checker,
|
|
@@ -125,102 +143,146 @@ function createTypeResolver(ts, files, dir) {
|
|
|
125
143
|
}
|
|
126
144
|
function resolveParamType(ts, resolver, sourceFile, exportName, paramIndex) {
|
|
127
145
|
let result = null;
|
|
128
|
-
|
|
129
|
-
if (result
|
|
146
|
+
ts.forEachChild(sourceFile, node => {
|
|
147
|
+
if (result) return;
|
|
148
|
+
if (!ts.isVariableStatement(node)) return;
|
|
149
|
+
if (!node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
|
|
130
150
|
const decl = node.declarationList.declarations[0];
|
|
131
|
-
if (!
|
|
151
|
+
if (!decl || !ts.isVariableDeclaration(decl)) return;
|
|
152
|
+
if (decl.name.getText(sourceFile) !== exportName) return;
|
|
153
|
+
if (decl.initializer && ts.isArrowFunction(decl.initializer)) {
|
|
132
154
|
const param = decl.initializer.parameters[paramIndex];
|
|
133
|
-
|
|
155
|
+
if (param?.type) {
|
|
156
|
+
result = resolver.resolveType(param.type);
|
|
157
|
+
}
|
|
134
158
|
}
|
|
135
|
-
})
|
|
159
|
+
});
|
|
160
|
+
return result;
|
|
136
161
|
}
|
|
137
162
|
function resolveMutationParamTypes(ts, resolver, sourceFile) {
|
|
138
163
|
const resolved = /* @__PURE__ */new Map();
|
|
139
|
-
|
|
140
|
-
if (!ts.isVariableStatement(node)
|
|
164
|
+
ts.forEachChild(sourceFile, node => {
|
|
165
|
+
if (!ts.isVariableStatement(node)) return;
|
|
166
|
+
if (!node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
|
|
141
167
|
const decl = node.declarationList.declarations[0];
|
|
142
|
-
if (!decl || !ts.isVariableDeclaration(decl)
|
|
168
|
+
if (!decl || !ts.isVariableDeclaration(decl)) return;
|
|
169
|
+
if (decl.name.getText(sourceFile) !== "mutate") return;
|
|
170
|
+
if (!decl.initializer || !ts.isCallExpression(decl.initializer)) return;
|
|
143
171
|
const args = decl.initializer.arguments;
|
|
144
172
|
let handlersArg = null;
|
|
145
|
-
for (let i = args.length - 1; i >= 0; i--)
|
|
146
|
-
|
|
147
|
-
|
|
173
|
+
for (let i = args.length - 1; i >= 0; i--) {
|
|
174
|
+
if (ts.isObjectLiteralExpression(args[i])) {
|
|
175
|
+
handlersArg = args[i];
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
148
178
|
}
|
|
149
|
-
if (handlersArg)
|
|
179
|
+
if (!handlersArg) return;
|
|
180
|
+
for (const prop of handlersArg.properties) {
|
|
150
181
|
if (!ts.isPropertyAssignment(prop) && !ts.isMethodDeclaration(prop)) continue;
|
|
151
182
|
const name = prop.name?.getText(sourceFile);
|
|
152
183
|
if (!name) continue;
|
|
153
184
|
let params = null;
|
|
154
185
|
if (ts.isPropertyAssignment(prop)) {
|
|
155
186
|
const init = prop.initializer;
|
|
156
|
-
(ts.isArrowFunction(init) || ts.isFunctionExpression(init))
|
|
157
|
-
|
|
187
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) {
|
|
188
|
+
params = init.parameters;
|
|
189
|
+
}
|
|
190
|
+
} else if (ts.isMethodDeclaration(prop)) {
|
|
191
|
+
params = prop.parameters;
|
|
192
|
+
}
|
|
158
193
|
if (!params || params.length < 2) continue;
|
|
159
194
|
const typeNode = params[1].type;
|
|
160
195
|
if (!typeNode) continue;
|
|
161
196
|
const expanded = resolver.resolveType(typeNode);
|
|
162
|
-
|
|
197
|
+
if (expanded) {
|
|
198
|
+
resolved.set(name, expanded);
|
|
199
|
+
}
|
|
163
200
|
}
|
|
164
|
-
})
|
|
201
|
+
});
|
|
202
|
+
return resolved;
|
|
165
203
|
}
|
|
166
204
|
function extractMutationsFromModel(ts, sourceFile, content, fileName, silent, typeToValibot, resolvedTypes, resolvedTypeToValibot) {
|
|
167
205
|
let mutateNode = null;
|
|
168
|
-
|
|
169
|
-
if (!ts.isVariableStatement(node)
|
|
206
|
+
ts.forEachChild(sourceFile, node => {
|
|
207
|
+
if (!ts.isVariableStatement(node)) return;
|
|
208
|
+
if (!node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) return;
|
|
170
209
|
const decl = node.declarationList.declarations[0];
|
|
171
|
-
!decl || !ts.isVariableDeclaration(decl)
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
210
|
+
if (!decl || !ts.isVariableDeclaration(decl)) return;
|
|
211
|
+
if (decl.name.getText(sourceFile) !== "mutate") return;
|
|
212
|
+
if (decl.initializer && ts.isCallExpression(decl.initializer)) {
|
|
213
|
+
mutateNode = decl.initializer;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
if (!mutateNode) {
|
|
217
|
+
return {
|
|
218
|
+
modelName: (0, import_node_path.basename)(fileName, ".ts"),
|
|
219
|
+
hasCRUD: false,
|
|
220
|
+
columns: {},
|
|
221
|
+
primaryKeys: [],
|
|
222
|
+
custom: []
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const call = mutateNode;
|
|
226
|
+
const args = call.arguments;
|
|
227
|
+
const hasCRUD = args.length >= 2;
|
|
181
228
|
let handlersArg = null;
|
|
182
|
-
args.length === 1 && ts.isObjectLiteralExpression(args[0])
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
229
|
+
if (args.length === 1 && ts.isObjectLiteralExpression(args[0])) {
|
|
230
|
+
handlersArg = args[0];
|
|
231
|
+
} else if (args.length === 3 && ts.isObjectLiteralExpression(args[2])) {
|
|
232
|
+
handlersArg = args[2];
|
|
233
|
+
}
|
|
234
|
+
const columns = {};
|
|
235
|
+
const primaryKeys = [];
|
|
236
|
+
if (hasCRUD) {
|
|
237
|
+
extractSchemaColumns(ts, sourceFile, columns, primaryKeys);
|
|
238
|
+
}
|
|
186
239
|
const custom = [];
|
|
187
|
-
if (handlersArg)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
240
|
+
if (handlersArg) {
|
|
241
|
+
for (const prop of handlersArg.properties) {
|
|
242
|
+
if (!ts.isPropertyAssignment(prop) && !ts.isMethodDeclaration(prop)) continue;
|
|
243
|
+
const name = prop.name?.getText(sourceFile);
|
|
244
|
+
if (!name) continue;
|
|
245
|
+
let params = null;
|
|
246
|
+
if (ts.isPropertyAssignment(prop)) {
|
|
247
|
+
const init = prop.initializer;
|
|
248
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) {
|
|
249
|
+
params = init.parameters;
|
|
250
|
+
}
|
|
251
|
+
} else if (ts.isMethodDeclaration(prop)) {
|
|
252
|
+
params = prop.parameters;
|
|
253
|
+
}
|
|
254
|
+
if (!params) continue;
|
|
255
|
+
if (params.length < 2) {
|
|
256
|
+
custom.push({
|
|
257
|
+
name,
|
|
258
|
+
paramType: "void",
|
|
259
|
+
valibotCode: ""
|
|
260
|
+
});
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const secondParam = params[1];
|
|
264
|
+
const paramType = secondParam.type?.getText(sourceFile) || "unknown";
|
|
265
|
+
if (paramType === "unknown") {
|
|
266
|
+
custom.push({
|
|
267
|
+
name,
|
|
268
|
+
paramType: "unknown",
|
|
269
|
+
valibotCode: ""
|
|
270
|
+
});
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
let valibotCode = typeToValibot(paramType);
|
|
274
|
+
if (!valibotCode && resolvedTypes && resolvedTypeToValibot) {
|
|
275
|
+
const resolvedType = resolvedTypes.get(name);
|
|
276
|
+
if (resolvedType) {
|
|
277
|
+
valibotCode = resolvedTypeToValibot(resolvedType);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
207
280
|
custom.push({
|
|
208
281
|
name,
|
|
209
|
-
paramType
|
|
210
|
-
valibotCode: ""
|
|
282
|
+
paramType,
|
|
283
|
+
valibotCode: valibotCode || ""
|
|
211
284
|
});
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
let valibotCode = typeToValibot(paramType);
|
|
215
|
-
if (!valibotCode && resolvedTypes && resolvedTypeToValibot) {
|
|
216
|
-
const resolvedType = resolvedTypes.get(name);
|
|
217
|
-
resolvedType && (valibotCode = resolvedTypeToValibot(resolvedType));
|
|
218
285
|
}
|
|
219
|
-
custom.push({
|
|
220
|
-
name,
|
|
221
|
-
paramType,
|
|
222
|
-
valibotCode: valibotCode || ""
|
|
223
|
-
});
|
|
224
286
|
}
|
|
225
287
|
return {
|
|
226
288
|
modelName: "",
|
|
@@ -234,16 +296,24 @@ function extractSchemaColumns(ts, sourceFile, columns, primaryKeys) {
|
|
|
234
296
|
function visit(node) {
|
|
235
297
|
if (ts.isCallExpression(node)) {
|
|
236
298
|
const text = node.expression.getText(sourceFile);
|
|
237
|
-
if (text.endsWith(".primaryKey"))
|
|
299
|
+
if (text.endsWith(".primaryKey")) {
|
|
300
|
+
for (const arg of node.arguments) {
|
|
301
|
+
if (ts.isStringLiteral(arg)) {
|
|
302
|
+
primaryKeys.push(arg.text);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
238
306
|
if (text.endsWith(".columns") && node.arguments.length === 1) {
|
|
239
307
|
const obj = node.arguments[0];
|
|
240
|
-
if (ts.isObjectLiteralExpression(obj))
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
308
|
+
if (ts.isObjectLiteralExpression(obj)) {
|
|
309
|
+
for (const prop of obj.properties) {
|
|
310
|
+
if (!ts.isPropertyAssignment(prop)) continue;
|
|
311
|
+
const colName = prop.name?.getText(sourceFile);
|
|
312
|
+
if (!colName) continue;
|
|
313
|
+
const initText = prop.initializer.getText(sourceFile);
|
|
314
|
+
const colType = (0, import_generate_helpers.parseColumnType)(initText);
|
|
315
|
+
columns[colName] = colType;
|
|
316
|
+
}
|
|
247
317
|
}
|
|
248
318
|
}
|
|
249
319
|
}
|
|
@@ -252,7 +322,7 @@ function extractSchemaColumns(ts, sourceFile, columns, primaryKeys) {
|
|
|
252
322
|
visit(sourceFile);
|
|
253
323
|
}
|
|
254
324
|
function tsTypeToValibot(ts, checker, type, seen) {
|
|
255
|
-
|
|
325
|
+
if (!seen) seen = /* @__PURE__ */new Set();
|
|
256
326
|
const flags = type.getFlags();
|
|
257
327
|
if (flags & (ts.TypeFlags.Object | ts.TypeFlags.Intersection)) {
|
|
258
328
|
if (seen.has(type)) return "v.unknown()";
|
|
@@ -266,23 +336,38 @@ function tsTypeToValibot(ts, checker, type, seen) {
|
|
|
266
336
|
if (flags & ts.TypeFlags.Null) return "v.null_()";
|
|
267
337
|
if (flags & ts.TypeFlags.Any || flags & ts.TypeFlags.Unknown) return "v.unknown()";
|
|
268
338
|
if (flags & ts.TypeFlags.Never) return "v.never()";
|
|
269
|
-
if (flags & ts.TypeFlags.StringLiteral)
|
|
270
|
-
|
|
271
|
-
|
|
339
|
+
if (flags & ts.TypeFlags.StringLiteral) {
|
|
340
|
+
return `v.literal(${JSON.stringify(type.value)})`;
|
|
341
|
+
}
|
|
342
|
+
if (flags & ts.TypeFlags.NumberLiteral) {
|
|
343
|
+
return `v.literal(${type.value})`;
|
|
344
|
+
}
|
|
345
|
+
if (flags & ts.TypeFlags.BooleanLiteral) {
|
|
346
|
+
const name = type.intrinsicName;
|
|
347
|
+
return `v.literal(${name === "true"})`;
|
|
348
|
+
}
|
|
272
349
|
if (type.isUnion()) {
|
|
273
|
-
const members = type.types
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
350
|
+
const members = type.types;
|
|
351
|
+
const hasNull = members.some(t => t.getFlags() & ts.TypeFlags.Null);
|
|
352
|
+
const hasUndefined = members.some(t => t.getFlags() & (ts.TypeFlags.Undefined | ts.TypeFlags.Void));
|
|
353
|
+
const rest = members.filter(t => !(t.getFlags() & (ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.Void)));
|
|
277
354
|
if (rest.length === 2 && rest.every(t => t.getFlags() & ts.TypeFlags.BooleanLiteral)) {
|
|
278
355
|
let inner2 = "v.boolean()";
|
|
279
|
-
|
|
356
|
+
if (hasNull) inner2 = `v.nullable(${inner2})`;
|
|
357
|
+
if (hasUndefined) inner2 = `v.optional(${inner2})`;
|
|
358
|
+
return inner2;
|
|
280
359
|
}
|
|
281
360
|
if (rest.length === 0) return "v.unknown()";
|
|
282
361
|
let inner = rest.length === 1 ? recurse(rest[0]) : `v.union([${rest.map(t => recurse(t)).join(", ")}])`;
|
|
283
|
-
|
|
362
|
+
if (hasNull) inner = `v.nullable(${inner})`;
|
|
363
|
+
if (hasUndefined) inner = `v.optional(${inner})`;
|
|
364
|
+
return inner;
|
|
284
365
|
}
|
|
285
|
-
const resolveSymbolType = prop =>
|
|
366
|
+
const resolveSymbolType = prop => {
|
|
367
|
+
if (prop.valueDeclaration) return checker.getTypeOfSymbolAtLocation(prop, prop.valueDeclaration);
|
|
368
|
+
if (prop.declarations?.[0]) return checker.getTypeOfSymbolAtLocation(prop, prop.declarations[0]);
|
|
369
|
+
return checker.getDeclaredTypeOfSymbol(prop);
|
|
370
|
+
};
|
|
286
371
|
if (type.isIntersection()) {
|
|
287
372
|
const props2 = type.getProperties();
|
|
288
373
|
if (props2.length === 0) return "v.object({})";
|
|
@@ -290,69 +375,98 @@ function tsTypeToValibot(ts, checker, type, seen) {
|
|
|
290
375
|
for (const prop of props2) {
|
|
291
376
|
const name = prop.getName();
|
|
292
377
|
if ((0, import_generate_helpers.shouldSkipObjectKey)(name)) continue;
|
|
293
|
-
const propType = resolveSymbolType(prop)
|
|
294
|
-
|
|
378
|
+
const propType = resolveSymbolType(prop);
|
|
379
|
+
const isOptional = !!(prop.getFlags() & ts.SymbolFlags.Optional);
|
|
295
380
|
let val = recurse(propType);
|
|
296
|
-
isOptional && !val.startsWith("v.optional(")
|
|
381
|
+
if (isOptional && !val.startsWith("v.optional(")) {
|
|
382
|
+
val = `v.optional(${val})`;
|
|
383
|
+
}
|
|
384
|
+
entries.push(`${(0, import_generate_helpers.formatObjectKey)(name)}: ${val}`);
|
|
297
385
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
386
|
+
if (entries.length === 0) return "v.object({})";
|
|
387
|
+
return `v.object({
|
|
388
|
+
${entries.join(",\n ")},
|
|
301
389
|
})`;
|
|
302
390
|
}
|
|
303
391
|
const props = type.getProperties();
|
|
304
392
|
if (props.length > 0 && (type.getFlags() & ts.TypeFlags.Object || type.objectFlags)) {
|
|
305
393
|
const objectFlags = type.objectFlags ?? 0;
|
|
306
394
|
if (objectFlags & ts.ObjectFlags.Reference) {
|
|
307
|
-
const typeRef = type
|
|
308
|
-
|
|
309
|
-
|
|
395
|
+
const typeRef = type;
|
|
396
|
+
const symbol = type.getSymbol();
|
|
397
|
+
const name = symbol?.getName();
|
|
398
|
+
if ((name === "Array" || name === "ReadonlyArray") && typeRef.typeArguments?.length === 1) {
|
|
399
|
+
return `v.array(${recurse(typeRef.typeArguments[0])})`;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (objectFlags & ts.ObjectFlags.Tuple) {
|
|
403
|
+
const typeRef = type;
|
|
404
|
+
const typeArgs = typeRef.typeArguments || [];
|
|
405
|
+
return `v.tuple([${typeArgs.map(t => recurse(t)).join(", ")}])`;
|
|
310
406
|
}
|
|
311
|
-
if (objectFlags & ts.ObjectFlags.Tuple) return `v.tuple([${(type.typeArguments || []).map(t => recurse(t)).join(", ")}])`;
|
|
312
407
|
const entries = [];
|
|
313
408
|
for (const prop of props) {
|
|
314
409
|
const name = prop.getName();
|
|
315
410
|
if ((0, import_generate_helpers.shouldSkipObjectKey)(name)) continue;
|
|
316
|
-
const propType = resolveSymbolType(prop)
|
|
317
|
-
|
|
411
|
+
const propType = resolveSymbolType(prop);
|
|
412
|
+
const isOptional = !!(prop.getFlags() & ts.SymbolFlags.Optional);
|
|
318
413
|
let val = recurse(propType);
|
|
319
|
-
isOptional && !val.startsWith("v.optional(")
|
|
414
|
+
if (isOptional && !val.startsWith("v.optional(")) {
|
|
415
|
+
val = `v.optional(${val})`;
|
|
416
|
+
}
|
|
417
|
+
entries.push(`${(0, import_generate_helpers.formatObjectKey)(name)}: ${val}`);
|
|
320
418
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
419
|
+
if (entries.length === 0) return "v.object({})";
|
|
420
|
+
return `v.object({
|
|
421
|
+
${entries.join(",\n ")},
|
|
324
422
|
})`;
|
|
325
423
|
}
|
|
326
424
|
const stringIndex = type.getStringIndexType();
|
|
327
|
-
if (stringIndex)
|
|
425
|
+
if (stringIndex) {
|
|
426
|
+
return `v.record(v.string(), ${recurse(stringIndex)})`;
|
|
427
|
+
}
|
|
328
428
|
const numberIndex = type.getNumberIndexType();
|
|
329
|
-
|
|
429
|
+
if (numberIndex) {
|
|
430
|
+
return `v.record(v.number(), ${recurse(numberIndex)})`;
|
|
431
|
+
}
|
|
432
|
+
return "v.unknown()";
|
|
330
433
|
}
|
|
331
434
|
function serializeColumnBuilder(col) {
|
|
332
|
-
|
|
333
|
-
|
|
435
|
+
const zeroType = col.type === "string" ? "string" : col.type === "number" ? "number" : col.type === "boolean" ? "boolean" : "json";
|
|
436
|
+
let expr = `${zeroType}()`;
|
|
437
|
+
if (col.serverName) {
|
|
438
|
+
expr += `.from('${col.serverName}')`;
|
|
439
|
+
}
|
|
440
|
+
if (col.optional) {
|
|
441
|
+
expr += ".optional()";
|
|
442
|
+
}
|
|
443
|
+
return expr;
|
|
334
444
|
}
|
|
335
445
|
function generateDrizzleSchemaFile(schema) {
|
|
336
|
-
const lines = [
|
|
337
|
-
|
|
446
|
+
const lines = [`// auto-generated by: on-zero generate (from drizzle schema)`, `import { boolean, createSchema, json, number, relationships, string, table } from '@rocicorp/zero'`, ``];
|
|
447
|
+
const tableNames = Object.keys(schema.tables).sort();
|
|
338
448
|
for (const tableName of tableNames) {
|
|
339
|
-
const t = schema.tables[tableName]
|
|
340
|
-
|
|
341
|
-
`),
|
|
342
|
-
|
|
343
|
-
lines.push(`
|
|
449
|
+
const t = schema.tables[tableName];
|
|
450
|
+
const colEntries = Object.entries(t.columns).map(([colName, col]) => ` ${colName}: ${serializeColumnBuilder(col)},`).join("\n");
|
|
451
|
+
const pkArgs = t.primaryKey.map(k => `'${k}'`).join(", ");
|
|
452
|
+
lines.push(`const ${tableName}Table = table('${t.name}')`);
|
|
453
|
+
lines.push(` .columns({`);
|
|
454
|
+
lines.push(colEntries);
|
|
455
|
+
lines.push(` })`);
|
|
456
|
+
lines.push(` .primaryKey(${pkArgs})`);
|
|
457
|
+
lines.push(``);
|
|
344
458
|
}
|
|
345
459
|
const relTableNames = Object.keys(schema.relationships).sort();
|
|
346
460
|
for (const tableName of relTableNames) {
|
|
347
|
-
const rels = schema.relationships[tableName]
|
|
348
|
-
|
|
461
|
+
const rels = schema.relationships[tableName];
|
|
462
|
+
const relEntries = Object.entries(rels);
|
|
349
463
|
if (relEntries.length === 0) continue;
|
|
350
464
|
const relBody = relEntries.map(([relName, hops]) => {
|
|
351
465
|
if (hops.length === 1) {
|
|
352
|
-
const hop = hops[0]
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
466
|
+
const hop = hops[0];
|
|
467
|
+
const fn = hop.cardinality === "one" ? "one" : "many";
|
|
468
|
+
const sf = hop.sourceField.map(f => `'${f}'`).join(", ");
|
|
469
|
+
const df = hop.destField.map(f => `'${f}'`).join(", ");
|
|
356
470
|
return ` ${relName}: ${fn}({
|
|
357
471
|
sourceField: [${sf}],
|
|
358
472
|
destSchema: ${hop.destSchema}Table,
|
|
@@ -360,61 +474,73 @@ function generateDrizzleSchemaFile(schema) {
|
|
|
360
474
|
})`;
|
|
361
475
|
}
|
|
362
476
|
const hopCode = hops.map(hop => {
|
|
363
|
-
const fn = hop.cardinality === "one" ? "one" : "many"
|
|
364
|
-
|
|
365
|
-
|
|
477
|
+
const fn = hop.cardinality === "one" ? "one" : "many";
|
|
478
|
+
const sf = hop.sourceField.map(f => `'${f}'`).join(", ");
|
|
479
|
+
const df = hop.destField.map(f => `'${f}'`).join(", ");
|
|
366
480
|
return `${fn}({ sourceField: [${sf}], destSchema: ${hop.destSchema}Table, destField: [${df}] })`;
|
|
367
481
|
}).join(", ");
|
|
368
482
|
return ` ${relName}: [${hopCode}]`;
|
|
369
|
-
}).join(
|
|
370
|
-
`);
|
|
371
|
-
lines.push(
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
`);
|
|
377
|
-
|
|
378
|
-
`);
|
|
483
|
+
}).join(",\n");
|
|
484
|
+
lines.push(`const ${tableName}Relationships = relationships(${tableName}Table, ({ one, many }) => ({`);
|
|
485
|
+
lines.push(relBody);
|
|
486
|
+
lines.push(`}))`);
|
|
487
|
+
lines.push(``);
|
|
488
|
+
}
|
|
489
|
+
const tableList = tableNames.map(n => ` ${n}Table,`).join("\n");
|
|
490
|
+
const relList = relTableNames.filter(n => Object.keys(schema.relationships[n]).length > 0).map(n => ` ${n}Relationships,`).join("\n");
|
|
491
|
+
lines.push(`export const schema = createSchema({`);
|
|
492
|
+
lines.push(` tables: [`);
|
|
493
|
+
lines.push(tableList);
|
|
494
|
+
lines.push(` ],`);
|
|
495
|
+
lines.push(` relationships: [`);
|
|
496
|
+
lines.push(relList);
|
|
497
|
+
lines.push(` ],`);
|
|
498
|
+
lines.push(`})`);
|
|
499
|
+
lines.push(``);
|
|
500
|
+
return lines.join("\n");
|
|
379
501
|
}
|
|
380
502
|
async function generate(options) {
|
|
381
503
|
const {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
(0, import_node_fs.existsSync)(generatedDir)
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
504
|
+
dir,
|
|
505
|
+
after,
|
|
506
|
+
silent
|
|
507
|
+
} = options;
|
|
508
|
+
const baseDir = (0, import_node_path.resolve)(dir);
|
|
509
|
+
const mutationsDir = (0, import_node_path.resolve)(baseDir, "mutations");
|
|
510
|
+
const usesMutationsDir = (0, import_node_fs.existsSync)(mutationsDir);
|
|
511
|
+
const modelsDir = usesMutationsDir ? mutationsDir : (0, import_node_path.resolve)(baseDir, "models");
|
|
512
|
+
const modelsDirName = usesMutationsDir ? "mutations" : "models";
|
|
513
|
+
const generatedDir = (0, import_node_path.resolve)(baseDir, "generated");
|
|
514
|
+
const queriesDir = (0, import_node_path.resolve)(baseDir, "queries");
|
|
515
|
+
if (!(0, import_node_fs.existsSync)(generatedDir)) {
|
|
516
|
+
(0, import_node_fs.mkdirSync)(generatedDir, {
|
|
517
|
+
recursive: true
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
loadCache();
|
|
521
|
+
const allModelFiles = (0, import_node_fs.readdirSync)(modelsDir).filter(f => f.endsWith(".ts")).sort();
|
|
522
|
+
const filesWithSchema = allModelFiles.filter(f => (0, import_node_fs.readFileSync)((0, import_node_path.resolve)(modelsDir, f), "utf-8").includes("export const schema = table("));
|
|
523
|
+
const allModelNames = allModelFiles.map(f => (0, import_node_path.basename)(f, ".ts"));
|
|
524
|
+
const schemaModelNames = filesWithSchema.map(f => (0, import_node_path.basename)(f, ".ts"));
|
|
525
|
+
const writeResults = [writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "models.ts"), (0, import_generate_helpers.generateModelsFile)(allModelNames, modelsDirName)),
|
|
526
|
+
// only generate types.ts and tables.ts when model files define schemas.
|
|
527
|
+
// when using drizzle-zero CLI for schema generation, these files are
|
|
528
|
+
// managed externally and should not be overwritten.
|
|
529
|
+
...(filesWithSchema.length > 0 ? [writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "types.ts"), (0, import_generate_helpers.generateTypesFile)(schemaModelNames)), writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "tables.ts"), (0, import_generate_helpers.generateTablesFile)(schemaModelNames, modelsDirName))] : []), writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "README.md"), (0, import_generate_helpers.generateReadmeFile)())];
|
|
530
|
+
let filesChanged = writeResults.filter(Boolean).length;
|
|
531
|
+
let queryCount = 0;
|
|
532
|
+
let mutationCount = 0;
|
|
533
|
+
const ts = await import("typescript");
|
|
534
|
+
const typeToValibot = paramType => {
|
|
535
|
+
try {
|
|
536
|
+
return (0, import_generate_helpers.parseTypeString)(paramType.trim());
|
|
537
|
+
} catch {
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
};
|
|
415
541
|
if ((0, import_node_fs.existsSync)(queriesDir)) {
|
|
416
|
-
const queryFiles = (0, import_node_fs.readdirSync)(queriesDir).filter(f => f.endsWith(".ts"))
|
|
417
|
-
|
|
542
|
+
const queryFiles = (0, import_node_fs.readdirSync)(queriesDir).filter(f => f.endsWith(".ts"));
|
|
543
|
+
const allQueries = [];
|
|
418
544
|
let queryResolver = null;
|
|
419
545
|
const getQueryResolver = () => {
|
|
420
546
|
if (!queryResolver) {
|
|
@@ -427,14 +553,15 @@ async function generate(options) {
|
|
|
427
553
|
return queryResolver;
|
|
428
554
|
};
|
|
429
555
|
for (const file of queryFiles) {
|
|
430
|
-
const filePath = (0, import_node_path.resolve)(queriesDir, file)
|
|
431
|
-
|
|
556
|
+
const filePath = (0, import_node_path.resolve)(queriesDir, file);
|
|
557
|
+
const fileBaseName = (0, import_node_path.basename)(file, ".ts");
|
|
432
558
|
try {
|
|
433
|
-
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8")
|
|
434
|
-
|
|
559
|
+
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8");
|
|
560
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
435
561
|
ts.forEachChild(sourceFile, node => {
|
|
436
562
|
if (ts.isVariableStatement(node)) {
|
|
437
|
-
|
|
563
|
+
const exportModifier = node.modifiers?.find(m => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
564
|
+
if (!exportModifier) return;
|
|
438
565
|
const declaration = node.declarationList.declarations[0];
|
|
439
566
|
if (!declaration || !ts.isVariableDeclaration(declaration)) return;
|
|
440
567
|
const name = declaration.name.getText(sourceFile);
|
|
@@ -442,40 +569,50 @@ async function generate(options) {
|
|
|
442
569
|
if (declaration.initializer && ts.isArrowFunction(declaration.initializer)) {
|
|
443
570
|
const params = declaration.initializer.parameters;
|
|
444
571
|
let paramType = "void";
|
|
445
|
-
params.length > 0
|
|
572
|
+
if (params.length > 0) {
|
|
573
|
+
const param = params[0];
|
|
574
|
+
paramType = param.type?.getText(sourceFile) || "unknown";
|
|
575
|
+
}
|
|
446
576
|
let valibotCode = typeToValibot(paramType);
|
|
447
577
|
if (!valibotCode && params.length > 0 && params[0].type) {
|
|
448
|
-
const resolver = getQueryResolver()
|
|
449
|
-
|
|
578
|
+
const resolver = getQueryResolver();
|
|
579
|
+
const resolverSourceFile = resolver.program.getSourceFile(filePath);
|
|
450
580
|
if (resolverSourceFile) {
|
|
451
581
|
const resolvedType = resolveParamType(ts, resolver, resolverSourceFile, name, 0);
|
|
452
|
-
|
|
582
|
+
if (resolvedType) {
|
|
583
|
+
valibotCode = resolver.typeToValibot(resolvedType);
|
|
584
|
+
}
|
|
453
585
|
}
|
|
454
586
|
}
|
|
455
|
-
valibotCode
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
587
|
+
if (valibotCode) {
|
|
588
|
+
allQueries.push({
|
|
589
|
+
name,
|
|
590
|
+
params: paramType,
|
|
591
|
+
valibotCode,
|
|
592
|
+
sourceFile: fileBaseName
|
|
593
|
+
});
|
|
594
|
+
} else if (!silent && paramType !== "void") {
|
|
595
|
+
console.error(`\u2717 ${name}: could not resolve type "${paramType}"`);
|
|
596
|
+
}
|
|
461
597
|
}
|
|
462
598
|
}
|
|
463
599
|
});
|
|
464
600
|
} catch (err) {
|
|
465
|
-
silent
|
|
601
|
+
if (!silent) console.error(`Error processing ${file}:`, err);
|
|
466
602
|
}
|
|
467
603
|
}
|
|
468
604
|
queryCount = allQueries.length;
|
|
469
|
-
const groupedChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "groupedQueries.ts"), (0, import_generate_helpers.generateGroupedQueriesFile)(allQueries))
|
|
470
|
-
|
|
471
|
-
groupedChanged
|
|
605
|
+
const groupedChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "groupedQueries.ts"), (0, import_generate_helpers.generateGroupedQueriesFile)(allQueries));
|
|
606
|
+
const syncedChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "syncedQueries.ts"), (0, import_generate_helpers.generateSyncedQueriesFile)(allQueries));
|
|
607
|
+
if (groupedChanged) filesChanged++;
|
|
608
|
+
if (syncedChanged) filesChanged++;
|
|
472
609
|
}
|
|
473
|
-
const allModelMutations = []
|
|
474
|
-
|
|
475
|
-
|
|
610
|
+
const allModelMutations = [];
|
|
611
|
+
const mutationFiles = [];
|
|
612
|
+
const unresolvedModels = [];
|
|
476
613
|
for (const file of allModelFiles) {
|
|
477
|
-
const filePath = (0, import_node_path.resolve)(modelsDir, file)
|
|
478
|
-
|
|
614
|
+
const filePath = (0, import_node_path.resolve)(modelsDir, file);
|
|
615
|
+
const fileBaseName = (0, import_node_path.basename)(file, ".ts");
|
|
479
616
|
try {
|
|
480
617
|
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8");
|
|
481
618
|
mutationFiles.push({
|
|
@@ -483,32 +620,43 @@ async function generate(options) {
|
|
|
483
620
|
content,
|
|
484
621
|
baseName: fileBaseName
|
|
485
622
|
});
|
|
486
|
-
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest,
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
623
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
624
|
+
const result = extractMutationsFromModel(ts, sourceFile, content, filePath, !!silent, typeToValibot);
|
|
625
|
+
if (result) {
|
|
626
|
+
result.modelName = fileBaseName;
|
|
627
|
+
allModelMutations.push(result);
|
|
628
|
+
const hasUnresolved = result.custom.some(m => m.paramType !== "void" && m.paramType !== "unknown" && !m.valibotCode);
|
|
629
|
+
if (hasUnresolved) {
|
|
630
|
+
unresolvedModels.push({
|
|
631
|
+
baseName: fileBaseName,
|
|
632
|
+
filePath
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}
|
|
492
636
|
} catch (err) {
|
|
493
|
-
silent
|
|
637
|
+
if (!silent) console.error(`Error extracting mutations from ${file}:`, err);
|
|
494
638
|
}
|
|
495
639
|
}
|
|
496
640
|
if (unresolvedModels.length > 0) {
|
|
497
641
|
const collectTsFiles = dir2 => {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
642
|
+
const results = [];
|
|
643
|
+
for (const entry of (0, import_node_fs.readdirSync)(dir2, {
|
|
644
|
+
withFileTypes: true
|
|
645
|
+
})) {
|
|
646
|
+
const fullPath = (0, import_node_path.resolve)(dir2, entry.name);
|
|
647
|
+
if (entry.isDirectory() && entry.name !== "node_modules") {
|
|
648
|
+
results.push(...collectTsFiles(fullPath));
|
|
649
|
+
} else if (entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".d.ts")) {
|
|
650
|
+
results.push({
|
|
504
651
|
path: fullPath,
|
|
505
652
|
content: (0, import_node_fs.readFileSync)(fullPath, "utf-8")
|
|
506
653
|
});
|
|
507
654
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
655
|
+
}
|
|
656
|
+
return results;
|
|
657
|
+
};
|
|
658
|
+
const allFiles = collectTsFiles(baseDir);
|
|
659
|
+
const modelResolver = createTypeResolver(ts, allFiles, baseDir);
|
|
512
660
|
for (const {
|
|
513
661
|
baseName,
|
|
514
662
|
filePath
|
|
@@ -517,18 +665,28 @@ async function generate(options) {
|
|
|
517
665
|
if (!resolverSourceFile) continue;
|
|
518
666
|
const resolvedTypes = resolveMutationParamTypes(ts, modelResolver, resolverSourceFile);
|
|
519
667
|
if (resolvedTypes.size === 0) continue;
|
|
520
|
-
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8")
|
|
521
|
-
|
|
522
|
-
|
|
668
|
+
const content = (0, import_node_fs.readFileSync)(filePath, "utf-8");
|
|
669
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
670
|
+
const result = extractMutationsFromModel(ts, sourceFile, content, filePath, !!silent, typeToValibot, resolvedTypes, modelResolver.typeToValibot);
|
|
523
671
|
if (result) {
|
|
524
672
|
result.modelName = baseName;
|
|
525
673
|
const idx = allModelMutations.findIndex(m => m.modelName === baseName);
|
|
526
|
-
idx >= 0
|
|
674
|
+
if (idx >= 0) allModelMutations[idx] = result;
|
|
527
675
|
}
|
|
528
676
|
}
|
|
529
677
|
}
|
|
530
|
-
for (const model of allModelMutations)
|
|
531
|
-
|
|
678
|
+
for (const model of allModelMutations) {
|
|
679
|
+
if (model.hasCRUD) mutationCount += 3;
|
|
680
|
+
mutationCount += model.custom.filter(m => !model.hasCRUD || !["insert", "update", "delete", "upsert"].includes(m.name)).length;
|
|
681
|
+
}
|
|
682
|
+
if (allModelMutations.length > 0) {
|
|
683
|
+
const mutationsChanged = writeFileIfChanged((0, import_node_path.resolve)(generatedDir, "syncedMutations.ts"), (0, import_generate_helpers.generateSyncedMutationsFile)(allModelMutations));
|
|
684
|
+
if (mutationsChanged) filesChanged++;
|
|
685
|
+
}
|
|
686
|
+
if (filesChanged > 0 && !silent) {
|
|
687
|
+
console.info(`\u2713 ${allModelFiles.length} models (${filesWithSchema.length} schemas)${queryCount ? `, ${queryCount} queries` : ""}${mutationCount ? `, ${mutationCount} mutations` : ""}`);
|
|
688
|
+
}
|
|
689
|
+
if (filesChanged > 0 && after) {
|
|
532
690
|
const {
|
|
533
691
|
execSync
|
|
534
692
|
} = await import("node:child_process");
|
|
@@ -541,10 +699,11 @@ async function generate(options) {
|
|
|
541
699
|
}
|
|
542
700
|
});
|
|
543
701
|
} catch (err) {
|
|
544
|
-
silent
|
|
702
|
+
if (!silent) console.error(`Error running after command: ${err}`);
|
|
545
703
|
}
|
|
546
704
|
}
|
|
547
|
-
|
|
705
|
+
saveCache();
|
|
706
|
+
return {
|
|
548
707
|
filesChanged,
|
|
549
708
|
modelCount: allModelFiles.length,
|
|
550
709
|
schemaCount: filesWithSchema.length,
|
|
@@ -554,34 +713,39 @@ async function generate(options) {
|
|
|
554
713
|
}
|
|
555
714
|
async function watch(options) {
|
|
556
715
|
const {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
716
|
+
dir,
|
|
717
|
+
debounce = 1e3
|
|
718
|
+
} = options;
|
|
719
|
+
const baseDir = (0, import_node_path.resolve)(dir);
|
|
720
|
+
const mutationsDir = (0, import_node_path.resolve)(baseDir, "mutations");
|
|
721
|
+
const modelsDir = (0, import_node_fs.existsSync)(mutationsDir) ? mutationsDir : (0, import_node_path.resolve)(baseDir, "models");
|
|
722
|
+
const queriesDir = (0, import_node_path.resolve)(baseDir, "queries");
|
|
723
|
+
const generatedDir = (0, import_node_path.resolve)(baseDir, "generated");
|
|
565
724
|
await generate({
|
|
566
725
|
...options,
|
|
567
|
-
silent:
|
|
568
|
-
})
|
|
569
|
-
|
|
726
|
+
silent: true
|
|
727
|
+
});
|
|
728
|
+
console.info("\u{1F440} watching...\n");
|
|
570
729
|
const chokidar = await import("chokidar");
|
|
571
730
|
let debounceTimer = null;
|
|
572
731
|
const debouncedRegenerate = (path, event) => {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
732
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
733
|
+
console.info(`
|
|
734
|
+
${event} ${path}`);
|
|
735
|
+
debounceTimer = setTimeout(() => {
|
|
736
|
+
generate({
|
|
737
|
+
...options,
|
|
738
|
+
silent: false
|
|
739
|
+
});
|
|
740
|
+
}, debounce);
|
|
741
|
+
};
|
|
742
|
+
const watcher = chokidar.watch([modelsDir, queriesDir], {
|
|
743
|
+
persistent: true,
|
|
744
|
+
ignoreInitial: true,
|
|
745
|
+
ignored: [generatedDir]
|
|
746
|
+
});
|
|
747
|
+
watcher.on("change", path => debouncedRegenerate(path, "\u{1F4DD}"));
|
|
748
|
+
watcher.on("add", path => debouncedRegenerate(path, "\u2795"));
|
|
749
|
+
watcher.on("unlink", path => debouncedRegenerate(path, "\u{1F5D1}\uFE0F "));
|
|
750
|
+
return watcher;
|
|
587
751
|
}
|