webspresso 0.0.88 → 0.0.89

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.
@@ -63,39 +63,37 @@ async function loadRelations(records, relationNames, model, knex, scopeContext)
63
63
  * @param {import('./types').ScopeContext} scopeContext - Scope context
64
64
  */
65
65
  async function loadBelongsTo(records, relationName, localKey, foreignKey, relatedModel, knex, scopeContext) {
66
- // Collect unique foreign key values
67
- const foreignKeyValues = [...new Set(
68
- records
69
- .map(r => r[foreignKey])
70
- .filter(v => v !== null && v !== undefined)
71
- )];
72
-
73
- if (foreignKeyValues.length === 0) {
74
- // No foreign keys, set all relations to null
75
- for (const record of records) {
76
- record[relationName] = null;
66
+ const fkSet = new Set();
67
+ const len = records.length;
68
+ for (let i = 0; i < len; i++) {
69
+ const v = records[i][foreignKey];
70
+ if (v !== null && v !== undefined) fkSet.add(v);
71
+ }
72
+
73
+ if (fkSet.size === 0) {
74
+ for (let i = 0; i < len; i++) {
75
+ records[i][relationName] = null;
77
76
  }
78
77
  return;
79
78
  }
80
79
 
81
- // Build query for related records
80
+ const foreignKeyValues = Array.from(fkSet);
82
81
  let qb = knex(relatedModel.table).whereIn(relatedModel.primaryKey, foreignKeyValues);
83
-
84
- // Apply scopes to related model
85
82
  qb = applyScopes(qb, scopeContext, relatedModel);
86
-
87
83
  const relatedRecords = await qb;
88
84
 
89
- // Index related records by primary key
90
85
  const relatedMap = new Map();
91
- for (const related of relatedRecords) {
92
- relatedMap.set(related[relatedModel.primaryKey], related);
86
+ const rLen = relatedRecords.length;
87
+ const pk = relatedModel.primaryKey;
88
+ for (let i = 0; i < rLen; i++) {
89
+ const item = relatedRecords[i];
90
+ relatedMap.set(item[pk], item);
93
91
  }
94
92
 
95
- // Attach related records to parent records
96
- for (const record of records) {
97
- const fkValue = record[foreignKey];
98
- record[relationName] = fkValue !== null && fkValue !== undefined
93
+ for (let i = 0; i < len; i++) {
94
+ const rec = records[i];
95
+ const fkValue = rec[foreignKey];
96
+ rec[relationName] = fkValue !== null && fkValue !== undefined
99
97
  ? relatedMap.get(fkValue) || null
100
98
  : null;
101
99
  }
@@ -115,43 +113,42 @@ async function loadBelongsTo(records, relationName, localKey, foreignKey, relate
115
113
  * @param {import('./types').ScopeContext} scopeContext - Scope context
116
114
  */
117
115
  async function loadHasMany(records, relationName, localKey, foreignKey, relatedModel, knex, scopeContext) {
118
- // Collect unique primary key values from parent records
119
- const primaryKeyValues = [...new Set(
120
- records
121
- .map(r => r[localKey])
122
- .filter(v => v !== null && v !== undefined)
123
- )];
124
-
125
- if (primaryKeyValues.length === 0) {
126
- // No primary keys, set all relations to empty arrays
127
- for (const record of records) {
128
- record[relationName] = [];
116
+ const pkSet = new Set();
117
+ const len = records.length;
118
+ for (let i = 0; i < len; i++) {
119
+ const v = records[i][localKey];
120
+ if (v !== null && v !== undefined) pkSet.add(v);
121
+ }
122
+
123
+ if (pkSet.size === 0) {
124
+ for (let i = 0; i < len; i++) {
125
+ records[i][relationName] = [];
129
126
  }
130
127
  return;
131
128
  }
132
129
 
133
- // Build query for related records
130
+ const primaryKeyValues = Array.from(pkSet);
134
131
  let qb = knex(relatedModel.table).whereIn(foreignKey, primaryKeyValues);
135
-
136
- // Apply scopes to related model
137
132
  qb = applyScopes(qb, scopeContext, relatedModel);
138
-
139
133
  const relatedRecords = await qb;
140
134
 
141
- // Group related records by foreign key
142
135
  const relatedGroups = new Map();
143
- for (const related of relatedRecords) {
144
- const fkValue = related[foreignKey];
145
- if (!relatedGroups.has(fkValue)) {
146
- relatedGroups.set(fkValue, []);
136
+ const rLen = relatedRecords.length;
137
+ for (let i = 0; i < rLen; i++) {
138
+ const item = relatedRecords[i];
139
+ const fkValue = item[foreignKey];
140
+ let group = relatedGroups.get(fkValue);
141
+ if (group === undefined) {
142
+ group = [];
143
+ relatedGroups.set(fkValue, group);
147
144
  }
148
- relatedGroups.get(fkValue).push(related);
145
+ group.push(item);
149
146
  }
150
147
 
151
- // Attach related records to parent records
152
- for (const record of records) {
153
- const pkValue = record[localKey];
154
- record[relationName] = relatedGroups.get(pkValue) || [];
148
+ for (let i = 0; i < len; i++) {
149
+ const rec = records[i];
150
+ const pkValue = rec[localKey];
151
+ rec[relationName] = (pkValue !== null && pkValue !== undefined ? relatedGroups.get(pkValue) : undefined) || [];
155
152
  }
156
153
  }
157
154
 
@@ -169,42 +166,39 @@ async function loadHasMany(records, relationName, localKey, foreignKey, relatedM
169
166
  * @param {import('./types').ScopeContext} scopeContext - Scope context
170
167
  */
171
168
  async function loadHasOne(records, relationName, localKey, foreignKey, relatedModel, knex, scopeContext) {
172
- // Collect unique primary key values from parent records
173
- const primaryKeyValues = [...new Set(
174
- records
175
- .map(r => r[localKey])
176
- .filter(v => v !== null && v !== undefined)
177
- )];
178
-
179
- if (primaryKeyValues.length === 0) {
180
- // No primary keys, set all relations to null
181
- for (const record of records) {
182
- record[relationName] = null;
169
+ const pkSet = new Set();
170
+ const len = records.length;
171
+ for (let i = 0; i < len; i++) {
172
+ const v = records[i][localKey];
173
+ if (v !== null && v !== undefined) pkSet.add(v);
174
+ }
175
+
176
+ if (pkSet.size === 0) {
177
+ for (let i = 0; i < len; i++) {
178
+ records[i][relationName] = null;
183
179
  }
184
180
  return;
185
181
  }
186
182
 
187
- // Build query for related records
183
+ const primaryKeyValues = Array.from(pkSet);
188
184
  let qb = knex(relatedModel.table).whereIn(foreignKey, primaryKeyValues);
189
-
190
- // Apply scopes to related model
191
185
  qb = applyScopes(qb, scopeContext, relatedModel);
192
-
193
186
  const relatedRecords = await qb;
194
187
 
195
- // Index related records by foreign key (first occurrence wins for hasOne)
196
188
  const relatedMap = new Map();
197
- for (const related of relatedRecords) {
198
- const fkValue = related[foreignKey];
189
+ const rLen = relatedRecords.length;
190
+ for (let i = 0; i < rLen; i++) {
191
+ const item = relatedRecords[i];
192
+ const fkValue = item[foreignKey];
199
193
  if (!relatedMap.has(fkValue)) {
200
- relatedMap.set(fkValue, related);
194
+ relatedMap.set(fkValue, item);
201
195
  }
202
196
  }
203
197
 
204
- // Attach related records to parent records
205
- for (const record of records) {
206
- const pkValue = record[localKey];
207
- record[relationName] = relatedMap.get(pkValue) || null;
198
+ for (let i = 0; i < len; i++) {
199
+ const rec = records[i];
200
+ const pkValue = rec[localKey];
201
+ rec[relationName] = (pkValue !== null && pkValue !== undefined ? relatedMap.get(pkValue) : undefined) || null;
208
202
  }
209
203
  }
210
204
 
@@ -154,6 +154,23 @@ class ModelEventsClass {
154
154
  return listeners.length > 0 ? listeners : EMPTY_ARRAY;
155
155
  }
156
156
 
157
+ /**
158
+ * Fast boolean check whether any listener matches
159
+ * @param {string} model - Model name
160
+ * @param {string} hook - Hook name
161
+ * @returns {boolean}
162
+ */
163
+ hasListeners(model, hook) {
164
+ if (this.listeners.size === 0) return false;
165
+ const l = this.listeners;
166
+ return (
167
+ l.has(`${model}.${hook}`) ||
168
+ l.has(`*.${hook}`) ||
169
+ l.has(`${model}.*`) ||
170
+ l.has('*.*')
171
+ );
172
+ }
173
+
157
174
  /**
158
175
  * Emit an event synchronously (for after hooks)
159
176
  * @param {string} model - Model name
@@ -163,6 +180,7 @@ class ModelEventsClass {
163
180
  */
164
181
  emit(model, hook, data, context) {
165
182
  const listeners = this.getMatchingListeners(model, hook);
183
+ if (listeners.length === 0) return;
166
184
  const ctx = context || createEventContext(model, hook);
167
185
 
168
186
  for (const listener of listeners) {
@@ -187,6 +205,7 @@ class ModelEventsClass {
187
205
  async emitAsync(model, hook, data, context) {
188
206
  const listeners = this.getMatchingListeners(model, hook);
189
207
  const ctx = context || createEventContext(model, hook);
208
+ if (listeners.length === 0) return ctx;
190
209
 
191
210
  for (const listener of listeners) {
192
211
  if (ctx.isCancelled) break;
@@ -28,6 +28,7 @@ const { getJsonColumns, serializeJsonFields, deserializeJsonFields } = require('
28
28
  function createRepository(model, knex, initialContext, cacheLayer = null) {
29
29
  const scopeContext = initialContext || createScopeContext();
30
30
  const jsonColumns = getJsonColumns(model);
31
+ const hasJson = jsonColumns.size > 0;
31
32
 
32
33
  /**
33
34
  * Get base query builder
@@ -47,12 +48,15 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
47
48
  */
48
49
  async function findById(id, options = {}, emitEvents = true) {
49
50
  const { with: withs = [], select } = options;
50
- const ctx = createEventContext(model.name, 'find');
51
- const query = { [model.primaryKey]: id };
51
+ const shouldEmit = emitEvents && (
52
+ ModelEvents.hasListeners(model.name, Hooks.BEFORE_FIND) ||
53
+ ModelEvents.hasListeners(model.name, Hooks.AFTER_FIND)
54
+ );
55
+ const ctx = shouldEmit ? createEventContext(model.name, 'find') : null;
52
56
 
53
57
  async function loadFromDb() {
54
- if (emitEvents) {
55
- await ModelEvents.emitAsync(model.name, Hooks.BEFORE_FIND, query, ctx);
58
+ if (shouldEmit) {
59
+ await ModelEvents.emitAsync(model.name, Hooks.BEFORE_FIND, { [model.primaryKey]: id }, ctx);
56
60
  if (ctx.isCancelled) {
57
61
  throw new HookCancellationError(ctx.cancelReason, model.name, Hooks.BEFORE_FIND);
58
62
  }
@@ -70,13 +74,15 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
70
74
  return null;
71
75
  }
72
76
 
73
- deserializeJsonFields(record, jsonColumns);
77
+ if (hasJson) {
78
+ deserializeJsonFields(record, jsonColumns);
79
+ }
74
80
 
75
81
  if (withs.length > 0) {
76
82
  await loadRelations([record], ensureArray(withs), model, knex, scopeContext);
77
83
  }
78
84
 
79
- if (emitEvents) {
85
+ if (shouldEmit) {
80
86
  ModelEvents.emit(model.name, Hooks.AFTER_FIND, record, ctx);
81
87
  }
82
88
 
@@ -113,7 +119,11 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
113
119
  */
114
120
  async function findOne(conditions, options = {}) {
115
121
  const { with: withs = [], select } = options;
116
- const ctx = createEventContext(model.name, 'find');
122
+ const shouldEmit = (
123
+ ModelEvents.hasListeners(model.name, Hooks.BEFORE_FIND) ||
124
+ ModelEvents.hasListeners(model.name, Hooks.AFTER_FIND)
125
+ );
126
+ const ctx = shouldEmit ? createEventContext(model.name, 'find') : null;
117
127
 
118
128
  const condKeys = Object.keys(conditions);
119
129
  const isPkOnly =
@@ -122,9 +132,11 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
122
132
  condKeys[0] === model.primaryKey;
123
133
 
124
134
  async function loadFromDb() {
125
- await ModelEvents.emitAsync(model.name, Hooks.BEFORE_FIND, conditions, ctx);
126
- if (ctx.isCancelled) {
127
- throw new HookCancellationError(ctx.cancelReason, model.name, Hooks.BEFORE_FIND);
135
+ if (shouldEmit) {
136
+ await ModelEvents.emitAsync(model.name, Hooks.BEFORE_FIND, conditions, ctx);
137
+ if (ctx.isCancelled) {
138
+ throw new HookCancellationError(ctx.cancelReason, model.name, Hooks.BEFORE_FIND);
139
+ }
128
140
  }
129
141
 
130
142
  let qb = baseQuery();
@@ -143,13 +155,17 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
143
155
  return null;
144
156
  }
145
157
 
146
- deserializeJsonFields(record, jsonColumns);
158
+ if (hasJson) {
159
+ deserializeJsonFields(record, jsonColumns);
160
+ }
147
161
 
148
162
  if (withs.length > 0) {
149
163
  await loadRelations([record], ensureArray(withs), model, knex, scopeContext);
150
164
  }
151
165
 
152
- ModelEvents.emit(model.name, Hooks.AFTER_FIND, record, ctx);
166
+ if (shouldEmit) {
167
+ ModelEvents.emit(model.name, Hooks.AFTER_FIND, record, ctx);
168
+ }
153
169
 
154
170
  return record;
155
171
  }
@@ -183,12 +199,18 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
183
199
  */
184
200
  async function findAll(options = {}) {
185
201
  const { with: withs = [], select } = options;
186
- const ctx = createEventContext(model.name, 'find');
202
+ const shouldEmit = (
203
+ ModelEvents.hasListeners(model.name, Hooks.BEFORE_FIND) ||
204
+ ModelEvents.hasListeners(model.name, Hooks.AFTER_FIND)
205
+ );
206
+ const ctx = shouldEmit ? createEventContext(model.name, 'find') : null;
187
207
 
188
208
  async function loadFromDb() {
189
- await ModelEvents.emitAsync(model.name, Hooks.BEFORE_FIND, {}, ctx);
190
- if (ctx.isCancelled) {
191
- throw new HookCancellationError(ctx.cancelReason, model.name, Hooks.BEFORE_FIND);
209
+ if (shouldEmit) {
210
+ await ModelEvents.emitAsync(model.name, Hooks.BEFORE_FIND, {}, ctx);
211
+ if (ctx.isCancelled) {
212
+ throw new HookCancellationError(ctx.cancelReason, model.name, Hooks.BEFORE_FIND);
213
+ }
192
214
  }
193
215
 
194
216
  let qb = baseQuery();
@@ -199,16 +221,22 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
199
221
 
200
222
  const records = await qb;
201
223
 
202
- for (const record of records) {
203
- deserializeJsonFields(record, jsonColumns);
224
+ if (hasJson) {
225
+ const len = records.length;
226
+ for (let i = 0; i < len; i++) {
227
+ deserializeJsonFields(records[i], jsonColumns);
228
+ }
204
229
  }
205
230
 
206
231
  if (withs.length > 0 && records.length > 0) {
207
232
  await loadRelations(records, ensureArray(withs), model, knex, scopeContext);
208
233
  }
209
234
 
210
- for (const record of records) {
211
- ModelEvents.emit(model.name, Hooks.AFTER_FIND, record, ctx);
235
+ if (shouldEmit) {
236
+ const len = records.length;
237
+ for (let i = 0; i < len; i++) {
238
+ ModelEvents.emit(model.name, Hooks.AFTER_FIND, records[i], ctx);
239
+ }
212
240
  }
213
241
 
214
242
  return records;
@@ -261,7 +289,9 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
261
289
  let insertData = applyInsertModifiers(validated, scopeContext, model);
262
290
 
263
291
  // Serialize JSON fields
264
- insertData = serializeJsonFields(insertData, jsonColumns);
292
+ if (hasJson) {
293
+ insertData = serializeJsonFields(insertData, jsonColumns);
294
+ }
265
295
 
266
296
  // Insert and return the record
267
297
  const [id] = await knex(model.table).insert(insertData).returning(model.primaryKey);
@@ -341,7 +371,9 @@ function createRepository(model, knex, initialContext, cacheLayer = null) {
341
371
  delete updateData[model.primaryKey];
342
372
 
343
373
  // Serialize JSON fields
344
- updateData = serializeJsonFields(updateData, jsonColumns);
374
+ if (hasJson) {
375
+ updateData = serializeJsonFields(updateData, jsonColumns);
376
+ }
345
377
 
346
378
  // Update the record
347
379
  const updated = await baseQuery()
@@ -5,6 +5,9 @@
5
5
  */
6
6
 
7
7
  const METADATA_MARKER = '__wdb__';
8
+ const metaWeakMap = new WeakMap();
9
+ const metaStringCache = new Map();
10
+ const schemaColumnsWeakMap = new WeakMap();
8
11
 
9
12
  /**
10
13
  * Encode column metadata into a JSON string for Zod .describe()
@@ -22,14 +25,25 @@ function encodeColumnMeta(meta) {
22
25
  */
23
26
  function decodeColumnMeta(description) {
24
27
  if (!description) return null;
28
+ const cached = metaStringCache.get(description);
29
+ if (cached !== undefined) return cached;
30
+
25
31
  try {
26
32
  const parsed = JSON.parse(description);
27
33
  if (parsed && parsed[METADATA_MARKER]) {
28
- return parsed.meta;
34
+ const meta = parsed.meta;
35
+ if (metaStringCache.size < 500) {
36
+ metaStringCache.set(description, meta);
37
+ }
38
+ return meta;
29
39
  }
30
40
  } catch {
31
41
  // Not our metadata, ignore
32
42
  }
43
+
44
+ if (metaStringCache.size < 500) {
45
+ metaStringCache.set(description, null);
46
+ }
33
47
  return null;
34
48
  }
35
49
 
@@ -39,7 +53,7 @@ function decodeColumnMeta(description) {
39
53
  * @returns {boolean}
40
54
  */
41
55
  function hasColumnMeta(schema) {
42
- return decodeColumnMeta(schema.description) !== null;
56
+ return getColumnMeta(schema) !== null;
43
57
  }
44
58
 
45
59
  /**
@@ -48,6 +62,10 @@ function hasColumnMeta(schema) {
48
62
  * @returns {import('./types').ColumnMeta|null}
49
63
  */
50
64
  function getColumnMeta(schema) {
65
+ if (!schema) return null;
66
+ const direct = metaWeakMap.get(schema);
67
+ if (direct !== undefined) return direct;
68
+ if (schema._wdbMeta) return schema._wdbMeta;
51
69
  return decodeColumnMeta(schema.description);
52
70
  }
53
71
 
@@ -253,6 +271,7 @@ class SchemaBuilder {
253
271
 
254
272
  // Apply metadata to schema
255
273
  this._schema = this._schema.describe(encodeColumnMeta(finalMeta));
274
+ metaWeakMap.set(this._schema, finalMeta);
256
275
  this._finalized = true;
257
276
  return this._schema;
258
277
  }
@@ -770,6 +789,15 @@ function createSchemaHelpers(z) {
770
789
  * @returns {Map<string, import('./types').ColumnMeta>}
771
790
  */
772
791
  function extractColumnsFromSchema(schema) {
792
+ if (!schema || typeof schema !== 'object' || !schema.shape) {
793
+ return new Map();
794
+ }
795
+
796
+ const cached = schemaColumnsWeakMap.get(schema);
797
+ if (cached !== undefined) {
798
+ return cached;
799
+ }
800
+
773
801
  const columns = new Map();
774
802
  const shape = schema.shape;
775
803
 
@@ -806,6 +834,7 @@ function extractColumnsFromSchema(schema) {
806
834
  }
807
835
  }
808
836
 
837
+ schemaColumnsWeakMap.set(schema, columns);
809
838
  return columns;
810
839
  }
811
840
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webspresso",
3
- "version": "0.0.88",
3
+ "version": "0.0.89",
4
4
  "description": "Minimal, production-ready SSR framework for Node.js with file-based routing, Nunjucks templating, built-in i18n, and CLI tooling",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",