-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (158 loc) · 5.56 KB
/
index.js
File metadata and controls
189 lines (158 loc) · 5.56 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"use strict";
// Dependencies
const crypto = require("crypto")
const path = require("path")
const fs = require("fs")
// Main
class Collection {
constructor(collectionPath) {
this.collectionPath = collectionPath
if (!this.collectionPath.endsWith(".json")) this.collectionPath += ".json"
if (!fs.existsSync(this.collectionPath)) fs.writeFileSync(this.collectionPath, JSON.stringify([], null, 2), "utf8")
}
_read() {
try {
const data = fs.readFileSync(this.collectionPath, "utf8")
return JSON.parse(data)
} catch {
return []
}
}
_write(data) {
fs.writeFileSync(this.collectionPath, JSON.stringify(data, null, 2), "utf8")
}
_match(doc, query) {
if (!query || !Object.keys(query).length) return true
for (const [key, value] of Object.entries(query)) if (doc[key] !== value) return false
return true
}
_applyUpdate(doc, update) {
// Variables
const hasOperators = Object.keys(update).some((k) => k.startsWith("$"))
const updateSet = update.$set || (!hasOperators ? update : null)
// Core
if (updateSet) for (const [key, value] of Object.entries(updateSet)) if (key !== "_id") doc[key] = value
if (update.$push) {
for (const [key, value] of Object.entries(update.$push)) {
if (!Array.isArray(doc[key])) doc[key] = []
if (value && typeof value === "object" && Array.isArray(value.$each)) {
doc[key].push(...value.$each)
} else {
doc[key].push(value)
}
}
}
if (update.$pull) {
for (const [key, value] of Object.entries(update.$pull)) {
if (Array.isArray(doc[key])) {
doc[key] = doc[key].filter((item) => {
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
return !this._match(item, value)
} else {
return item !== value
}
})
}
}
}
}
insertOne(doc) {
const data = this._read()
const newDoc = { _id: crypto.randomUUID(), ...doc }
data.push(newDoc)
this._write(data)
return { acknowledged: true, insertedId: newDoc._id, doc: newDoc }
}
insertMany(docs) {
// Variables
if (!Array.isArray(docs)) throw new Error("insertMany requires an array of documents.")
const data = this._read()
const newDocs = docs.map((doc) => ({ _id: crypto.randomUUID(), ...doc }))
// Core
data.push(...newDocs)
this._write(data)
return { acknowledged: true, insertedCount: newDocs.length, insertedIds: newDocs.map(d => d._id) }
}
findOne(query = {}) {
const data = this._read()
const found = data.find((doc) => this._match(doc, query))
return found || null
}
findMany(query = {}) {
const data = this._read()
return data.filter((doc) => this._match(doc, query))
}
updateOne(query, update) {
// Variables
const data = this._read()
const index = data.findIndex((doc) => this._match(doc, query))
// Core
if (index !== -1) {
this._applyUpdate(data[index], update)
this._write(data)
return { acknowledged: true, matchedCount: 1, modifiedCount: 1 }
}
return { acknowledged: true, matchedCount: 0, modifiedCount: 0 }
}
updateMany(query, update) {
// Variables
const data = this._read()
var modifiedCount = 0
var matchedCount = 0
// Core
for (var i = 0; i < data.length; i++) {
if (this._match(data[i], query)) {
matchedCount++
this._applyUpdate(data[i], update)
modifiedCount++
}
}
if (modifiedCount > 0) this._write(data)
return { acknowledged: true, matchedCount, modifiedCount }
}
deleteOne(query) {
// Variables
const data = this._read()
const index = data.findIndex((doc) => this._match(doc, query))
// Core
if (index !== -1) {
data.splice(index, 1)
this._write(data)
return { acknowledged: true, deletedCount: 1 }
}
return { acknowledged: true, deletedCount: 0 }
}
deleteMany(query) {
// Variables
const data = this._read()
const initialLength = data.length
const newData = data.filter((doc) => !this._match(doc, query))
const deletedCount = initialLength - newData.length
// Core
if (deletedCount > 0) this._write(newData)
return { acknowledged: true, deletedCount }
}
}
class Database {
constructor(dbPath) {
this.dbPath = dbPath
if (!fs.existsSync(this.dbPath)) fs.mkdirSync(this.dbPath, { recursive: true })
}
collection(name) {
const safeName = path.basename(name)
const collectionPath = path.join(this.dbPath, safeName)
return new Collection(collectionPath)
}
}
class LocalMongo {
constructor(basePath) {
this.basePath = basePath
if (!fs.existsSync(this.basePath)) fs.mkdirSync(this.basePath, { recursive: true })
}
db(name) {
const safeName = path.basename(name)
const dbPath = path.join(this.basePath, safeName)
return new Database(dbPath)
}
}
module.exports = { LocalMongo }