working with db, docker working, small error with personal data

This commit is contained in:
metacryst
2026-03-10 19:09:47 -05:00
parent 4d2c515b7d
commit 7cfcc01c99
25 changed files with 337 additions and 469 deletions

View File

@@ -40,6 +40,7 @@ export default class Conversation {
function populateMemberProfilesFromIDs(ids) {
let result = []
for(let i=0; i<ids.length; i++) {
console.warn("conversation.js 43: This call to global.db needs to be awaited")
result[i] = global.db.members.get(ids[i])
}
return result

View File

@@ -48,6 +48,7 @@ export default class Message {
let entry = this.entries[i]
if(entry.conversation = convoID) {
let userID = entry.from
console.warn("dm.js 51: This call to global.db needs to be awaited")
let fromUser = global.db.members.get(userID)
let newObj = {
...entry

View File

@@ -1,39 +1,16 @@
import { z } from 'zod'
// server/db/model/edges/MEMBER_IN_NETWORK.js
import sql from '../../sql.js';
export default class MEMBER_IN_NETWORK {
prefix = "MEMBER_IN_NETWORK"
indices = null
async getByMember(memberId) {
return await sql`
SELECT * FROM member_network WHERE member_id = ${memberId}
`;
}
constructor(indices) {
this.indices = indices
}
schema = z.object({
id: z.number(),
type: z.string(),
from: z.string(),
to: z.string(),
created: z.string()
})
.strict()
getByMember(stringID) {
let result = []
for(let i = this.indices[0]; i < this.indices[1]; i++) {
if(global.db.edges[i].from === stringID) {
result.push(global.db.edges[i])
}
}
return result
}
getByNetwork(id) {
let result = []
for(let i = this.indices[0]; i < this.indices[1]; i++) {
if(global.db.edges[i].to === `${global.db.networks.prefix}-${id}`) {
result.push(global.db.edges[i])
}
}
return result
}
async getByNetwork(networkId) {
return await sql`
SELECT * FROM member_network WHERE network_id = ${networkId}
`;
}
}

View File

@@ -20,6 +20,7 @@ export default class POST_FROM_NETWORK {
getByNetwork(id) {
let result = []
for(let i = this.indices[0]; i < this.indices[1]; i++) {
console.warn("POST_FROM_NETWORK.js 23: This call to global.db needs to be awaited")
if(global.db.edges[i].to === `${global.db.networks.prefix}-${id}`) {
result.push(global.db.edges[i])
}

View File

@@ -41,6 +41,7 @@ export default class Post {
if (by === "member" && authorId) {
result = this.getByMember(authorId)
} else { // network
console.warn("post.js 44: This call to global.db needs to be awaited")
let { id: networkId } = global.db.networks.getByAbbreviation(forum)
result = this.getByNetwork(networkId)
}
@@ -82,6 +83,7 @@ export default class Post {
async edit(id, text, userEmail) {
try {
console.warn("post.js 86: This call to global.db needs to be awaited")
let userId = global.db.members.getByEmail(userEmail).id
let postToEdit = this.getByID(id)
@@ -105,7 +107,9 @@ export default class Post {
async delete(id, forum, userEmail) {
try {
console.warn("post.js 110: This call to global.db needs to be awaited")
let userId = global.db.members.getByEmail(userEmail).id
console.warn("post.js 110: This call to global.db needs to be awaited")
let network = global.db.networks.getByAbbreviation(forum)
if (this.getAuthor(id) !== userId) {
@@ -134,7 +138,9 @@ export default class Post {
async add(text, forum, userEmail) {
let newPost = {}
console.warn("post.js 141: This call to global.db needs to be awaited")
let sender = global.db.members.getByEmail(userEmail)
console.warn("post.js 143: This call to global.db needs to be awaited")
let network = global.db.networks.getByAbbreviation(forum)
newPost.text = text

View File

@@ -1,7 +1,5 @@
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"
@@ -12,8 +10,6 @@ import POST_BY_MEMBER from "./edges/POST_BY_MEMBER.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],
@@ -28,8 +24,6 @@ let eIndices = {
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),

View File

@@ -2,80 +2,84 @@ import path from 'path';
import fs from 'fs';
import argon2 from 'argon2';
import { z } from 'zod';
import sql from '../sql.js';
export default class Member {
prefix = "MEMBER"
indices = null
schema = z.object({
email: z.string().email(),
firstName: z.string(),
lastName: z.string(),
password: z.string(),
apps: z.array(z.string())
});
constructor(indices) {
this.indices = indices
isHashed = (s) => s.startsWith("$argon2");
async add(newMember, networkId = null) {
if (!this.isHashed(newMember.password)) {
newMember.password = await argon2.hash(newMember.password);
}
schema = z.object({
id: z.number(),
email: z.string().email(),
firstName: z.string(),
lastName: z.string(),
password: z.string(),
apps: z.array(z.string()),
created: z.string()
})
isHashed = (s) => {return s.startsWith("$argon2")}
async add(newMember, network = null) {
const hash = await argon2.hash(newMember.password);
newMember.password = hash
newMember.apps = ["Dashboard"]
try {
global.db.addNode(this.prefix, newMember)
if(network) {
global.db.edge.add({
type: "IN",
from: `${this.prefix}-${global.db.getCurrentIndex(this)}`,
to: "NETWORK-1"
})
}
} catch(e) {
console.error(e)
throw new global.ServerError(400, "Failed to add member!");
}
const result = this.schema.safeParse(newMember);
if (!result.success) {
console.error(result.error);
throw new global.ServerError(400, "Invalid Member Data!");
}
async getPersonalData(id) {
const filePath = path.join(global.db.PERSONAL_DATA_PATH, id, "db.json");
try {
const [member] = await sql`
INSERT INTO members ${sql({
email: newMember.email,
first_name: newMember.firstName,
last_name: newMember.lastName,
password: newMember.password,
apps: newMember.apps ?? ['Dashboard']
})}
RETURNING *
`;
const [raw] = await Promise.all([
fs.promises.readFile(filePath, "utf8"),
]);
if (networkId) {
await sql`
INSERT INTO member_network ${sql({
member_id: member.id,
network_id: networkId,
type: 'IN'
})}
`;
}
const result = raw.trim() ? JSON.parse(raw) : [];
return result
return member;
} catch (e) {
console.error(e);
throw new global.ServerError(400, "Failed to add member!");
}
}
getByNetwork(id) {
let connections = db.MEMBER_IN_NETWORK.getByNetwork(id)
let members = []
connections.forEach((conn) => {
members.push(this.getByID(conn.from))
})
return members
}
async getByID(id) {
const [member] = await sql`
SELECT * FROM members WHERE id = ${id}
`;
return member ?? null;
}
getByID(id) {
if(typeof id === 'string') {
id = id.split("-")[1]
}
return global.db.nodes[this.indices[0] + id - 1]
}
async getByEmail(email) {
const [member] = await sql`
SELECT * FROM members WHERE email = ${email}
`;
return member ?? null;
}
getByEmail(email) {
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
}
async getByNetwork(networkId) {
return await sql`
SELECT m.* FROM members m
JOIN member_network mn ON mn.member_id = m.id
WHERE mn.network_id = ${networkId}
`;
}
async getPersonalData(id) {
const filePath = path.join(global.db.PERSONAL_DATA_PATH, id, "db.json");
const raw = await fs.promises.readFile(filePath, "utf8");
return raw.trim() ? JSON.parse(raw) : [];
}
}

View File

@@ -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;
}
}

View File

@@ -1,47 +0,0 @@
import { z } from 'zod';
export default class Payment {
prefix = "PAYMENT"
indices = null
constructor(indices) {
this.indices = indices
}
schema = z.object({
id: z.number(),
name: z.string(),
email: z.string(),
time: z.string(),
amount: z.number(),
product: z.string(),
})
save(payment) {
let id = `${this.prefix}-${payment.id}`
let result = this.schema.safeParse(payment)
if(result.success) {
try {
super.add(id, payment)
} catch(e) {
console.error(e)
throw e
}
} else {
console.error(result.error)
throw new global.ServerError(400, "Invalid Member Data!");
}
}
add(paymentObj) {
let toSave = {
id: this.entries.length+1,
...paymentObj
}
this.save(toSave)
}
get(id) {
return this.entries[this.ids[`${this.prefix}-${id}`]]
}
}

View File

@@ -1,29 +0,0 @@
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!");
}
}
}