aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/Message.ts42
-rw-r--r--src/server/ServerUtil.ts60
-rw-r--r--src/server/database.ts60
-rw-r--r--src/server/index.ts20
4 files changed, 156 insertions, 26 deletions
diff --git a/src/server/Message.ts b/src/server/Message.ts
index 44df7be1c..0391b6671 100644
--- a/src/server/Message.ts
+++ b/src/server/Message.ts
@@ -1,10 +1,10 @@
import { Utils } from "../Utils";
import { FIELD_ID, Field } from "../fields/Field";
+import { ObjectId } from "bson";
export class Message<T> {
private name: string;
private guid: string;
- readonly ArgsCtor: new (...args: any) => T;
get Name(): string {
return this.name;
@@ -14,10 +14,9 @@ export class Message<T> {
return this.guid
}
- constructor(name: string, ctor: new (...args: any) => T) {
+ constructor(name: string) {
this.name = name;
this.guid = Utils.GenerateDeterministicGuid(name)
- this.ArgsCtor = ctor;
}
GetValue() {
@@ -53,70 +52,73 @@ export enum Types {
export class DocumentTransfer implements Transferable {
readonly type = Types.Document
+ _id: ObjectId;
- constructor(readonly id: string) { }
+ constructor(readonly obj: { type: Types, data: [string, string][], _id: ObjectId }) {
+ this._id = obj._id
+ }
}
export class ImageTransfer implements Transferable {
readonly type = Types.Image
- constructor(readonly id: string) { }
+ constructor(readonly _id: ObjectId) { }
}
export class KeyTransfer implements Transferable {
name: string
- readonly id: string
+ readonly _id: ObjectId
readonly type = Types.Key
constructor(i: string, n: string) {
this.name = n
- this.id = i
+ this._id = new ObjectId(i)
}
}
export class ListTransfer implements Transferable {
type = Types.List;
- constructor(readonly id: string) { }
+ constructor(readonly _id: ObjectId) { }
}
export class NumberTransfer implements Transferable {
readonly type = Types.Number
- constructor(readonly value: number, readonly id: string) { }
+ constructor(readonly value: number, readonly _id: ObjectId) { }
}
export class TextTransfer implements Transferable {
value: string
- readonly id: string
+ readonly _id: ObjectId
readonly type = Types.Text
constructor(t: string, i: string) {
this.value = t
- this.id = i
+ this._id = new ObjectId(i)
}
}
export class RichTextTransfer implements Transferable {
value: string
- readonly id: string
+ readonly _id: ObjectId
readonly type = Types.Text
constructor(t: string, i: string) {
this.value = t
- this.id = i
+ this._id = new ObjectId(i)
}
}
-interface Transferable {
- readonly id: string
+export interface Transferable {
+ readonly _id: ObjectId
readonly type: Types
}
export namespace MessageStore {
- export const Foo = new Message("Foo", String);
- export const Bar = new Message("Bar", String);
- export const AddDocument = new Message("Add Document", TestMessageArgs);
- export const SetField = new Message("Set Field", SetFieldArgs)
- export const GetField = new Message("Get Field", GetFieldArgs)
+ export const Foo = new Message<string>("Foo");
+ export const Bar = new Message<string>("Bar");
+ export const AddDocument = new Message<DocumentTransfer>("Add Document");
+ export const SetField = new Message<{ _id: ObjectId, data: any, type: Types }>("Set Field")
+ export const GetField = new Message<GetFieldArgs>("Get Field")
} \ No newline at end of file
diff --git a/src/server/ServerUtil.ts b/src/server/ServerUtil.ts
new file mode 100644
index 000000000..6757615fb
--- /dev/null
+++ b/src/server/ServerUtil.ts
@@ -0,0 +1,60 @@
+import { Field } from './../fields/Field';
+import { TextField } from './../fields/TextField';
+import { NumberField } from './../fields/NumberField';
+import { RichTextField } from './../fields/RichTextField';
+import { Key } from './../fields/Key';
+import { ImageField } from './../fields/ImageField';
+import { ListField } from './../fields/ListField';
+import { Document } from './../fields/Document';
+import { Server } from './../client/Server';
+import { Types } from './Message';
+import { Utils } from '../Utils';
+
+export class ServerUtils {
+ public static FromJson(json: string): Field {
+ let obj = JSON.parse(json)
+ let data: any = obj.data
+ let id: string = obj.id
+ let type: Types = obj.type
+
+ if (!(data && id && type != undefined)) {
+ console.log("how did you manage to get an object that doesn't have a data or an id?")
+ return new TextField("Something to fill the space", Utils.GenerateGuid());
+ }
+
+ switch (type) {
+ case Types.Number:
+ return new NumberField(data, id)
+ case Types.Text:
+ return new TextField(data, id)
+ case Types.RichText:
+ return new RichTextField(data, id)
+ case Types.Key:
+ return new Key(data, id)
+ case Types.Image:
+ return new ImageField(data, id)
+ case Types.List:
+ return new ListField(data, id)
+ case Types.Document:
+ let doc: Document = new Document(id)
+ let fields: [string, string][] = data as [string, string][]
+ fields.forEach(element => {
+ doc._proxies.set(element[0], element[1]);
+ let keyId: string = element[0]
+ let valueId: string = element[1]
+ Server.GetField(keyId, (key: Field) => {
+ if (key instanceof Key) {
+ Server.GetField(valueId, (field: Field) => {
+ doc.Set(key as Key, field)
+ })
+ }
+ else {
+ console.log("how did you get a key that isnt a key wtf")
+ }
+ })
+ });
+ return doc
+ }
+ return new TextField(data, id)
+ }
+} \ No newline at end of file
diff --git a/src/server/database.ts b/src/server/database.ts
new file mode 100644
index 000000000..72ddbc82c
--- /dev/null
+++ b/src/server/database.ts
@@ -0,0 +1,60 @@
+import { action, configure } from 'mobx';
+import * as mongodb from 'mongodb';
+import { ObjectID } from 'mongodb';
+import { Transferable } from './Message';
+import { Utils } from '../Utils';
+
+export class Database {
+ public static Instance = new Database()
+ private MongoClient = mongodb.MongoClient;
+ private url = 'mongodb://localhost:27017/Dash';
+
+ public update(id: mongodb.ObjectID, value: any) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.update({ _id: id }, { $set: value });
+ db.close();
+ });
+ }
+
+ public delete(id: string) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.remove({ _id: id });
+ db.close();
+ });
+ }
+
+ public insert(kvpairs: any) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.insertOne(kvpairs, () => { });
+ db.close();
+ });
+ }
+
+ public getDocument(id: mongodb.ObjectID): string | undefined {
+ var result: JSON;
+ this.MongoClient.connect(this.url, (err, db) => {
+ if (err) {
+ console.log(err)
+ return undefined
+ }
+ let collection = db.db().collection('documents');
+ collection.findOne({ _id: Utils.GenerateDeterministicGuid(id.toHexString()) }, (err: any, res: any) => result = res)
+ console.log(result)
+ db.close();
+ if (!result) {
+ console.log("not found")
+ return undefined
+ }
+ console.log("found")
+ return result;
+ });
+ return undefined
+ }
+
+ public print() {
+ console.log("db says hi!")
+ }
+}
diff --git a/src/server/index.ts b/src/server/index.ts
index f5a0fcfe2..98d897d2f 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -4,13 +4,15 @@ import * as webpack from 'webpack'
import * as wdm from 'webpack-dev-middleware';
import * as whm from 'webpack-hot-middleware';
import * as path from 'path'
-import { MessageStore, Message, SetFieldArgs, GetFieldArgs } from "./Message";
+import { MessageStore, Message, SetFieldArgs, GetFieldArgs, Transferable } from "./Message";
import { Client } from './Client';
import { Socket } from 'socket.io';
import { Utils } from '../Utils';
import { ObservableMap } from 'mobx';
import { FIELD_ID, Field } from '../fields/Field';
import { Database } from './database';
+import { ServerUtils } from './ServerUtil';
+import { ObjectID } from 'mongodb';
const config = require('../../webpack.config')
const compiler = webpack(config)
const port = 1050; // default port to listen
@@ -63,16 +65,22 @@ function addDocument(document: Document) {
}
-function setField(newValue: SetFieldArgs) {
- Database.Instance.update(newValue.field, newValue.value)
- if (FieldStore.get(newValue.field)) {
- FieldStore.get(newValue.field)!.TrySetValue(newValue.value);
+function setField(newValue: Transferable) {
+ console.log(newValue._id)
+ if (Database.Instance.getDocument(newValue._id)) {
+ Database.Instance.update(newValue._id, newValue)
+ }
+ else {
+ Database.Instance.insert(newValue)
}
}
function getField([fieldRequest, callback]: [GetFieldArgs, (field: Field) => void]) {
let fieldid: string = fieldRequest.field
- callback(FieldStore.get(fieldid) as Field)
+ let result: string | undefined = Database.Instance.getDocument(new ObjectID(fieldid))
+ if (result) {
+ let fromJson: Field = ServerUtils.FromJson(result)
+ }
}
server.listen(serverPort);