vasuki-db 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.
Files changed (3) hide show
  1. package/index.js +128 -0
  2. package/package.json +12 -0
  3. package/test.js +44 -0
package/index.js ADDED
@@ -0,0 +1,128 @@
1
+ /* * Vasuki iTech - Ultimate Cloud DB SDK
2
+ * CEO: Adamya Khairwal
3
+ */
4
+
5
+ class Vasuki {
6
+ constructor() {
7
+ this.apiBase = "https://adamyakhairwal2011-vasuki-cloud.hf.space";
8
+ this.adminEmail = "codeclutch.adamyarao@gmail.com";
9
+ this.userEmail = null;
10
+ this.isAuthed = false;
11
+ }
12
+
13
+ // ================= AUTH =================
14
+ async login(email, password) {
15
+ try {
16
+ const res = await fetch(`${this.apiBase}/get-file-content`, {
17
+ method: 'POST',
18
+ headers: { 'Content-Type': 'application/json' },
19
+ body: JSON.stringify({ email: this.adminEmail, path: "users_rows.json" })
20
+ });
21
+ const data = await res.json();
22
+ const jsonData = JSON.parse(data.content);
23
+ const users = jsonData.Sheet1 || [];
24
+ const foundUser = users.find(u => u.username === email && u.password === password);
25
+
26
+ if (foundUser) {
27
+ this.userEmail = email;
28
+ this.isAuthed = true;
29
+ return { success: true, user: foundUser };
30
+ } else {
31
+ throw new Error("Invalid credentials");
32
+ }
33
+ } catch (err) { throw new Error("Auth Failed: " + err.message); }
34
+ }
35
+
36
+ // ================= INTERNAL HELPER =================
37
+ async _req(endpoint, body, isFormData = false) {
38
+ if (!this.isAuthed) throw "Bhai pehle login() kar!";
39
+ const options = {
40
+ method: 'POST',
41
+ body: isFormData ? body : JSON.stringify({ email: this.userEmail, ...body })
42
+ };
43
+ if (!isFormData) options.headers = { 'Content-Type': 'application/json' };
44
+ const res = await fetch(`${this.apiBase}${endpoint}`, options);
45
+ return await res.json();
46
+ }
47
+
48
+ // ================= FILE OPERATIONS =================
49
+ async getDB(dbName) {
50
+ const path = dbName.endsWith('.json') ? dbName : `${dbName}.json`;
51
+ const res = await this._req('/get-file-content', { path });
52
+ return JSON.parse(res.content);
53
+ }
54
+
55
+ async saveDB(dbName, data) {
56
+ const path = dbName.endsWith('.json') ? dbName : `${dbName}.json`;
57
+ return await this._req('/save-file-content', { path, content: JSON.stringify(data, null, 2) });
58
+ }
59
+
60
+ async listFiles(folder = "") { return await this._req('/list-files', { folder }); }
61
+
62
+ async deleteFile(path) { return await this._req('/delete-file', { path }); }
63
+
64
+ // ================= ROW OPERATIONS (CRUD) =================
65
+
66
+ // FETCH: Poora table nikalne ke liye
67
+ async fetchTable(dbName, tableName) {
68
+ const db = await this.getDB(dbName);
69
+ return db[tableName] || [];
70
+ }
71
+
72
+ // INSERT ROW
73
+ async insertRow(dbName, tableName, rowData) {
74
+ const db = await this.getDB(dbName);
75
+ if (!db[tableName]) db[tableName] = [];
76
+ db[tableName].push({ id: crypto.randomUUID(), created_at: new Date().toISOString(), ...rowData });
77
+ return await this.saveDB(dbName, db);
78
+ }
79
+
80
+ // UPDATE/EDIT ROW: Index ya ID se update karega
81
+ async updateRow(dbName, tableName, identifier, newData) {
82
+ const db = await this.getDB(dbName);
83
+ const index = typeof identifier === 'number' ? identifier : db[tableName].findIndex(r => r.id === identifier);
84
+ if (index === -1) throw "Row nahi mili!";
85
+ db[tableName][index] = { ...db[tableName][index], ...newData };
86
+ return await this.saveDB(dbName, db);
87
+ }
88
+
89
+ // DELETE ROW
90
+ async deleteRow(dbName, tableName, identifier) {
91
+ const db = await this.getDB(dbName);
92
+ const index = typeof identifier === 'number' ? identifier : db[tableName].findIndex(r => r.id === identifier);
93
+ if (index !== -1) {
94
+ db[tableName].splice(index, 1);
95
+ return await this.saveDB(dbName, db);
96
+ }
97
+ throw "Delete failed: Row not found";
98
+ }
99
+
100
+ // ================= COLUMN OPERATIONS =================
101
+
102
+ // INSERT COLUMN: Pura schema aur saari rows mein naya column jod dega
103
+ async insertColumn(dbName, tableName, columnName, defaultValue = "") {
104
+ const db = await this.getDB(dbName);
105
+ // Update Schema
106
+ if (db._vasuki_schema && db._vasuki_schema[tableName]) {
107
+ db._vasuki_schema[tableName][columnName] = typeof defaultValue;
108
+ }
109
+ // Update all existing rows
110
+ db[tableName] = db[tableName].map(row => ({ ...row, [columnName]: defaultValue }));
111
+ return await this.saveDB(dbName, db);
112
+ }
113
+
114
+ // DELETE COLUMN: Column ko schema aur data dono se uda dega
115
+ async deleteColumn(dbName, tableName, columnName) {
116
+ const db = await this.getDB(dbName);
117
+ if (db._vasuki_schema && db._vasuki_schema[tableName]) {
118
+ delete db._vasuki_schema[tableName][columnName];
119
+ }
120
+ db[tableName] = db[tableName].map(row => {
121
+ delete row[columnName];
122
+ return row;
123
+ });
124
+ return await this.saveDB(dbName, db);
125
+ }
126
+ }
127
+
128
+ export default Vasuki;
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "vasuki-db",
3
+ "version": "1.0.0",
4
+ "main": "test.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "author": "",
10
+ "license": "ISC",
11
+ "description": ""
12
+ }
package/test.js ADDED
@@ -0,0 +1,44 @@
1
+ import Vasuki from 'https://vasuki.cloud/vasuki.min.js'; // Ensure path is correct
2
+
3
+ const vdb = new Vasuki();
4
+
5
+ async function runTest() {
6
+ console.log("Starting Vasuki SDK Test...");
7
+
8
+ try {
9
+ // 1. LOGIN TEST
10
+ // Ye codeclutch.adamyarao@gmail.com/users_rows.json se check karega
11
+ console.log("Attempting Login...");
12
+ await vdb.login('[YOUR EMAIL]', '[YOUR PASSWORD]');
13
+ console.log("Login Successful!");
14
+
15
+ // 2. LIST FILES TEST
16
+ console.log("Fetching file list...");
17
+ const files = await vdb.listFiles();
18
+ console.log("Files found in your account:", files);
19
+
20
+ // 3. CREATE/GET DB TEST
21
+ // Maan lo 'TestDB' naam ki file hai ya banani hai
22
+ console.log("Getting DB content...");
23
+ const dbData = await vdb.getDB('[YOUR DB NAME]');
24
+ console.log("Current DB Content:", dbData);
25
+
26
+ // 4. INSERT ROW TEST
27
+ console.log("Inserting new row into 'Users' table...");
28
+ const newRow = { id: Date.now(), name: "[DEMO USER]", role: "[DEMO ROLE]" };
29
+ const result = await vdb.insertRow('[YOUR DB NAME]', '[YOUR SHEET NAME]', newRow);
30
+
31
+ if(result.ok) {
32
+ console.log("Row inserted successfully!");
33
+ }
34
+
35
+ // 5. VERIFY INSERT
36
+ const updatedData = await vdb.getDB('[YOUR DB NAME]');
37
+ console.log("Updated Table:", updatedData['[YOUR SHEET NAME]']);
38
+
39
+ } catch (error) {
40
+ console.error("Test Failed:", error.message);
41
+ }
42
+ }
43
+
44
+ runTest();