Added SSR
This commit is contained in:
@@ -1,19 +1,44 @@
|
||||
import {database} from "./start";
|
||||
import {Dict, Group} from "../types";
|
||||
import crypto from "crypto"
|
||||
import {Filter, ObjectId, WithId} from "mongodb";
|
||||
|
||||
export async function setGroups(groups: Dict<Group>, ignored: string[], base: string[]) {
|
||||
const collection = (await database.resolve())?.collection('setups')
|
||||
export interface GroupData {
|
||||
groups: Dict<Group>,
|
||||
ignored: string[],
|
||||
base: string[]
|
||||
}
|
||||
|
||||
type InsertMeta<T> = T & {
|
||||
createdOn: Date,
|
||||
modifiedOn: Date,
|
||||
accessedOn: Date
|
||||
}
|
||||
|
||||
type GroupFilter = Filter<InsertMeta<GroupData>>
|
||||
|
||||
export async function setGroups(data: GroupData) {
|
||||
const collection = (await database.resolve())?.collection<InsertMeta<GroupData>>('setups')
|
||||
if (!collection) return
|
||||
await collection.deleteMany({}).catch(e => {
|
||||
if (e.message !== 'ns not found') throw e
|
||||
})
|
||||
const result = await collection.insertOne({
|
||||
groups,
|
||||
ignored,
|
||||
base,
|
||||
...data,
|
||||
createdOn: new Date(),
|
||||
modifiedOn: new Date()
|
||||
modifiedOn: new Date(),
|
||||
accessedOn: new Date()
|
||||
})
|
||||
console.log(result.insertedId, result.insertedId.toString())
|
||||
return result.insertedId.toString()
|
||||
}
|
||||
|
||||
|
||||
export async function getGroup(uuid: string) {
|
||||
const collection = (await database.resolve())?.collection<InsertMeta<GroupData>>('setups')
|
||||
if (!collection) return
|
||||
console.log(uuid)
|
||||
const data = (await collection.findOne({
|
||||
_id: new ObjectId(uuid)
|
||||
} as GroupFilter)) ?? undefined
|
||||
if (data) {
|
||||
await collection.updateOne({_id: new ObjectId(uuid)}, { $set: {accessedOn: new Date()}})
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user