new services + new scripts
This commit is contained in:
@ -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;
|
@ -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;
|
||||
|
Reference in New Issue
Block a user