From 86ac3662626963b86a02ccbf46950bc4eccb997d Mon Sep 17 00:00:00 2001 From: 728 Date: Mon, 6 Nov 2023 11:56:30 +0800 Subject: [PATCH] =?UTF-8?q?db=20=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=B8=8E=E7=B4=A2=E5=BC=95=20#45?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ eid/db.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ mongod.service | 31 ++++++++++++++++++ mongod.yaml | 10 ++++++ ont/json.ts | 7 ++++ 5 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 eid/db.ts create mode 100644 mongod.service create mode 100644 mongod.yaml create mode 100644 ont/json.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..504180d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/log +/db diff --git a/eid/db.ts b/eid/db.ts new file mode 100644 index 0000000..3c2aa6c --- /dev/null +++ b/eid/db.ts @@ -0,0 +1,89 @@ +import type { Agd, Aut, Cdt, Dbt, Ern, Lit, Soc, Usr, Wsl } from "./typ.ts" +import { Collection, Document, Filter, IndexOptions, MongoClient, UpdateFilter } from "https://deno.land/x/mongo@v0.32.0/mod.ts" + +export type Coll = Collection +export type Fltr = Filter +export type Proj = Partial<{ [K in P]: 0 } | { [K in P]: 1 }> +export type Updt = UpdateFilter + +export type DocC<_Id> = Promise | null> +export type DocR = Promise | null> +export type DocU = Promise<0 | 1 | null> +export type DocD = Promise<0 | 1 | null> + +export const conn = new MongoClient() +conn.connect("mongodb://127.0.0.1:27017") + +const nam: IndexOptions[] = [{ + key: { nam: 1 }, name: "nam", unique: true, +}] +const nbr: IndexOptions[] = [{ + key: { nbr: 1 }, name: "nbr", unique: true, + partialFilterExpression: { nbr: { $exists: true } }, +}] +const adm: IndexOptions[] = [{ + key: { adm1: 1 }, name: "adm1", +}, { + key: { adm2: 1 }, name: "adm2", +}] +const sec: IndexOptions[] = [{ + key: { sec: 1 }, name: "sec", +}] +const soc: IndexOptions[] = [{ + key: { soc: 1 }, name: "soc", +}] +const rec: IndexOptions[] = [{ + key: { "_id.usr": 1, "_id.utc": -1 }, name: "usr-utc", +}, { + key: { "_id.soc": 1, "_id.utc": -1 }, name: "soc-utc", +}, { + key: { "_id.usr": 1, "_id.soc": 1, "_id.utc": -1 }, name: "usr-soc-utc", +}] +const utc: IndexOptions[] = [{ + key: { "_id.usr": 1, "_id.soc": 1, "utc.eft": -1, "utc.exp": -1 }, name: "usr-eft", +}, { + key: { "_id.soc": 1, "utc.eft": -1, "utc.exp": -1 }, name: "soc-eft", +}] +const pin: IndexOptions[] = [{ + key: { pin: 1, "utc.put": -1 }, name: "pin", + partialFilterExpression: { pin: { $exists: true } }, +}] + +export async function db( + dbnam: "ismism" | "tst", + reset = false +) { + const db = conn.database(dbnam); + const c = { + usr: db.collection("usr"), + soc: db.collection("soc"), + agd: db.collection("agd"), + + cdt: db.collection("cdt"), + dbt: db.collection("dbt"), + ern: db.collection("ern"), + + wsl: db.collection("wsl"), + lit: db.collection("lit"), + + aut: db.collection("aut"), + } + + if (reset) { + await db.dropDatabase() + await c.usr.createIndexes({ indexes: [...nam, ...nbr] }) + await c.soc.createIndexes({ indexes: [...nam, ...adm, ...sec] }) + await c.agd.createIndexes({ indexes: [...soc] }) + await c.cdt.createIndexes({ indexes: [...rec, ...utc] }) + await c.dbt.createIndexes({ indexes: [...rec] }) + await c.ern.createIndexes({ indexes: [...rec] }) + await c.wsl.createIndexes({ indexes: [...pin] }) + await c.lit.createIndexes({ indexes: [...pin] }) + await c.aut.insertOne({ _id: 1, sup: [1, 2], aut: [2], wsl: [], lit: [] }) + } + + if (dbnam == "ismism") coll = c + return c +} + +export let coll = await db("tst") diff --git a/mongod.service b/mongod.service new file mode 100644 index 0000000..f24bb54 --- /dev/null +++ b/mongod.service @@ -0,0 +1,31 @@ +Description=MongoDB Database Server +Documentation=https://docs.mongodb.org/manual +After=network-online.target +Wants=network-online.target + +[Service] +EnvironmentFile=-/etc/default/mongod +WorkingDirectory=/root/ismism +ExecStart=mongod --config mongod.yaml +PIDFile=/var/run/mongodb/mongod.pid +# file size +LimitFSIZE=infinity +# cpu time +LimitCPU=infinity +# virtual memory size +LimitAS=infinity +# open files +LimitNOFILE=64000 +# processes/threads +LimitNPROC=64000 +# locked memory +LimitMEMLOCK=infinity +# total threads (user+kernel) +TasksMax=infinity +TasksAccounting=false + +# Recommended limits for mongod as specified in +# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings + +[Install] +WantedBy=multi-user.target diff --git a/mongod.yaml b/mongod.yaml new file mode 100644 index 0000000..5a9c633 --- /dev/null +++ b/mongod.yaml @@ -0,0 +1,10 @@ +systemLog: + destination: file + path: log/mongo.log + logAppend: false +storage: + dbPath: db +net: + bindIp: 127.0.0.1, ::1 +setParameter: + notablescan: 1 diff --git a/ont/json.ts b/ont/json.ts new file mode 100644 index 0000000..44fe7a7 --- /dev/null +++ b/ont/json.ts @@ -0,0 +1,7 @@ +export type Json = + | null + | boolean + | number + | string + | Json[] + | { [key: string]: Json }