https://github.com/damianmcclure/jsalphadb
Database system for NodeJS & Port of phpAlphaDB
https://github.com/damianmcclure/jsalphadb
database javascript nodejs
Last synced: 2 months ago
JSON representation
Database system for NodeJS & Port of phpAlphaDB
- Host: GitHub
- URL: https://github.com/damianmcclure/jsalphadb
- Owner: damianmcclure
- Created: 2018-01-06T04:25:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-02-15T06:42:58.000Z (over 5 years ago)
- Last Synced: 2024-03-30T09:20:31.670Z (over 2 years ago)
- Topics: database, javascript, nodejs
- Language: JavaScript
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jsalphadb
Simple flat-file database system for NodeJS & Port of phpAlphaDB
Version: 1.0.3
## Installation
I recommend you install from NPM
`npm install jsalphadb`
Put this at the top of your javascript file.
```js
var key = "Something"; // The secret key, though theres no point to changing it.
var ext = ".4db"; // The extension of the database.
const jsAlphaDB = require("jsalphadb");
const db = new jsAlphaDB(key, ext);
```
Make sure node is allowed to write to the filesystem.
## Description
This is a Node.js remake of the flatfile database system made by HeapOverride called phpAlphaDB.
The purpose is to create a basic, easy to learn and use flat database system for JavaScript
This was made when I was in my JavaScript infancy, so I did a lot of things wrong and wrote a lot of messy code.
Since I saw there were still weekly downloads, I decided I should maintain it again and give it a little clean-up.
## Docs
There is the legacy fully synchronous version still in the program, and it's exactly the same syntax as before. However, I will always recommend you use promises as they are non-blocking.
```js
const Alpha = require("jsalphadb");
// make a new database instance
const db = new Alpha("test");
const table = "my table";
// create a table
db.create(table); // will be "my_table" on the filesystem, returns true or false if it was successful in creating it.
// add some data
db.write(table, `id=${Date.now()} name=${db.en("John Smith")} role=admin`); // each of these returns true or false if it was successful in writing it.
db.write(name, `id=${Date.now()} name=${db.en("James Kenny")} role=user`); // since we have spaces in the name, we need to encode it for it to be stored properly.
db.write(name, `id=${Date.now()} name=${db.en("Jim Johnson")} role=user`);
// get some data
const rows = db.read(name, "role=admin", "name role"); // will return James Kenny and Jim Johnson rows, with name and role. excluses the id row cuz we didn't select it.
for(const row of rows){
// extract the data from the rows
const name = db.de(db.column(row, "name")); // since we have encoded the name, we need to decode it now.
const role = db.column(row, "role"); // role is plaintext
}
// delete some data
db.rowdelete(name, `name=${db.en("Jim Johnson")}`); // will remove the Jim Johnson row in our table.
// all of the same stuff but with promises
(async () => {
const db = new Alpha.promises("test");
const table = "my async table";
await db.create(table);
await db.write(table, `id=${Date.now()} name=${db.en("John Smith")} role=admin`);
await db.write(name, `id=${Date.now()} name=${db.en("James Kenny")} role=user`);
await db.write(name, `id=${Date.now()} name=${db.en("Jim Johnson")} role=user`);
const rows = await db.read(name, "role=admin", "name role");
for await(const row of rows){
const name = await db.de(db.column(row, "name"));
const role = await db.column(row, "role");
}
// delete some data
await db.rowdelete(name, `name=${db.en("Jim Johnson")}`);
})();
```
## License
WTFPL - http://www.wtfpl.net/