noxium 1.0.0
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 +674 -0
- package/README.md +27 -0
- package/dist/cjs/createClient.cjs +55 -0
- package/dist/cjs/createClient.cjs.map +1 -0
- package/dist/cjs/drivers/mongo.cjs +63 -0
- package/dist/cjs/drivers/mongo.cjs.map +1 -0
- package/dist/cjs/drivers/sqlite.cjs +77 -0
- package/dist/cjs/drivers/sqlite.cjs.map +1 -0
- package/dist/cjs/error.cjs +50 -0
- package/dist/cjs/error.cjs.map +1 -0
- package/dist/cjs/noxa.cjs +41 -0
- package/dist/cjs/noxa.cjs.map +1 -0
- package/dist/cjs/schema.cjs +3 -0
- package/dist/cjs/schema.cjs.map +1 -0
- package/dist/createClient.d.ts +6 -0
- package/dist/drivers/mongo.d.ts +11 -0
- package/dist/drivers/sqlite.d.ts +12 -0
- package/dist/error.d.ts +15 -0
- package/dist/esm/createClient.js +53 -0
- package/dist/esm/createClient.js.map +1 -0
- package/dist/esm/drivers/mongo.js +61 -0
- package/dist/esm/drivers/mongo.js.map +1 -0
- package/dist/esm/drivers/sqlite.js +75 -0
- package/dist/esm/drivers/sqlite.js.map +1 -0
- package/dist/esm/error.js +45 -0
- package/dist/esm/error.js.map +1 -0
- package/dist/esm/noxa.js +39 -0
- package/dist/esm/noxa.js.map +1 -0
- package/dist/esm/schema.js +2 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/noxa.d.ts +12 -0
- package/dist/schema.d.ts +38 -0
- package/mydb.sqlite +0 -0
- package/package.json +75 -0
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Library For Noxium
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
6
|
+
[npm registry](https://www.npmjs.com/).
|
|
7
|
+
|
|
8
|
+
Before installing, [download and install Node.js](https://nodejs.org/en/download/).
|
|
9
|
+
Node.js 0.10 or higher is required.
|
|
10
|
+
|
|
11
|
+
If this is a brand new project, make sure to create a `package.json` first with
|
|
12
|
+
the [`npm init` command](https://docs.npmjs.com/creating-a-package-json-file).
|
|
13
|
+
|
|
14
|
+
Installation is done using the
|
|
15
|
+
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
16
|
+
|
|
17
|
+
```console
|
|
18
|
+
$ npm install noxium
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- This is for me to learn javascript
|
|
24
|
+
|
|
25
|
+
## License
|
|
26
|
+
|
|
27
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Database = require('better-sqlite3');
|
|
4
|
+
var drivers_sqlite = require('./drivers/sqlite.cjs');
|
|
5
|
+
var drivers_mongo = require('./drivers/mongo.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 drivers_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 drivers_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;
|
|
55
|
+
//# sourceMappingURL=createClient.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createClient.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var mongoose = require('mongoose');
|
|
4
|
+
var error = require('../error.cjs');
|
|
5
|
+
|
|
6
|
+
class NoxiumMongo {
|
|
7
|
+
model;
|
|
8
|
+
constructor(schema, config){
|
|
9
|
+
mongoose.connect(config.mongo.uri, {
|
|
10
|
+
dbName: config.mongo.dbName ?? "test"
|
|
11
|
+
});
|
|
12
|
+
// generate definisi schema mongoose
|
|
13
|
+
const mongooseSchemaDef = {};
|
|
14
|
+
for (const col of schema.columns){
|
|
15
|
+
const def = {};
|
|
16
|
+
def.type = col.type === "int" ? Number : String;
|
|
17
|
+
if (col.required) def.required = true;
|
|
18
|
+
if (col.unique) def.unique = true;
|
|
19
|
+
mongooseSchemaDef[col.name] = def;
|
|
20
|
+
}
|
|
21
|
+
const mongooseSchema = new mongoose.Schema(mongooseSchemaDef);
|
|
22
|
+
this.model = mongoose.model(schema.name, mongooseSchema);
|
|
23
|
+
}
|
|
24
|
+
async insert(data) {
|
|
25
|
+
try {
|
|
26
|
+
const doc = new this.model(data);
|
|
27
|
+
await doc.save();
|
|
28
|
+
} catch (err) {
|
|
29
|
+
if (err.code === 11000) {
|
|
30
|
+
const field = Object.keys(err.keyValue)[0];
|
|
31
|
+
throw new error.DuplicateError(field, err.keyValue[field]);
|
|
32
|
+
}
|
|
33
|
+
if (err.errors) {
|
|
34
|
+
const field = Object.keys(err.errors)[0];
|
|
35
|
+
throw new error.ValidationError(field);
|
|
36
|
+
}
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async update(id, data) {
|
|
41
|
+
await this.model.updateOne({
|
|
42
|
+
_id: id
|
|
43
|
+
}, {
|
|
44
|
+
$set: data
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async findAll() {
|
|
48
|
+
return this.model.find().exec();
|
|
49
|
+
}
|
|
50
|
+
async findUnique(column, value) {
|
|
51
|
+
return this.model.findOne({
|
|
52
|
+
[column]: value
|
|
53
|
+
}).exec();
|
|
54
|
+
}
|
|
55
|
+
async findMany(column, value) {
|
|
56
|
+
return this.model.find({
|
|
57
|
+
[column]: value
|
|
58
|
+
}).exec();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
exports.NoxiumMongo = NoxiumMongo;
|
|
63
|
+
//# sourceMappingURL=mongo.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var error = require('../error.cjs');
|
|
4
|
+
|
|
5
|
+
class NoxiumSqlite {
|
|
6
|
+
db;
|
|
7
|
+
schema;
|
|
8
|
+
constructor(db, schema){
|
|
9
|
+
this.db = db;
|
|
10
|
+
this.schema = schema;
|
|
11
|
+
this.createTable(); // ✅ otomatis buat table saat instance dibuat
|
|
12
|
+
}
|
|
13
|
+
createTable() {
|
|
14
|
+
const cols = this.schema.columns.map((c)=>{
|
|
15
|
+
let def = `${c.name} ${c.type.toUpperCase()}`;
|
|
16
|
+
if (c.unique) def += " UNIQUE";
|
|
17
|
+
if (c.required) def += " NOT NULL";
|
|
18
|
+
return def;
|
|
19
|
+
}).join(", ");
|
|
20
|
+
this.db.prepare(`CREATE TABLE IF NOT EXISTS ${this.schema.name} (${cols})`).run();
|
|
21
|
+
}
|
|
22
|
+
// ✅ insert dengan validasi unique
|
|
23
|
+
async insert(data) {
|
|
24
|
+
// ✅ cek required
|
|
25
|
+
for (const col of this.schema.columns){
|
|
26
|
+
if (col.required && data[col.name] === undefined) {
|
|
27
|
+
throw new error.ValidationError(col.name);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// validasi unique
|
|
31
|
+
for (const col of this.schema.columns){
|
|
32
|
+
if (col.unique && data[col.name] !== undefined) {
|
|
33
|
+
const value = data[col.name];
|
|
34
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ?`);
|
|
35
|
+
const existing = stmt.get(value);
|
|
36
|
+
if (existing) {
|
|
37
|
+
throw new error.DuplicateError(col.name, value);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const keys = Object.keys(data);
|
|
42
|
+
const placeholders = keys.map(()=>"?").join(", ");
|
|
43
|
+
const stmt = this.db.prepare(`INSERT INTO ${this.schema.name} (${keys.join(",")}) VALUES (${placeholders})`);
|
|
44
|
+
stmt.run(...keys.map((k)=>data[k]));
|
|
45
|
+
}
|
|
46
|
+
// ✅ update dengan validasi unique
|
|
47
|
+
async update(id, data) {
|
|
48
|
+
for (const col of this.schema.columns){
|
|
49
|
+
if (col.unique && data[col.name] !== undefined) {
|
|
50
|
+
const value = data[col.name];
|
|
51
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ? AND id != ?`);
|
|
52
|
+
const existing = stmt.get(value, id);
|
|
53
|
+
if (existing) {
|
|
54
|
+
throw new error.DuplicateError(col.name, value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const keys = Object.keys(data);
|
|
59
|
+
const setClause = keys.map((k)=>`${String(k)} = ?`).join(", ");
|
|
60
|
+
const stmt = this.db.prepare(`UPDATE ${this.schema.name} SET ${setClause} WHERE id = ?`);
|
|
61
|
+
stmt.run(...keys.map((k)=>data[k]), id);
|
|
62
|
+
}
|
|
63
|
+
async findAll() {
|
|
64
|
+
return this.db.prepare(`SELECT * FROM ${this.schema.name}`).all();
|
|
65
|
+
}
|
|
66
|
+
async findUnique(column, value) {
|
|
67
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
|
|
68
|
+
return stmt.get(value);
|
|
69
|
+
}
|
|
70
|
+
async findMany(column, value) {
|
|
71
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
|
|
72
|
+
return stmt.all(value);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
exports.NoxiumSqlite = NoxiumSqlite;
|
|
77
|
+
//# sourceMappingURL=sqlite.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// errors.ts
|
|
4
|
+
class DuplicateError extends Error {
|
|
5
|
+
column;
|
|
6
|
+
value;
|
|
7
|
+
constructor(column, value){
|
|
8
|
+
super(`Duplicate unique value: ${column} = ${value}`);
|
|
9
|
+
this.name = "DuplicateError";
|
|
10
|
+
this.column = column;
|
|
11
|
+
this.value = value;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
class ValidationError extends Error {
|
|
15
|
+
column;
|
|
16
|
+
constructor(column){
|
|
17
|
+
super(`Missing required field: ${column}`);
|
|
18
|
+
this.name = "ValidationError";
|
|
19
|
+
this.column = column;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
class RequiredConfigError extends Error {
|
|
23
|
+
constructor(){
|
|
24
|
+
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
|
|
25
|
+
export default function getConfig() {
|
|
26
|
+
return {
|
|
27
|
+
database: {
|
|
28
|
+
type: "sqlite", // default minimal pakai SQLite
|
|
29
|
+
sqlite: {
|
|
30
|
+
path: "./data.db", // file database lokal
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
this.name = "RequiredConfigError";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
class InvalidConfigError extends Error {
|
|
40
|
+
constructor(message){
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = "InvalidConfigError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
exports.DuplicateError = DuplicateError;
|
|
47
|
+
exports.InvalidConfigError = InvalidConfigError;
|
|
48
|
+
exports.RequiredConfigError = RequiredConfigError;
|
|
49
|
+
exports.ValidationError = ValidationError;
|
|
50
|
+
//# sourceMappingURL=error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var fs = require('fs');
|
|
5
|
+
var error = require('./error.cjs');
|
|
6
|
+
var createClient = require('./createClient.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
|
+
orm;
|
|
16
|
+
constructor(schema){
|
|
17
|
+
this.orm = createClient.createClient(config, schema);
|
|
18
|
+
}
|
|
19
|
+
async insert(data) {
|
|
20
|
+
return await this.orm.insert(data);
|
|
21
|
+
}
|
|
22
|
+
async update(id, data) {
|
|
23
|
+
return await this.orm.update(id, data);
|
|
24
|
+
}
|
|
25
|
+
async findAll() {
|
|
26
|
+
return await this.orm.findAll();
|
|
27
|
+
}
|
|
28
|
+
async findUnique(data) {
|
|
29
|
+
const column = Object.keys(data)[0];
|
|
30
|
+
const value = data[column];
|
|
31
|
+
return await this.orm.findUnique(column, value);
|
|
32
|
+
}
|
|
33
|
+
async findMany(data) {
|
|
34
|
+
const column = Object.keys(data)[0];
|
|
35
|
+
const value = data[column];
|
|
36
|
+
return await this.orm.findMany(column, value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.Noxium = Noxium;
|
|
41
|
+
//# sourceMappingURL=noxa.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noxa.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { NoxiumClient, SchemaToType, TableSchema } from "./schema";
|
|
2
|
+
import { NoxiumConfig } from "./config";
|
|
3
|
+
/**
|
|
4
|
+
* Validasi config dan kembalikan driver ORM sesuai type database
|
|
5
|
+
*/
|
|
6
|
+
export declare function createClient<Schema extends TableSchema>(config: NoxiumConfig, schema?: Schema): NoxiumClient<SchemaToType<Schema>>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TableSchema, SchemaToType } from "../schema";
|
|
2
|
+
import { MongoConfig } from "../config";
|
|
3
|
+
export declare class NoxiumMongo<Schema extends TableSchema> {
|
|
4
|
+
private model;
|
|
5
|
+
constructor(schema: Schema, config: MongoConfig);
|
|
6
|
+
insert(data: any): Promise<void>;
|
|
7
|
+
update(id: any, data: any): Promise<void>;
|
|
8
|
+
findAll(): Promise<any[]>;
|
|
9
|
+
findUnique<K extends keyof SchemaToType<Schema>>(column: K, value: SchemaToType<Schema>[K]): Promise<any>;
|
|
10
|
+
findMany<K extends keyof SchemaToType<Schema>>(column: K, value: SchemaToType<Schema>[K]): Promise<any[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NoxiumClient, SchemaToType, TableSchema } from "../schema";
|
|
2
|
+
export declare class NoxiumSqlite<Schema extends TableSchema> implements NoxiumClient<SchemaToType<Schema>> {
|
|
3
|
+
private db;
|
|
4
|
+
private schema;
|
|
5
|
+
constructor(db: any, schema: Schema);
|
|
6
|
+
private createTable;
|
|
7
|
+
insert(data: SchemaToType<Schema>): Promise<void>;
|
|
8
|
+
update(id: number, data: Partial<SchemaToType<Schema>>): Promise<void>;
|
|
9
|
+
findAll(): Promise<any>;
|
|
10
|
+
findUnique<K extends keyof SchemaToType<Schema>>(column: K, value: SchemaToType<Schema>[K]): Promise<any>;
|
|
11
|
+
findMany<K extends keyof SchemaToType<Schema>>(column: K, value: SchemaToType<Schema>[K]): Promise<any>;
|
|
12
|
+
}
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class DuplicateError extends Error {
|
|
2
|
+
column: string;
|
|
3
|
+
value: any;
|
|
4
|
+
constructor(column: string, value: any);
|
|
5
|
+
}
|
|
6
|
+
export declare class ValidationError extends Error {
|
|
7
|
+
column: string;
|
|
8
|
+
constructor(column: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class RequiredConfigError extends Error {
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
13
|
+
export declare class InvalidConfigError extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
import { NoxiumSqlite } from './drivers/sqlite.js';
|
|
3
|
+
import { NoxiumMongo } from './drivers/mongo.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 };
|
|
53
|
+
//# sourceMappingURL=createClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createClient.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import mongoose, { Schema } from 'mongoose';
|
|
2
|
+
import { DuplicateError, ValidationError } from '../error.js';
|
|
3
|
+
|
|
4
|
+
class NoxiumMongo {
|
|
5
|
+
model;
|
|
6
|
+
constructor(schema, config){
|
|
7
|
+
mongoose.connect(config.mongo.uri, {
|
|
8
|
+
dbName: config.mongo.dbName ?? "test"
|
|
9
|
+
});
|
|
10
|
+
// generate definisi schema mongoose
|
|
11
|
+
const mongooseSchemaDef = {};
|
|
12
|
+
for (const col of schema.columns){
|
|
13
|
+
const def = {};
|
|
14
|
+
def.type = col.type === "int" ? Number : String;
|
|
15
|
+
if (col.required) def.required = true;
|
|
16
|
+
if (col.unique) def.unique = true;
|
|
17
|
+
mongooseSchemaDef[col.name] = def;
|
|
18
|
+
}
|
|
19
|
+
const mongooseSchema = new Schema(mongooseSchemaDef);
|
|
20
|
+
this.model = mongoose.model(schema.name, mongooseSchema);
|
|
21
|
+
}
|
|
22
|
+
async insert(data) {
|
|
23
|
+
try {
|
|
24
|
+
const doc = new this.model(data);
|
|
25
|
+
await doc.save();
|
|
26
|
+
} catch (err) {
|
|
27
|
+
if (err.code === 11000) {
|
|
28
|
+
const field = Object.keys(err.keyValue)[0];
|
|
29
|
+
throw new DuplicateError(field, err.keyValue[field]);
|
|
30
|
+
}
|
|
31
|
+
if (err.errors) {
|
|
32
|
+
const field = Object.keys(err.errors)[0];
|
|
33
|
+
throw new ValidationError(field);
|
|
34
|
+
}
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async update(id, data) {
|
|
39
|
+
await this.model.updateOne({
|
|
40
|
+
_id: id
|
|
41
|
+
}, {
|
|
42
|
+
$set: data
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async findAll() {
|
|
46
|
+
return this.model.find().exec();
|
|
47
|
+
}
|
|
48
|
+
async findUnique(column, value) {
|
|
49
|
+
return this.model.findOne({
|
|
50
|
+
[column]: value
|
|
51
|
+
}).exec();
|
|
52
|
+
}
|
|
53
|
+
async findMany(column, value) {
|
|
54
|
+
return this.model.find({
|
|
55
|
+
[column]: value
|
|
56
|
+
}).exec();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { NoxiumMongo };
|
|
61
|
+
//# sourceMappingURL=mongo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ValidationError, DuplicateError } from '../error.js';
|
|
2
|
+
|
|
3
|
+
class NoxiumSqlite {
|
|
4
|
+
db;
|
|
5
|
+
schema;
|
|
6
|
+
constructor(db, schema){
|
|
7
|
+
this.db = db;
|
|
8
|
+
this.schema = schema;
|
|
9
|
+
this.createTable(); // ✅ otomatis buat table saat instance dibuat
|
|
10
|
+
}
|
|
11
|
+
createTable() {
|
|
12
|
+
const cols = this.schema.columns.map((c)=>{
|
|
13
|
+
let def = `${c.name} ${c.type.toUpperCase()}`;
|
|
14
|
+
if (c.unique) def += " UNIQUE";
|
|
15
|
+
if (c.required) def += " NOT NULL";
|
|
16
|
+
return def;
|
|
17
|
+
}).join(", ");
|
|
18
|
+
this.db.prepare(`CREATE TABLE IF NOT EXISTS ${this.schema.name} (${cols})`).run();
|
|
19
|
+
}
|
|
20
|
+
// ✅ insert dengan validasi unique
|
|
21
|
+
async insert(data) {
|
|
22
|
+
// ✅ cek required
|
|
23
|
+
for (const col of this.schema.columns){
|
|
24
|
+
if (col.required && data[col.name] === undefined) {
|
|
25
|
+
throw new ValidationError(col.name);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// validasi unique
|
|
29
|
+
for (const col of this.schema.columns){
|
|
30
|
+
if (col.unique && data[col.name] !== undefined) {
|
|
31
|
+
const value = data[col.name];
|
|
32
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ?`);
|
|
33
|
+
const existing = stmt.get(value);
|
|
34
|
+
if (existing) {
|
|
35
|
+
throw new DuplicateError(col.name, value);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const keys = Object.keys(data);
|
|
40
|
+
const placeholders = keys.map(()=>"?").join(", ");
|
|
41
|
+
const stmt = this.db.prepare(`INSERT INTO ${this.schema.name} (${keys.join(",")}) VALUES (${placeholders})`);
|
|
42
|
+
stmt.run(...keys.map((k)=>data[k]));
|
|
43
|
+
}
|
|
44
|
+
// ✅ update dengan validasi unique
|
|
45
|
+
async update(id, data) {
|
|
46
|
+
for (const col of this.schema.columns){
|
|
47
|
+
if (col.unique && data[col.name] !== undefined) {
|
|
48
|
+
const value = data[col.name];
|
|
49
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${col.name} = ? AND id != ?`);
|
|
50
|
+
const existing = stmt.get(value, id);
|
|
51
|
+
if (existing) {
|
|
52
|
+
throw new DuplicateError(col.name, value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const keys = Object.keys(data);
|
|
57
|
+
const setClause = keys.map((k)=>`${String(k)} = ?`).join(", ");
|
|
58
|
+
const stmt = this.db.prepare(`UPDATE ${this.schema.name} SET ${setClause} WHERE id = ?`);
|
|
59
|
+
stmt.run(...keys.map((k)=>data[k]), id);
|
|
60
|
+
}
|
|
61
|
+
async findAll() {
|
|
62
|
+
return this.db.prepare(`SELECT * FROM ${this.schema.name}`).all();
|
|
63
|
+
}
|
|
64
|
+
async findUnique(column, value) {
|
|
65
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
|
|
66
|
+
return stmt.get(value);
|
|
67
|
+
}
|
|
68
|
+
async findMany(column, value) {
|
|
69
|
+
const stmt = this.db.prepare(`SELECT * FROM ${this.schema.name} WHERE ${column} = ?`);
|
|
70
|
+
return stmt.all(value);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { NoxiumSqlite };
|
|
75
|
+
//# sourceMappingURL=sqlite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// errors.ts
|
|
2
|
+
class DuplicateError extends Error {
|
|
3
|
+
column;
|
|
4
|
+
value;
|
|
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
|
+
column;
|
|
14
|
+
constructor(column){
|
|
15
|
+
super(`Missing required field: ${column}`);
|
|
16
|
+
this.name = "ValidationError";
|
|
17
|
+
this.column = column;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class RequiredConfigError extends Error {
|
|
21
|
+
constructor(){
|
|
22
|
+
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
|
|
23
|
+
export default function getConfig() {
|
|
24
|
+
return {
|
|
25
|
+
database: {
|
|
26
|
+
type: "sqlite", // default minimal pakai SQLite
|
|
27
|
+
sqlite: {
|
|
28
|
+
path: "./data.db", // file database lokal
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
`);
|
|
34
|
+
this.name = "RequiredConfigError";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class InvalidConfigError extends Error {
|
|
38
|
+
constructor(message){
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = "InvalidConfigError";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { DuplicateError, InvalidConfigError, RequiredConfigError, ValidationError };
|
|
45
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|