Skip to content

Commit ee6f0cd

Browse files
committed
Created dango, model and schema files. Began work on query schema functions.
1 parent 0e115ad commit ee6f0cd

9 files changed

+167
-316
lines changed

types_ts_readme.md renamed to datatypes_ts_readme.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@ Notes:
1515
- Methods on Each:
1616
- ConvertToType - Attempts to convert the input to the given data type. Returns undefined if not possible. Null remains null.
1717
- ValidateType - If a type conversion is possible, returns true. If undefined is the input returns false.
18-
- ValidateConstraints - General function that accepts a callback and will validate whether the raw input ran through the callback will return true or false.
18+
19+
TO BE REMOVED - Will move to Query Class
20+
- ValidateConstraints - General function that accepts a callback and will validate whether the raw input ran through the callback will return true or false.
1921

2022
Issues:
21-
- Unclear right now is Classes for each Schema type is the correct path. It is also unclear of how these will relate to the overall schema that we generate. We may move some of these methods around or create new ones as it is fleshed out.
23+
~~- Unclear right now is Classes for each Schema type is the correct path. It is also unclear of how these will relate to the overall schema that we generate. We may move some of these methods around or create new ones as it is fleshed out.~~
2224
- Some primitive data types like number, string, booleans may need additional logic to accomodate their object forms.
2325
- No nested or complex data types like objects/arrays are current represented
2426
- No Symbols as of now
2527
- No Int32
2628
- We can split this file per data type if it becomes cumbersome
27-
- Export structure not entirely clear.
29+
~~- Export structure not entirely clear.~~
2830

2931
Complete:
3032
- Number
3133
- Decimal128
3234
- String
3335
- Boolean
34-
35-
TODO:
3636
- ObjectID
3737
- UUID
3838
- Date
39+
40+
TODO:
3941
- Full testing
40-
- Exports
4142
- Comments

lib/dango.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// deno-lint-ignore-file no-explicit-any
2+
3+
/**
4+
*
5+
* @description This file exports the main dango object.
6+
*
7+
*/
8+
9+
import { Connection } from './connections.ts';
10+
import { model } from './model.ts';
11+
import { Schema } from './schema.ts';
12+
13+
class Dango {
14+
15+
currentConnection: boolean | Connection
16+
model: typeof model;
17+
18+
constructor() {
19+
this.currentConnection = false;
20+
this.model = model;
21+
}
22+
23+
connect(connectionString: string) {
24+
this.currentConnection = new Connection(connectionString)
25+
this.currentConnection.connect()
26+
return this.currentConnection;
27+
}
28+
29+
disconnect() {
30+
if (typeof this.currentConnection === 'boolean') {
31+
if (this.currentConnection === false) {
32+
throw new Error('No database connection exists to disconnect.');
33+
}
34+
} else {
35+
this.currentConnection.disconnect();
36+
this.currentConnection = false;
37+
return;
38+
}
39+
}
40+
41+
schema(schemaObj: Record<string, any>) {
42+
return new Schema(schemaObj);
43+
}
44+
}
45+
46+
const dango: Dango = new Dango();
47+
48+
export { dango };

lib/datatypes.ts

+3-17
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ export class SchemaNumber {
6161
this.valid = this.convertedValue === undefined ? false : true;
6262
return this.valid;
6363
}
64-
65-
validateConstraints(callback: ((arg: any) => boolean)): boolean {
66-
if (typeof callback !== 'function') return false;
67-
return callback(this.value)
68-
}
6964
}
7065

7166
export class SchemaDecimal128 {
@@ -119,11 +114,6 @@ export class SchemaDecimal128 {
119114
this.valid = this.convertedValue === undefined ? false : true;
120115
return this.valid;
121116
}
122-
123-
validateConstraints(callback: ((arg: any) => boolean)): boolean {
124-
if (typeof callback !== 'function') return false;
125-
return callback(this.value)
126-
}
127117
}
128118

129119
export class SchemaString {
@@ -175,11 +165,6 @@ export class SchemaString {
175165
this.valid = this.convertedValue === undefined ? false : true;
176166
return this.valid;
177167
}
178-
179-
validateConstraints(callback: ((arg: any) => boolean)): boolean {
180-
if (typeof callback !== 'function') return false;
181-
return callback(this.value)
182-
}
183168
}
184169

185170
export class SchemaBoolean {
@@ -268,7 +253,8 @@ export class SchemaObjectId {
268253
}
269254

270255
else if (Bson.ObjectId.isValid(this.value)) {
271-
this.convertedValue = this.value;
256+
//TODO
257+
this.convertedValue = new Bson.ObjectId(this.value);
272258
}
273259

274260
return this.convertedValue;
@@ -312,7 +298,7 @@ export class SchemaUUID {
312298
}
313299

314300
else if (Bson.UUID.isValid(this.value)) {
315-
this.convertedValue = this.value;
301+
this.convertedValue = new Bson.UUID(this.value);
316302
}
317303

318304
return this.convertedValue;

lib/model.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// deno-lint-ignore-file no-explicit-any
2+
3+
import { Query } from './query.ts';
4+
import { Schema } from './schema.ts';
5+
6+
export function model (dbName: string, schema: Record<string, any>) {
7+
return new Model(dbName, schema);
8+
}
9+
10+
class Model extends Query {
11+
dbName: string;
12+
schema: Schema;
13+
14+
constructor(dbName: string, schema: Schema) {
15+
// super(dbName, schema)
16+
super(dbName);
17+
this.dbName = dbName;
18+
this.schema = schema;
19+
}
20+
}

lib/query.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Query {
1717
public collectionName: string;
1818
// Refactor how connection is brought in
1919
public connection: Connection;
20-
20+
// We need to add schema to the collection
2121
constructor(collectionName: string) {
2222
this.collectionName = collectionName;
2323
this.connection = new Connection(

lib/queryfunctions.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// deno-lint-ignore-file no-explicit-any
2+
3+
import { optionsObject } from './schema.ts'
4+
5+
function checkDataFields(queryObject: Record<string, any>) {
6+
for (const property in queryObject) {
7+
if (Object.prototype.hasOwnProperty.call(this.schema.schemaMap, property) {
8+
throw new Error ('Requested query object contains properties not present in the Schema.')
9+
}
10+
}
11+
return;
12+
}
13+
14+
function checkRequired(property: null | Record<string, any>, propertyName: string, propertyOptions: optionsObject) {
15+
if (propertyOptions.required === true) {
16+
if (property === null) {
17+
throw new Error (`${propertyName} is Required by the Schema.`)
18+
}
19+
}
20+
return true;
21+
}
22+
23+
function setDefault(queryObject: Record<string, any>, propertyName: string, propertyOptions: optionsObject) {
24+
if (!Object.prototype.hasOwnProperty.call(queryObject, propertyName)) {
25+
queryObject[propertyName] = propertyOptions.default;
26+
}
27+
return true;
28+
}
29+
30+
populateQuery()
31+
32+
33+
checkUnique() {
34+
35+
}
36+
37+
checkConstraints(callback: ((arg: any) => boolean)): boolean {
38+
if (typeof callback !== 'function') return false;
39+
return callback(this.value)
40+
}
41+
42+
validateAgainstSchema() {
43+
44+
}

lib/schema.ts

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
// deno-lint-ignore-file no-explicit-any
2-
// deno-lint-allow-any-bracket-notation
32

43
/**
54
*
65
* @description This file defines the schema class.
76
*
87
*/
98

10-
import { SchemaNumber, SchemaDecimal128, SchemaString, SchemaBoolean, SchemaObjectId, SchemaUUID, SchemaDate } from './datatypes.ts'
9+
import {
10+
SchemaNumber,
11+
SchemaDecimal128,
12+
SchemaString, SchemaBoolean,
13+
SchemaObjectId,
14+
SchemaUUID,
15+
SchemaDate
16+
} from './datatypes.ts'
1117

1218
export class Schema {
1319

1420
schemaMap: Record<string, any>;
21+
types: Record<string, any>
1522

1623
constructor(schemaObj: Record<string, any>) {
24+
this.types = {
25+
number: SchemaNumber,
26+
decimal128: SchemaDecimal128,
27+
string: SchemaString,
28+
boolean: SchemaBoolean,
29+
objectid: SchemaObjectId,
30+
UUID: SchemaUUID,
31+
date: SchemaDate,
32+
}
33+
1734
this.schemaMap = {};
1835
for (const property in schemaObj) {
1936
if (typeof schemaObj[property] === 'object') {
@@ -25,22 +42,6 @@ export class Schema {
2542
}
2643
}
2744
}
28-
29-
isUnique() {
30-
31-
}
32-
33-
isRequired() {
34-
35-
}
36-
37-
setDefault() {
38-
39-
}
40-
41-
isValid() {
42-
43-
}
4445
}
4546

4647

@@ -73,18 +74,4 @@ class SchemaOptions {
7374
}
7475
}
7576
}
76-
}
77-
78-
const type = {
79-
number: SchemaNumber,
80-
decimal128: SchemaDecimal128,
81-
string: SchemaString,
82-
boolean: SchemaBoolean,
83-
objectid: SchemaObjectId,
84-
UUID: SchemaUUID,
85-
date: SchemaDate,
86-
}
87-
88-
name {
89-
type: dango.type.number
9077
}

0 commit comments

Comments
 (0)