Skip to content

Commit fcd75e2

Browse files
v0.6.2
1 parent 52d4fb8 commit fcd75e2

7 files changed

Lines changed: 78 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src="https://static.wikia.nocookie.net/arnelify/images/c/c8/Arnelify-logo-2024.png/revision/latest?cb=20240701012515" style="width:336px;" alt="Arnelify Logo" />
22

3-
![Arnelify ORM for NodeJS](https://img.shields.io/badge/Arnelify%20ORM%20for%20NodeJS-0.6.1-yellow) ![C++](https://img.shields.io/badge/C++-2b-red) ![G++](https://img.shields.io/badge/G++-14.2.0-blue) ![NodeJS](https://img.shields.io/badge/NodeJS-22.13.1-green) ![Bun](https://img.shields.io/badge/Bun-1.2.0-green)
3+
![Arnelify ORM for NodeJS](https://img.shields.io/badge/Arnelify%20ORM%20for%20NodeJS-0.6.2-yellow) ![C++](https://img.shields.io/badge/C++-2b-red) ![G++](https://img.shields.io/badge/G++-14.2.0-blue) ![NodeJS](https://img.shields.io/badge/NodeJS-22.13.1-green) ![Bun](https://img.shields.io/badge/Bun-1.2.0-green)
44

55
## 🚀 About
66
**Arnelify® ORM for NodeJS** - is a minimalistic NodeJS (Bun) addon which is an ORM written in C and C++.
@@ -53,7 +53,7 @@ This software is licensed under the <a href="https://github.com/arnelify/arnelif
5353
Join us to help improve this software, fix bugs or implement new functionality. Active participation will help keep the software up-to-date, reliable, and aligned with the needs of its users.
5454

5555
## ⭐ Release Notes
56-
Version 0.6.1 - NodeJS (Bun) Addon
56+
Version 0.6.2 - NodeJS (Bun) Addon
5757

5858
We are excited to introduce the Arnelify ORM for NodeJS (Bun) addon! Please note that this version is raw and still in active development.
5959

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arnelify-orm",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"description": "Minimalistic NodeJS (Bun) addon which is an ORM written in C and C++.",
55
"keywords": [
66
"arnelify",

src/cpp/addon.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,16 @@ Napi::Value orm_exec(const Napi::CallbackInfo& info) {
140140
return Napi::String::New(env, out);
141141
}
142142

143+
Napi::Value orm_get_uuid(const Napi::CallbackInfo& info) {
144+
Napi::Env env = info.Env();
145+
return Napi::String::New(env, orm->getUuId());
146+
}
147+
143148
Napi::Object Init(Napi::Env env, Napi::Object exports) {
144149
exports.Set("orm_create", Napi::Function::New(env, orm_create));
145150
exports.Set("orm_destroy", Napi::Function::New(env, orm_destroy));
146151
exports.Set("orm_exec", Napi::Function::New(env, orm_exec));
152+
exports.Set("orm_get_uuid", Napi::Function::New(env, orm_get_uuid));
147153
return exports;
148154
}
149155

src/cpp/ffi.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ const char* orm_exec(const char* cQuery, const char* cSerialized) {
107107
void orm_free(const char* cPtr) {
108108
if (cPtr) delete[] cPtr;
109109
}
110+
111+
const char* orm_get_uuid() {
112+
const std::string uuid = orm->getUuId();
113+
char* cUuId = new char[uuid.length() + 1];
114+
std::strcpy(cUuId, uuid.c_str());
115+
return cUuId;
116+
}
110117
}
111118

112119
#endif

src/cpp/index.cpp

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include <functional>
2+
#include <future>
23
#include <iostream>
34
#include <map>
45
#include <vector>
56

67
#include <iostream>
8+
#include <iomanip>
9+
#include <random>
710
#include <sstream>
811
#include <string>
912
#include <optional>
@@ -161,14 +164,16 @@ class ArnelifyORM {
161164
} else if (std::holds_alternative<bool>(default_)) {
162165
const bool value = std::get<bool>(default_);
163166
query += " " + std::string(value ? "DEFAULT NULL" : "NOT NULL");
167+
} else if (std::holds_alternative<double>(default_)) {
168+
query += " NOT NULL DEFAULT " + std::to_string(std::get<double>(default_));
164169
} else if (std::holds_alternative<int>(default_)) {
165-
query += std::to_string(std::get<int>(default_));
170+
query += " NOT NULL DEFAULT " + std::to_string(std::get<int>(default_));
166171
} else if (std::holds_alternative<std::string>(default_)) {
167172
const std::string value = std::get<std::string>(default_);
168173
if (value == "CURRENT_TIMESTAMP") {
169-
query += " DEFAULT CURRENT_TIMESTAMP";
174+
query += " NOT NULL DEFAULT CURRENT_TIMESTAMP";
170175
} else {
171-
query += "'" + std::get<std::string>(default_) + "'";
176+
query += " NOT NULL DEFAULT '" + value + "'";
172177
}
173178
}
174179

@@ -278,6 +283,37 @@ class ArnelifyORM {
278283
return res;
279284
}
280285

286+
const std::string getUuId() {
287+
std::random_device rd;
288+
std::mt19937 gen(rd());
289+
std::uniform_int_distribution<> dis(10000, 19999);
290+
int random = dis(gen);
291+
const auto now = std::chrono::system_clock::now();
292+
const auto milliseconds =
293+
std::chrono::duration_cast<std::chrono::milliseconds>(
294+
now.time_since_epoch())
295+
.count();
296+
297+
const std::string code =
298+
std::to_string(milliseconds) + std::to_string(random);
299+
std::hash<std::string> hasher;
300+
size_t v1 = hasher(code);
301+
size_t v2 = hasher(std::to_string(v1));
302+
unsigned char hash[16];
303+
for (int i = 0; i < 8; ++i) {
304+
hash[i] = (v1 >> (i * 8)) & 0xFF;
305+
hash[i + 8] = (v2 >> (i * 8)) & 0xFF;
306+
}
307+
308+
std::stringstream ss;
309+
for (int i = 0; i < 16; ++i) {
310+
ss << std::hex << std::setw(2) << std::setfill('0')
311+
<< static_cast<int>(hash[i]);
312+
}
313+
314+
return ss.str();
315+
}
316+
281317
ArnelifyORM* groupBy(const std::vector<std::string>& args) {
282318
this->query += " GROUP BY ";
283319
for (size_t i = 0; args.size() > i; i++) {
@@ -554,14 +590,15 @@ class ArnelifyORM {
554590
void reference(const std::string& column, const std::string& tableName,
555591
const std::string& foreign,
556592
const std::vector<std::string> args) {
557-
std::string query = "CONSTRAINT fk_" + tableName + " FOREIGN KEY (" +
558-
column + ") REFERENCES " + tableName + "(" + foreign +
559-
")";
593+
std::string query = "CONSTRAINT fk_" + tableName + "_" + this->getUuId() +
594+
" FOREIGN KEY (" + column + ") REFERENCES " +
595+
tableName + "(" + foreign + ")";
560596

561597
const bool isAlter = this->query.starts_with("ALTER");
562598
if (isAlter) {
563-
query = "ADD CONSTRAINT fk_" + tableName + " FOREIGN KEY (" + column +
564-
") REFERENCES " + tableName + "(" + foreign + ")";
599+
query = "ADD CONSTRAINT fk_" + tableName + "_" + this->getUuId() +
600+
" FOREIGN KEY (" + column + ") REFERENCES " + tableName + "(" +
601+
foreign + ")";
565602
}
566603

567604
for (size_t i = 0; args.size() > i; i++) {

src/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ class ArnelifyORM {
136136
} else if (typeof default_ === 'boolean') {
137137
query += ` ${default_ ? 'DEFAULT NULL' : 'NOT NULL'}`;
138138
} else if (typeof default_ === 'number') {
139-
query += `${default_}`;
139+
query += ` NOT NULL DEFAULT ${default_}`;
140140
} else if (typeof default_ === 'string') {
141141
if (default_ === 'CURRENT_TIMESTAMP') {
142-
query += ' DEFAULT CURRENT_TIMESTAMP';
142+
query += ' NOT NULL DEFAULT CURRENT_TIMESTAMP';
143143
} else {
144-
query += `'${default_}'`;
144+
query += ` NOT NULL DEFAULT '${default_}'`;
145145
}
146146
}
147147

@@ -290,6 +290,14 @@ class ArnelifyORM {
290290
return res;
291291
}
292292

293+
/**
294+
* getUuid
295+
* @returns
296+
*/
297+
getUuid(): string {
298+
return this.#lib.orm_get_uuid();
299+
}
300+
293301
/**
294302
* Group By
295303
* @param {string} args
@@ -631,12 +639,12 @@ class ArnelifyORM {
631639
* @param {Array} args
632640
*/
633641
reference(column: string, tableName: string, foreign: string, args: string[] = []): void {
634-
let query: string = `CONSTRAINT fk_${tableName} FOREIGN KEY (${column}) `
642+
let query: string = `CONSTRAINT fk_${tableName}_${this.getUuid()} FOREIGN KEY (${column}) `
635643
+ `REFERENCES ${tableName}(${foreign})`;
636644

637645
const isAlter: boolean = this.#query.startsWith('ALTER');
638646
if (isAlter) {
639-
query = `ADD CONSTRAINT fk_${tableName} FOREIGN KEY (${column}) `
647+
query = `ADD CONSTRAINT fk_${tableName}_${this.getUuid()} FOREIGN KEY (${column}) `
640648
+ `REFERENCES ${tableName}(${foreign})`;
641649
}
642650

@@ -775,4 +783,5 @@ class ArnelifyORM {
775783
}
776784
}
777785

778-
export default ArnelifyORM;
786+
export type { ArnelifyORMRes };
787+
export { ArnelifyORM };

src/tests/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env bun
22

3-
import ArnelifyORMRes from "contracts/res";
4-
import ArnelifyORM from "../index";
3+
import { ArnelifyORM, ArnelifyORMRes } from "../index";
54

65
(function main(): number {
76

@@ -13,7 +12,7 @@ import ArnelifyORM from "../index";
1312
"ORM_PASS": "pass",
1413
"ORM_PORT": 3306
1514
});
16-
15+
1716
let res: ArnelifyORMRes = {};
1817

1918
db.dropTable("users");

0 commit comments

Comments
 (0)