marcidb-client 0.0.9 → 0.1.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/bin/marci-generate-linux-x64 +0 -0
- package/bin/marci-generate-win32-x64.exe +0 -0
- package/bin.js +152 -90
- package/package.json +1 -1
|
Binary file
|
|
Binary file
|
package/bin.js
CHANGED
|
@@ -1,90 +1,152 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { execFileSync } from "node:child_process";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import path from "node:path";
|
|
6
|
-
import os from "node:os";
|
|
7
|
-
import fs from "node:fs"
|
|
8
|
-
|
|
9
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
-
|
|
11
|
-
const BINS = {
|
|
12
|
-
"linux-x64": "marci-generate-linux-x64",
|
|
13
|
-
"darwin-arm64": "marci-generate-darwin-arm64",
|
|
14
|
-
"win32-x64": "marci-generate-win32-x64.exe",
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
|
|
9
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
|
|
11
|
+
const BINS = {
|
|
12
|
+
"linux-x64": "marci-generate-linux-x64",
|
|
13
|
+
"darwin-arm64": "marci-generate-darwin-arm64",
|
|
14
|
+
"win32-x64": "marci-generate-win32-x64.exe",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function binPath() {
|
|
18
|
+
const key = `${os.platform()}-${os.arch()}`;
|
|
19
|
+
const name = BINS[key];
|
|
20
|
+
if (!name) {
|
|
21
|
+
console.error(`marcidb: unsupported platform ${key}`);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
return path.join(__dirname, "bin", name);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Разбирает позиционные аргументы и флаги (--flag value | --flag)
|
|
28
|
+
function parseArgs(args) {
|
|
29
|
+
const positional = [];
|
|
30
|
+
const flags = {};
|
|
31
|
+
for (let i = 0; i < args.length; i++) {
|
|
32
|
+
const a = args[i];
|
|
33
|
+
if (a.startsWith("--")) {
|
|
34
|
+
const key = a.slice(2);
|
|
35
|
+
const next = args[i + 1];
|
|
36
|
+
if (next === undefined || next.startsWith("--")) {
|
|
37
|
+
flags[key] = true;
|
|
38
|
+
} else {
|
|
39
|
+
flags[key] = next;
|
|
40
|
+
i++;
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
positional.push(a);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { positional, flags };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function help() {
|
|
50
|
+
console.log(`
|
|
51
|
+
marcidb <command> [options]
|
|
52
|
+
|
|
53
|
+
Commands:
|
|
54
|
+
generate [schema] [output] Generate TS types (always) and a migration file
|
|
55
|
+
--migrations <dir> migrations directory (default: migrations)
|
|
56
|
+
--name <name> migration name (default: migration)
|
|
57
|
+
--no-migrations types only, skip the migration file
|
|
58
|
+
|
|
59
|
+
migrate push <db> [options] Push the schema to a running server (creates the db if absent)
|
|
60
|
+
--url <url> server url (default: http://localhost:3000)
|
|
61
|
+
--schema <path> schema file (default: schema.marci)
|
|
62
|
+
|
|
63
|
+
Examples:
|
|
64
|
+
marcidb generate
|
|
65
|
+
marcidb generate schema.marci --name add_users
|
|
66
|
+
marcidb migrate push myapp --url http://localhost:3000
|
|
67
|
+
`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function cmdGenerate(args) {
|
|
71
|
+
const { positional, flags } = parseArgs(args);
|
|
72
|
+
const schema = positional[0] ?? "schema.marci";
|
|
73
|
+
const output = positional[1] ?? path.join(__dirname, "..", ".marcidb", "client");
|
|
74
|
+
const bin = binPath();
|
|
75
|
+
|
|
76
|
+
// 1) TypeScript-типы (всегда — без них миграция бессмысленна)
|
|
77
|
+
if (fs.existsSync(output)) {
|
|
78
|
+
fs.rmSync(output, { recursive: true, force: true });
|
|
79
|
+
}
|
|
80
|
+
console.log(`Generating types from ${schema} into ${output}...`);
|
|
81
|
+
execFileSync(bin, ["types", schema, output], { stdio: "inherit" });
|
|
82
|
+
fs.writeFileSync(path.join(output, "package.json"), `{
|
|
83
|
+
"name": "marci-client",
|
|
84
|
+
"main": "index.js",
|
|
85
|
+
"types": "index.d.ts"
|
|
86
|
+
}`);
|
|
87
|
+
|
|
88
|
+
// 2) Файл миграции (если не отключено)
|
|
89
|
+
if (!flags["no-migrations"]) {
|
|
90
|
+
const dir = flags.migrations ?? "migrations";
|
|
91
|
+
const name = flags.name ?? "migration";
|
|
92
|
+
execFileSync(bin, ["migration", schema, dir, name], { stdio: "inherit" });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
console.log("Done.");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function cmdMigrate(args) {
|
|
99
|
+
const [sub, ...rest] = args;
|
|
100
|
+
if (sub !== "push") {
|
|
101
|
+
console.error(`marcidb migrate: unknown subcommand "${sub ?? ""}" (expected "push")`);
|
|
102
|
+
help();
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { positional, flags } = parseArgs(rest);
|
|
107
|
+
const db = positional[0];
|
|
108
|
+
if (!db) {
|
|
109
|
+
console.error("marcidb migrate push: <db> name required");
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const url = (flags.url ?? "http://localhost:3000").replace(/\/+$/, "");
|
|
114
|
+
const schemaPath = flags.schema ?? "schema.marci";
|
|
115
|
+
const schemaText = fs.readFileSync(schemaPath, "utf8");
|
|
116
|
+
|
|
117
|
+
const res = await fetch(`${url}/${db}/$migrate`, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: { "Content-Type": "text/plain" },
|
|
120
|
+
body: schemaText,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (!res.ok) {
|
|
124
|
+
console.error(`Migration failed [${res.status}]: ${await res.text()}`);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
console.log(`Migrated '${db}' on ${url}`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const COMMANDS = {
|
|
131
|
+
generate: cmdGenerate,
|
|
132
|
+
migrate: cmdMigrate,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// --- main ---
|
|
136
|
+
|
|
137
|
+
const [command, ...args] = process.argv.slice(2);
|
|
138
|
+
|
|
139
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
140
|
+
help();
|
|
141
|
+
process.exit(0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const fn = COMMANDS[command];
|
|
145
|
+
|
|
146
|
+
if (!fn) {
|
|
147
|
+
console.error(`marcidb: unknown command "${command}"`);
|
|
148
|
+
help();
|
|
149
|
+
process.exit(1);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
await fn(args);
|