database basically working
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
export default class OrderedObject {
|
||||
entries = []
|
||||
ids = {}
|
||||
indexes = []
|
||||
|
||||
add(id, data) {
|
||||
if(this.ids[id]) {
|
||||
console.error(`Can't add item ${id}: id already exists`)
|
||||
throw new global.ServerError(400, `Member with this email already exists`)
|
||||
}
|
||||
this.entries.push(data)
|
||||
this.ids[id] = this.entries.length - 1
|
||||
}
|
||||
|
||||
update(id, data) {
|
||||
let index = this.ids[id]
|
||||
this.entries[index] = data
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
if (typeof key === "number") {
|
||||
return this.entries[key]
|
||||
} else {
|
||||
return this.entries[this.ids[key]]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
|
||||
export default class Titles extends OrderedObject {
|
||||
|
||||
save(newTitle) {
|
||||
let id = `HY-${this.entries.length+1}`
|
||||
if(this.validate(id, newTitle)) {
|
||||
try {
|
||||
super.add(id, newTitle)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
validate(id, node) {
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split.length === 2
|
||||
&& split[0] === "HY"
|
||||
&& !isNaN(Number(split[1]))
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
let fields = [
|
||||
"fullName",
|
||||
]
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Title ${id} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Tokens extends OrderedObject {
|
||||
|
||||
schema = z.object({
|
||||
index: z.number(),
|
||||
url: z.string(),
|
||||
uuid: z.string().regex(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
||||
"Invalid UUID"
|
||||
),
|
||||
used: z.boolean(),
|
||||
})
|
||||
|
||||
markUsed(uuid) {
|
||||
let token = this.get(uuid)
|
||||
token.used = true
|
||||
super.update(`TOKEN-${uuid}`, token)
|
||||
}
|
||||
|
||||
save(token) {
|
||||
let id = `TOKEN-${token.uuid}`
|
||||
let result = this.schema.safeParse(token)
|
||||
if(result.success) {
|
||||
try {
|
||||
super.add(id, token)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
console.error(result.error)
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
get(uuid) {
|
||||
return this.entries[this.ids[`TOKEN-${uuid}`]]
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
import OrderedObject from "../OrderedObject.js"
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Conversations extends OrderedObject {
|
||||
export default class Conversation {
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
@@ -1,7 +1,11 @@
|
||||
import OrderedObject from "../OrderedObject.js"
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Messages extends OrderedObject {
|
||||
export default class Message {
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
36
server/db/model/edge.js
Normal file
36
server/db/model/edge.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Edge {
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
from: z.string(),
|
||||
to: z.string(),
|
||||
created: z.string()
|
||||
})
|
||||
.strict()
|
||||
|
||||
save(n, id) {
|
||||
let result = this.schema.safeParse(n)
|
||||
if(result.success) {
|
||||
try {
|
||||
super.add(id, n)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
console.error(result.error)
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
getByFrom(fromID) {
|
||||
let result = []
|
||||
for(let i = 0; i < this.entries.length; i++) {
|
||||
if(entries[i].from === fromID) {
|
||||
// let
|
||||
}
|
||||
}
|
||||
console.log(fromID)
|
||||
}
|
||||
}
|
||||
17
server/db/model/edges/MemberInNetwork.js
Normal file
17
server/db/model/edges/MemberInNetwork.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
export default class MemberInNetwork {
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
from: z.string(),
|
||||
to: z.string(),
|
||||
created: z.string()
|
||||
})
|
||||
.strict()
|
||||
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
import OrderedObject from "../OrderedObject.js"
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Posts extends OrderedObject {
|
||||
export default class Post {
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
schema = z.object({
|
||||
text: z.string(),
|
||||
time: z.string(),
|
||||
36
server/db/model/import.js
Normal file
36
server/db/model/import.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import Title from "./title.js"
|
||||
import Network from "./network.js"
|
||||
import Member from './member.js'
|
||||
import Payment from "./payment.js"
|
||||
import Post from "./forum/post.js"
|
||||
import Conversation from "./dms/conversation.js"
|
||||
import DM from "./dms/dm.js"
|
||||
import MemberInNetwork from "./edges/MemberInNetwork.js"
|
||||
|
||||
let nIndices = {
|
||||
"MEMBER" : [0, 0], // [id, startIndex, nextEmptyIndex
|
||||
"NETWORK" : [100, 100],
|
||||
"TITLE" : [200, 200],
|
||||
"PAYMENT" : [300, 300],
|
||||
"POST" : [400, 400],
|
||||
"CONVERSATION" : [3000, 3000],
|
||||
"DM" : [6000, 6000],
|
||||
}
|
||||
|
||||
let eIndices = {
|
||||
"MEMBER_IN_NETWORK": [0, 0]
|
||||
}
|
||||
|
||||
export let nodeModels = {
|
||||
Member: new Member(nIndices.MEMBER),
|
||||
Network: new Network(nIndices.NETWORK),
|
||||
Title: new Title(nIndices.TITLE),
|
||||
Payment: new Payment(nIndices.PAYMENT),
|
||||
Post: new Post(nIndices.POST),
|
||||
Conversation: new Conversation(nIndices.CONVERSATION),
|
||||
DM: new DM(nIndices.DM),
|
||||
}
|
||||
|
||||
export let edgeModels = {
|
||||
MEMBER_IN_NETWORK: new MemberInNetwork(eIndices.MEMBER_IN_NETWORK)
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
import argon2 from 'argon2';
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Members extends OrderedObject {
|
||||
export default class Member {
|
||||
prefix = "MEMBER"
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
addressSchema = z.object({
|
||||
address1: z.string(),
|
||||
address2: z.string().optional(),
|
||||
@@ -21,14 +27,13 @@ export default class Members extends OrderedObject {
|
||||
"Invalid UUID"
|
||||
),
|
||||
joined: z.string(),
|
||||
address: this.addressSchema,
|
||||
networks: z.array(z.number())
|
||||
address: this.addressSchema
|
||||
})
|
||||
|
||||
isHashed = (s) => {return s.startsWith("$argon2")}
|
||||
|
||||
save(member) {
|
||||
let id = `MEMBER-${member.id}`
|
||||
let id = `${this.prefix}-${member.id}`
|
||||
let result = this.schema.safeParse(member)
|
||||
if(result.success) {
|
||||
try {
|
||||
@@ -57,9 +62,9 @@ export default class Members extends OrderedObject {
|
||||
}
|
||||
|
||||
getByEmail(email) {
|
||||
for(let i=0; i<this.entries.length; i++) {
|
||||
if(this.entries[i].email === email) {
|
||||
return this.entries[i]
|
||||
for(let i=this.indices[0]; i<this.indices[1]; i++) {
|
||||
if(global.db.nodes[i].email === email) {
|
||||
return global.db.nodes[i]
|
||||
}
|
||||
}
|
||||
return null
|
||||
@@ -1,13 +1,20 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Networks extends OrderedObject {
|
||||
export default class Network {
|
||||
prefix = `NETWORK`
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string(),
|
||||
apps: z.array(z.string()),
|
||||
logo: z.string()
|
||||
})
|
||||
.strict()
|
||||
|
||||
save(n) {
|
||||
let id = `${this.prefix}-${n.id}`
|
||||
@@ -1,7 +1,12 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Payments extends OrderedObject {
|
||||
export default class Payment {
|
||||
prefix = "PAYMENT"
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
@@ -13,7 +18,7 @@ export default class Payments extends OrderedObject {
|
||||
})
|
||||
|
||||
save(payment) {
|
||||
let id = `PAYMENT-${payment.id}`
|
||||
let id = `${this.prefix}-${payment.id}`
|
||||
let result = this.schema.safeParse(payment)
|
||||
if(result.success) {
|
||||
try {
|
||||
@@ -37,6 +42,6 @@ export default class Payments extends OrderedObject {
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.entries[this.ids[`PAYMENT-${id}`]]
|
||||
return this.entries[this.ids[`${this.prefix}-${id}`]]
|
||||
}
|
||||
}
|
||||
29
server/db/model/title.js
Normal file
29
server/db/model/title.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Title {
|
||||
prefix = `TITLE`
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string()
|
||||
})
|
||||
|
||||
save(newTitle) {
|
||||
let id = `HY-${this.entries.length+1}`
|
||||
if(this.validate(id, newTitle)) {
|
||||
try {
|
||||
global.db.add(id, newTitle)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user