🗃️ Migrate from SQLite to PostgreSQL

This commit is contained in:
KaKi87 2022-02-17 20:18:14 +01:00
parent a0cf05e65a
commit d617804aec
4 changed files with 63 additions and 30 deletions

3
.gitignore vendored
View File

@ -1,2 +1 @@
config.js
db
config.js

View File

@ -8,5 +8,29 @@ export default {
* Website URL
* @type {string}
*/
webUrl: undefined
webUrl: undefined,
/**
* Database username
* @type {string}
*/
dbUsername: undefined,
/**
* Database password
* @type {string}
*/
dbPassword: undefined,
/**
* Database name
* @type {string}
*/
dbName: undefined,
/**
* @type {string}
*/
dbHost: undefined,
/**
* Database port
* @type {number}
*/
dbPort: undefined
};

52
mod.js
View File

@ -1,5 +1,5 @@
import pogo from 'https://deno.land/x/pogo@v0.5.2/main.ts';
import { DB } from 'https://deno.land/x/sqlite@v3.1.3/mod.ts';
import { Client as DB } from 'https://deno.land/x/postgres@v0.15.0/mod.ts';
import { stringSpliceMulti } from 'https://git.kaki87.net/KaKi87/strsplice/raw/branch/master/mod.js';
import route from './lib/route.js';
@ -9,32 +9,43 @@ import verifyConditions from './lib/conditions.js';
import config from './config.js';
const {
port,
webUrl
webUrl,
dbUsername,
dbPassword,
dbName,
dbHost,
dbPort
} = config;
const
server = pogo.server({ port }),
db = new DB('db');
db = new DB({
user: dbUsername,
password: dbPassword,
database: dbName,
hostname: dbHost,
port: dbPort
});
const createRoute = route(server);
createRoute({
method: 'GET',
path: '/',
handler: (request, h, { response }) => {
handler: async (request, h, { response }) => {
if(typeof request.searchParams.get('list') === 'string'){
response.body = db.query(
'SELECT id, creationTimestamp FROM url'
).map(([
response.body = (await db.queryArray(
'SELECT "id", "creationTimestamp" FROM "url"'
)).rows.map(([
id,
creationTimestamp
]) => ({
id,
creationTimestamp
creationTimestamp: Number(creationTimestamp)
}));
}
else if(typeof request.searchParams.get('count') === 'string')
response.body = db.query('SELECT COUNT(id) FROM url')[0][0];
response.body = Number((await db.queryArray('SELECT COUNT("id") FROM "url"')).rows[0][0]);
else if(webUrl)
response.redirect(webUrl);
else
@ -57,17 +68,17 @@ createRoute({
}
) => {
if(!idSize)
idSize = JSON.parse(db.query(`SELECT v FROM setting WHERE k = 'idSize'`)[0][0]);
idSize = JSON.parse((await db.queryArray(`SELECT "value" FROM "setting" WHERE "key" = 'idSize'`)).rows[0][0]);
let id = nanoid({ size: idSize });
if(db.query('SELECT 1 FROM url WHERE id = ?', [id])[0]?.[0] === 1){
if((await db.queryArray('SELECT 1 FROM "url" WHERE "id" = $1', [id])).rows[0]?.[0] === 1){
idSize += 1;
id = nanoid({ size: idSize });
db.query(`UPDATE setting SET v = ? WHERE k = 'idSize'`, [idSize]);
await db.queryArray(`UPDATE "setting" SET "value" = $1 WHERE "key" = 'idSize'`, [idSize]);
}
const creationTimestamp = Date.now();
db.query(
'INSERT INTO url (id, creationTimestamp, jsonConfig) VALUES (?, ?, ?)',
[id, creationTimestamp, JSON.stringify(jsonBody)]
await db.queryArray(
'INSERT INTO url ("id", "creationTimestamp", "config") VALUES ($1, $2, $3)',
[id, creationTimestamp, jsonBody]
);
response.body = {
id
@ -87,14 +98,13 @@ createRoute({
response
}
) => {
const [[creationTimestamp, jsonConfig] = []] = db.query('SELECT creationTimestamp, jsonConfig FROM url WHERE id = ?', [request.params.id]);
const { rows: [[creationTimestamp, config] = []] } = await db.queryArray('SELECT "creationTimestamp", "config" FROM "url" WHERE id = $1', [request.params.id]);
if(!creationTimestamp){
response.code(404);
return response;
}
let url, randomConfigIndex, conditionalConfigIndex;
const
config = JSON.parse(jsonConfig),
{
conditions,
configs,
@ -167,7 +177,7 @@ createRoute({
}
if(typeof response.body === 'object') response.body = {
...response.body,
creationTimestamp,
creationTimestamp: Number(creationTimestamp),
config
};
return response;
@ -184,10 +194,10 @@ createRoute({
response
}
) => {
const [[creationTimestamp, jsonConfig] = []] = db.query('SELECT creationTimestamp, jsonConfig FROM url WHERE id = ?', [request.params.id]);
const { rows: [[creationTimestamp, config] = []] } = await db.queryArray('SELECT "creationTimestamp", "config" FROM "url" WHERE "id" = $1', [request.params.id]);
if(creationTimestamp) response.body = {
creationTimestamp,
config: JSON.parse(jsonConfig)
creationTimestamp: Number(creationTimestamp),
config
};
else response.code(404);
return response;

View File

@ -1,12 +1,12 @@
CREATE TABLE url (
id VARCHAR PRIMARY KEY,
creationTimestamp BIGINT NOT NULL,
jsonConfig TEXT NOT NULL
"id" VARCHAR(10) PRIMARY KEY,
"creationTimestamp" BIGINT NOT NULL,
"config" JSON NOT NULL
);
CREATE TABLE setting (
k VARCHAR PRIMARY KEY,
v VARCHAR NOT NULL
"key" VARCHAR(10) PRIMARY KEY,
"value" VARCHAR(10) NOT NULL
);
INSERT INTO setting (k, v) VALUES ('idSize', 3);
INSERT INTO setting ("key", "value") VALUES ('idSize', 3);