This commit is contained in:
728
2022-11-19 15:03:30 +08:00
parent a08831118d
commit c5a2d7c96b
3 changed files with 139 additions and 20 deletions

View File

@ -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<User> = 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")}`)

64
ismism.ts/src/db.ts Normal file
View File

@ -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>("user"),
soc: db.collection<Soc>("soc"),
agenda: db.collection<Agenda>("agenda"),
work: db.collection<Work>("work"),
fund: db.collection<Fund>("fund"),
dat: db.collection<Dat>("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()
}

View File

@ -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 }[]
})