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.
Files changed (45) hide show
  1. package/README.md +169 -14
  2. package/build.ts +65 -0
  3. package/dist/drivers/mongo.cjs +1080 -0
  4. package/dist/drivers/mongo.cjs.map +456 -0
  5. package/dist/drivers/mongo.d.ts +4 -0
  6. package/dist/drivers/mongo.d.ts.map +1 -0
  7. package/dist/drivers/mongo.mjs +1081 -0
  8. package/dist/drivers/mongo.mjs.map +456 -0
  9. package/dist/drivers/sqlite.cjs +18 -0
  10. package/dist/drivers/sqlite.cjs.map +11 -0
  11. package/dist/drivers/sqlite.d.ts +4 -0
  12. package/dist/drivers/sqlite.d.ts.map +1 -0
  13. package/dist/drivers/sqlite.mjs +19 -0
  14. package/dist/drivers/sqlite.mjs.map +11 -0
  15. package/dist/error.cjs +18 -0
  16. package/dist/error.cjs.map +10 -0
  17. package/dist/error.d.ts +16 -0
  18. package/dist/error.d.ts.map +1 -0
  19. package/dist/error.mjs +19 -0
  20. package/dist/error.mjs.map +10 -0
  21. package/dist/noxium.cjs +1080 -0
  22. package/dist/noxium.cjs.map +473 -0
  23. package/dist/noxium.d.ts +21 -0
  24. package/dist/noxium.d.ts.map +1 -0
  25. package/dist/noxium.mjs +1081 -0
  26. package/dist/noxium.mjs.map +473 -0
  27. package/dist/schema.cjs +4 -0
  28. package/dist/schema.cjs.map +9 -0
  29. package/dist/schema.d.ts +84 -0
  30. package/dist/schema.d.ts.map +1 -0
  31. package/dist/schema.mjs +4 -0
  32. package/dist/schema.mjs.map +9 -0
  33. package/package.json +21 -23
  34. package/dist/cjs/createClient.cjs +0 -54
  35. package/dist/cjs/drivers/mongo.cjs +0 -61
  36. package/dist/cjs/drivers/sqlite.cjs +0 -74
  37. package/dist/cjs/error.cjs +0 -46
  38. package/dist/cjs/noxium.cjs +0 -39
  39. package/dist/cjs/schema.cjs +0 -2
  40. package/dist/esm/createClient.js +0 -52
  41. package/dist/esm/drivers/mongo.js +0 -59
  42. package/dist/esm/drivers/sqlite.js +0 -72
  43. package/dist/esm/error.js +0 -41
  44. package/dist/esm/noxium.js +0 -37
  45. package/dist/esm/schema.js +0 -1
@@ -0,0 +1,4 @@
1
+ var y=Object.create;var{getPrototypeOf:M,defineProperty:r,getOwnPropertyNames:u,getOwnPropertyDescriptor:x}=Object,s=Object.prototype.hasOwnProperty;function p(e){return this[e]}var c,K,C=(e,n,t)=>{var i=e!=null&&typeof e==="object";if(i){var l=n?c??=new WeakMap:K??=new WeakMap,f=l.get(e);if(f)return f}t=e!=null?y(M(e)):{};let o=n||!e||!e.__esModule||!s.call(e,"default")?r(t,"default",{value:e,enumerable:!0}):t;if(e&&typeof e==="object"||typeof e==="function"){for(let d of u(e))if(!s.call(o,d))r(o,d,{get:p.bind(e,d),enumerable:!0})}if(i)l.set(e,o);return o},m=(e)=>{var n=(a??=new WeakMap).get(e),t;if(n)return n;if(n=r({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var i of u(e))if(!s.call(n,i))r(n,i,{get:p.bind(e,i),enumerable:!(t=x(e,i))||t.enumerable})}return a.set(e,n),n},a,q=(e,n)=>()=>(n||e((n={exports:{}}).exports,n),n.exports);var g=(e)=>e;function F(e,n){this[e]=g.bind(null,n)}var I=(e,n)=>{for(var t in n)r(e,t,{get:n[t],enumerable:!0,configurable:!0,set:F.bind(n,t)})};var T={};module.exports=m(T);
2
+
3
+ //# debugId=2415BFDCE9414E9664756E2164756E21
4
+ //# sourceMappingURL=schema.cjs.map
@@ -0,0 +1,9 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [
5
+ ],
6
+ "mappings": "",
7
+ "debugId": "2415BFDCE9414E9664756E2164756E21",
8
+ "names": []
9
+ }
@@ -0,0 +1,84 @@
1
+ export type FieldConstructor = StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor;
2
+ export interface FieldDefinition {
3
+ type: FieldConstructor | "ref";
4
+ ref?: string;
5
+ required?: boolean;
6
+ unique?: boolean;
7
+ default?: any;
8
+ lowercase?: boolean;
9
+ trim?: boolean;
10
+ }
11
+ export type ModelFields = Record<string, FieldDefinition>;
12
+ export interface ModelConfig {
13
+ fields: ModelFields;
14
+ timestamps?: boolean;
15
+ ttl?: {
16
+ field: string;
17
+ seconds: number;
18
+ };
19
+ hooks?: {
20
+ afterFind?: (docs: any[]) => Promise<void> | void;
21
+ };
22
+ }
23
+ export type SchemasMap = Record<string, ModelConfig>;
24
+ export interface QueryOptions {
25
+ populate?: Record<string, string[]>;
26
+ }
27
+ type InferFieldType<F extends FieldDefinition> = F["type"] extends NumberConstructor ? number : F["type"] extends BooleanConstructor ? boolean : F["type"] extends DateConstructor ? Date : F["type"] extends "ref" ? string : string;
28
+ type RequiredInsertKeys<M extends ModelConfig> = {
29
+ [K in keyof M["fields"]]: M["fields"][K] extends {
30
+ required: true;
31
+ default?: undefined;
32
+ } ? K : never;
33
+ }[keyof M["fields"]];
34
+ export type InferSchemaRow<M extends ModelConfig> = {
35
+ id: string;
36
+ } & {
37
+ [K in keyof M["fields"] as M["fields"][K] extends {
38
+ required: true;
39
+ default?: undefined;
40
+ } ? K : never]: InferFieldType<M["fields"][K]>;
41
+ } & {
42
+ [K in keyof M["fields"] as M["fields"][K] extends {
43
+ required: true;
44
+ default?: undefined;
45
+ } ? never : K]?: InferFieldType<M["fields"][K]>;
46
+ } & (M["timestamps"] extends false ? {} : {
47
+ createdAt: Date;
48
+ updatedAt: Date;
49
+ });
50
+ export type InferInsertData<M extends ModelConfig> = {
51
+ id?: string;
52
+ } & {
53
+ [K in RequiredInsertKeys<M>]: InferFieldType<M["fields"][K]>;
54
+ } & {
55
+ [K in Exclude<keyof M["fields"], RequiredInsertKeys<M>>]?: InferFieldType<M["fields"][K]>;
56
+ } & (M["timestamps"] extends false ? {} : {
57
+ createdAt?: Date;
58
+ updatedAt?: Date;
59
+ });
60
+ export type UniqueKeys<M extends ModelConfig> = "id" | {
61
+ [K in keyof M["fields"]]: M["fields"][K] extends {
62
+ unique: true;
63
+ } ? K : never;
64
+ }[keyof M["fields"]];
65
+ export type UniqueWhere<M extends ModelConfig> = {
66
+ [K in UniqueKeys<M>]: {
67
+ [P in K]?: P extends keyof InferSchemaRow<M> ? InferSchemaRow<M>[P] : string;
68
+ };
69
+ }[UniqueKeys<M>];
70
+ export type WhereFilter<T> = {
71
+ [K in keyof T]?: T[K];
72
+ };
73
+ type KeysOfUnion<U> = U extends unknown ? keyof U : never;
74
+ export interface NoxiumModelClient<T = Record<string, any>, InsertT = Partial<T> & Record<string, any>, UniqueW = Record<string, any>> {
75
+ insert(data: InsertT): Promise<T>;
76
+ update(where: UniqueW, data: Partial<Omit<T, KeysOfUnion<UniqueW> | "id">>): Promise<T | null>;
77
+ findAll(options?: QueryOptions): Promise<T[]>;
78
+ findUnique(where: UniqueW, options?: QueryOptions): Promise<T | null>;
79
+ findMany(where?: WhereFilter<T>, options?: QueryOptions): Promise<T[]>;
80
+ deleteMany(where: WhereFilter<T>): Promise<number>;
81
+ }
82
+ export type TypedModelClient<M extends ModelConfig> = NoxiumModelClient<InferSchemaRow<M>, InferInsertData<M>, UniqueWhere<M>>;
83
+ export {};
84
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GACxB,iBAAiB,GACjB,iBAAiB,GACjB,kBAAkB,GAClB,eAAe,CAAC;AAEpB,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,gBAAgB,GAAG,KAAK,CAAC;IAC/B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,IAAI,C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AI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,GAClD,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IACrB,OAAO,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IAC9C,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IACtE,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IACvE,UAAU,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACpD;AAGD,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,WAAW,IAAI,iBAAiB,CACrE,cAAc,CAAC,CAAC,CAAC,EACjB,eAAe,CAAC,CAAC,CAAC,EAClB,WAAW,CAAC,CAAC,CAAC,CACf,CAAC"}
@@ -0,0 +1,4 @@
1
+ // @bun
2
+
3
+ //# debugId=A0E4D24D4940809F64756E2164756E21
4
+ //# sourceMappingURL=schema.mjs.map
@@ -0,0 +1,9 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [
5
+ ],
6
+ "mappings": "",
7
+ "debugId": "A0E4D24D4940809F64756E2164756E21",
8
+ "names": []
9
+ }
package/package.json CHANGED
@@ -1,33 +1,37 @@
1
1
  {
2
2
  "name": "noxium",
3
- "version": "1.0.5",
3
+ "version": "1.0.9",
4
4
  "description": "Unified ORM for SQLite and MongoDB with TypeScript",
5
- "main": "./dist/cjs/noxium.cjs",
6
- "module": "./dist/esm/noxium.js",
7
- "types": "./dist/src/noxium.d.ts",
8
5
  "type": "module",
6
+ "main": "./dist/noxium.cjs",
7
+ "module": "./dist/noxium.mjs",
8
+ "types": "./dist/noxium.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": {
12
- "require": "./dist/src/noxium.d.cts",
13
- "default": "./dist/src/noxium.d.ts"
14
- },
15
11
  "import": {
16
- "default": "./dist/esm/noxium.js",
17
- "require": "./dist/cjs/noxium.cjs"
12
+ "types": "./dist/noxium.d.ts",
13
+ "default": "./dist/noxium.mjs"
18
14
  },
19
15
  "require": {
20
- "default": "./dist/esm/noxium.js",
21
- "require": "./dist/cjs/noxium.cjs"
16
+ "types": "./dist/noxium.d.cts",
17
+ "default": "./dist/noxium.cjs"
18
+ }
19
+ },
20
+ "./error": {
21
+ "import": {
22
+ "types": "./dist/error.d.ts",
23
+ "default": "./dist/error.mjs"
22
24
  },
23
- "default": {
24
- "require": "./dist/cjs/noxium.cjs",
25
- "default": "./dist/esm/noxium.js"
25
+ "require": {
26
+ "types": "./dist/error.d.cts",
27
+ "default": "./dist/error.cjs"
26
28
  }
27
29
  }
28
30
  },
29
31
  "scripts": {
30
- "build": "rollup -c rollup.config.mjs"
32
+ "typecheck": "bun check",
33
+ "build": "bun run build.ts",
34
+ "prepublishOnly": "bun run build"
31
35
  },
32
36
  "repository": {
33
37
  "type": "git",
@@ -54,14 +58,8 @@
54
58
  "typescript": "^7.0.2"
55
59
  },
56
60
  "dependencies": {
57
- "@rollup/plugin-commonjs": "^29.0.3",
58
- "@rollup/plugin-json": "^6.1.0",
59
- "@rollup/plugin-node-resolve": "^16.0.3",
60
- "@rollup/plugin-swc": "^0.4.1",
61
61
  "better-sqlite3": "^13.0.3",
62
62
  "glob": "^13.0.6",
63
- "mongoose": "^9.9.4",
64
- "rollup": "^4.63.1",
65
- "rollup-plugin-dts": "^6.5.1"
63
+ "mongoose": "^9.9.4"
66
64
  }
67
65
  }
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- var Database = require('better-sqlite3');
4
- var mongo = require('./drivers/mongo.cjs');
5
- var sqlite = require('./drivers/sqlite.cjs');
6
- var error = require('./error.cjs');
7
-
8
- /**
9
- * Validasi config dan kembalikan driver ORM sesuai type database
10
- */ function createClient(config, schema) {
11
- var dbConfig = config.database;
12
- switch(dbConfig.type){
13
- case "sqlite":
14
- {
15
- if (!dbConfig.sqlite?.path || dbConfig.sqlite.path.trim() === "") {
16
- throw new error.InvalidConfigError("Konfigurasi SQLite tidak valid: field `path` wajib diisi.");
17
- }
18
- if (!schema) throw new error.InvalidConfigError("SQLite membutuhkan schema.");
19
- const db = new Database(dbConfig.sqlite.path);
20
- return new sqlite.NoxiumSqlite(db, schema);
21
- }
22
- case "mongo":
23
- {
24
- // Validasi config MongoDB
25
- if (!dbConfig.mongo?.uri || dbConfig.mongo.uri.trim() === "") {
26
- throw new error.InvalidConfigError("Konfigurasi MongoDB tidak valid: field `uri` wajib diisi.");
27
- }
28
- if (!schema) throw new error.InvalidConfigError("Mongo membutuhkan schema.");
29
- return new mongo.NoxiumMongo(schema, dbConfig);
30
- }
31
- case "postgres":
32
- {
33
- if (!dbConfig.postgres?.uri || dbConfig.postgres.uri.trim() === "") {
34
- throw new error.InvalidConfigError("Konfigurasi PostgreSQL tidak valid: field `uri` wajib diisi.");
35
- }
36
- if (!schema) throw new error.InvalidConfigError("Postgres membutuhkan schema.");
37
- // TODO: return new NoxiumPostgres(schema, dbConfig.postgres.uri);
38
- throw new error.InvalidConfigError("Driver Postgres belum diimplementasikan.");
39
- }
40
- case "mysql":
41
- {
42
- if (!dbConfig.mysql?.uri || dbConfig.mysql.uri.trim() === "") {
43
- throw new error.InvalidConfigError("Konfigurasi MySQL tidak valid: field `uri` wajib diisi.");
44
- }
45
- if (!schema) throw new error.InvalidConfigError("MySQL membutuhkan schema.");
46
- // TODO: return new NoxiumMysql(schema, dbConfig.mysql.uri);
47
- throw new error.InvalidConfigError("Driver MySQL belum diimplementasikan.");
48
- }
49
- default:
50
- throw new error.InvalidConfigError("Tipe database tidak dikenali.");
51
- }
52
- }
53
-
54
- exports.createClient = createClient;
@@ -1,61 +0,0 @@
1
- 'use strict';
2
-
3
- var mongoose = require('mongoose');
4
- var error = require('../error.cjs');
5
-
6
- class NoxiumMongo {
7
- async insert(data) {
8
- try {
9
- const doc = new this.model(data);
10
- await doc.save();
11
- } catch (err) {
12
- if (err.code === 11000) {
13
- const field = Object.keys(err.keyValue)[0];
14
- throw new error.DuplicateError(field, err.keyValue[field]);
15
- }
16
- if (err.errors) {
17
- const field = Object.keys(err.errors)[0];
18
- throw new error.ValidationError(field);
19
- }
20
- throw err;
21
- }
22
- }
23
- async update(id, data) {
24
- await this.model.updateOne({
25
- _id: id
26
- }, {
27
- $set: data
28
- });
29
- }
30
- async findAll() {
31
- return this.model.find().exec();
32
- }
33
- async findUnique(column, value) {
34
- return this.model.findOne({
35
- [column]: value
36
- }).exec();
37
- }
38
- async findMany(column, value) {
39
- return this.model.find({
40
- [column]: value
41
- }).exec();
42
- }
43
- constructor(schema, config){
44
- mongoose.connect(config.mongo.uri, {
45
- dbName: config.mongo.dbName ?? "test"
46
- });
47
- // generate definisi schema mongoose
48
- const mongooseSchemaDef = {};
49
- for (const col of schema.columns){
50
- const def = {};
51
- def.type = col.type === "int" ? Number : String;
52
- if (col.required) def.required = true;
53
- if (col.unique) def.unique = true;
54
- mongooseSchemaDef[col.name] = def;
55
- }
56
- const mongooseSchema = new mongoose.Schema(mongooseSchemaDef);
57
- this.model = mongoose.model(schema.name, mongooseSchema);
58
- }
59
- }
60
-
61
- exports.NoxiumMongo = NoxiumMongo;
@@ -1,74 +0,0 @@
1
- 'use strict';
2
-
3
- var error = require('../error.cjs');
4
-
5
- class NoxiumSqlite {
6
- createTable() {
7
- const cols = this.schema.columns.map((c)=>{
8
- let def = `${c.name} ${c.type.toUpperCase()}`;
9
- if (c.unique) def += " UNIQUE";
10
- if (c.required) def += " NOT NULL";
11
- return def;
12
- }).join(", ");
13
- this.db.prepare(`CREATE TABLE IF NOT EXISTS ${this.schema.name} (${cols})`).run();
14
- }
15
- // ✅ insert dengan validasi unique
16
- async insert(data) {
17
- // ✅ cek required
18
- for (const col of this.schema.columns){
19
- if (col.required && data[col.name] === undefined) {
20
- throw new error.ValidationError(col.name);
21
- }
22
- }
23
- // validasi unique
24
- for (const col of this.schema.columns){
25
- if (col.unique && data[col.name] !== undefined) {
26
- const value = data[col.name];
27
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ?`);
28
- const existing = stmt.get(value);
29
- if (existing) {
30
- throw new error.DuplicateError(col.name, value);
31
- }
32
- }
33
- }
34
- const keys = Object.keys(data);
35
- const placeholders = keys.map(()=>"?").join(", ");
36
- const stmt = this.db.prepare(`INSERT INTO ${this.schema.name} (${keys.join(",")}) VALUES (${placeholders})`);
37
- stmt.run(...keys.map((k)=>data[k]));
38
- }
39
- // ✅ update dengan validasi unique
40
- async update(id, data) {
41
- for (const col of this.schema.columns){
42
- if (col.unique && data[col.name] !== undefined) {
43
- const value = data[col.name];
44
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ? AND id != ?`);
45
- const existing = stmt.get(value, id);
46
- if (existing) {
47
- throw new error.DuplicateError(col.name, value);
48
- }
49
- }
50
- }
51
- const keys = Object.keys(data);
52
- const setClause = keys.map((k)=>`${String(k)} = ?`).join(", ");
53
- const stmt = this.db.prepare(`UPDATE ${this.schema.name} SET ${setClause} WHERE id = ?`);
54
- stmt.run(...keys.map((k)=>data[k]), id);
55
- }
56
- async findAll() {
57
- return this.db.prepare(`SELECT * FROM ${this.schema.name}`).all();
58
- }
59
- async findUnique(column, value) {
60
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
61
- return stmt.get(value);
62
- }
63
- async findMany(column, value) {
64
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
65
- return stmt.all(value);
66
- }
67
- constructor(db, schema){
68
- this.db = db;
69
- this.schema = schema;
70
- this.createTable(); // ✅ otomatis buat table saat instance dibuat
71
- }
72
- }
73
-
74
- exports.NoxiumSqlite = NoxiumSqlite;
@@ -1,46 +0,0 @@
1
- 'use strict';
2
-
3
- // errors.ts
4
- class DuplicateError extends Error {
5
- constructor(column, value){
6
- super(`Duplicate unique value: ${column} = ${value}`);
7
- this.name = "DuplicateError";
8
- this.column = column;
9
- this.value = value;
10
- }
11
- }
12
- class ValidationError extends Error {
13
- constructor(column){
14
- super(`Missing required field: ${column}`);
15
- this.name = "ValidationError";
16
- this.column = column;
17
- }
18
- }
19
- class RequiredConfigError extends Error {
20
- constructor(){
21
- super("File konfigurasi wajib `noxium.config.ts` tidak ditemukan.\nSilakan buat file tersebut di direktori root proyek Anda (misalnya: `noxium.config.ts`).\n\n" + `// noxium.config.ts
22
- export default function getConfig() {
23
- return {
24
- database: {
25
- type: "sqlite", // default minimal pakai SQLite
26
- sqlite: {
27
- path: "./data.db", // file database lokal
28
- },
29
- },
30
- };
31
- }
32
- `);
33
- this.name = "RequiredConfigError";
34
- }
35
- }
36
- class InvalidConfigError extends Error {
37
- constructor(message){
38
- super(message);
39
- this.name = "InvalidConfigError";
40
- }
41
- }
42
-
43
- exports.DuplicateError = DuplicateError;
44
- exports.InvalidConfigError = InvalidConfigError;
45
- exports.RequiredConfigError = RequiredConfigError;
46
- exports.ValidationError = ValidationError;
@@ -1,39 +0,0 @@
1
- 'use strict';
2
-
3
- var fs = require('fs');
4
- var path = require('path');
5
- var createClient = require('./createClient.cjs');
6
- var error = require('./error.cjs');
7
-
8
- const configPath = path.resolve(process.cwd(), "noxium.config.ts");
9
- if (!fs.existsSync(configPath)) {
10
- throw new error.RequiredConfigError();
11
- }
12
- const rawConfig = require(configPath).default;
13
- const config = typeof rawConfig === "function" ? rawConfig() : rawConfig;
14
- class Noxium {
15
- async insert(data) {
16
- return await this.orm.insert(data);
17
- }
18
- async update(id, data) {
19
- return await this.orm.update(id, data);
20
- }
21
- async findAll() {
22
- return await this.orm.findAll();
23
- }
24
- async findUnique(data) {
25
- const column = Object.keys(data)[0];
26
- const value = data[column];
27
- return await this.orm.findUnique(column, value);
28
- }
29
- async findMany(data) {
30
- const column = Object.keys(data)[0];
31
- const value = data[column];
32
- return await this.orm.findMany(column, value);
33
- }
34
- constructor(schema){
35
- this.orm = createClient.createClient(config, schema);
36
- }
37
- }
38
-
39
- exports.Noxium = Noxium;
@@ -1,2 +0,0 @@
1
- 'use strict';
2
-
@@ -1,52 +0,0 @@
1
- import Database from 'better-sqlite3';
2
- import { NoxiumMongo } from './drivers/mongo.js';
3
- import { NoxiumSqlite } from './drivers/sqlite.js';
4
- import { InvalidConfigError } from './error.js';
5
-
6
- /**
7
- * Validasi config dan kembalikan driver ORM sesuai type database
8
- */ function createClient(config, schema) {
9
- var dbConfig = config.database;
10
- switch(dbConfig.type){
11
- case "sqlite":
12
- {
13
- if (!dbConfig.sqlite?.path || dbConfig.sqlite.path.trim() === "") {
14
- throw new InvalidConfigError("Konfigurasi SQLite tidak valid: field `path` wajib diisi.");
15
- }
16
- if (!schema) throw new InvalidConfigError("SQLite membutuhkan schema.");
17
- const db = new Database(dbConfig.sqlite.path);
18
- return new NoxiumSqlite(db, schema);
19
- }
20
- case "mongo":
21
- {
22
- // Validasi config MongoDB
23
- if (!dbConfig.mongo?.uri || dbConfig.mongo.uri.trim() === "") {
24
- throw new InvalidConfigError("Konfigurasi MongoDB tidak valid: field `uri` wajib diisi.");
25
- }
26
- if (!schema) throw new InvalidConfigError("Mongo membutuhkan schema.");
27
- return new NoxiumMongo(schema, dbConfig);
28
- }
29
- case "postgres":
30
- {
31
- if (!dbConfig.postgres?.uri || dbConfig.postgres.uri.trim() === "") {
32
- throw new InvalidConfigError("Konfigurasi PostgreSQL tidak valid: field `uri` wajib diisi.");
33
- }
34
- if (!schema) throw new InvalidConfigError("Postgres membutuhkan schema.");
35
- // TODO: return new NoxiumPostgres(schema, dbConfig.postgres.uri);
36
- throw new InvalidConfigError("Driver Postgres belum diimplementasikan.");
37
- }
38
- case "mysql":
39
- {
40
- if (!dbConfig.mysql?.uri || dbConfig.mysql.uri.trim() === "") {
41
- throw new InvalidConfigError("Konfigurasi MySQL tidak valid: field `uri` wajib diisi.");
42
- }
43
- if (!schema) throw new InvalidConfigError("MySQL membutuhkan schema.");
44
- // TODO: return new NoxiumMysql(schema, dbConfig.mysql.uri);
45
- throw new InvalidConfigError("Driver MySQL belum diimplementasikan.");
46
- }
47
- default:
48
- throw new InvalidConfigError("Tipe database tidak dikenali.");
49
- }
50
- }
51
-
52
- export { createClient };
@@ -1,59 +0,0 @@
1
- import mongoose, { Schema } from 'mongoose';
2
- import { DuplicateError, ValidationError } from '../error.js';
3
-
4
- class NoxiumMongo {
5
- async insert(data) {
6
- try {
7
- const doc = new this.model(data);
8
- await doc.save();
9
- } catch (err) {
10
- if (err.code === 11000) {
11
- const field = Object.keys(err.keyValue)[0];
12
- throw new DuplicateError(field, err.keyValue[field]);
13
- }
14
- if (err.errors) {
15
- const field = Object.keys(err.errors)[0];
16
- throw new ValidationError(field);
17
- }
18
- throw err;
19
- }
20
- }
21
- async update(id, data) {
22
- await this.model.updateOne({
23
- _id: id
24
- }, {
25
- $set: data
26
- });
27
- }
28
- async findAll() {
29
- return this.model.find().exec();
30
- }
31
- async findUnique(column, value) {
32
- return this.model.findOne({
33
- [column]: value
34
- }).exec();
35
- }
36
- async findMany(column, value) {
37
- return this.model.find({
38
- [column]: value
39
- }).exec();
40
- }
41
- constructor(schema, config){
42
- mongoose.connect(config.mongo.uri, {
43
- dbName: config.mongo.dbName ?? "test"
44
- });
45
- // generate definisi schema mongoose
46
- const mongooseSchemaDef = {};
47
- for (const col of schema.columns){
48
- const def = {};
49
- def.type = col.type === "int" ? Number : String;
50
- if (col.required) def.required = true;
51
- if (col.unique) def.unique = true;
52
- mongooseSchemaDef[col.name] = def;
53
- }
54
- const mongooseSchema = new Schema(mongooseSchemaDef);
55
- this.model = mongoose.model(schema.name, mongooseSchema);
56
- }
57
- }
58
-
59
- export { NoxiumMongo };
@@ -1,72 +0,0 @@
1
- import { ValidationError, DuplicateError } from '../error.js';
2
-
3
- class NoxiumSqlite {
4
- createTable() {
5
- const cols = this.schema.columns.map((c)=>{
6
- let def = `${c.name} ${c.type.toUpperCase()}`;
7
- if (c.unique) def += " UNIQUE";
8
- if (c.required) def += " NOT NULL";
9
- return def;
10
- }).join(", ");
11
- this.db.prepare(`CREATE TABLE IF NOT EXISTS ${this.schema.name} (${cols})`).run();
12
- }
13
- // ✅ insert dengan validasi unique
14
- async insert(data) {
15
- // ✅ cek required
16
- for (const col of this.schema.columns){
17
- if (col.required && data[col.name] === undefined) {
18
- throw new ValidationError(col.name);
19
- }
20
- }
21
- // validasi unique
22
- for (const col of this.schema.columns){
23
- if (col.unique && data[col.name] !== undefined) {
24
- const value = data[col.name];
25
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ?`);
26
- const existing = stmt.get(value);
27
- if (existing) {
28
- throw new DuplicateError(col.name, value);
29
- }
30
- }
31
- }
32
- const keys = Object.keys(data);
33
- const placeholders = keys.map(()=>"?").join(", ");
34
- const stmt = this.db.prepare(`INSERT INTO ${this.schema.name} (${keys.join(",")}) VALUES (${placeholders})`);
35
- stmt.run(...keys.map((k)=>data[k]));
36
- }
37
- // ✅ update dengan validasi unique
38
- async update(id, data) {
39
- for (const col of this.schema.columns){
40
- if (col.unique && data[col.name] !== undefined) {
41
- const value = data[col.name];
42
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ? AND id != ?`);
43
- const existing = stmt.get(value, id);
44
- if (existing) {
45
- throw new DuplicateError(col.name, value);
46
- }
47
- }
48
- }
49
- const keys = Object.keys(data);
50
- const setClause = keys.map((k)=>`${String(k)} = ?`).join(", ");
51
- const stmt = this.db.prepare(`UPDATE ${this.schema.name} SET ${setClause} WHERE id = ?`);
52
- stmt.run(...keys.map((k)=>data[k]), id);
53
- }
54
- async findAll() {
55
- return this.db.prepare(`SELECT * FROM ${this.schema.name}`).all();
56
- }
57
- async findUnique(column, value) {
58
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
59
- return stmt.get(value);
60
- }
61
- async findMany(column, value) {
62
- const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
63
- return stmt.all(value);
64
- }
65
- constructor(db, schema){
66
- this.db = db;
67
- this.schema = schema;
68
- this.createTable(); // ✅ otomatis buat table saat instance dibuat
69
- }
70
- }
71
-
72
- export { NoxiumSqlite };