@lark-apaas/db-schema-sync 0.1.26-alpha.20261202064925 → 0.1.26-beta.0
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/README.md +94 -0
- package/dist/bin.js +44 -436
- package/dist/check-bin.d.ts +1 -0
- package/dist/check-bin.js +448 -0
- package/dist/chunk-IMNF2S2C.js +2413 -0
- package/dist/{chunk-WHMUF2LL.js → chunk-LNC6F2DI.js} +1333 -760
- package/dist/index.d.ts +126 -3
- package/dist/index.js +7 -1
- package/package.json +8 -3
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# db-schema-sync
|
|
2
|
+
|
|
3
|
+
从应用 schema 接口生成 Drizzle 定义。多数据源配置使用 `MIAODA_DATASOURCE_CONF`,PG 写入 `server/database/schema.ts`,Base 在同目录生成三个文件,请一起提交或部署。
|
|
4
|
+
|
|
5
|
+
- `base.ts` 包含表定义、选项常量、对应的选项名称类型和表别名,业务继续从 `./base` 导入表、函数及类型。
|
|
6
|
+
- `base-helper.ts` 包含 Base 列构造器、查询和编解码函数。
|
|
7
|
+
- `type.ts` 包含通用的 Base 值类型,内容由 SDK 定义。
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npx @lark-apaas/db-schema-sync --output server/database/schema.ts --type base
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 多个多维表格
|
|
14
|
+
|
|
15
|
+
当前 Base 接口结构:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"engine": "base",
|
|
20
|
+
"schemas": [
|
|
21
|
+
{ "schemaName": "BaseIT", "apiID": "1001", "comment": "IT 服务台" },
|
|
22
|
+
{ "schemaName": "BaseAdmin", "apiID": "1002", "comment": "行政服务台" }
|
|
23
|
+
],
|
|
24
|
+
"tables": {
|
|
25
|
+
"count": 2,
|
|
26
|
+
"data": [
|
|
27
|
+
{ "tableName": "ticket", "apiID": "tblIT", "schema": "BaseIT", "fields": [] },
|
|
28
|
+
{ "tableName": "ticket", "apiID": "tblAdmin", "schema": "BaseAdmin", "fields": [] }
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
在同一个 `base.ts` 中生成:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
// 此处省略字段
|
|
38
|
+
export const baseit = pgSchema('BaseIT');
|
|
39
|
+
export const baseitTicket = baseit.table('tblIT', {});
|
|
40
|
+
export const baseadmin = pgSchema('BaseAdmin');
|
|
41
|
+
export const baseadminTicket = baseadmin.table('tblAdmin', {});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
- 物理 schema 使用 `schemaName`;`apiID` 不作为 schema 名称。
|
|
45
|
+
- Base 的 SQL 表名取表级 `apiID`。缺失或空白时生成失败。TS 导出名和选项说明仍使用 `tableName`,列仍使用 `fieldName`。
|
|
46
|
+
- 表按 `table.schema` 归属;同名表的变量加 Base 前缀,SQL 路由由生成的表对象携带。
|
|
47
|
+
- 当前接口不提供稳定别名,因此变量名从 `schemaName` 生成。`comment` 只作注释,改标题或改变目录顺序不会重命名这些导出。
|
|
48
|
+
- 没有表的 Base 仍生成 `pgSchema` 声明,不为它补造其他 Base 的表。
|
|
49
|
+
- 已声明 schemas 的响应中,表若缺少 schema 归属,当前数据源生成失败,原文件保留。
|
|
50
|
+
- 旧 `id/name/description` 元数据仍可解析;没有 schemas 的旧响应使用平铺的 `pgTable(apiID, ...)`。通过 `getTableConfig(table).name` 读取到的是表 ID。
|
|
51
|
+
|
|
52
|
+
## NestJS 消费
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { Inject, Injectable } from '@nestjs/common';
|
|
56
|
+
import {
|
|
57
|
+
BASE_DATABASE,
|
|
58
|
+
PG_DATABASE,
|
|
59
|
+
type BaseDatabase,
|
|
60
|
+
type PostgresJsDatabase,
|
|
61
|
+
} from '@lark-apaas/fullstack-nestjs-core';
|
|
62
|
+
import { baseitTicket, baseadminTicket } from './database/base';
|
|
63
|
+
|
|
64
|
+
@Injectable()
|
|
65
|
+
export class Service {
|
|
66
|
+
constructor(
|
|
67
|
+
@Inject(BASE_DATABASE) private readonly base: BaseDatabase,
|
|
68
|
+
@Inject(PG_DATABASE) private readonly pg: PostgresJsDatabase,
|
|
69
|
+
) {}
|
|
70
|
+
|
|
71
|
+
listIT() {
|
|
72
|
+
return this.base.select().from(baseitTicket);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
listAdministration() {
|
|
76
|
+
return this.base.select().from(baseadminTicket);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
多个 Base 共用同一个 `BASE_DATABASE` 实例,应用层无需再次声明 schema 或建立连接。PG 的表仍从 `database/schema.ts` 引用,并交给 `PG_DATABASE` 实例执行。
|
|
82
|
+
|
|
83
|
+
这项改动负责 schema 生成及其消费;数据库端仍需支持 schema-qualified SQL。生成和编译成功不等于实际引擎、权限及业务 CRUD 已验证。
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
### 检查关联 Base 的结构变化
|
|
87
|
+
|
|
88
|
+
在应用根目录运行 `miaoda db schema check --json` 或 `miaoda db schema check --source base --json`,以工程现有的 `server/database/base.ts` 为基准。检测在内存中生成最新的 `base.ts`,根据文件头 JSDoc 匹配 Base、表和字段名,再从生成的列声明和字段注释读取类型。`db-schema-check` 将结构化差异放在 `data.results[].changes`,`miaoda db schema check` 将其逐项汇总为 `data.results[].message`。
|
|
89
|
+
|
|
90
|
+
检查请求通过 `MIAODA_CANARY_HEADER` 设置泳道,默认 `prod`。显式传空字符串时省略泳道头,`ppe_` 泳道同时发送 `x-use-ppe: 1`。
|
|
91
|
+
|
|
92
|
+
检查只跟踪基线中已有的 Base 和数据表。仅新增多维表格或数据表时返回 `unchanged`,`changes` 为空。同时修改已有表时,仍会报告该表的变化,包括新增字段。
|
|
93
|
+
|
|
94
|
+
设置 `DB_SCHEMA_OUTPUT` 时使用其所在目录的 `base.ts`。原始 `base.ts` 缺失时返回 `no_baseline`。Base 与表按生成文件中的标识匹配,字段按列名匹配。字段仅调序时返回 `unchanged`。同一张表的字段名同时有增减时返回一条表级 `updated`,`before.field_names` 和 `after.field_names` 分别列出变更前后的完整字段名,各自保留 schema 中的顺序。仅新增或仅移除时按字段报告。同名字段的 Base 类型、公式或查找引用的结果类型、标量与数组之间的变化返回 `field / updated`,新旧类型分别在 `before.field_type` 和 `after.field_type`。已有文件缺少可识别的字段清单时检查失败。Base 检查失败时对应结果项包含 `status: "failed"`、空的 `changes` 和 `error`。
|
package/dist/bin.js
CHANGED
|
@@ -1,415 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createRequire } from "node:module"; const require = createRequire(import.meta.url);
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
require_main,
|
|
5
|
+
runSync
|
|
6
|
+
} from "./chunk-IMNF2S2C.js";
|
|
7
|
+
import {
|
|
8
|
+
DATASOURCE_TYPES,
|
|
6
9
|
__toESM,
|
|
7
|
-
generateSchema,
|
|
8
|
-
resolveEnvOptions,
|
|
9
10
|
validateGeneratedSchema
|
|
10
|
-
} from "./chunk-
|
|
11
|
-
|
|
12
|
-
// node_modules/dotenv/package.json
|
|
13
|
-
var require_package = __commonJS({
|
|
14
|
-
"node_modules/dotenv/package.json"(exports, module) {
|
|
15
|
-
module.exports = {
|
|
16
|
-
name: "dotenv",
|
|
17
|
-
version: "17.3.1",
|
|
18
|
-
description: "Loads environment variables from .env file",
|
|
19
|
-
main: "lib/main.js",
|
|
20
|
-
types: "lib/main.d.ts",
|
|
21
|
-
exports: {
|
|
22
|
-
".": {
|
|
23
|
-
types: "./lib/main.d.ts",
|
|
24
|
-
require: "./lib/main.js",
|
|
25
|
-
default: "./lib/main.js"
|
|
26
|
-
},
|
|
27
|
-
"./config": "./config.js",
|
|
28
|
-
"./config.js": "./config.js",
|
|
29
|
-
"./lib/env-options": "./lib/env-options.js",
|
|
30
|
-
"./lib/env-options.js": "./lib/env-options.js",
|
|
31
|
-
"./lib/cli-options": "./lib/cli-options.js",
|
|
32
|
-
"./lib/cli-options.js": "./lib/cli-options.js",
|
|
33
|
-
"./package.json": "./package.json"
|
|
34
|
-
},
|
|
35
|
-
scripts: {
|
|
36
|
-
"dts-check": "tsc --project tests/types/tsconfig.json",
|
|
37
|
-
lint: "standard",
|
|
38
|
-
pretest: "npm run lint && npm run dts-check",
|
|
39
|
-
test: "tap run tests/**/*.js --allow-empty-coverage --disable-coverage --timeout=60000",
|
|
40
|
-
"test:coverage": "tap run tests/**/*.js --show-full-coverage --timeout=60000 --coverage-report=text --coverage-report=lcov",
|
|
41
|
-
prerelease: "npm test",
|
|
42
|
-
release: "standard-version"
|
|
43
|
-
},
|
|
44
|
-
repository: {
|
|
45
|
-
type: "git",
|
|
46
|
-
url: "git://github.com/motdotla/dotenv.git"
|
|
47
|
-
},
|
|
48
|
-
homepage: "https://github.com/motdotla/dotenv#readme",
|
|
49
|
-
funding: "https://dotenvx.com",
|
|
50
|
-
keywords: [
|
|
51
|
-
"dotenv",
|
|
52
|
-
"env",
|
|
53
|
-
".env",
|
|
54
|
-
"environment",
|
|
55
|
-
"variables",
|
|
56
|
-
"config",
|
|
57
|
-
"settings"
|
|
58
|
-
],
|
|
59
|
-
readmeFilename: "README.md",
|
|
60
|
-
license: "BSD-2-Clause",
|
|
61
|
-
devDependencies: {
|
|
62
|
-
"@types/node": "^18.11.3",
|
|
63
|
-
decache: "^4.6.2",
|
|
64
|
-
sinon: "^14.0.1",
|
|
65
|
-
standard: "^17.0.0",
|
|
66
|
-
"standard-version": "^9.5.0",
|
|
67
|
-
tap: "^19.2.0",
|
|
68
|
-
typescript: "^4.8.4"
|
|
69
|
-
},
|
|
70
|
-
engines: {
|
|
71
|
-
node: ">=12"
|
|
72
|
-
},
|
|
73
|
-
browser: {
|
|
74
|
-
fs: false
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// node_modules/dotenv/lib/main.js
|
|
81
|
-
var require_main = __commonJS({
|
|
82
|
-
"node_modules/dotenv/lib/main.js"(exports, module) {
|
|
83
|
-
"use strict";
|
|
84
|
-
var fs = __require("fs");
|
|
85
|
-
var path = __require("path");
|
|
86
|
-
var os = __require("os");
|
|
87
|
-
var crypto = __require("crypto");
|
|
88
|
-
var packageJson = require_package();
|
|
89
|
-
var version = packageJson.version;
|
|
90
|
-
var TIPS = [
|
|
91
|
-
"\u{1F510} encrypt with Dotenvx: https://dotenvx.com",
|
|
92
|
-
"\u{1F510} prevent committing .env to code: https://dotenvx.com/precommit",
|
|
93
|
-
"\u{1F510} prevent building .env in docker: https://dotenvx.com/prebuild",
|
|
94
|
-
"\u{1F916} agentic secret storage: https://dotenvx.com/as2",
|
|
95
|
-
"\u26A1\uFE0F secrets for agents: https://dotenvx.com/as2",
|
|
96
|
-
"\u{1F6E1}\uFE0F auth for agents: https://vestauth.com",
|
|
97
|
-
"\u{1F6E0}\uFE0F run anywhere with `dotenvx run -- yourcommand`",
|
|
98
|
-
"\u2699\uFE0F specify custom .env file path with { path: '/custom/path/.env' }",
|
|
99
|
-
"\u2699\uFE0F enable debug logging with { debug: true }",
|
|
100
|
-
"\u2699\uFE0F override existing env vars with { override: true }",
|
|
101
|
-
"\u2699\uFE0F suppress all logs with { quiet: true }",
|
|
102
|
-
"\u2699\uFE0F write to custom object with { processEnv: myObject }",
|
|
103
|
-
"\u2699\uFE0F load multiple .env files with { path: ['.env.local', '.env'] }"
|
|
104
|
-
];
|
|
105
|
-
function _getRandomTip() {
|
|
106
|
-
return TIPS[Math.floor(Math.random() * TIPS.length)];
|
|
107
|
-
}
|
|
108
|
-
function parseBoolean(value) {
|
|
109
|
-
if (typeof value === "string") {
|
|
110
|
-
return !["false", "0", "no", "off", ""].includes(value.toLowerCase());
|
|
111
|
-
}
|
|
112
|
-
return Boolean(value);
|
|
113
|
-
}
|
|
114
|
-
function supportsAnsi() {
|
|
115
|
-
return process.stdout.isTTY;
|
|
116
|
-
}
|
|
117
|
-
function dim(text) {
|
|
118
|
-
return supportsAnsi() ? `\x1B[2m${text}\x1B[0m` : text;
|
|
119
|
-
}
|
|
120
|
-
var LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
|
|
121
|
-
function parse(src) {
|
|
122
|
-
const obj = {};
|
|
123
|
-
let lines = src.toString();
|
|
124
|
-
lines = lines.replace(/\r\n?/mg, "\n");
|
|
125
|
-
let match;
|
|
126
|
-
while ((match = LINE.exec(lines)) != null) {
|
|
127
|
-
const key = match[1];
|
|
128
|
-
let value = match[2] || "";
|
|
129
|
-
value = value.trim();
|
|
130
|
-
const maybeQuote = value[0];
|
|
131
|
-
value = value.replace(/^(['"`])([\s\S]*)\1$/mg, "$2");
|
|
132
|
-
if (maybeQuote === '"') {
|
|
133
|
-
value = value.replace(/\\n/g, "\n");
|
|
134
|
-
value = value.replace(/\\r/g, "\r");
|
|
135
|
-
}
|
|
136
|
-
obj[key] = value;
|
|
137
|
-
}
|
|
138
|
-
return obj;
|
|
139
|
-
}
|
|
140
|
-
function _parseVault(options) {
|
|
141
|
-
options = options || {};
|
|
142
|
-
const vaultPath = _vaultPath(options);
|
|
143
|
-
options.path = vaultPath;
|
|
144
|
-
const result = DotenvModule.configDotenv(options);
|
|
145
|
-
if (!result.parsed) {
|
|
146
|
-
const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
|
|
147
|
-
err.code = "MISSING_DATA";
|
|
148
|
-
throw err;
|
|
149
|
-
}
|
|
150
|
-
const keys = _dotenvKey(options).split(",");
|
|
151
|
-
const length = keys.length;
|
|
152
|
-
let decrypted;
|
|
153
|
-
for (let i = 0; i < length; i++) {
|
|
154
|
-
try {
|
|
155
|
-
const key = keys[i].trim();
|
|
156
|
-
const attrs = _instructions(result, key);
|
|
157
|
-
decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
|
|
158
|
-
break;
|
|
159
|
-
} catch (error) {
|
|
160
|
-
if (i + 1 >= length) {
|
|
161
|
-
throw error;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return DotenvModule.parse(decrypted);
|
|
166
|
-
}
|
|
167
|
-
function _warn(message) {
|
|
168
|
-
console.error(`[dotenv@${version}][WARN] ${message}`);
|
|
169
|
-
}
|
|
170
|
-
function _debug(message) {
|
|
171
|
-
console.log(`[dotenv@${version}][DEBUG] ${message}`);
|
|
172
|
-
}
|
|
173
|
-
function _log(message) {
|
|
174
|
-
console.log(`[dotenv@${version}] ${message}`);
|
|
175
|
-
}
|
|
176
|
-
function _dotenvKey(options) {
|
|
177
|
-
if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
|
|
178
|
-
return options.DOTENV_KEY;
|
|
179
|
-
}
|
|
180
|
-
if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
|
|
181
|
-
return process.env.DOTENV_KEY;
|
|
182
|
-
}
|
|
183
|
-
return "";
|
|
184
|
-
}
|
|
185
|
-
function _instructions(result, dotenvKey) {
|
|
186
|
-
let uri;
|
|
187
|
-
try {
|
|
188
|
-
uri = new URL(dotenvKey);
|
|
189
|
-
} catch (error) {
|
|
190
|
-
if (error.code === "ERR_INVALID_URL") {
|
|
191
|
-
const err = new Error("INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development");
|
|
192
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
193
|
-
throw err;
|
|
194
|
-
}
|
|
195
|
-
throw error;
|
|
196
|
-
}
|
|
197
|
-
const key = uri.password;
|
|
198
|
-
if (!key) {
|
|
199
|
-
const err = new Error("INVALID_DOTENV_KEY: Missing key part");
|
|
200
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
201
|
-
throw err;
|
|
202
|
-
}
|
|
203
|
-
const environment = uri.searchParams.get("environment");
|
|
204
|
-
if (!environment) {
|
|
205
|
-
const err = new Error("INVALID_DOTENV_KEY: Missing environment part");
|
|
206
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
207
|
-
throw err;
|
|
208
|
-
}
|
|
209
|
-
const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
|
|
210
|
-
const ciphertext = result.parsed[environmentKey];
|
|
211
|
-
if (!ciphertext) {
|
|
212
|
-
const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
|
|
213
|
-
err.code = "NOT_FOUND_DOTENV_ENVIRONMENT";
|
|
214
|
-
throw err;
|
|
215
|
-
}
|
|
216
|
-
return { ciphertext, key };
|
|
217
|
-
}
|
|
218
|
-
function _vaultPath(options) {
|
|
219
|
-
let possibleVaultPath = null;
|
|
220
|
-
if (options && options.path && options.path.length > 0) {
|
|
221
|
-
if (Array.isArray(options.path)) {
|
|
222
|
-
for (const filepath of options.path) {
|
|
223
|
-
if (fs.existsSync(filepath)) {
|
|
224
|
-
possibleVaultPath = filepath.endsWith(".vault") ? filepath : `${filepath}.vault`;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
} else {
|
|
228
|
-
possibleVaultPath = options.path.endsWith(".vault") ? options.path : `${options.path}.vault`;
|
|
229
|
-
}
|
|
230
|
-
} else {
|
|
231
|
-
possibleVaultPath = path.resolve(process.cwd(), ".env.vault");
|
|
232
|
-
}
|
|
233
|
-
if (fs.existsSync(possibleVaultPath)) {
|
|
234
|
-
return possibleVaultPath;
|
|
235
|
-
}
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
function _resolveHome(envPath) {
|
|
239
|
-
return envPath[0] === "~" ? path.join(os.homedir(), envPath.slice(1)) : envPath;
|
|
240
|
-
}
|
|
241
|
-
function _configVault(options) {
|
|
242
|
-
const debug = parseBoolean(process.env.DOTENV_CONFIG_DEBUG || options && options.debug);
|
|
243
|
-
const quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || options && options.quiet);
|
|
244
|
-
if (debug || !quiet) {
|
|
245
|
-
_log("Loading env from encrypted .env.vault");
|
|
246
|
-
}
|
|
247
|
-
const parsed = DotenvModule._parseVault(options);
|
|
248
|
-
let processEnv = process.env;
|
|
249
|
-
if (options && options.processEnv != null) {
|
|
250
|
-
processEnv = options.processEnv;
|
|
251
|
-
}
|
|
252
|
-
DotenvModule.populate(processEnv, parsed, options);
|
|
253
|
-
return { parsed };
|
|
254
|
-
}
|
|
255
|
-
function configDotenv(options) {
|
|
256
|
-
const dotenvPath = path.resolve(process.cwd(), ".env");
|
|
257
|
-
let encoding = "utf8";
|
|
258
|
-
let processEnv = process.env;
|
|
259
|
-
if (options && options.processEnv != null) {
|
|
260
|
-
processEnv = options.processEnv;
|
|
261
|
-
}
|
|
262
|
-
let debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || options && options.debug);
|
|
263
|
-
let quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || options && options.quiet);
|
|
264
|
-
if (options && options.encoding) {
|
|
265
|
-
encoding = options.encoding;
|
|
266
|
-
} else {
|
|
267
|
-
if (debug) {
|
|
268
|
-
_debug("No encoding is specified. UTF-8 is used by default");
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
let optionPaths = [dotenvPath];
|
|
272
|
-
if (options && options.path) {
|
|
273
|
-
if (!Array.isArray(options.path)) {
|
|
274
|
-
optionPaths = [_resolveHome(options.path)];
|
|
275
|
-
} else {
|
|
276
|
-
optionPaths = [];
|
|
277
|
-
for (const filepath of options.path) {
|
|
278
|
-
optionPaths.push(_resolveHome(filepath));
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
let lastError;
|
|
283
|
-
const parsedAll = {};
|
|
284
|
-
for (const path2 of optionPaths) {
|
|
285
|
-
try {
|
|
286
|
-
const parsed = DotenvModule.parse(fs.readFileSync(path2, { encoding }));
|
|
287
|
-
DotenvModule.populate(parsedAll, parsed, options);
|
|
288
|
-
} catch (e) {
|
|
289
|
-
if (debug) {
|
|
290
|
-
_debug(`Failed to load ${path2} ${e.message}`);
|
|
291
|
-
}
|
|
292
|
-
lastError = e;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
const populated = DotenvModule.populate(processEnv, parsedAll, options);
|
|
296
|
-
debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || debug);
|
|
297
|
-
quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || quiet);
|
|
298
|
-
if (debug || !quiet) {
|
|
299
|
-
const keysCount = Object.keys(populated).length;
|
|
300
|
-
const shortPaths = [];
|
|
301
|
-
for (const filePath of optionPaths) {
|
|
302
|
-
try {
|
|
303
|
-
const relative = path.relative(process.cwd(), filePath);
|
|
304
|
-
shortPaths.push(relative);
|
|
305
|
-
} catch (e) {
|
|
306
|
-
if (debug) {
|
|
307
|
-
_debug(`Failed to load ${filePath} ${e.message}`);
|
|
308
|
-
}
|
|
309
|
-
lastError = e;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
_log(`injecting env (${keysCount}) from ${shortPaths.join(",")} ${dim(`-- tip: ${_getRandomTip()}`)}`);
|
|
313
|
-
}
|
|
314
|
-
if (lastError) {
|
|
315
|
-
return { parsed: parsedAll, error: lastError };
|
|
316
|
-
} else {
|
|
317
|
-
return { parsed: parsedAll };
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
function config(options) {
|
|
321
|
-
if (_dotenvKey(options).length === 0) {
|
|
322
|
-
return DotenvModule.configDotenv(options);
|
|
323
|
-
}
|
|
324
|
-
const vaultPath = _vaultPath(options);
|
|
325
|
-
if (!vaultPath) {
|
|
326
|
-
_warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
|
|
327
|
-
return DotenvModule.configDotenv(options);
|
|
328
|
-
}
|
|
329
|
-
return DotenvModule._configVault(options);
|
|
330
|
-
}
|
|
331
|
-
function decrypt(encrypted, keyStr) {
|
|
332
|
-
const key = Buffer.from(keyStr.slice(-64), "hex");
|
|
333
|
-
let ciphertext = Buffer.from(encrypted, "base64");
|
|
334
|
-
const nonce = ciphertext.subarray(0, 12);
|
|
335
|
-
const authTag = ciphertext.subarray(-16);
|
|
336
|
-
ciphertext = ciphertext.subarray(12, -16);
|
|
337
|
-
try {
|
|
338
|
-
const aesgcm = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
339
|
-
aesgcm.setAuthTag(authTag);
|
|
340
|
-
return `${aesgcm.update(ciphertext)}${aesgcm.final()}`;
|
|
341
|
-
} catch (error) {
|
|
342
|
-
const isRange = error instanceof RangeError;
|
|
343
|
-
const invalidKeyLength = error.message === "Invalid key length";
|
|
344
|
-
const decryptionFailed = error.message === "Unsupported state or unable to authenticate data";
|
|
345
|
-
if (isRange || invalidKeyLength) {
|
|
346
|
-
const err = new Error("INVALID_DOTENV_KEY: It must be 64 characters long (or more)");
|
|
347
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
348
|
-
throw err;
|
|
349
|
-
} else if (decryptionFailed) {
|
|
350
|
-
const err = new Error("DECRYPTION_FAILED: Please check your DOTENV_KEY");
|
|
351
|
-
err.code = "DECRYPTION_FAILED";
|
|
352
|
-
throw err;
|
|
353
|
-
} else {
|
|
354
|
-
throw error;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
function populate(processEnv, parsed, options = {}) {
|
|
359
|
-
const debug = Boolean(options && options.debug);
|
|
360
|
-
const override = Boolean(options && options.override);
|
|
361
|
-
const populated = {};
|
|
362
|
-
if (typeof parsed !== "object") {
|
|
363
|
-
const err = new Error("OBJECT_REQUIRED: Please check the processEnv argument being passed to populate");
|
|
364
|
-
err.code = "OBJECT_REQUIRED";
|
|
365
|
-
throw err;
|
|
366
|
-
}
|
|
367
|
-
for (const key of Object.keys(parsed)) {
|
|
368
|
-
if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
|
|
369
|
-
if (override === true) {
|
|
370
|
-
processEnv[key] = parsed[key];
|
|
371
|
-
populated[key] = parsed[key];
|
|
372
|
-
}
|
|
373
|
-
if (debug) {
|
|
374
|
-
if (override === true) {
|
|
375
|
-
_debug(`"${key}" is already defined and WAS overwritten`);
|
|
376
|
-
} else {
|
|
377
|
-
_debug(`"${key}" is already defined and was NOT overwritten`);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
} else {
|
|
381
|
-
processEnv[key] = parsed[key];
|
|
382
|
-
populated[key] = parsed[key];
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
return populated;
|
|
386
|
-
}
|
|
387
|
-
var DotenvModule = {
|
|
388
|
-
configDotenv,
|
|
389
|
-
_configVault,
|
|
390
|
-
_parseVault,
|
|
391
|
-
config,
|
|
392
|
-
decrypt,
|
|
393
|
-
parse,
|
|
394
|
-
populate
|
|
395
|
-
};
|
|
396
|
-
module.exports.configDotenv = DotenvModule.configDotenv;
|
|
397
|
-
module.exports._configVault = DotenvModule._configVault;
|
|
398
|
-
module.exports._parseVault = DotenvModule._parseVault;
|
|
399
|
-
module.exports.config = DotenvModule.config;
|
|
400
|
-
module.exports.decrypt = DotenvModule.decrypt;
|
|
401
|
-
module.exports.parse = DotenvModule.parse;
|
|
402
|
-
module.exports.populate = DotenvModule.populate;
|
|
403
|
-
module.exports = DotenvModule;
|
|
404
|
-
}
|
|
405
|
-
});
|
|
11
|
+
} from "./chunk-LNC6F2DI.js";
|
|
406
12
|
|
|
407
13
|
// src/bin.ts
|
|
408
14
|
var import_dotenv = __toESM(require_main(), 1);
|
|
409
|
-
import { resolve
|
|
410
|
-
import {
|
|
15
|
+
import { resolve } from "path";
|
|
16
|
+
import { existsSync } from "fs";
|
|
411
17
|
|
|
412
18
|
// src/cli-args.ts
|
|
19
|
+
var ENGINES = ["auto", "dataloom", "base"];
|
|
413
20
|
function parseArgs(argv) {
|
|
414
21
|
const args = argv.slice(2);
|
|
415
22
|
const command = args[0] === "validate" ? "validate" : "generate";
|
|
@@ -424,9 +31,25 @@ function parseArgs(argv) {
|
|
|
424
31
|
output: getArg("--output"),
|
|
425
32
|
input: getArg("--input"),
|
|
426
33
|
project: getArg("--project"),
|
|
427
|
-
exportCustomTypes: hasFlag("--export-custom-types")
|
|
34
|
+
exportCustomTypes: hasFlag("--export-custom-types"),
|
|
35
|
+
engine: parseEngine(getArg("--engine")),
|
|
36
|
+
type: parseType(getArg("--type"))
|
|
428
37
|
};
|
|
429
38
|
}
|
|
39
|
+
function parseType(value) {
|
|
40
|
+
if (value === void 0) return void 0;
|
|
41
|
+
if (DATASOURCE_TYPES.includes(value)) return value;
|
|
42
|
+
throw new Error(
|
|
43
|
+
`[db-schema-sync] Invalid --type "${value}", expected one of: ${DATASOURCE_TYPES.join(", ")}`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
function parseEngine(value) {
|
|
47
|
+
if (value === void 0) return "auto";
|
|
48
|
+
if (ENGINES.includes(value)) return value;
|
|
49
|
+
throw new Error(
|
|
50
|
+
`[db-schema-sync] Invalid --engine "${value}", expected one of: ${ENGINES.join(", ")}`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
430
53
|
|
|
431
54
|
// src/bin.ts
|
|
432
55
|
function getValidateExitCode(result) {
|
|
@@ -436,25 +59,31 @@ function getValidateExitCode(result) {
|
|
|
436
59
|
return 1;
|
|
437
60
|
}
|
|
438
61
|
async function main() {
|
|
439
|
-
|
|
62
|
+
let cliArgs;
|
|
63
|
+
try {
|
|
64
|
+
cliArgs = parseArgs(process.argv);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
440
69
|
if (cliArgs.command === "validate") {
|
|
441
70
|
if (!cliArgs.input) {
|
|
442
|
-
const
|
|
71
|
+
const output2 = {
|
|
443
72
|
ok: false,
|
|
444
73
|
status: "inconclusive",
|
|
445
74
|
errorType: "validation_runner_failed",
|
|
446
75
|
durationMs: 0,
|
|
447
76
|
message: "--input is required for validate command"
|
|
448
77
|
};
|
|
449
|
-
console.log(JSON.stringify(
|
|
78
|
+
console.log(JSON.stringify(output2));
|
|
450
79
|
process.exit(1);
|
|
451
80
|
}
|
|
452
|
-
const
|
|
81
|
+
const output = validateGeneratedSchema({
|
|
453
82
|
input: cliArgs.input,
|
|
454
83
|
project: cliArgs.project
|
|
455
84
|
});
|
|
456
|
-
console.log(JSON.stringify(
|
|
457
|
-
process.exit(getValidateExitCode(
|
|
85
|
+
console.log(JSON.stringify(output));
|
|
86
|
+
process.exit(getValidateExitCode(output));
|
|
458
87
|
}
|
|
459
88
|
for (const name of [".env.local", ".env"]) {
|
|
460
89
|
const envPath = resolve(process.cwd(), name);
|
|
@@ -463,32 +92,11 @@ async function main() {
|
|
|
463
92
|
console.log(`[db-schema-sync] \u2713 Loaded ${name}`);
|
|
464
93
|
}
|
|
465
94
|
}
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
);
|
|
473
|
-
const schema = await generateSchema({
|
|
474
|
-
...options,
|
|
475
|
-
exportCustomTypes: cliArgs.exportCustomTypes
|
|
476
|
-
});
|
|
477
|
-
if (!schema.includes("pgTable") && !schema.includes("pgView")) {
|
|
478
|
-
console.warn(
|
|
479
|
-
"[db-schema-sync] \u26A0 Warning: No tables or views found in database. The generated schema will be empty."
|
|
480
|
-
);
|
|
481
|
-
}
|
|
482
|
-
const outputPath = resolve(process.cwd(), output);
|
|
483
|
-
mkdirSync(dirname(outputPath), { recursive: true });
|
|
484
|
-
writeFileSync(outputPath, schema, "utf8");
|
|
485
|
-
console.log(`[db-schema-sync] \u2713 Schema written to ${output}`);
|
|
486
|
-
} catch (err) {
|
|
487
|
-
console.error(
|
|
488
|
-
`[db-schema-sync] \u2717 Failed:`,
|
|
489
|
-
err instanceof Error ? err.message : String(err)
|
|
490
|
-
);
|
|
491
|
-
process.exit(1);
|
|
492
|
-
}
|
|
95
|
+
const { failures } = await runSync({
|
|
96
|
+
cwd: process.cwd(),
|
|
97
|
+
env: process.env,
|
|
98
|
+
args: cliArgs
|
|
99
|
+
});
|
|
100
|
+
if (failures.length > 0) process.exit(1);
|
|
493
101
|
}
|
|
494
102
|
main();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|