-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (85 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import fs from 'fs'
import { isNull, isNumber, isObject, isArray, isDate, isBoolean, isFunction, isString } from 'util'
import * as types from './constants/types.js'
const { ID, NAN, NUMBER, NULL, DATE, OBJECT, STRING, ARRAY } = types
let dbName = './db.json'
const getFile = async () => {
let db = await fs.readFileSync(dbName)
let parsedData = JSON.parse(db)
return parsedData
}
const writeFile = async (data) => {
await fs.writeFileSync(dbName, data)
}
const isCollectionExists = (db, collectionName) => Boolean(db[collectionName])
const createCollection = async (collectionName) => {
let db = await getFile()
if (isCollectionExists(db, collectionName)) {
return 0
} else {
db[collectionName] = []
let stringified = JSON.stringify(db)
await writeFile(stringified)
return 1
}
}
createCollection('orders')
const isID = (el1) => el1.startsWith(ID)
const checkSchema = (schema, data, key) => {
let schemaKeys = Object.keys(schema)
const makeError = (el, type) => {
throw new Error(`el ${el} of ${type} schema error`)
}
let newData = schema;
console.log(schema)
schemaKeys.map(key => {
let type = schema[key]
let el = data[key]
switch (type) {
case ID:
if (isID(el)) {
newData[key] = el
} else {
makeError(el, type)
}
break;
case STRING:
if (isString(el)) {
newData[key] = el
} else {
makeError(el, type)
}
default:
return;
// makeError(el, type)
}
})
return newData;
}
let obj = checkSchema(
{
$id: ID,
name: STRING
}
, {
$id: 'ID1',
name: 'anand'
})
console.log(obj)
class Model {
constructor(name, structure) {
this.name = name;
this.structure = structure;
this.data = {}
}
create(data) {
}
}
const animalSchema = new Model('Animal', {
$id: ID,
name: STRING,
})
// animalSchema.create({
// name: 'jerry',
// $id: ID + '1'
// })