working with db, docker working, small error with personal data
This commit is contained in:
@@ -1,78 +1,61 @@
|
||||
import { z } from 'zod';
|
||||
import sql from '../sql.js';
|
||||
|
||||
export default class Network {
|
||||
prefix = `NETWORK`
|
||||
indices = null
|
||||
|
||||
constructor(indices) {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
export default class Network {
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string(),
|
||||
apps: z.array(z.string()),
|
||||
logo: z.string(),
|
||||
abbreviation: z.string(),
|
||||
created: z.string(),
|
||||
stripeAccountId: z.string().nullable(),
|
||||
stripeAccessToken: z.string().nullable()
|
||||
})
|
||||
.strict()
|
||||
}).strict();
|
||||
|
||||
get(id) {
|
||||
if(id.length > 1) {
|
||||
id = id.split("-")[1]
|
||||
}
|
||||
let index = this.indices[0] + (id - 1)
|
||||
return structuredClone(global.db.nodes[index])
|
||||
async get(id) {
|
||||
const [network] = await sql`
|
||||
SELECT * FROM networks WHERE id = ${id}
|
||||
`;
|
||||
return network ?? null;
|
||||
}
|
||||
|
||||
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
|
||||
async getByAbbreviation(abbreviation) {
|
||||
const [network] = await sql`
|
||||
SELECT * FROM networks WHERE abbreviation = ${abbreviation}
|
||||
`;
|
||||
return network ?? null;
|
||||
}
|
||||
|
||||
update(id, data) {
|
||||
if(data.id || data.created) {
|
||||
throw new Error("Can't update node id or created time!")
|
||||
}
|
||||
let currentObject = this.get(id)
|
||||
let newObject = {...currentObject, ...data}
|
||||
|
||||
try {
|
||||
global.db.updateNode(this.prefix, newObject.id, newObject)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw new global.ServerError(400, "Failed to add member!");
|
||||
async update(id, data) {
|
||||
if (data.id || data.created) {
|
||||
throw new Error("Can't update id or created time!");
|
||||
}
|
||||
|
||||
const [updated] = await sql`
|
||||
UPDATE networks SET ${sql(data, ...Object.keys(data))}
|
||||
WHERE id = ${id}
|
||||
RETURNING *
|
||||
`;
|
||||
return updated;
|
||||
}
|
||||
|
||||
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!");
|
||||
async add(n) {
|
||||
let result = this.schema.safeParse(n);
|
||||
if (!result.success) {
|
||||
console.error(result.error);
|
||||
throw new global.ServerError(400, "Invalid Network Data!");
|
||||
}
|
||||
}
|
||||
|
||||
add(n) {
|
||||
let toSave = {
|
||||
id: this.entries.length+1,
|
||||
...n
|
||||
}
|
||||
this.save(toSave)
|
||||
const [network] = await sql`
|
||||
INSERT INTO networks ${sql({
|
||||
name: n.name,
|
||||
apps: n.apps,
|
||||
logo: n.logo,
|
||||
abbreviation: n.abbreviation,
|
||||
stripe_account_id: n.stripeAccountId,
|
||||
stripe_access_token: n.stripeAccessToken
|
||||
})}
|
||||
RETURNING *
|
||||
`;
|
||||
return network;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user