@yanit/jsondb 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +903 -0
  2. package/dist/bin/cli-export.d.ts +7 -0
  3. package/dist/bin/cli-export.d.ts.map +1 -0
  4. package/dist/bin/cli-export.js +318 -0
  5. package/dist/bin/cli-export.js.map +1 -0
  6. package/dist/bin/cli-import.d.ts +7 -0
  7. package/dist/bin/cli-import.d.ts.map +1 -0
  8. package/dist/bin/cli-import.js +298 -0
  9. package/dist/bin/cli-import.js.map +1 -0
  10. package/dist/bin/server.d.ts +7 -0
  11. package/dist/bin/server.d.ts.map +1 -0
  12. package/dist/bin/server.js +92 -0
  13. package/dist/bin/server.js.map +1 -0
  14. package/dist/examples/sql-example.d.ts +7 -0
  15. package/dist/examples/sql-example.d.ts.map +1 -0
  16. package/dist/examples/sql-example.js +131 -0
  17. package/dist/examples/sql-example.js.map +1 -0
  18. package/dist/src/BulkOp.d.ts +74 -0
  19. package/dist/src/BulkOp.d.ts.map +1 -0
  20. package/dist/src/BulkOp.js +143 -0
  21. package/dist/src/BulkOp.js.map +1 -0
  22. package/dist/src/Collection.d.ts +232 -0
  23. package/dist/src/Collection.d.ts.map +1 -0
  24. package/dist/src/Collection.js +705 -0
  25. package/dist/src/Collection.js.map +1 -0
  26. package/dist/src/Cursor.d.ts +94 -0
  27. package/dist/src/Cursor.d.ts.map +1 -0
  28. package/dist/src/Cursor.js +259 -0
  29. package/dist/src/Cursor.js.map +1 -0
  30. package/dist/src/Database.d.ts +98 -0
  31. package/dist/src/Database.d.ts.map +1 -0
  32. package/dist/src/Database.js +198 -0
  33. package/dist/src/Database.js.map +1 -0
  34. package/dist/src/Operators.d.ts +73 -0
  35. package/dist/src/Operators.d.ts.map +1 -0
  36. package/dist/src/Operators.js +339 -0
  37. package/dist/src/Operators.js.map +1 -0
  38. package/dist/src/QueryCache.d.ts +87 -0
  39. package/dist/src/QueryCache.d.ts.map +1 -0
  40. package/dist/src/QueryCache.js +155 -0
  41. package/dist/src/QueryCache.js.map +1 -0
  42. package/dist/src/SQLExecutor.d.ts +60 -0
  43. package/dist/src/SQLExecutor.d.ts.map +1 -0
  44. package/dist/src/SQLExecutor.js +317 -0
  45. package/dist/src/SQLExecutor.js.map +1 -0
  46. package/dist/src/SQLParser.d.ts +181 -0
  47. package/dist/src/SQLParser.d.ts.map +1 -0
  48. package/dist/src/SQLParser.js +640 -0
  49. package/dist/src/SQLParser.js.map +1 -0
  50. package/dist/src/Schema.d.ts +92 -0
  51. package/dist/src/Schema.d.ts.map +1 -0
  52. package/dist/src/Schema.js +253 -0
  53. package/dist/src/Schema.js.map +1 -0
  54. package/dist/src/Transaction.d.ts +118 -0
  55. package/dist/src/Transaction.d.ts.map +1 -0
  56. package/dist/src/Transaction.js +233 -0
  57. package/dist/src/Transaction.js.map +1 -0
  58. package/dist/src/Utils.d.ts +68 -0
  59. package/dist/src/Utils.d.ts.map +1 -0
  60. package/dist/src/Utils.js +187 -0
  61. package/dist/src/Utils.js.map +1 -0
  62. package/dist/src/errors.d.ts +58 -0
  63. package/dist/src/errors.d.ts.map +1 -0
  64. package/dist/src/errors.js +85 -0
  65. package/dist/src/errors.js.map +1 -0
  66. package/dist/src/index.d.ts +39 -0
  67. package/dist/src/index.d.ts.map +1 -0
  68. package/dist/src/index.js +44 -0
  69. package/dist/src/index.js.map +1 -0
  70. package/dist/test/basic.test.d.ts +5 -0
  71. package/dist/test/basic.test.d.ts.map +1 -0
  72. package/dist/test/basic.test.js +283 -0
  73. package/dist/test/basic.test.js.map +1 -0
  74. package/dist/test/index.test.d.ts +5 -0
  75. package/dist/test/index.test.d.ts.map +1 -0
  76. package/dist/test/index.test.js +126 -0
  77. package/dist/test/index.test.js.map +1 -0
  78. package/dist/test/jsonb.test.d.ts +5 -0
  79. package/dist/test/jsonb.test.d.ts.map +1 -0
  80. package/dist/test/jsonb.test.js +165 -0
  81. package/dist/test/jsonb.test.js.map +1 -0
  82. package/dist/test/optimization.test.d.ts +6 -0
  83. package/dist/test/optimization.test.d.ts.map +1 -0
  84. package/dist/test/optimization.test.js +196 -0
  85. package/dist/test/optimization.test.js.map +1 -0
  86. package/dist/test/schema.test.d.ts +5 -0
  87. package/dist/test/schema.test.d.ts.map +1 -0
  88. package/dist/test/schema.test.js +197 -0
  89. package/dist/test/schema.test.js.map +1 -0
  90. package/dist/test/sql.test.d.ts +7 -0
  91. package/dist/test/sql.test.d.ts.map +1 -0
  92. package/dist/test/sql.test.js +21 -0
  93. package/dist/test/sql.test.js.map +1 -0
  94. package/package.json +73 -0
  95. package/src/BulkOp.js +181 -0
  96. package/src/BulkOp.ts +191 -0
  97. package/src/Collection.js +843 -0
  98. package/src/Collection.ts +896 -0
  99. package/src/Cursor.js +315 -0
  100. package/src/Cursor.ts +319 -0
  101. package/src/Database.js +244 -0
  102. package/src/Database.ts +268 -0
  103. package/src/Operators.js +382 -0
  104. package/src/Operators.ts +375 -0
  105. package/src/QueryCache.js +190 -0
  106. package/src/QueryCache.ts +208 -0
  107. package/src/SQLExecutor.ts +391 -0
  108. package/src/SQLParser.ts +814 -0
  109. package/src/Schema.js +292 -0
  110. package/src/Schema.ts +317 -0
  111. package/src/Transaction.js +291 -0
  112. package/src/Transaction.ts +313 -0
  113. package/src/Utils.js +205 -0
  114. package/src/Utils.ts +205 -0
  115. package/src/errors.js +93 -0
  116. package/src/errors.ts +93 -0
  117. package/src/index.js +90 -0
  118. package/src/index.ts +106 -0
package/src/Utils.js ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * 工具函数模块
3
+ */
4
+
5
+ import { createHash } from 'crypto';
6
+ import { existsSync, mkdirSync } from 'fs';
7
+ import { dirname } from 'path';
8
+
9
+ /**
10
+ * 生成唯一 ID(类似 MongoDB ObjectId 简化版)
11
+ * @returns {string} 24 位十六进制字符串
12
+ */
13
+ export function generateId() {
14
+ const timestamp = Math.floor(Date.now() / 1000).toString(16).padStart(8, '0');
15
+ const randomPart = createHash('sha256')
16
+ .update(Math.random().toString() + Date.now().toString())
17
+ .digest('hex')
18
+ .substring(0, 16);
19
+ return timestamp + randomPart;
20
+ }
21
+
22
+ /**
23
+ * 深拷贝对象
24
+ * @param {any} obj - 要拷贝的对象
25
+ * @returns {any} 拷贝后的对象
26
+ */
27
+ export function deepClone(obj) {
28
+ return JSON.parse(JSON.stringify(obj));
29
+ }
30
+
31
+ /**
32
+ * 获取嵌套对象的值
33
+ * @param {Object} obj - 对象
34
+ * @param {string} path - 路径,如 'user.name'
35
+ * @returns {any} 值
36
+ */
37
+ export function getNestedValue(obj, path) {
38
+ if (!path || typeof path !== 'string') {
39
+ return obj;
40
+ }
41
+
42
+ const keys = path.split('.');
43
+ let result = obj;
44
+
45
+ for (const key of keys) {
46
+ if (result === null || result === undefined) {
47
+ return undefined;
48
+ }
49
+ result = result[key];
50
+ }
51
+
52
+ return result;
53
+ }
54
+
55
+ /**
56
+ * 设置嵌套对象的值
57
+ * @param {Object} obj - 对象
58
+ * @param {string} path - 路径,如 'user.name'
59
+ * @param {any} value - 值
60
+ */
61
+ export function setNestedValue(obj, path, value) {
62
+ const keys = path.split('.');
63
+ let current = obj;
64
+
65
+ for (let i = 0; i < keys.length - 1; i++) {
66
+ const key = keys[i];
67
+ if (!(key in current)) {
68
+ current[key] = {};
69
+ }
70
+ current = current[key];
71
+ }
72
+
73
+ current[keys[keys.length - 1]] = value;
74
+ }
75
+
76
+ /**
77
+ * 删除嵌套对象的属性
78
+ * @param {Object} obj - 对象
79
+ * @param {string} path - 路径,如 'user.name'
80
+ * @returns {boolean} 是否删除成功
81
+ */
82
+ export function deleteNestedValue(obj, path) {
83
+ const keys = path.split('.');
84
+ let current = obj;
85
+
86
+ for (let i = 0; i < keys.length - 1; i++) {
87
+ if (!(keys[i] in current)) {
88
+ return false;
89
+ }
90
+ current = current[keys[i]];
91
+ }
92
+
93
+ return delete current[keys[keys.length - 1]];
94
+ }
95
+
96
+ /**
97
+ * 确保目录存在
98
+ * @param {string} dirPath - 目录路径
99
+ */
100
+ export function ensureDir(dirPath) {
101
+ if (!existsSync(dirPath)) {
102
+ mkdirSync(dirname(dirPath), { recursive: true });
103
+ }
104
+ }
105
+
106
+ /**
107
+ * 检查值是否匹配类型
108
+ * @param {any} value - 值
109
+ * @param {string} type - 类型名称
110
+ * @returns {boolean}
111
+ */
112
+ export function isType(value, type) {
113
+ if (type === 'array') {
114
+ return Array.isArray(value);
115
+ }
116
+ if (type === 'null') {
117
+ return value === null;
118
+ }
119
+ if (type === 'object') {
120
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
121
+ }
122
+ return typeof value === type;
123
+ }
124
+
125
+ /**
126
+ * 比较两个值
127
+ * @param {any} a - 值 a
128
+ * @param {any} b - 值 b
129
+ * @returns {number} -1, 0, 1
130
+ */
131
+ export function compareValues(a, b) {
132
+ if (a === b) return 0;
133
+ if (a === null) return -1;
134
+ if (b === null) return 1;
135
+ if (a < b) return -1;
136
+ if (a > b) return 1;
137
+ return 0;
138
+ }
139
+
140
+ /**
141
+ * 将排序规范转换为比较函数
142
+ * @param {Object} sortSpec - 排序规范,如 { age: -1, name: 1 }
143
+ * @returns {Function} 比较函数
144
+ */
145
+ export function createSortFunction(sortSpec) {
146
+ const keys = Object.keys(sortSpec);
147
+
148
+ return (a, b) => {
149
+ for (const key of keys) {
150
+ const direction = sortSpec[key];
151
+ const aVal = getNestedValue(a, key);
152
+ const bVal = getNestedValue(b, key);
153
+
154
+ const cmp = compareValues(aVal, bVal);
155
+ if (cmp !== 0) {
156
+ return cmp * direction;
157
+ }
158
+ }
159
+ return 0;
160
+ };
161
+ }
162
+
163
+ /**
164
+ * 投影文档字段
165
+ * @param {Object} doc - 文档
166
+ * @param {Object} projection - 投影规范
167
+ * @returns {Object} 投影后的文档
168
+ */
169
+ export function projectDocument(doc, projection) {
170
+ if (!projection) {
171
+ return deepClone(doc);
172
+ }
173
+
174
+ const result = {};
175
+ const keys = Object.keys(projection);
176
+
177
+ // 检查是包含模式还是排除模式
178
+ let isIncludeMode = false;
179
+ for (const key of keys) {
180
+ if (key !== '_id' && projection[key]) {
181
+ isIncludeMode = true;
182
+ break;
183
+ }
184
+ }
185
+
186
+ // 处理 _id 字段
187
+ if (projection._id !== undefined) {
188
+ if (projection._id) {
189
+ result._id = doc._id;
190
+ }
191
+ } else if (isIncludeMode) {
192
+ result._id = doc._id;
193
+ }
194
+
195
+ // 处理其他字段
196
+ for (const key of keys) {
197
+ if (key === '_id') continue;
198
+
199
+ if (projection[key]) {
200
+ result[key] = getNestedValue(doc, key);
201
+ }
202
+ }
203
+
204
+ return result;
205
+ }
package/src/Utils.ts ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * 工具函数模块
3
+ */
4
+
5
+ import { createHash } from 'crypto';
6
+ import { existsSync, mkdirSync } from 'fs';
7
+ import { dirname } from 'path';
8
+
9
+ /**
10
+ * 生成唯一 ID(类似 MongoDB ObjectId 简化版)
11
+ * @returns 24 位十六进制字符串
12
+ */
13
+ export function generateId(): string {
14
+ const timestamp = Math.floor(Date.now() / 1000).toString(16).padStart(8, '0');
15
+ const randomPart = createHash('sha256')
16
+ .update(Math.random().toString() + Date.now().toString())
17
+ .digest('hex')
18
+ .substring(0, 16);
19
+ return timestamp + randomPart;
20
+ }
21
+
22
+ /**
23
+ * 深拷贝对象
24
+ * @param obj - 要拷贝的对象
25
+ * @returns 拷贝后的对象
26
+ */
27
+ export function deepClone<T>(obj: T): T {
28
+ return JSON.parse(JSON.stringify(obj));
29
+ }
30
+
31
+ /**
32
+ * 获取嵌套对象的值
33
+ * @param obj - 对象
34
+ * @param path - 路径,如 'user.name'
35
+ * @returns 值
36
+ */
37
+ export function getNestedValue(obj: Record<string, unknown>, path: string): unknown {
38
+ if (!path || typeof path !== 'string') {
39
+ return obj;
40
+ }
41
+
42
+ const keys = path.split('.');
43
+ let result: unknown = obj;
44
+
45
+ for (const key of keys) {
46
+ if (result === null || result === undefined) {
47
+ return undefined;
48
+ }
49
+ result = (result as Record<string, unknown>)[key];
50
+ }
51
+
52
+ return result;
53
+ }
54
+
55
+ /**
56
+ * 设置嵌套对象的值
57
+ * @param obj - 对象
58
+ * @param path - 路径,如 'user.name'
59
+ * @param value - 值
60
+ */
61
+ export function setNestedValue(obj: Record<string, unknown>, path: string, value: unknown): void {
62
+ const keys = path.split('.');
63
+ let current: Record<string, unknown> = obj;
64
+
65
+ for (let i = 0; i < keys.length - 1; i++) {
66
+ const key = keys[i];
67
+ if (!(key in current)) {
68
+ current[key] = {};
69
+ }
70
+ current = current[key] as Record<string, unknown>;
71
+ }
72
+
73
+ current[keys[keys.length - 1]] = value;
74
+ }
75
+
76
+ /**
77
+ * 删除嵌套对象的属性
78
+ * @param obj - 对象
79
+ * @param path - 路径,如 'user.name'
80
+ * @returns 是否删除成功
81
+ */
82
+ export function deleteNestedValue(obj: Record<string, unknown>, path: string): boolean {
83
+ const keys = path.split('.');
84
+ let current: Record<string, unknown> = obj;
85
+
86
+ for (let i = 0; i < keys.length - 1; i++) {
87
+ if (!(keys[i] in current)) {
88
+ return false;
89
+ }
90
+ current = current[keys[i]] as Record<string, unknown>;
91
+ }
92
+
93
+ return delete current[keys[keys.length - 1]];
94
+ }
95
+
96
+ /**
97
+ * 确保目录存在
98
+ * @param dirPath - 目录路径
99
+ */
100
+ export function ensureDir(dirPath: string): void {
101
+ if (!existsSync(dirPath)) {
102
+ mkdirSync(dirname(dirPath), { recursive: true });
103
+ }
104
+ }
105
+
106
+ /**
107
+ * 检查值是否匹配类型
108
+ * @param value - 值
109
+ * @param type - 类型名称
110
+ * @returns 是否匹配
111
+ */
112
+ export function isType(value: unknown, type: string): boolean {
113
+ if (type === 'array') {
114
+ return Array.isArray(value);
115
+ }
116
+ if (type === 'null') {
117
+ return value === null;
118
+ }
119
+ if (type === 'object') {
120
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
121
+ }
122
+ return typeof value === type;
123
+ }
124
+
125
+ /**
126
+ * 比较两个值
127
+ * @param a - 值 a
128
+ * @param b - 值 b
129
+ * @returns -1, 0, 1
130
+ */
131
+ export function compareValues(a: unknown, b: unknown): number {
132
+ if (a === b) return 0;
133
+ if (a === null) return -1;
134
+ if (b === null) return 1;
135
+ if (a < b) return -1;
136
+ if (a > b) return 1;
137
+ return 0;
138
+ }
139
+
140
+ /**
141
+ * 将排序规范转换为比较函数
142
+ * @param sortSpec - 排序规范,如 { age: -1, name: 1 }
143
+ * @returns 比较函数
144
+ */
145
+ export function createSortFunction(sortSpec: Record<string, number>): (a: Record<string, unknown>, b: Record<string, unknown>) => number {
146
+ const keys = Object.keys(sortSpec);
147
+
148
+ return (a: Record<string, unknown>, b: Record<string, unknown>) => {
149
+ for (const key of keys) {
150
+ const direction = sortSpec[key];
151
+ const aVal = getNestedValue(a, key);
152
+ const bVal = getNestedValue(b, key);
153
+
154
+ const cmp = compareValues(aVal, bVal);
155
+ if (cmp !== 0) {
156
+ return cmp * direction;
157
+ }
158
+ }
159
+ return 0;
160
+ };
161
+ }
162
+
163
+ /**
164
+ * 投影文档字段
165
+ * @param doc - 文档
166
+ * @param projection - 投影规范
167
+ * @returns 投影后的文档
168
+ */
169
+ export function projectDocument(doc: Record<string, unknown>, projection?: Record<string, unknown>): Record<string, unknown> {
170
+ if (!projection) {
171
+ return deepClone(doc);
172
+ }
173
+
174
+ const result: Record<string, unknown> = {};
175
+ const keys = Object.keys(projection);
176
+
177
+ // 检查是包含模式还是排除模式
178
+ let isIncludeMode = false;
179
+ for (const key of keys) {
180
+ if (key !== '_id' && projection[key]) {
181
+ isIncludeMode = true;
182
+ break;
183
+ }
184
+ }
185
+
186
+ // 处理 _id 字段
187
+ if (projection._id !== undefined) {
188
+ if (projection._id) {
189
+ result._id = doc._id;
190
+ }
191
+ } else if (isIncludeMode) {
192
+ result._id = doc._id;
193
+ }
194
+
195
+ // 处理其他字段
196
+ for (const key of keys) {
197
+ if (key === '_id') continue;
198
+
199
+ if (projection[key]) {
200
+ result[key] = getNestedValue(doc, key);
201
+ }
202
+ }
203
+
204
+ return result;
205
+ }
package/src/errors.js ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * JSONDB 错误类定义
3
+ */
4
+
5
+ /**
6
+ * 基础错误类
7
+ */
8
+ export class JSONDBError extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = 'JSONDBError';
12
+ }
13
+ }
14
+
15
+ /**
16
+ * 数据库不存在错误
17
+ */
18
+ export class DatabaseNotFoundError extends JSONDBError {
19
+ constructor(dbPath) {
20
+ super(`数据库不存在:${dbPath}`);
21
+ this.name = 'DatabaseNotFoundError';
22
+ }
23
+ }
24
+
25
+ /**
26
+ * 数据库已存在错误
27
+ */
28
+ export class DatabaseExistsError extends JSONDBError {
29
+ constructor(dbPath) {
30
+ super(`数据库已存在:${dbPath}`);
31
+ this.name = 'DatabaseExistsError';
32
+ }
33
+ }
34
+
35
+ /**
36
+ * 集合不存在错误
37
+ */
38
+ export class CollectionNotFoundError extends JSONDBError {
39
+ constructor(collectionName) {
40
+ super(`集合不存在:${collectionName}`);
41
+ this.name = 'CollectionNotFoundError';
42
+ }
43
+ }
44
+
45
+ /**
46
+ * 集合已存在错误
47
+ */
48
+ export class CollectionExistsError extends JSONDBError {
49
+ constructor(collectionName) {
50
+ super(`集合已存在:${collectionName}`);
51
+ this.name = 'CollectionExistsError';
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 文档未找到错误
57
+ */
58
+ export class DocumentNotFoundError extends JSONDBError {
59
+ constructor(query) {
60
+ super(`文档未找到:${JSON.stringify(query)}`);
61
+ this.name = 'DocumentNotFoundError';
62
+ }
63
+ }
64
+
65
+ /**
66
+ * 索引错误
67
+ */
68
+ export class IndexError extends JSONDBError {
69
+ constructor(message) {
70
+ super(message);
71
+ this.name = 'IndexError';
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 查询错误
77
+ */
78
+ export class QueryError extends JSONDBError {
79
+ constructor(message) {
80
+ super(message);
81
+ this.name = 'QueryError';
82
+ }
83
+ }
84
+
85
+ /**
86
+ * 验证错误
87
+ */
88
+ export class ValidationError extends JSONDBError {
89
+ constructor(message) {
90
+ super(message);
91
+ this.name = 'ValidationError';
92
+ }
93
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * JSONDB 错误类定义
3
+ */
4
+
5
+ /**
6
+ * 基础错误类
7
+ */
8
+ export class JSONDBError extends Error {
9
+ constructor(message: string) {
10
+ super(message);
11
+ this.name = 'JSONDBError';
12
+ }
13
+ }
14
+
15
+ /**
16
+ * 数据库不存在错误
17
+ */
18
+ export class DatabaseNotFoundError extends JSONDBError {
19
+ constructor(dbPath: string) {
20
+ super(`数据库不存在:${dbPath}`);
21
+ this.name = 'DatabaseNotFoundError';
22
+ }
23
+ }
24
+
25
+ /**
26
+ * 数据库已存在错误
27
+ */
28
+ export class DatabaseExistsError extends JSONDBError {
29
+ constructor(dbPath: string) {
30
+ super(`数据库已存在:${dbPath}`);
31
+ this.name = 'DatabaseExistsError';
32
+ }
33
+ }
34
+
35
+ /**
36
+ * 集合不存在错误
37
+ */
38
+ export class CollectionNotFoundError extends JSONDBError {
39
+ constructor(collectionName: string) {
40
+ super(`集合不存在:${collectionName}`);
41
+ this.name = 'CollectionNotFoundError';
42
+ }
43
+ }
44
+
45
+ /**
46
+ * 集合已存在错误
47
+ */
48
+ export class CollectionExistsError extends JSONDBError {
49
+ constructor(collectionName: string) {
50
+ super(`集合已存在:${collectionName}`);
51
+ this.name = 'CollectionExistsError';
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 文档未找到错误
57
+ */
58
+ export class DocumentNotFoundError extends JSONDBError {
59
+ constructor(query: Record<string, unknown>) {
60
+ super(`文档未找到:${JSON.stringify(query)}`);
61
+ this.name = 'DocumentNotFoundError';
62
+ }
63
+ }
64
+
65
+ /**
66
+ * 索引错误
67
+ */
68
+ export class IndexError extends JSONDBError {
69
+ constructor(message: string) {
70
+ super(message);
71
+ this.name = 'IndexError';
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 查询错误
77
+ */
78
+ export class QueryError extends JSONDBError {
79
+ constructor(message: string) {
80
+ super(message);
81
+ this.name = 'QueryError';
82
+ }
83
+ }
84
+
85
+ /**
86
+ * 验证错误
87
+ */
88
+ export class ValidationError extends JSONDBError {
89
+ constructor(message: string) {
90
+ super(message);
91
+ this.name = 'ValidationError';
92
+ }
93
+ }
package/src/index.js ADDED
@@ -0,0 +1,90 @@
1
+ /**
2
+ * JSONDB - 基于 Node.js 的轻量级 JSON 数据库
3
+ * 支持 MongoDB 风格语法
4
+ *
5
+ * @example
6
+ * import { Database } from 'jsondb';
7
+ *
8
+ * const db = new Database('./data/mydb');
9
+ * await db.open();
10
+ *
11
+ * const users = db.collection('users');
12
+ * await users.insertOne({ name: 'Alice', age: 25 });
13
+ *
14
+ * await db.close();
15
+ */
16
+
17
+ export { Database } from './Database.js';
18
+ export { Collection } from './Collection.js';
19
+ export { Cursor } from './Cursor.js';
20
+
21
+ // 导出错误类
22
+ export {
23
+ JSONDBError,
24
+ DatabaseNotFoundError,
25
+ DatabaseExistsError,
26
+ CollectionNotFoundError,
27
+ CollectionExistsError,
28
+ DocumentNotFoundError,
29
+ IndexError,
30
+ QueryError,
31
+ ValidationError
32
+ } from './errors.js';
33
+
34
+ // 导出工具函数
35
+ export {
36
+ generateId,
37
+ deepClone,
38
+ getNestedValue,
39
+ setNestedValue,
40
+ deleteNestedValue
41
+ } from './Utils.js';
42
+
43
+ // 导出查询操作符
44
+ export {
45
+ matchQuery,
46
+ applyUpdate,
47
+ comparisonOperators,
48
+ logicalOperators,
49
+ elementOperators,
50
+ arrayOperators
51
+ } from './Operators.js';
52
+
53
+ // 导出 Schema 验证
54
+ export {
55
+ Schema,
56
+ createSchema,
57
+ createValidator,
58
+ validators
59
+ } from './Schema.js';
60
+
61
+ // 导出批量操作
62
+ export {
63
+ BulkOperation,
64
+ createBulkOp,
65
+ bulkInsert,
66
+ bulkUpdate,
67
+ bulkDelete
68
+ } from './BulkOp.js';
69
+
70
+ // 导出查询缓存
71
+ export {
72
+ QueryCache,
73
+ globalQueryCache,
74
+ cacheQuery
75
+ } from './QueryCache.js';
76
+
77
+ // 导出事务
78
+ export {
79
+ Transaction,
80
+ TransactionStatus,
81
+ OperationType,
82
+ createTransaction,
83
+ withTransaction
84
+ } from './Transaction.js';
85
+
86
+ /**
87
+ * 默认导出
88
+ */
89
+ import { Database } from './Database.js';
90
+ export default Database;