summaryrefslogtreecommitdiff
path: root/nodejs/database
diff options
context:
space:
mode:
Diffstat (limited to 'nodejs/database')
-rw-r--r--nodejs/database/agents.js21
-rw-r--r--nodejs/database/agents.ts20
-rw-r--r--nodejs/database/contracts.ts (renamed from nodejs/database/contracts.js)23
-rw-r--r--nodejs/database/db.ts (renamed from nodejs/database/db.js)13
-rw-r--r--nodejs/database/ships.ts (renamed from nodejs/database/ships.js)45
-rw-r--r--nodejs/database/tokens.js16
-rw-r--r--nodejs/database/tokens.ts14
7 files changed, 73 insertions, 79 deletions
diff --git a/nodejs/database/agents.js b/nodejs/database/agents.js
deleted file mode 100644
index 8b7203b..0000000
--- a/nodejs/database/agents.js
+++ /dev/null
@@ -1,21 +0,0 @@
-import db from './db.js';
-
-const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`);
-const getAgentStatement = db.prepare(`SELECT data FROM agents;`);
-const setAgentStatement = db.prepare(`UPDATE agents SET data = json(?);`);
-
-export function addAgent(agent) {
- return addAgentStatement.run(JSON.stringify(agent)).lastInsertRowid;
-}
-
-export function getAgent() {
- const data = getAgentStatement.get();
- if (data === undefined) {
- return null;
- }
- return JSON.parse(data.data);
-}
-
-export function setAgent(agent) {
- return setAgentStatement.run(JSON.stringify(agent)).changes;
-}
diff --git a/nodejs/database/agents.ts b/nodejs/database/agents.ts
new file mode 100644
index 0000000..41b956a
--- /dev/null
+++ b/nodejs/database/agents.ts
@@ -0,0 +1,20 @@
+import { Agent } from '../model/agent.ts';
+import db from './db.js';
+
+const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`);
+const getAgentStatement = db.prepare(`SELECT data FROM agents;`);
+const setAgentStatement = db.prepare(`UPDATE agents SET data = json(?);`);
+
+export function addAgent(agent: Agent) {
+ addAgentStatement.run(JSON.stringify(agent));
+}
+
+export function getAgent(): Agent|null {
+ const data = getAgentStatement.get() as {data: string}|undefined;
+ if (!data) return null;
+ return JSON.parse(data.data);
+}
+
+export function setAgent(agent: Agent) {
+ setAgentStatement.run(JSON.stringify(agent));
+}
diff --git a/nodejs/database/contracts.js b/nodejs/database/contracts.ts
index d7f9aab..576f8dd 100644
--- a/nodejs/database/contracts.js
+++ b/nodejs/database/contracts.ts
@@ -1,30 +1,29 @@
-import db from './db.js';
+import { Contract } from '../model/contract.ts';
+import db from './db.ts';
const addContractStatement = db.prepare(`INSERT INTO contracts(data) VALUES (json(?));`);
const getContractStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'id' = ?;`);
const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'fulfilled' = false;`);
const updateContractStatement = db.prepare(`UPDATE contracts SET data = json(:data) WHERE data->>'id' = :id;`);
-export function getContract(id) {
- const data = getContractStatement.get(id);
- if (data === undefined) {
- return null;
- }
+export function getContract(id: string): Contract|null {
+ const data = getContractStatement.get(id) as {data: string}|undefined;
+ if (!data) return null;
return JSON.parse(data.data);
}
-export function getContracts() {
- const data = getContractsStatement.all();
+export function getContracts(): Array<Contract> {
+ const data = getContractsStatement.all() as Array<{data: string}>;
return data.map(contractData => JSON.parse(contractData.data));
}
-export function setContract(data) {
+export function setContract(data: Contract) {
if (getContract(data.id) === null) {
- return addContractStatement.run(JSON.stringify(data)).lastInsertRowid;
+ addContractStatement.run(JSON.stringify(data));
} else {
- return updateContractStatement.run({
+ updateContractStatement.run({
data: JSON.stringify(data),
id: data.id,
- }).changes;
+ });
}
}
diff --git a/nodejs/database/db.js b/nodejs/database/db.ts
index 4438fec..247ee5a 100644
--- a/nodejs/database/db.js
+++ b/nodejs/database/db.ts
@@ -11,7 +11,7 @@ const allMigrations = [
const db = new Database(
process.env.NODE_ENV === 'test' ? 'test.db' : 'spacetraders.db',
- process.env.NODE_ENV === 'development' ? { verbose: console.log } : null
+ process.env.NODE_ENV === 'development' ? { verbose: console.log } : undefined
);
db.pragma('foreign_keys = ON');
db.pragma('journal_mode = WAL');
@@ -20,7 +20,8 @@ function init() {
db.transaction(function migrate() {
let version;
try {
- version = db.prepare('SELECT version FROM schema_version').get().version;
+ const res = db.prepare('SELECT version FROM schema_version').get() as {version: number};
+ version = res.version;
} catch {
version = 0;
}
@@ -34,10 +35,10 @@ function init() {
}
export function reset() {
- const indices = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'index';`).all();
- const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table';`).all();
- const triggers = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'trigger';`).all();
- const views = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'view';`).all();
+ const indices = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'index';`).all() as Array<{name: string}>;
+ const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table';`).all() as Array<{name: string}>;
+ const triggers = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'trigger';`).all() as Array<{name: string}>;
+ const views = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'view';`).all() as Array<{name: string}>;
indices.forEach(elt => db.exec(`DROP INDEX ${elt.name};`));
tables.forEach(elt => db.exec(`DROP TABLE ${elt.name};`));
triggers.forEach(elt => db.exec(`DROP TRIGGER ${elt.name};`));
diff --git a/nodejs/database/ships.js b/nodejs/database/ships.ts
index f9eb668..58c8abf 100644
--- a/nodejs/database/ships.js
+++ b/nodejs/database/ships.ts
@@ -1,4 +1,6 @@
-import db from './db.js';
+import db from './db.ts';
+import { Cargo } from '../model/cargo.ts';
+import { Fuel, Nav, Ship } from '../model/ship.ts';
const addShipStatement = db.prepare(`INSERT INTO ships(data) VALUES (json(?));`);
const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`);
@@ -8,51 +10,46 @@ const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_se
const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data) WHERE data->>'symbol' = :symbol;`);
-export function getShip(symbol) {
- const data = getShipStatement.get(symbol);
- if (data === undefined) {
- return null;
- }
+export function getShip(symbol: string): Ship|null {
+ const data = getShipStatement.get(symbol) as {data: string}|undefined;
+ if (!data) return null;
return JSON.parse(data.data);
}
-export function getShipsAt(symbol) {
- const data = getShipsAtStatement.all(symbol);
- if (data === undefined) {
- return null;
- }
+export function getShipsAt(symbol: string) {
+ const data = getShipsAtStatement.all(symbol) as Array<{data: string}>;
return data.map(elt => JSON.parse(elt.data));
}
-export function setShip(data) {
+export function setShip(data: Ship) {
if (getShip(data.symbol) === null) {
- return addShipStatement.run(JSON.stringify(data)).lastInsertRowid;
+ addShipStatement.run(JSON.stringify(data));
} else {
- return updateShipStatement.run({
+ updateShipStatement.run({
data: JSON.stringify(data),
symbol: data.symbol,
- }).changes;
+ });
}
}
-export function setShipCargo(symbol, cargo) {
- return setShipCargoStatement.run({
+export function setShipCargo(symbol: string, cargo: Cargo) {
+ setShipCargoStatement.run({
cargo: JSON.stringify(cargo),
symbol: symbol,
- }).changes;
+ });
}
-export function setShipFuel(symbol, fuel) {
- return setShipFuelStatement.run({
+export function setShipFuel(symbol: string, fuel: Fuel) {
+ setShipFuelStatement.run({
fuel: JSON.stringify(fuel),
symbol: symbol,
- }).changes;
+ });
}
-export function setShipNav(symbol, nav) {
- return setShipNavStatement.run({
+export function setShipNav(symbol: string, nav: Nav) {
+ setShipNavStatement.run({
nav: JSON.stringify(nav),
symbol: symbol,
- }).changes;
+ });
}
diff --git a/nodejs/database/tokens.js b/nodejs/database/tokens.js
deleted file mode 100644
index 2a781af..0000000
--- a/nodejs/database/tokens.js
+++ /dev/null
@@ -1,16 +0,0 @@
-import db from './db.js';
-
-const addTokenStatement = db.prepare(`INSERT INTO tokens(data) VALUES (?);`);
-const getTokenStatement = db.prepare(`SELECT data FROM tokens;`);
-
-export function addToken(token) {
- return addTokenStatement.run(token).lastInsertRowid;
-}
-
-export function getToken() {
- const data = getTokenStatement.get();
- if (data === undefined) {
- return null;
- }
- return data.data;
-}
diff --git a/nodejs/database/tokens.ts b/nodejs/database/tokens.ts
new file mode 100644
index 0000000..4495a65
--- /dev/null
+++ b/nodejs/database/tokens.ts
@@ -0,0 +1,14 @@
+import db from './db.ts';
+
+const addTokenStatement = db.prepare(`INSERT INTO tokens(data) VALUES (?);`);
+const getTokenStatement = db.prepare(`SELECT data FROM tokens;`);
+
+export function addToken(token: string) {
+ addTokenStatement.run(token);
+}
+
+export function getToken(): string|null {
+ const data = getTokenStatement.get() as {data: string}|undefined;
+ if (data === undefined) return null;
+ return data.data;
+}