Skip to content

Commit

Permalink
REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
easbittm committed Oct 14, 2021
1 parent 908a2b9 commit d22aa6e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 12 deletions.
5 changes: 4 additions & 1 deletion lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export default class Database {

#loadedTables;
table(name) {
if(!name) {
throw new Error("No able to work without tableName");
}
if (this.#deleted === true) {
throw new Error("Database is deleted");
}
Expand Down Expand Up @@ -126,7 +129,7 @@ export default class Database {

}
if(this.#serverSettings.port) {
this.#server = await new Server({
this.#server = await new Server(this, {
port: this.#serverSettings.port
})
}
Expand Down
29 changes: 26 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ import fastify from "fastify"

export default class Server {
#server
constructor({port = 8000} = {}) {
constructor(db, {port = 8000} = {}) {
return (async () => {
this.#server = fastify()
//DB
this.#server.get('/meta', async (req) => {
return db.meta;
})
//TABLE
this.#server.get('/table/:table/meta', async (req) => {
return db.table(req.params.table).meta
})

//GET but with body
this.#server.post('/table/:table/', async (req) => {
reply.send(db.meta)
})

this.#server.put('/table/:table/', async (req) => {
return await db.table(req.params.table).push(req.body)
})

this.#server.delete('/table/:table/', async (req) => {
return await db.table(req.params.table).remove(req.body._id)
})


await new Promise((res, rej) => {
this.#server.listen(port, (err) => {
this.#server.listen(port, '0.0.0.0', (err, address) => {
if(err) {
rej();
rej(err);
return;
}
res();
Expand Down
3 changes: 1 addition & 2 deletions lib/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ export default class Table {
if (!index || index.build !== true) {
this.#meta.indexes[name] = { name, build: false };
await this.#metaChange();
//await this.#dataDb.ensureIndex(name, true);


this.#meta.indexes[name] = { name, build: true };
await this.#metaChange();
return;
}
//await this.#dataDb.ensureIndex(name, rebuild);
}

async removeIndex(name) {
Expand Down
2 changes: 1 addition & 1 deletion test/001_Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect } from "chai";

let dbname = "databaseTest";

describe("Database", () => {
describe("Database (class)", () => {
let db;
before(async () => {
try {
Expand Down
2 changes: 1 addition & 1 deletion test/002_Backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from "chai";

let dbname = "backupTest";

describe("Backup", () => {
describe("Backup (class)", () => {
let db;
let id;

Expand Down
8 changes: 4 additions & 4 deletions test/003_Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from "chai";
let dbname = "tableTest";
let tableName = "test"

describe("Table", () => {
describe("Table (class)", () => {
let db;

before(async () => {
Expand All @@ -22,9 +22,9 @@ describe("Table", () => {

describe("#constructor(name)", () => {
it("should not allow empty name", async () => {
expect(db.table.bind(null)).to.throw
expect(db.table.bind(null, null)).to.throw
expect(db.table.bind(null, "")).to.throw
expect(() => { db.table() }).to.throw()
expect(() => { db.table(null) }).to.throw()
expect(() => { db.table("") }).to.throw()
});

});
Expand Down
57 changes: 57 additions & 0 deletions test/004_Server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Database from "../lib/Database.js";
import fs from "fs/promises";
import axios from "axios"
import { expect } from "chai";

let dbname = "serverTest";

describe("Server", () => {
let db;
before(async () => {
try {
await fs.rm(`./storage/${dbname}`, {
recursive: true,
});
} catch {}

db = await new Database(dbname, {
server: {
port: 8080
}
});
await db.table("persons").ensureIndex("name");
await db.table("persons").push({name: "Max Mustermann"})
});
after(async () => {
try {
db.close();
await fs.rm(`./storage/${dbname}`, {
recursive: true,
});
} catch {}
});

describe("/meta", () => {
it("GET: should return database information", async () => {
let { data } = await axios.get("http://localhost:8080/meta")
expect(data).to.be.eql(db.meta);
});
});

describe("/table/:table/meta", () => {
it("GET: should return table information", async () => {
let { data } = await axios.get("http://localhost:8080/table/persons/meta")
expect(data).to.be.eql(db.table("persons").meta);
});
});

describe("/table/:table/", () => {
it("PUT: should insert new db enty", async () => {
let item = { name: "Gerlinde Mustermann" }
let { data } = await axios.put("http://localhost:8080/table/persons/", item)

let d = await db.table("persons").get(data)
expect({... item, _id: data}).to.be.eql(d);
});
});
});
File renamed without changes.

0 comments on commit d22aa6e

Please sign in to comment.