marcidb-client 0.0.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.
- package/README.md +85 -0
- package/bin/marci-generate-linux-x64 +0 -0
- package/bin/marci-generate-win32-x64.exe +0 -0
- package/bin.js +90 -0
- package/client/index.d.ts +1 -0
- package/client/index.js +1 -0
- package/package.json +38 -0
- package/runtime/index.d.ts +3 -0
- package/runtime/index.js +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# marci-db
|
|
2
|
+
|
|
3
|
+
TypeScript client for [MarciDB](https://github.com/den59k/marci-db.git) — a NoSQL database with a schema-first approach.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install marcidb-client
|
|
9
|
+
# or
|
|
10
|
+
bun add marcidb-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
1. Create a `schema.marci` file in your project root:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
model User {
|
|
19
|
+
name String
|
|
20
|
+
posts Post[] @bind(Post.author)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
model Post {
|
|
24
|
+
title String
|
|
25
|
+
author User?
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. Generate the client:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
marci generate
|
|
33
|
+
# or with custom paths
|
|
34
|
+
marci generate schema.marci node_modules/.marci/client
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
3. Start your MarciDB server and connect:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { marci } from "marci-db";
|
|
41
|
+
|
|
42
|
+
const db = marci("http://localhost:3000");
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { marci } from "marci-db";
|
|
49
|
+
|
|
50
|
+
const db = marci("http://localhost:3000");
|
|
51
|
+
|
|
52
|
+
// Find
|
|
53
|
+
const users = await db.user.findMany({
|
|
54
|
+
id: true,
|
|
55
|
+
name: true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Insert
|
|
59
|
+
const id = await db.user.insert({
|
|
60
|
+
name: "Alice",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Update
|
|
64
|
+
await db.user.update({ id: 1 }, {
|
|
65
|
+
name: "Bob",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Delete
|
|
69
|
+
await db.user.delete({ id: 1 });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Requirements
|
|
73
|
+
|
|
74
|
+
- Node.js 18+ or Bun
|
|
75
|
+
- MarciDB server running
|
|
76
|
+
|
|
77
|
+
## Re-generating types
|
|
78
|
+
|
|
79
|
+
After every change to `schema.marci`, re-run:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
marci generate
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
If types don't update in VSCode, run **TypeScript: Restart TS Server** (`Ctrl+Shift+P`).
|
|
Binary file
|
|
Binary file
|
package/bin.js
ADDED
|
@@ -0,0 +1,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
|
+
const COMMANDS = {
|
|
18
|
+
generate: cmdGenerate,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function help() {
|
|
22
|
+
console.log(`
|
|
23
|
+
marci <command> [options]
|
|
24
|
+
|
|
25
|
+
Commands:
|
|
26
|
+
generate [schema] [output] Generate TypeScript types from schema
|
|
27
|
+
schema — path to .marci file (default: schema.marci)
|
|
28
|
+
output — output directory (default: node_modules/marci-db/generated)
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
marci generate
|
|
32
|
+
marci generate schema.marci
|
|
33
|
+
marci generate schema.marci node_modules/.marci/client
|
|
34
|
+
`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function cmdGenerate(args) {
|
|
38
|
+
const schema = args[0] ?? "schema.marci";
|
|
39
|
+
const output = args[1] ?? "node_modules/.marci/client";
|
|
40
|
+
|
|
41
|
+
const key = `${os.platform()}-${os.arch()}`;
|
|
42
|
+
const name = BINS[key];
|
|
43
|
+
|
|
44
|
+
if (!name) {
|
|
45
|
+
console.error(`marci-db: unsupported platform ${key}`);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const bin = path.join(__dirname, "bin", name);
|
|
50
|
+
|
|
51
|
+
// Удаляем папку перед генерацией
|
|
52
|
+
if (fs.existsSync(output)) {
|
|
53
|
+
fs.rmSync(output, { recursive: true, force: true });
|
|
54
|
+
console.log(`Cleared ${output}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
console.log(`Generating from ${schema} into ${output}...`);
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
execFileSync(bin, [schema, output], { stdio: "inherit" });
|
|
61
|
+
|
|
62
|
+
fs.writeFileSync(output + "/package.json", `{
|
|
63
|
+
"name": "maci-client",
|
|
64
|
+
"main": "index.js",
|
|
65
|
+
"types": "index.d.ts"
|
|
66
|
+
}`)
|
|
67
|
+
|
|
68
|
+
console.log("Done.");
|
|
69
|
+
} catch (e) {
|
|
70
|
+
process.exit(e.status ?? 1);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// --- main ---
|
|
74
|
+
|
|
75
|
+
const [command, ...args] = process.argv.slice(2);
|
|
76
|
+
|
|
77
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
78
|
+
help();
|
|
79
|
+
process.exit(0);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const fn = COMMANDS[command];
|
|
83
|
+
|
|
84
|
+
if (!fn) {
|
|
85
|
+
console.error(`marci: unknown command "${command}"`);
|
|
86
|
+
help();
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fn(args);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '.marci/client'
|
package/client/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '.marci/client'
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "marcidb-client",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "den59k"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/den59k/marci-db.git"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "bunup src/index.ts --external .marci/client --out-dir runtime"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"marci": "bin.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin/",
|
|
21
|
+
"client/",
|
|
22
|
+
"runtime/",
|
|
23
|
+
"bin.js"
|
|
24
|
+
],
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"bunup": "^0.16.31"
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": "./client/index.js",
|
|
31
|
+
"types": "./client/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./runtime": {
|
|
34
|
+
"import": "./runtime/index.js",
|
|
35
|
+
"types": "./runtime/index.d.ts"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
package/runtime/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
class MarciClient {
|
|
3
|
+
baseUrl;
|
|
4
|
+
constructor(url) {
|
|
5
|
+
this.baseUrl = url.replace(/\/$/, "");
|
|
6
|
+
}
|
|
7
|
+
async#request(method, path, body) {
|
|
8
|
+
const res = await fetch(`${this.baseUrl}${path}`, {
|
|
9
|
+
method,
|
|
10
|
+
headers: { "Content-Type": "application/json" },
|
|
11
|
+
body: body !== undefined ? JSON.stringify(body) : undefined
|
|
12
|
+
});
|
|
13
|
+
if (!res.ok) {
|
|
14
|
+
const text = await res.text();
|
|
15
|
+
throw new Error(`MarciDB [${res.status}]: ${text}`);
|
|
16
|
+
}
|
|
17
|
+
return res.json();
|
|
18
|
+
}
|
|
19
|
+
findMany(model, select) {
|
|
20
|
+
return this.#request("POST", `/${model}/findMany`, select);
|
|
21
|
+
}
|
|
22
|
+
insert(model, data) {
|
|
23
|
+
return this.#request("POST", `/${model}/insert`, data);
|
|
24
|
+
}
|
|
25
|
+
update(model, id, data) {
|
|
26
|
+
return this.#request("POST", `/${model}/update/${encodeId(id)}`, data);
|
|
27
|
+
}
|
|
28
|
+
delete(model, id) {
|
|
29
|
+
return this.#request("POST", `/${model}/delete/${encodeId(id)}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function encodeId(id) {
|
|
33
|
+
if (typeof id === "object" && id !== null) {
|
|
34
|
+
return Object.entries(id).map(([k, v]) => `${k}=${encodeURIComponent(String(v))}`).join("&");
|
|
35
|
+
}
|
|
36
|
+
return String(id);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/index.ts
|
|
40
|
+
import { connect } from ".marci/client";
|
|
41
|
+
var marci = (url) => {
|
|
42
|
+
const client = new MarciClient(url);
|
|
43
|
+
return connect(client);
|
|
44
|
+
};
|
|
45
|
+
export {
|
|
46
|
+
marci
|
|
47
|
+
};
|