notificationserver/core/database.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-10-24 10:46:40 +03:00
import knex from "knex";
import {resolve} from "node:path";
let EventDB = knex({
client: "sqlite3",
connection:{
filename: resolve(import.meta.dirname,"../data/events.db")
},
useNullAsDefault: true
});
let CollectionDB = knex({
client: "sqlite3",
connection:{
filename: resolve(import.meta.dirname,"../data/collection.db")
},
useNullAsDefault: true
});
export async function migrate()
{
if(await EventDB.schema.hasTable("events") == false)
{
await EventDB.schema.createTable("events",function(table){
table.bigIncrements("id",{primaryKey: true}).unsigned();
table.json("payload").notNullable();
table.string("domain",255).index().notNullable();
table.string("type",100).index().notNullable();
table.datetime("time").notNullable();
table.datetime("expire").notNullable();
})
};
if(await CollectionDB.schema.hasTable("collection") == false)
{
await CollectionDB.schema.createTable("collection",function(table){
table.bigIncrements("id",{primaryKey: true}).unsigned();
table.json("data").notNullable();
table.string("catalog",255).index().notNullable();
table.string("domain",255).index().notNullable();
table.datetime("time").notNullable();
table.datetime("expire").notNullable();
})
}
}
/**
* @returns {import("knex").Knex}
*/
export function Event()
{
return EventDB.table("events");
}
/**
* @returns {import("knex").Knex}
*/
export function Collection()
{
return CollectionDB.table("collection");
}
process.nextTick(migrate);