@lark-apaas/db-schema-sync 0.1.0-alpha.4 → 0.1.0-alpha.6
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/bin.js +5 -2
- package/dist/{chunk-JZGFS3J2.js → chunk-MTFKSLNU.js} +11 -11
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
__toESM,
|
|
7
7
|
generateSchema,
|
|
8
8
|
resolveEnvOptions
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-MTFKSLNU.js";
|
|
10
10
|
|
|
11
11
|
// node_modules/dotenv/package.json
|
|
12
12
|
var require_package = __commonJS({
|
|
@@ -428,8 +428,11 @@ async function main() {
|
|
|
428
428
|
try {
|
|
429
429
|
const options = resolveEnvOptions();
|
|
430
430
|
console.log("[db-schema-sync] Starting...");
|
|
431
|
-
console.log(`[db-schema-sync] app_id=${options.appId},
|
|
431
|
+
console.log(`[db-schema-sync] app_id=${options.appId}, dbBranch=${options.dbBranch}`);
|
|
432
432
|
const schema = await generateSchema(options);
|
|
433
|
+
if (!schema.includes("pgTable") && !schema.includes("pgView")) {
|
|
434
|
+
console.warn("[db-schema-sync] \u26A0 Warning: No tables or views found in database. The generated schema will be empty.");
|
|
435
|
+
}
|
|
433
436
|
const outputPath = resolve(process.cwd(), output);
|
|
434
437
|
mkdirSync(dirname(outputPath), { recursive: true });
|
|
435
438
|
writeFileSync(outputPath, schema, "utf8");
|
|
@@ -929,22 +929,17 @@ function getHttpClient() {
|
|
|
929
929
|
}
|
|
930
930
|
function resolveEnvOptions(env = process.env) {
|
|
931
931
|
const appId = env.app_id;
|
|
932
|
-
const workspace = env.suda_workspace_id;
|
|
933
932
|
if (!appId) {
|
|
934
933
|
throw new Error("[db-schema-sync] Error: app_id environment variable is required. Set it in your .env file or shell environment.");
|
|
935
934
|
}
|
|
936
|
-
if (!workspace) {
|
|
937
|
-
throw new Error("[db-schema-sync] Error: suda_workspace_id environment variable is required. Set it in your .env file or shell environment.");
|
|
938
|
-
}
|
|
939
935
|
return {
|
|
940
936
|
appId,
|
|
941
|
-
workspace,
|
|
942
937
|
dbBranch: env.FORCE_DB_BRANCH || "main",
|
|
943
938
|
timeoutMs: env.SCHEMA_API_TIMEOUT_MS ? Number(env.SCHEMA_API_TIMEOUT_MS) : 3e4
|
|
944
939
|
};
|
|
945
940
|
}
|
|
946
941
|
async function fetchListTableView(options) {
|
|
947
|
-
const { appId,
|
|
942
|
+
const { appId, dbBranch = "main", timeoutMs = 3e4 } = options;
|
|
948
943
|
const client = getHttpClient();
|
|
949
944
|
const controller = new AbortController();
|
|
950
945
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
@@ -956,7 +951,7 @@ async function fetchListTableView(options) {
|
|
|
956
951
|
};
|
|
957
952
|
if (ttEnv) headers["x-tt-env"] = ttEnv;
|
|
958
953
|
if (webUser) headers["X-Larkgw-Suda-Webuser"] = webUser;
|
|
959
|
-
console.log(`[db-schema-sync] GET /v1/app/${appId}/dataloom/schema?dbBranch=${dbBranch}
|
|
954
|
+
console.log(`[db-schema-sync] GET /v1/app/${appId}/dataloom/schema?dbBranch=${dbBranch}`);
|
|
960
955
|
if (ttEnv) console.log(`[db-schema-sync] x-tt-env: ${ttEnv}`);
|
|
961
956
|
const response = await client.get(
|
|
962
957
|
`/v1/app/${appId}/dataloom/schema`,
|
|
@@ -978,7 +973,10 @@ Body: ${body.slice(0, 500)}`);
|
|
|
978
973
|
}
|
|
979
974
|
const json = await response.json();
|
|
980
975
|
const data = json.data;
|
|
981
|
-
|
|
976
|
+
if (!data) {
|
|
977
|
+
return {};
|
|
978
|
+
}
|
|
979
|
+
return data?.data ?? data?.schema ?? data ?? {};
|
|
982
980
|
} catch (err) {
|
|
983
981
|
if (err?.response) {
|
|
984
982
|
const headers = err.response.headers;
|
|
@@ -1158,6 +1156,9 @@ function extractSyncedTableMap(tables) {
|
|
|
1158
1156
|
return map;
|
|
1159
1157
|
}
|
|
1160
1158
|
function normalizeApiResponse(response) {
|
|
1159
|
+
if (!response) {
|
|
1160
|
+
return { tables: [], views: [], materializedViews: [], enums: [], sequences: [], syncedTableMap: /* @__PURE__ */ new Map() };
|
|
1161
|
+
}
|
|
1161
1162
|
const usedTableIdentifiers = /* @__PURE__ */ new Set();
|
|
1162
1163
|
const usedViewIdentifiers = /* @__PURE__ */ new Set();
|
|
1163
1164
|
const usedMViewIdentifiers = /* @__PURE__ */ new Set();
|
|
@@ -1830,8 +1831,7 @@ var TYPE_RULES = [
|
|
|
1830
1831
|
{
|
|
1831
1832
|
usageName: "customTimestamptz",
|
|
1832
1833
|
definitionCheck: "const customTimestamptz = customType<",
|
|
1833
|
-
definition: CUSTOM_TIMESTAMPTZ_DEFINITION
|
|
1834
|
-
alwaysExport: true
|
|
1834
|
+
definition: CUSTOM_TIMESTAMPTZ_DEFINITION
|
|
1835
1835
|
},
|
|
1836
1836
|
{
|
|
1837
1837
|
usageName: "userProfile",
|
|
@@ -1893,7 +1893,7 @@ var inlineTypesEnhancer = {
|
|
|
1893
1893
|
for (const rule of TYPE_RULES) {
|
|
1894
1894
|
if (usedTypes.has(rule.usageName) && !text.includes(rule.definitionCheck)) {
|
|
1895
1895
|
let def = rule.definition;
|
|
1896
|
-
if (
|
|
1896
|
+
if (exportCustomTypes) {
|
|
1897
1897
|
def = def.replace(/^(const |function |type )/gm, "export $1");
|
|
1898
1898
|
}
|
|
1899
1899
|
definitions.push(def);
|
package/dist/index.d.ts
CHANGED
|
@@ -157,13 +157,12 @@ interface ApiTableViewResponse {
|
|
|
157
157
|
|
|
158
158
|
interface FetchOptions {
|
|
159
159
|
appId: string;
|
|
160
|
-
workspace: string;
|
|
161
160
|
dbBranch?: string;
|
|
162
161
|
timeoutMs?: number;
|
|
163
162
|
}
|
|
164
163
|
declare function resolveEnvOptions(env?: Record<string, string | undefined>): FetchOptions;
|
|
165
164
|
|
|
166
|
-
declare function normalizeApiResponse(response: ApiTableViewResponse): SchemaData;
|
|
165
|
+
declare function normalizeApiResponse(response: ApiTableViewResponse | null | undefined): SchemaData;
|
|
167
166
|
|
|
168
167
|
declare function generateSchemaCode(data: SchemaData): string;
|
|
169
168
|
|
package/dist/index.js
CHANGED