@zero-server/orm 0.9.1 → 0.9.3

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 (61) hide show
  1. package/LICENSE +21 -21
  2. package/index.d.ts +1 -1
  3. package/index.js +35 -35
  4. package/lib/debug.js +372 -0
  5. package/lib/orm/adapters/json.js +290 -0
  6. package/lib/orm/adapters/memory.js +764 -0
  7. package/lib/orm/adapters/mongo.js +764 -0
  8. package/lib/orm/adapters/mysql.js +933 -0
  9. package/lib/orm/adapters/postgres.js +1144 -0
  10. package/lib/orm/adapters/redis.js +1534 -0
  11. package/lib/orm/adapters/sql-base.js +212 -0
  12. package/lib/orm/adapters/sqlite.js +858 -0
  13. package/lib/orm/audit.js +649 -0
  14. package/lib/orm/cache.js +394 -0
  15. package/lib/orm/geo.js +387 -0
  16. package/lib/orm/index.js +784 -0
  17. package/lib/orm/migrate.js +432 -0
  18. package/lib/orm/model.js +1706 -0
  19. package/lib/orm/plugin.js +375 -0
  20. package/lib/orm/procedures.js +836 -0
  21. package/lib/orm/profiler.js +233 -0
  22. package/lib/orm/query.js +1772 -0
  23. package/lib/orm/replicas.js +241 -0
  24. package/lib/orm/schema.js +307 -0
  25. package/lib/orm/search.js +380 -0
  26. package/lib/orm/seed/data/commerce.js +136 -0
  27. package/lib/orm/seed/data/internet.js +111 -0
  28. package/lib/orm/seed/data/locations.js +204 -0
  29. package/lib/orm/seed/data/names.js +338 -0
  30. package/lib/orm/seed/data/person.js +128 -0
  31. package/lib/orm/seed/data/phone.js +211 -0
  32. package/lib/orm/seed/data/words.js +134 -0
  33. package/lib/orm/seed/factory.js +178 -0
  34. package/lib/orm/seed/fake.js +1186 -0
  35. package/lib/orm/seed/index.js +18 -0
  36. package/lib/orm/seed/rng.js +71 -0
  37. package/lib/orm/seed/seeder.js +125 -0
  38. package/lib/orm/seed/unique.js +68 -0
  39. package/lib/orm/snapshot.js +366 -0
  40. package/lib/orm/tenancy.js +605 -0
  41. package/lib/orm/views.js +350 -0
  42. package/package.json +12 -2
  43. package/types/app.d.ts +223 -0
  44. package/types/auth.d.ts +520 -0
  45. package/types/body.d.ts +14 -0
  46. package/types/cli.d.ts +2 -0
  47. package/types/cluster.d.ts +75 -0
  48. package/types/env.d.ts +80 -0
  49. package/types/errors.d.ts +316 -0
  50. package/types/fetch.d.ts +43 -0
  51. package/types/grpc.d.ts +432 -0
  52. package/types/index.d.ts +384 -0
  53. package/types/lifecycle.d.ts +60 -0
  54. package/types/middleware.d.ts +320 -0
  55. package/types/observe.d.ts +304 -0
  56. package/types/orm.d.ts +1887 -0
  57. package/types/request.d.ts +109 -0
  58. package/types/response.d.ts +157 -0
  59. package/types/router.d.ts +78 -0
  60. package/types/sse.d.ts +78 -0
  61. package/types/websocket.d.ts +126 -0
@@ -0,0 +1,233 @@
1
+ /**
2
+ * @module orm/profiler
3
+ * @description Query profiling, slow query detection, and automatic N+1 detection.
4
+ * Attach to a Database instance via `db.enableProfiling()`.
5
+ *
6
+ * @example
7
+ * const { Database, QueryProfiler } = require('@zero-server/sdk');
8
+ *
9
+ * const db = Database.connect('memory');
10
+ * const profiler = db.enableProfiling({ slowThreshold: 100 });
11
+ *
12
+ * // ... run queries ...
13
+ *
14
+ * console.log(profiler.metrics());
15
+ * console.log(profiler.slowQueries());
16
+ * console.log(profiler.n1Detections());
17
+ */
18
+
19
+ const log = require('../debug')('zero:profiler');
20
+
21
+ class QueryProfiler
22
+ {
23
+ /**
24
+ * @constructor
25
+ * @param {object} [options] - Configuration options.
26
+ * @param {boolean} [options.enabled=true] - Enable/disable profiling.
27
+ * @param {number} [options.slowThreshold=100] - Duration (ms) above which a query is "slow".
28
+ * @param {number} [options.maxHistory=1000] - Maximum recorded query entries.
29
+ * @param {Function} [options.onSlow] - Callback on slow query: (entry) => {}.
30
+ * @param {number} [options.n1Threshold=5] - Minimum rapid same-table SELECTs to flag N+1.
31
+ * @param {number} [options.n1Window=100] - Time window (ms) for N+1 detection.
32
+ * @param {Function} [options.onN1] - Callback on N+1 detection: (info) => {}.
33
+ */
34
+ constructor(options = {})
35
+ {
36
+ this._enabled = options.enabled !== false;
37
+ this._slowThreshold = Math.max(0, options.slowThreshold != null ? Number(options.slowThreshold) : 100);
38
+ this._maxHistory = Math.max(1, Math.floor(Number(options.maxHistory) || 1000));
39
+ this._onSlow = typeof options.onSlow === 'function' ? options.onSlow : null;
40
+
41
+ // N+1 detection
42
+ this._n1Threshold = Math.max(2, Math.floor(Number(options.n1Threshold) || 5));
43
+ this._n1Window = Math.max(10, Number(options.n1Window) || 100);
44
+ this._onN1 = typeof options.onN1 === 'function' ? options.onN1 : null;
45
+ this._maxN1History = Math.max(1, Math.floor(Number(options.maxN1History) || 100));
46
+ this._n1Detected = [];
47
+
48
+ // Per-table SELECT counters for O(1) N+1 detection
49
+ /** @private Map<table, { count, firstTs }> */
50
+ this._selectCounters = new Map();
51
+
52
+ // Query history
53
+ this._queries = [];
54
+
55
+ // Aggregate stats
56
+ this._totalQueries = 0;
57
+ this._totalTime = 0;
58
+ this._slowCount = 0;
59
+ this._startTime = Date.now();
60
+ }
61
+
62
+ /**
63
+ * Record a query execution.
64
+ *
65
+ * @param {object} entry - Profiler entry object.
66
+ * @param {string} entry.table - Table name.
67
+ * @param {string} entry.action - Query action (select, insert, update, delete, count).
68
+ * @param {number} entry.duration - Execution time in milliseconds.
69
+ */
70
+ record(entry)
71
+ {
72
+ if (!this._enabled) return;
73
+
74
+ // Sanitize
75
+ const record = {
76
+ table: String(entry.table || ''),
77
+ action: String(entry.action || 'unknown'),
78
+ duration: Number(entry.duration) || 0,
79
+ timestamp: Date.now(),
80
+ };
81
+
82
+ this._totalQueries++;
83
+ this._totalTime += record.duration;
84
+
85
+ // Enforce history limit (evict oldest)
86
+ if (this._queries.length >= this._maxHistory)
87
+ {
88
+ this._queries.shift();
89
+ }
90
+ this._queries.push(record);
91
+
92
+ // Slow query detection
93
+ if (record.duration > this._slowThreshold)
94
+ {
95
+ this._slowCount++;
96
+ log.warn('Slow query: %s %s (%dms)', record.action, record.table, Math.round(record.duration));
97
+ if (this._onSlow) this._onSlow(record);
98
+ }
99
+
100
+ // N+1 detection (only for SELECTs)
101
+ this._detectN1(record.table, record.action);
102
+ }
103
+
104
+ /**
105
+ * Detect potential N+1 query patterns.
106
+ * Uses per-table sliding window counters for O(1) detection instead of scanning history.
107
+ * @param {string} table - Table involved.
108
+ * @param {string} action - CRUD action performed.
109
+ * @private
110
+ */
111
+ _detectN1(table, action)
112
+ {
113
+ if (action !== 'select') return;
114
+
115
+ const now = Date.now();
116
+ let counter = this._selectCounters.get(table);
117
+
118
+ if (!counter || (now - counter.firstTs) >= this._n1Window)
119
+ {
120
+ // Window expired or first query — start a new window
121
+ counter = { count: 1, firstTs: now };
122
+ this._selectCounters.set(table, counter);
123
+ return;
124
+ }
125
+
126
+ counter.count++;
127
+
128
+ if (counter.count >= this._n1Threshold)
129
+ {
130
+ // Prevent duplicate detection for same burst
131
+ const last = this._n1Detected.find(d => d.table === table);
132
+ if (last && (now - last.timestamp) < this._n1Window) return;
133
+
134
+ const detection = {
135
+ table,
136
+ count: counter.count,
137
+ timestamp: now,
138
+ message: `Potential N+1: ${counter.count} SELECT queries to "${table}" in ${this._n1Window}ms`,
139
+ };
140
+ // Cap N+1 detection history to prevent memory exhaustion
141
+ if (this._n1Detected.length >= this._maxN1History)
142
+ {
143
+ this._n1Detected.shift();
144
+ }
145
+ this._n1Detected.push(detection);
146
+ log.warn(detection.message);
147
+ if (this._onN1) this._onN1(detection);
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Get aggregate profiling metrics.
153
+ *
154
+ * @returns {{
155
+ * totalQueries: number,
156
+ * totalTime: number,
157
+ * avgLatency: number,
158
+ * queriesPerSecond: number,
159
+ * slowQueries: number,
160
+ * n1Detections: number,
161
+ * }}
162
+ */
163
+ metrics()
164
+ {
165
+ const elapsed = (Date.now() - this._startTime) / 1000 || 1;
166
+ return {
167
+ totalQueries: this._totalQueries,
168
+ totalTime: Math.round(this._totalTime * 100) / 100,
169
+ avgLatency: this._totalQueries > 0
170
+ ? Math.round((this._totalTime / this._totalQueries) * 100) / 100
171
+ : 0,
172
+ queriesPerSecond: Math.round((this._totalQueries / elapsed) * 100) / 100,
173
+ slowQueries: this._slowCount,
174
+ n1Detections: this._n1Detected.length,
175
+ };
176
+ }
177
+
178
+ /**
179
+ * Get all slow queries from history.
180
+ * @returns {Array<{ table: string, action: string, duration: number, timestamp: number }>}
181
+ */
182
+ slowQueries()
183
+ {
184
+ return this._queries.filter(q => q.duration > this._slowThreshold);
185
+ }
186
+
187
+ /**
188
+ * Get all N+1 detections.
189
+ * @returns {Array<{ table: string, count: number, timestamp: number, message: string }>}
190
+ */
191
+ n1Detections()
192
+ {
193
+ return [...this._n1Detected];
194
+ }
195
+
196
+ /**
197
+ * Get filtered query history.
198
+ *
199
+ * @param {object} [options] - Configuration options.
200
+ * @param {string} [options.table] - Filter by table name.
201
+ * @param {string} [options.action] - Filter by action type.
202
+ * @param {number} [options.minDuration] - Minimum duration filter.
203
+ * @returns {Array<{ table: string, action: string, duration: number, timestamp: number }>} Filtered query history entries.
204
+ */
205
+ getQueries(options = {})
206
+ {
207
+ let results = [...this._queries];
208
+ if (options.table) results = results.filter(q => q.table === options.table);
209
+ if (options.action) results = results.filter(q => q.action === options.action);
210
+ if (options.minDuration !== undefined) results = results.filter(q => q.duration >= options.minDuration);
211
+ return results;
212
+ }
213
+
214
+ /**
215
+ * Reset all profiling state.
216
+ */
217
+ reset()
218
+ {
219
+ this._queries = [];
220
+ this._totalQueries = 0;
221
+ this._totalTime = 0;
222
+ this._slowCount = 0;
223
+ this._startTime = Date.now();
224
+ this._n1Detected = [];
225
+ this._selectCounters.clear();
226
+ }
227
+
228
+ /** @type {boolean} */
229
+ get enabled() { return this._enabled; }
230
+ set enabled(val) { this._enabled = !!val; }
231
+ }
232
+
233
+ module.exports = { QueryProfiler };