database basically working
This commit is contained in:
46
server/db/model/network.js
Normal file
46
server/db/model/network.js
Normal file
@@ -0,0 +1,46 @@
|
||||
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()
|
||||
})
|
||||
.strict()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.entries[this.ids[`${this.prefix}-${id}`]]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user