Skip to content

Commit

Permalink
👨‍💻CODE BY 🕊️★⃝AJAY O S©️🧚‍♂️
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajayos committed Apr 29, 2023
1 parent e3e9542 commit 158df80
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Dependency directories
node_modules/
jspm_packages/

package-lock.json
.npmrc
143 changes: 139 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteDB = exports.getDB = exports.setDB = exports.db = exports.allAsync = exports.runAsync = void 0;
exports.deleteDATA = exports.getDATA = exports.setDATA = exports.deleteDB = exports.getDB = exports.setDB = exports.db = exports.allAsync = exports.runAsync = void 0;
var fs = require("fs");
var path = require("path");
var sqlite3 = require("sqlite3");
var util_1 = require("util");
var nodelog = require("@ajayos/nodelog");
var DB_FOLDER = 'DB';
var DB_FILE = 'ajayos.sql';
var DB_PATH = './' + DB_FOLDER + '/' + DB_FILE;
Expand Down Expand Up @@ -88,7 +87,7 @@ var setDB = function (tableName, rowName, data) { return __awaiter(void 0, void
case 2:
// Table does not exist, create it and insert the data
_a.sent();
return [4 /*yield*/, runAsync("INSERT INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, JSON.stringify(data)])];
return [4 /*yield*/, runAsync("INSERT OR IGNORE INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, JSON.stringify(data)])];
case 3:
_a.sent();
return [3 /*break*/, 9];
Expand All @@ -104,7 +103,7 @@ var setDB = function (tableName, rowName, data) { return __awaiter(void 0, void
return [3 /*break*/, 9];
case 7:
// Row exists, update it
return [4 /*yield*/, runAsync("INSERT OR REPLACE INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, JSON.stringify(data)])];
return [4 /*yield*/, runAsync("INSERT OR REPLACE OR IGNORE INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, JSON.stringify(data)])];
case 8:
// Row exists, update it
_a.sent();
Expand Down Expand Up @@ -208,3 +207,139 @@ var deleteDB = function (tableName, rowName) { return __awaiter(void 0, void 0,
});
}); };
exports.deleteDB = deleteDB;
// Create the 'setDB' function
var setDATA = function (tableName, rowName, data) { return __awaiter(void 0, void 0, void 0, function () {
var result, result_4, err_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 10, , 11]);
return [4 /*yield*/, allAsync("SELECT name FROM sqlite_master WHERE type='table' AND name='".concat(tableName, "';"))];
case 1:
result = _a.sent();
if (!(result.length === 0)) return [3 /*break*/, 4];
// Table does not exist, create it and insert the data
return [4 /*yield*/, runAsync("CREATE TABLE ".concat(tableName, " (row_name TEXT PRIMARY KEY, data TEXT NOT NULL);"))];
case 2:
// Table does not exist, create it and insert the data
_a.sent();
return [4 /*yield*/, runAsync("INSERT OR IGNORE INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, data])];
case 3:
_a.sent();
return [3 /*break*/, 9];
case 4: return [4 /*yield*/, allAsync("SELECT row_name FROM ".concat(tableName, " WHERE row_name='").concat(rowName, "';"))];
case 5:
result_4 = _a.sent();
if (!(result_4.length === 0)) return [3 /*break*/, 7];
// Row does not exist, insert it
return [4 /*yield*/, runAsync("INSERT INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, data])];
case 6:
// Row does not exist, insert it
_a.sent();
return [3 /*break*/, 9];
case 7:
// Row exists, update it
return [4 /*yield*/, runAsync("INSERT OR REPLACE OR IGNORE INTO ".concat(tableName, " (row_name, data) VALUES (?, ?);"), [rowName, data])];
case 8:
// Row exists, update it
_a.sent();
_a.label = 9;
case 9: return [2 /*return*/, true];
case 10:
err_4 = _a.sent();
console.error(err_4.message);
return [2 /*return*/, false];
case 11: return [2 /*return*/];
}
});
}); };
exports.setDATA = setDATA;
// Create the 'getDB' function
var getDATA = function (tableName, rowName) { return __awaiter(void 0, void 0, void 0, function () {
var result, result_5, result_6, err_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 8, , 9]);
return [4 /*yield*/, allAsync("SELECT name FROM sqlite_master WHERE type='table' AND name='".concat(tableName, "';"))];
case 1:
result = _a.sent();
if (!(result.length === 0)) return [3 /*break*/, 3];
// Table does not exist, create it
return [4 /*yield*/, runAsync("CREATE TABLE ".concat(tableName, " (row_name TEXT PRIMARY KEY, data TEXT NOT NULL);"))];
case 2:
// Table does not exist, create it
_a.sent();
return [2 /*return*/, undefined];
case 3:
if (!rowName) return [3 /*break*/, 5];
return [4 /*yield*/, allAsync("SELECT data FROM ".concat(tableName, " WHERE row_name='").concat(rowName, "';"))];
case 4:
result_5 = _a.sent();
if (result_5.length === 0) {
// Row does not exist, return undefined
return [2 /*return*/, undefined];
}
else {
// Row exists, return the data
return [2 /*return*/, result_5[0].data];
}
return [3 /*break*/, 7];
case 5: return [4 /*yield*/, allAsync("SELECT row_name, data FROM ".concat(tableName, ";"))];
case 6:
result_6 = _a.sent();
return [2 /*return*/, result_6.map(function (row) {
return {
rowName: row.row_name,
data: row.data
};
})];
case 7: return [3 /*break*/, 9];
case 8:
err_5 = _a.sent();
console.error(err_5.message);
return [2 /*return*/, undefined];
case 9: return [2 /*return*/];
}
});
}); };
exports.getDATA = getDATA;
// Create the 'deleteDB' function
var deleteDATA = function (tableName, rowName) { return __awaiter(void 0, void 0, void 0, function () {
var result, err_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 8, , 9]);
if (!rowName) return [3 /*break*/, 5];
return [4 /*yield*/, allAsync("SELECT row_name FROM ".concat(tableName, " WHERE row_name='").concat(rowName, "';"))];
case 1:
result = _a.sent();
if (!(result.length === 0)) return [3 /*break*/, 2];
// Row does not exist, return false
return [2 /*return*/, false];
case 2:
// Row exists, delete it
return [4 /*yield*/, runAsync("DELETE FROM ".concat(tableName, " WHERE row_name='").concat(rowName, "';"))];
case 3:
// Row exists, delete it
_a.sent();
return [2 /*return*/, true];
case 4: return [3 /*break*/, 7];
case 5:
// Row name is not specified, delete the table
return [4 /*yield*/, runAsync("DROP TABLE ".concat(tableName, ";"))];
case 6:
// Row name is not specified, delete the table
_a.sent();
return [2 /*return*/, true];
case 7: return [3 /*break*/, 9];
case 8:
err_6 = _a.sent();
console.error(err_6.message);
return [2 /*return*/, false];
case 9: return [2 /*return*/];
}
});
}); };
exports.deleteDATA = deleteDATA;
100 changes: 98 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const setDB = async (tableName: string, rowName: string, data: any): Prom
if (result.length === 0) {
// Table does not exist, create it and insert the data
await runAsync(`CREATE TABLE ${tableName} (row_name TEXT PRIMARY KEY, data TEXT NOT NULL);`);
await runAsync(`INSERT INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, JSON.stringify(data)]);
await runAsync(`INSERT OR IGNORE INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, JSON.stringify(data)]);
} else {
// Table exists, check if row exists
const result = await allAsync(`SELECT row_name FROM ${tableName} WHERE row_name='${rowName}';`);
Expand All @@ -53,7 +53,7 @@ export const setDB = async (tableName: string, rowName: string, data: any): Prom
await runAsync(`INSERT INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, JSON.stringify(data)]);
} else {
// Row exists, update it
await runAsync(`INSERT OR REPLACE INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, JSON.stringify(data)]);
await runAsync(`INSERT OR REPLACE OR IGNORE INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, JSON.stringify(data)]);
}
}
return true;
Expand Down Expand Up @@ -129,3 +129,99 @@ export const deleteDB = async (tableName: string, rowName?: string): Promise<boo
return false;
}
}

// Create the 'setDB' function
export const setDATA = async (tableName: string, rowName: string, data: any): Promise<any> => {
try {
// Check if the table exists
const result = await allAsync(`SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}';`);

if (result.length === 0) {
// Table does not exist, create it and insert the data
await runAsync(`CREATE TABLE ${tableName} (row_name TEXT PRIMARY KEY, data TEXT NOT NULL);`);
await runAsync(`INSERT OR IGNORE INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, data]);
} else {
// Table exists, check if row exists
const result = await allAsync(`SELECT row_name FROM ${tableName} WHERE row_name='${rowName}';`);

if (result.length === 0) {
// Row does not exist, insert it
await runAsync(`INSERT INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, data]);
} else {
// Row exists, update it
await runAsync(`INSERT OR REPLACE OR IGNORE INTO ${tableName} (row_name, data) VALUES (?, ?);`, [rowName, data]);
}
}
return true;
} catch (err) {
console.error(err.message);
return false;
}
};

// Create the 'getDB' function
export const getDATA = async (tableName: string, rowName?: string): Promise<any> => {
try {
// Check if the table exists
const result = await allAsync(`SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}';`);

if (result.length === 0) {
// Table does not exist, create it
await runAsync(`CREATE TABLE ${tableName} (row_name TEXT PRIMARY KEY, data TEXT NOT NULL);`);
return undefined;
} else {
if (rowName) {
// Row name is specified, check if it exists
const result = await allAsync(`SELECT data FROM ${tableName} WHERE row_name='${rowName}';`);

if (result.length === 0) {
// Row does not exist, return undefined
return undefined;
} else {
// Row exists, return the data
return result[0].data;
}
} else {
// Row name is not specified, return all rows
const result = await allAsync(`SELECT row_name, data FROM ${tableName};`);
return result.map((row: any) => {
return {
rowName: row.row_name,
data: row.data
};
});
}
}

} catch (err) {
console.error(err.message);
return undefined;
}
};


// Create the 'deleteDB' function
export const deleteDATA = async (tableName: string, rowName?: string): Promise<boolean> => {
try {
if (rowName) {
// Row name is specified, check if it exists
const result = await allAsync(`SELECT row_name FROM ${tableName} WHERE row_name='${rowName}';`);

if (result.length === 0) {
// Row does not exist, return false
return false;
} else {
// Row exists, delete it
await runAsync(`DELETE FROM ${tableName} WHERE row_name='${rowName}';`);
return true;
}
} else {
// Row name is not specified, delete the table
await runAsync(`DROP TABLE ${tableName};`);
return true;
}
} catch (err) {
console.error(err.message);
return false;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ajayos/nodedb",
"version": "1.0.3",
"version": "1.0.4",
"description": "Database for nodejs",
"main": "index.js",
"ts": "index.ts",
Expand Down

0 comments on commit 158df80

Please sign in to comment.