marcidb-client 0.1.0 → 0.1.1

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.
Binary file
Binary file
package/bin.js CHANGED
@@ -56,9 +56,9 @@ Commands:
56
56
  --name <name> migration name (default: migration)
57
57
  --no-migrations types only, skip the migration file
58
58
 
59
- migrate push <db> [options] Push the schema to a running server (creates the db if absent)
59
+ migrate push <db> [options] Push migration files (.mig) to a server; it applies the new ones
60
60
  --url <url> server url (default: http://localhost:3000)
61
- --schema <path> schema file (default: schema.marci)
61
+ --migrations <dir> migrations directory (default: migrations)
62
62
 
63
63
  Examples:
64
64
  marcidb generate
@@ -97,12 +97,14 @@ function cmdGenerate(args) {
97
97
 
98
98
  async function cmdMigrate(args) {
99
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
- }
100
+ if (sub === "push") return migratePush(rest);
101
+ console.error(`marcidb migrate: unknown subcommand "${sub ?? ""}" (expected "push")`);
102
+ help();
103
+ process.exit(1);
104
+ }
105
105
 
106
+ // Императивный push: отправляет .mig-файлы; сервер по ledger'у применяет только новые
107
+ async function migratePush(rest) {
106
108
  const { positional, flags } = parseArgs(rest);
107
109
  const db = positional[0];
108
110
  if (!db) {
@@ -111,20 +113,40 @@ async function cmdMigrate(args) {
111
113
  }
112
114
 
113
115
  const url = (flags.url ?? "http://localhost:3000").replace(/\/+$/, "");
114
- const schemaPath = flags.schema ?? "schema.marci";
115
- const schemaText = fs.readFileSync(schemaPath, "utf8");
116
+ const dir = flags.migrations ?? "migrations";
117
+
118
+ if (!fs.existsSync(dir)) {
119
+ console.error(`marcidb migrate push: migrations directory "${dir}" not found — run "marcidb generate" first`);
120
+ process.exit(1);
121
+ }
122
+
123
+ // Все .mig по порядку имён → [{ id, ops }]; id — имя файла без расширения
124
+ const migrations = fs.readdirSync(dir)
125
+ .filter((f) => f.endsWith(".mig"))
126
+ .sort()
127
+ .map((f) => ({ id: f.replace(/\.mig$/, ""), ops: fs.readFileSync(path.join(dir, f), "utf8") }));
128
+
129
+ if (migrations.length === 0) {
130
+ console.log("No migration files — nothing to push");
131
+ return;
132
+ }
116
133
 
117
134
  const res = await fetch(`${url}/${db}/$migrate`, {
118
135
  method: "POST",
119
- headers: { "Content-Type": "text/plain" },
120
- body: schemaText,
136
+ headers: { "Content-Type": "application/json" },
137
+ body: JSON.stringify(migrations),
121
138
  });
122
139
 
123
140
  if (!res.ok) {
124
141
  console.error(`Migration failed [${res.status}]: ${await res.text()}`);
125
142
  process.exit(1);
126
143
  }
127
- console.log(`Migrated '${db}' on ${url}`);
144
+ const { applied } = await res.json();
145
+ if (applied && applied.length) {
146
+ console.log(`Applied ${applied.length} migration(s) to '${db}': ${applied.join(", ")}`);
147
+ } else {
148
+ console.log(`'${db}' is up to date — no new migrations`);
149
+ }
128
150
  }
129
151
 
130
152
  const COMMANDS = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "marcidb-client",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "den59k"