From c5a2d7c96b1ac427b39e42c2abb7d5b903bb585d Mon Sep 17 00:00:00 2001 From: 728 Date: Sat, 19 Nov 2022 15:03:30 +0800 Subject: [PATCH] db #25 --- cli/dbinit.ts | 23 ++------------ ismism.ts/src/db.ts | 64 +++++++++++++++++++++++++++++++++++++++ ismism.ts/src/typ.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 20 deletions(-) create mode 100644 ismism.ts/src/db.ts diff --git a/cli/dbinit.ts b/cli/dbinit.ts index 2c3a13a..c4bc982 100644 --- a/cli/dbinit.ts +++ b/cli/dbinit.ts @@ -1,21 +1,4 @@ -import { Collection, MongoClient } from "https://deno.land/x/mongo@v0.31.1/mod.ts" -import { User } from "../ismism.ts/src/typ.ts" +import { init } from "../ismism.ts/src/db.ts" -const mongo = new MongoClient() -await mongo.connect("mongodb://127.0.0.1:27017") - -console.log(await mongo.database("ismism").listCollectionNames()) - -const user: Collection = mongo.database("ismism").collection("user") -try { await user.drop() } catch (e) { console.error(e) } -console.log(await user.createIndexes({ - indexes: [{ - key: { nbr: 1 }, - name: "nbr", - unique: true, - }, { - key: { name: 1 }, - name: "name", - unique: true - }] -})) +const coll = await init() +console.log(`collections created:\n${coll.join("\n")}`) diff --git a/ismism.ts/src/db.ts b/ismism.ts/src/db.ts new file mode 100644 index 0000000..9c9ba0b --- /dev/null +++ b/ismism.ts/src/db.ts @@ -0,0 +1,64 @@ +import { MongoClient } from "https://deno.land/x/mongo@v0.31.1/mod.ts" +import { Agenda, Fund, Dat, Soc, User, Work } from "./typ.ts" + +const uri = "mongodb://127.0.0.1:27017" +const mongo = new MongoClient() +await mongo.connect(uri) + +export const db = mongo.database("ismism") +export const coll = { + user: db.collection("user"), + soc: db.collection("soc"), + agenda: db.collection("agenda"), + work: db.collection("work"), + fund: db.collection("fund"), + dat: db.collection("dat"), +} + +export async function init( +) { + try { await db.dropDatabase() } catch (e) { console.error(e) } + await coll.user.createIndexes({ + indexes: [{ + key: { nbr: 1 }, name: "nbr", unique: true, + }, { + key: { name: 1 }, name: "name", unique: true, + }] + }) + await coll.soc.createIndexes({ + indexes: [{ + key: { name: 1 }, name: "name", unique: true, + }, { + key: { uid: 1 }, name: "uid" + }] + }) + await coll.agenda.createIndexes({ + indexes: [{ + key: { name: 1 }, name: "name", unique: true, + }, { + key: { uid: 1 }, name: "uid" + }, { + key: { sid: 1 }, name: "sid" + }] + }) + await coll.work.createIndexes({ + indexes: [{ + key: { uid: 1 }, name: "uid" + }, { + key: { aid: 1 }, name: "aid" + }] + }) + await coll.fund.createIndexes({ + indexes: [{ + key: { uid: 1 }, name: "uid" + }, { + key: { aid: 1 }, name: "aid" + }] + }) + await coll.dat.createIndexes({ + indexes: [{ + key: { typ: 1, tid: 1, utc: -1 }, name: "typ-tid-utc" + }] + }) + return await db.listCollectionNames() +} diff --git a/ismism.ts/src/typ.ts b/ismism.ts/src/typ.ts index ee8d956..2fe7a34 100644 --- a/ismism.ts/src/typ.ts +++ b/ismism.ts/src/typ.ts @@ -4,3 +4,75 @@ export type User = { name: string, utc: number, } +export const uid_ofs = 10000 + +export type Soc = { + _id: number, + name: string, + uid: number[], + admin: number[], + intro: string, + uid_max: number, + utc: number, +} +export const sid_ofs = 10000 + +export type Agenda = { + _id: number, + name: string, + uid: number[], + sid: number[], + budget: number, + fund: number, + expense: number, + detail: string, + goal: Goal[], + done: string[], + utc: number, +} +export type Goal = { + name: string, + pct: number, +} + +export type Work = { + _id: number, + uid: number, + utc: number, +} & ({ + op: "init", + aid: number, +} | { + op: "goal", + aid: number, + goal: Goal, +} | { + op: "done", + aid: number, + done: string, +} | { + op: "join", + aid: number, + role: string, +} | { + op: "work", + aid: number, + msg: string +}) + +export type Fund = { + _id: number, + uid: number, + aid: number, + fund: number, + utc: number +} + +export type Dat = { + _id: number, + utc: number, +} & ({ + typ: "imgsrc-aid", + tid: number, + img: { title: string, src: string }[] +})