noxium 1.0.5 → 1.0.9
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 +169 -14
- package/build.ts +65 -0
- package/dist/drivers/mongo.cjs +1080 -0
- package/dist/drivers/mongo.cjs.map +456 -0
- package/dist/drivers/mongo.d.ts +4 -0
- package/dist/drivers/mongo.d.ts.map +1 -0
- package/dist/drivers/mongo.mjs +1081 -0
- package/dist/drivers/mongo.mjs.map +456 -0
- package/dist/drivers/sqlite.cjs +18 -0
- package/dist/drivers/sqlite.cjs.map +11 -0
- package/dist/drivers/sqlite.d.ts +4 -0
- package/dist/drivers/sqlite.d.ts.map +1 -0
- package/dist/drivers/sqlite.mjs +19 -0
- package/dist/drivers/sqlite.mjs.map +11 -0
- package/dist/error.cjs +18 -0
- package/dist/error.cjs.map +10 -0
- package/dist/error.d.ts +16 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.mjs +19 -0
- package/dist/error.mjs.map +10 -0
- package/dist/noxium.cjs +1080 -0
- package/dist/noxium.cjs.map +473 -0
- package/dist/noxium.d.ts +21 -0
- package/dist/noxium.d.ts.map +1 -0
- package/dist/noxium.mjs +1081 -0
- package/dist/noxium.mjs.map +473 -0
- package/dist/schema.cjs +4 -0
- package/dist/schema.cjs.map +9 -0
- package/dist/schema.d.ts +84 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.mjs +4 -0
- package/dist/schema.mjs.map +9 -0
- package/package.json +21 -23
- package/dist/cjs/createClient.cjs +0 -54
- package/dist/cjs/drivers/mongo.cjs +0 -61
- package/dist/cjs/drivers/sqlite.cjs +0 -74
- package/dist/cjs/error.cjs +0 -46
- package/dist/cjs/noxium.cjs +0 -39
- package/dist/cjs/schema.cjs +0 -2
- package/dist/esm/createClient.js +0 -52
- package/dist/esm/drivers/mongo.js +0 -59
- package/dist/esm/drivers/sqlite.js +0 -72
- package/dist/esm/error.js +0 -41
- package/dist/esm/noxium.js +0 -37
- package/dist/esm/schema.js +0 -1
package/README.md
CHANGED
|
@@ -1,27 +1,182 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Noxium
|
|
2
|
+
|
|
3
|
+
Lightweight, type-safe mini ORM for **Bun** and **TypeScript** that unifies **SQLite (`bun:sqlite`)** and **MongoDB (`Mongoose`)** behind a single declarative schema and query API.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Dual-Driver Support:** Switch seamlessly between `sqlite` (`bun:sqlite`) and `mongo` (`mongoose`) from a single configuration object.
|
|
8
|
+
- **Declarative Schema:** Define models, field types (`String`, `Number`, `Boolean`, `Date`, `"ref"`), `required`, `unique`, `default`, `trim`, and `lowercase` in one place.
|
|
9
|
+
- **Full TypeScript Type Safety:** Automatic type inference and IDE autocomplete (`Ctrl + Space`) for required fields on `insert`, unique keys on `findUnique`, and typed query results.
|
|
10
|
+
- **Built-in Relations (`populate`):** Unified relation loading using `.populate()` on MongoDB and automatic `LEFT JOIN` on SQLite.
|
|
11
|
+
- **Lifecycle Hooks & TTL:** Supports `afterFind` hooks (e.g., auto-expiring records) and automatic TTL cleanup across both drivers.
|
|
12
|
+
- **Consistent Error Handling:** Standardized `DuplicateError`, `ValidationError`, and `InvalidConfigError` regardless of the underlying database.
|
|
2
13
|
|
|
3
14
|
## Installation
|
|
4
15
|
|
|
5
|
-
|
|
6
|
-
|
|
16
|
+
Install `noxium` using **Bun** or **npm**:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
bun add noxium
|
|
20
|
+
# or
|
|
21
|
+
npm install noxium
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> **Note:** Make sure [Bun](https://bun.sh/) is installed if you plan to use the built-in `bun:sqlite` driver.
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### 1. Create Your Configuration File
|
|
30
|
+
|
|
31
|
+
You can name your configuration file anything (e.g., `noxium.config.ts` or `db.config.ts`). Use `as const satisfies SchemasMap` so TypeScript preserves literal types for `required` and `unique` fields.
|
|
7
32
|
|
|
8
|
-
|
|
9
|
-
|
|
33
|
+
```ts
|
|
34
|
+
// noxium.config.ts or db.config.ts
|
|
35
|
+
import { type NoxiumConfig, type SchemasMap } from "noxium";
|
|
10
36
|
|
|
11
|
-
|
|
12
|
-
|
|
37
|
+
const schemas = {
|
|
38
|
+
clients: {
|
|
39
|
+
fields: {
|
|
40
|
+
name: { type: String, required: true, unique: true, trim: true },
|
|
41
|
+
email: {
|
|
42
|
+
type: String,
|
|
43
|
+
required: true,
|
|
44
|
+
unique: true,
|
|
45
|
+
lowercase: true,
|
|
46
|
+
trim: true,
|
|
47
|
+
},
|
|
48
|
+
password: { type: String, required: true },
|
|
49
|
+
role: { type: String, default: "member" },
|
|
50
|
+
balance: { type: Number, default: 0 },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
products: {
|
|
54
|
+
fields: {
|
|
55
|
+
owner: { type: "ref", ref: "clients", required: true },
|
|
56
|
+
title: { type: String, required: true, trim: true },
|
|
57
|
+
slug: {
|
|
58
|
+
type: String,
|
|
59
|
+
required: true,
|
|
60
|
+
unique: true,
|
|
61
|
+
lowercase: true,
|
|
62
|
+
trim: true,
|
|
63
|
+
},
|
|
64
|
+
price: { type: Number, required: true, default: 0 },
|
|
65
|
+
stock: { type: Number, default: 0 },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
} as const satisfies SchemasMap;
|
|
13
69
|
|
|
14
|
-
|
|
15
|
-
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
70
|
+
export type AppSchemas = typeof schemas;
|
|
16
71
|
|
|
17
|
-
|
|
18
|
-
|
|
72
|
+
export default function getConfig(): NoxiumConfig<AppSchemas> {
|
|
73
|
+
const isMongo = process.env.DB_DRIVER === "mongo";
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
database: isMongo
|
|
77
|
+
? {
|
|
78
|
+
type: "mongo",
|
|
79
|
+
mongo: {
|
|
80
|
+
uri: process.env.MONGODB_URI || "mongodb://localhost:27017",
|
|
81
|
+
dbName: process.env.MONGODB_DB || "app",
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
: {
|
|
85
|
+
type: "sqlite",
|
|
86
|
+
sqlite: {
|
|
87
|
+
path: process.env.SQLITE_PATH || "./data.db",
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
schemas,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
19
93
|
```
|
|
20
94
|
|
|
21
|
-
|
|
95
|
+
### 2. Initialize & Query Data
|
|
96
|
+
|
|
97
|
+
Pass your configuration into `new Noxium<AppSchemas>(...)` and access your models via `noxium.model(...)` or directly as properties (`noxium.clients`, `noxium.shortlinks`).
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { Noxium } from "noxium";
|
|
101
|
+
import { DuplicateError, ValidationError } from "noxium/error";
|
|
102
|
+
import getConfig, { type AppSchemas } from "./noxium.config";
|
|
103
|
+
|
|
104
|
+
const noxium = new Noxium<AppSchemas>(getConfig());
|
|
105
|
+
const clients = noxium.model("clients");
|
|
106
|
+
|
|
107
|
+
async function main() {
|
|
108
|
+
try {
|
|
109
|
+
// Required fields without defaults (`name`, `email`, `password`) are enforced by TypeScript
|
|
110
|
+
const user = await clients.insert({
|
|
111
|
+
name: "Ayiin",
|
|
112
|
+
email: "ayiin@example.com",
|
|
113
|
+
password: "secret",
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Insert related record
|
|
117
|
+
await noxium.products.insert({
|
|
118
|
+
owner: user.id,
|
|
119
|
+
title: "Mechanical Keyboard",
|
|
120
|
+
slug: "mechanical-keyboard",
|
|
121
|
+
price: 750000,
|
|
122
|
+
stock: 10,
|
|
123
|
+
});
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof DuplicateError) {
|
|
126
|
+
console.error("Duplicate value:", error.column, error.value);
|
|
127
|
+
} else if (error instanceof ValidationError) {
|
|
128
|
+
console.error("Missing required field:", error.column);
|
|
129
|
+
} else {
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Query all records
|
|
135
|
+
const allClients = await clients.findAll();
|
|
136
|
+
console.log("All clients:", allClients);
|
|
137
|
+
|
|
138
|
+
// Query by unique field (`id`, `name`, or `email`)
|
|
139
|
+
const client = await clients.findUnique({ email: "ayiin@example.com" });
|
|
140
|
+
console.log("Found client:", client);
|
|
141
|
+
|
|
142
|
+
// Query with relation populate (MongoDB `.populate()` / SQLite `LEFT JOIN`)
|
|
143
|
+
const product = await noxium.products.findUnique(
|
|
144
|
+
{ slug: "mechanical-keyboard" },
|
|
145
|
+
{ populate: { owner: ["name", "email"] } },
|
|
146
|
+
);
|
|
147
|
+
console.log("Product with owner:", product);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
main();
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Schema Options
|
|
154
|
+
|
|
155
|
+
| Option | Type | Description |
|
|
156
|
+
| ----------- | --------- | ----------------------------------------------------------- | ------- | ---- | ------ | --------------------------------------------------- |
|
|
157
|
+
| `type` | `String | Number | Boolean | Date | "ref"` | Field data type or foreign key reference (`"ref"`). |
|
|
158
|
+
| `ref` | `string` | Target model/table name when `type: "ref"`. |
|
|
159
|
+
| `required` | `boolean` | Marks field as required on insert and `NOT NULL` in SQLite. |
|
|
160
|
+
| `unique` | `boolean` | Enforces uniqueness and enables lookup in `findUnique`. |
|
|
161
|
+
| `default` | `any` | Default value applied when omitted during `insert`. |
|
|
162
|
+
| `trim` | `boolean` | Automatically trims whitespace on string values. |
|
|
163
|
+
| `lowercase` | `boolean` | Automatically converts string values to lowercase. |
|
|
164
|
+
|
|
165
|
+
### Model-Level Options
|
|
166
|
+
|
|
167
|
+
- **`timestamps`**: Automatically manages `createdAt` and `updatedAt` (`true` by default).
|
|
168
|
+
- **`ttl`**: `{ field: "createdAt", seconds: 2592000 }` — Creates a MongoDB TTL index and automatically cleans up expired rows in SQLite.
|
|
169
|
+
- **`hooks.afterFind`**: Runs post-query transformations or maintenance logic (set `doc.__dirty = true` in SQLite to persist updates).
|
|
170
|
+
|
|
171
|
+
## Available Model Methods
|
|
22
172
|
|
|
23
|
-
-
|
|
173
|
+
- `insert(data)` — Inserts a new record and returns the created document.
|
|
174
|
+
- `update(id, data)` — Updates a record by `id` and returns the updated document.
|
|
175
|
+
- `findAll(options?)` — Retrieves all records in the table/collection.
|
|
176
|
+
- `findUnique(where, options?)` — Finds a single record by `id` or any `unique` field.
|
|
177
|
+
- `findMany(where?, options?)` — Finds all records matching the filter.
|
|
178
|
+
- `deleteMany(where)` — Deletes matching records and returns the deleted count.
|
|
24
179
|
|
|
25
180
|
## License
|
|
26
181
|
|
|
27
|
-
[MIT](LICENSE)
|
|
182
|
+
[MIT](https://www.google.com/search?q=LICENSE)
|
package/build.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { rm } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
// 1. Bersihkan folder dist sebelum build
|
|
4
|
+
await rm("./dist", { recursive: true, force: true });
|
|
5
|
+
console.log("🧹 Folder dist dibersihkan.");
|
|
6
|
+
|
|
7
|
+
const glob = new Bun.Glob("**/*.{ts,tsx}");
|
|
8
|
+
const entrypoints: string[] = [];
|
|
9
|
+
|
|
10
|
+
for (const path of glob.scanSync({
|
|
11
|
+
cwd: "./src",
|
|
12
|
+
onlyFiles: true,
|
|
13
|
+
})) {
|
|
14
|
+
entrypoints.push(`./src/${path.replaceAll("\\", "/")}`); // 👈 Path lengkap untuk dibaca oleh Bun.build
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
console.log("Entrypoints:", entrypoints);
|
|
18
|
+
|
|
19
|
+
// 2. Build untuk ESM (.mjs)
|
|
20
|
+
const esmResult = await Bun.build({
|
|
21
|
+
entrypoints,
|
|
22
|
+
root: "./src", // 👈 Beritahu Bun root-nya adalah src agar struktur dist tetap flat
|
|
23
|
+
outdir: "./dist",
|
|
24
|
+
target: "bun",
|
|
25
|
+
format: "esm",
|
|
26
|
+
naming: "[dir]/[name].mjs",
|
|
27
|
+
sourcemap: "linked",
|
|
28
|
+
minify: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (!esmResult.success) {
|
|
32
|
+
console.error("❌ ESM Build Gagal:", esmResult.logs);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
console.log("✅ ESM Build Selesai (.mjs)");
|
|
36
|
+
|
|
37
|
+
// 3. Build untuk CJS (.cjs)
|
|
38
|
+
const cjsResult = await Bun.build({
|
|
39
|
+
entrypoints,
|
|
40
|
+
root: "./src", // 👈 Terapkan juga untuk CJS
|
|
41
|
+
outdir: "./dist",
|
|
42
|
+
target: "node",
|
|
43
|
+
format: "cjs",
|
|
44
|
+
naming: "[dir]/[name].cjs",
|
|
45
|
+
sourcemap: "linked",
|
|
46
|
+
minify: true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (!cjsResult.success) {
|
|
50
|
+
console.error("❌ CJS Build Gagal:", cjsResult.logs);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
console.log("✅ CJS Build CJS Selesai (.cjs)");
|
|
54
|
+
|
|
55
|
+
// 4. Generate TypeScript Declaration Files (.d.ts)
|
|
56
|
+
console.log("⏳ Menggenerasi berkas .d.ts...");
|
|
57
|
+
const proc = Bun.spawn(["bunx", "tsc", "-p", "tsconfig.build.json"]);
|
|
58
|
+
await proc.exited;
|
|
59
|
+
|
|
60
|
+
if (proc.exitCode !== 0) {
|
|
61
|
+
console.error("❌ Generasi .d.ts gagal.");
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log("🎉 Build berhasil! Output tersedia di folder ./dist");
|