Files
frm.so/server/db/model/network.js
matiasc18 aaf9d56b1b CRUD operations for Posts
- added all CRUD operations for Post model
- add() also creates POST_BY_MEMBER and POST_FROM_NETWORK edges
- delete() also deletes associated edges
- delete() currently does not reallocate node/edge indices after deleting
- will write the data to the db.json file and global.db.nodes
2026-02-08 22:34:18 -05:00

59 lines
1.3 KiB
JavaScript

import { z } from 'zod';
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(),
abbreviation: z.string(),
created: z.string()
})
.strict()
get(stringID) {
let id = stringID.split("-")[1]
let index = this.indices[0] + (id - 1)
return global.db.nodes[index]
}
getByAbbreviation(abbreviation) {
for(let i=this.indices[0]; i<this.indices[1]; i++) {
if(global.db.nodes[i].abbreviation === abbreviation) {
return global.db.nodes[i]
}
}
return null
}
save(n) {
let id = `${this.prefix}-${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!");
}
}
add(n) {
let toSave = {
id: this.entries.length+1,
...n
}
this.save(toSave)
}
}