new services + new scripts

This commit is contained in:
2022-10-14 18:49:09 +02:00
parent 8866784d49
commit 5538a042df
23 changed files with 200 additions and 126 deletions

View File

@ -1,15 +0,0 @@
import { MongoClient, Db } from 'mongodb';
class Mongo {
private client: MongoClient;
constructor(uri: string) {
this.client = new MongoClient(uri);
}
getDb(dbName: string): Db {
return this.client.db(dbName);
}
}
export default Mongo;

View File

@ -1,28 +1,22 @@
import { Collection, Db } from 'mongodb';
import { User } from '../../entities/user';
import { Services } from '../express/server';
export function Create(
services: Services,
): (tracker: string, user: User) => Promise<User> {
const { db } = services;
const coll = db.collection<User>('users');
const readUser = Read(services);
class UserModel {
private collection: Collection<User>;
return async (tracker, user) => {
await coll.insertOne(user);
return readUser(tracker, user.uuid);
};
}
constructor(db: Db) {
this.collection = db.collection<User>('users');
}
export function Read(
services: Services,
): (tracker: string, uuid: string) => Promise<User> {
const { db } = services;
const coll = db.collection<User>('users');
return async (tracker, uuid) => {
const user = await coll.findOne({ uuid });
public async create(tracker: string, user: User) {
await this.collection.insertOne(user);
return this.read(tracker, user.uuid);
}
public async read(tracker: string, uuid: string) {
const user = await this.collection.findOne({ uuid });
return new User(user || { name: 'not found' });
};
}
}
export default UserModel;