@apex-stack/data 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andre Corugda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,36 @@
1
+ import { ApexResource } from '@apex-stack/core';
2
+ import Database from 'better-sqlite3';
3
+ import { drizzle } from 'drizzle-orm/better-sqlite3';
4
+ import { ZodRawShape } from 'zod';
5
+
6
+ type ApexDb = ReturnType<typeof drizzle>;
7
+ /** Open a SQLite database and wrap it with Drizzle. */
8
+ declare function createDb(path: string): {
9
+ db: ApexDb;
10
+ sqlite: Database.Database;
11
+ };
12
+ /**
13
+ * Apply pending `*.sql` migrations from `dir`, tracked in `_apex_migrations`.
14
+ * Idempotent: each file runs once, in filename order, inside a transaction.
15
+ * Returns the names of migrations applied this run.
16
+ */
17
+ declare function applyMigrations(sqlite: Database.Database, dir: string): string[];
18
+ interface DefineResourceOptions {
19
+ db: ApexDb;
20
+ /** A Drizzle table. */
21
+ table: any;
22
+ /** Zod raw shape validating the create payload. */
23
+ insert: ZodRawShape;
24
+ /** Primary-key column name. Defaults to `id`. */
25
+ pk?: string;
26
+ }
27
+ /**
28
+ * Turn one table into a REST + MCP resource: `list`, `get`, and `create` routes,
29
+ * each also an MCP tool. This is the moat — your data is AI-callable by construction.
30
+ * GET /api/<name> → list
31
+ * GET /api/<name>/:id → get
32
+ * POST /api/<name> → create
33
+ */
34
+ declare function defineResource(name: string, opts: DefineResourceOptions): ApexResource;
35
+
36
+ export { type ApexDb, type DefineResourceOptions, applyMigrations, createDb, defineResource };
package/dist/index.js ADDED
@@ -0,0 +1,111 @@
1
+ // src/index.ts
2
+ import { readdirSync, readFileSync } from "fs";
3
+ import { join } from "path";
4
+ import { defineApexRoute } from "@apex-stack/core";
5
+ import Database from "better-sqlite3";
6
+ import { drizzle } from "drizzle-orm/better-sqlite3";
7
+ import { eq } from "drizzle-orm";
8
+ import { z } from "zod";
9
+ function createDb(path) {
10
+ const sqlite = new Database(path);
11
+ sqlite.pragma("journal_mode = WAL");
12
+ return { db: drizzle(sqlite), sqlite };
13
+ }
14
+ function applyMigrations(sqlite, dir) {
15
+ sqlite.exec(
16
+ "CREATE TABLE IF NOT EXISTS _apex_migrations (name TEXT PRIMARY KEY, applied_at TEXT NOT NULL)"
17
+ );
18
+ const applied = new Set(
19
+ sqlite.prepare("SELECT name FROM _apex_migrations").all().map((r) => r.name)
20
+ );
21
+ const done = [];
22
+ for (const file of readdirSync(dir).filter((f) => f.endsWith(".sql")).sort()) {
23
+ if (applied.has(file)) continue;
24
+ const sql = readFileSync(join(dir, file), "utf8");
25
+ const at = (/* @__PURE__ */ new Date()).toISOString();
26
+ sqlite.transaction(() => {
27
+ sqlite.exec(sql);
28
+ sqlite.prepare("INSERT INTO _apex_migrations (name, applied_at) VALUES (?, ?)").run(file, at);
29
+ })();
30
+ done.push(file);
31
+ }
32
+ return done;
33
+ }
34
+ function defineResource(name, opts) {
35
+ const { db, table, insert, pk = "id" } = opts;
36
+ const pkCol = table[pk];
37
+ const updateShape = { id: z.coerce.number() };
38
+ for (const [key, schema] of Object.entries(
39
+ insert
40
+ )) {
41
+ updateShape[key] = schema.optional();
42
+ }
43
+ return {
44
+ __apexResource: true,
45
+ name,
46
+ routes: [
47
+ {
48
+ pathSuffix: "",
49
+ mcpName: `${name}_list`,
50
+ route: defineApexRoute({
51
+ method: "GET",
52
+ description: `List all ${name}`,
53
+ mcp: true,
54
+ handler: () => db.select().from(table).all()
55
+ })
56
+ },
57
+ {
58
+ pathSuffix: "/:id",
59
+ mcpName: `${name}_get`,
60
+ route: defineApexRoute({
61
+ method: "GET",
62
+ description: `Get a single ${name} by id`,
63
+ input: { id: z.coerce.number() },
64
+ mcp: true,
65
+ handler: ({ input }) => db.select().from(table).where(eq(pkCol, input.id)).get() ?? null
66
+ })
67
+ },
68
+ {
69
+ pathSuffix: "",
70
+ mcpName: `${name}_create`,
71
+ route: defineApexRoute({
72
+ method: "POST",
73
+ description: `Create a ${name}`,
74
+ input: insert,
75
+ mcp: true,
76
+ handler: ({ input }) => db.insert(table).values(input).returning().get()
77
+ })
78
+ },
79
+ {
80
+ pathSuffix: "/:id",
81
+ mcpName: `${name}_update`,
82
+ route: defineApexRoute({
83
+ method: "PATCH",
84
+ description: `Update a ${name} by id (partial)`,
85
+ input: updateShape,
86
+ mcp: true,
87
+ handler: ({ input }) => {
88
+ const { id, ...fields } = input;
89
+ return db.update(table).set(fields).where(eq(pkCol, id)).returning().get() ?? null;
90
+ }
91
+ })
92
+ },
93
+ {
94
+ pathSuffix: "/:id",
95
+ mcpName: `${name}_delete`,
96
+ route: defineApexRoute({
97
+ method: "DELETE",
98
+ description: `Delete a ${name} by id`,
99
+ input: { id: z.coerce.number() },
100
+ mcp: true,
101
+ handler: ({ input }) => db.delete(table).where(eq(pkCol, input.id)).returning().get() ?? null
102
+ })
103
+ }
104
+ ]
105
+ };
106
+ }
107
+ export {
108
+ applyMigrations,
109
+ createDb,
110
+ defineResource
111
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@apex-stack/data",
3
+ "version": "0.1.1",
4
+ "description": "Data layer for Apex JS — Drizzle-backed resources that are REST + MCP by default",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "dependencies": {
18
+ "better-sqlite3": "^11.0.0",
19
+ "drizzle-orm": "^0.38.0",
20
+ "zod": "^4.4.3",
21
+ "@apex-stack/core": "0.1.1"
22
+ },
23
+ "engines": {
24
+ "node": ">=20.19"
25
+ },
26
+ "devDependencies": {
27
+ "@types/better-sqlite3": "^7.6.13"
28
+ },
29
+ "scripts": {
30
+ "build": "tsup",
31
+ "typecheck": "tsc --noEmit"
32
+ }
33
+ }