@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.
- package/LICENSE +21 -21
- package/index.d.ts +1 -1
- package/index.js +35 -35
- package/lib/debug.js +372 -0
- package/lib/orm/adapters/json.js +290 -0
- package/lib/orm/adapters/memory.js +764 -0
- package/lib/orm/adapters/mongo.js +764 -0
- package/lib/orm/adapters/mysql.js +933 -0
- package/lib/orm/adapters/postgres.js +1144 -0
- package/lib/orm/adapters/redis.js +1534 -0
- package/lib/orm/adapters/sql-base.js +212 -0
- package/lib/orm/adapters/sqlite.js +858 -0
- package/lib/orm/audit.js +649 -0
- package/lib/orm/cache.js +394 -0
- package/lib/orm/geo.js +387 -0
- package/lib/orm/index.js +784 -0
- package/lib/orm/migrate.js +432 -0
- package/lib/orm/model.js +1706 -0
- package/lib/orm/plugin.js +375 -0
- package/lib/orm/procedures.js +836 -0
- package/lib/orm/profiler.js +233 -0
- package/lib/orm/query.js +1772 -0
- package/lib/orm/replicas.js +241 -0
- package/lib/orm/schema.js +307 -0
- package/lib/orm/search.js +380 -0
- package/lib/orm/seed/data/commerce.js +136 -0
- package/lib/orm/seed/data/internet.js +111 -0
- package/lib/orm/seed/data/locations.js +204 -0
- package/lib/orm/seed/data/names.js +338 -0
- package/lib/orm/seed/data/person.js +128 -0
- package/lib/orm/seed/data/phone.js +211 -0
- package/lib/orm/seed/data/words.js +134 -0
- package/lib/orm/seed/factory.js +178 -0
- package/lib/orm/seed/fake.js +1186 -0
- package/lib/orm/seed/index.js +18 -0
- package/lib/orm/seed/rng.js +71 -0
- package/lib/orm/seed/seeder.js +125 -0
- package/lib/orm/seed/unique.js +68 -0
- package/lib/orm/snapshot.js +366 -0
- package/lib/orm/tenancy.js +605 -0
- package/lib/orm/views.js +350 -0
- package/package.json +12 -2
- package/types/app.d.ts +223 -0
- package/types/auth.d.ts +520 -0
- package/types/body.d.ts +14 -0
- package/types/cli.d.ts +2 -0
- package/types/cluster.d.ts +75 -0
- package/types/env.d.ts +80 -0
- package/types/errors.d.ts +316 -0
- package/types/fetch.d.ts +43 -0
- package/types/grpc.d.ts +432 -0
- package/types/index.d.ts +384 -0
- package/types/lifecycle.d.ts +60 -0
- package/types/middleware.d.ts +320 -0
- package/types/observe.d.ts +304 -0
- package/types/orm.d.ts +1887 -0
- package/types/request.d.ts +109 -0
- package/types/response.d.ts +157 -0
- package/types/router.d.ts +78 -0
- package/types/sse.d.ts +78 -0
- package/types/websocket.d.ts +126 -0
package/lib/orm/views.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module orm/views
|
|
3
|
+
* @description Database view management for the ORM.
|
|
4
|
+
* Supports creating, dropping, and querying database views.
|
|
5
|
+
* View-backed models are read-only by default.
|
|
6
|
+
*
|
|
7
|
+
* @section Views
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const { DatabaseView } = require('@zero-server/sdk');
|
|
11
|
+
*
|
|
12
|
+
* // Define a view
|
|
13
|
+
* const activeUsers = new DatabaseView('active_users', {
|
|
14
|
+
* query: User.query().where('active', true).select('id', 'name', 'email'),
|
|
15
|
+
* model: User,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Create the view in the database
|
|
19
|
+
* await activeUsers.create(db);
|
|
20
|
+
*
|
|
21
|
+
* // Query the view
|
|
22
|
+
* const users = await activeUsers.all();
|
|
23
|
+
* const user = await activeUsers.findOne({ name: 'Alice' });
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const Model = require('./model');
|
|
27
|
+
const Query = require('./query');
|
|
28
|
+
const log = require('../debug')('zero:orm:views');
|
|
29
|
+
|
|
30
|
+
// -- DatabaseView class -----------------------------------
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Represents a database view.
|
|
34
|
+
* Wraps a query definition and provides read-only access through a model-like API.
|
|
35
|
+
*/
|
|
36
|
+
class DatabaseView
|
|
37
|
+
{
|
|
38
|
+
/**
|
|
39
|
+
* @constructor
|
|
40
|
+
* @param {string} name - View name.
|
|
41
|
+
* @param {object} options - View configuration.
|
|
42
|
+
* @param {Query} [options.query] - Query builder instance defining the view's SELECT.
|
|
43
|
+
* @param {string} [options.sql] - Raw SQL for the view definition (SQL adapters only).
|
|
44
|
+
* @param {typeof Model} [options.model] - Model class the view is based on.
|
|
45
|
+
* @param {object} [options.schema] - Column schema for the view (optional; inferred from model if omitted).
|
|
46
|
+
* @param {boolean} [options.materialized=false] - Whether to create a materialized view (PostgreSQL only).
|
|
47
|
+
*/
|
|
48
|
+
constructor(name, options = {})
|
|
49
|
+
{
|
|
50
|
+
if (!name || typeof name !== 'string')
|
|
51
|
+
{
|
|
52
|
+
throw new Error('DatabaseView requires a non-empty string name');
|
|
53
|
+
}
|
|
54
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name))
|
|
55
|
+
{
|
|
56
|
+
throw new Error(`Invalid view name: "${name}"`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @type {string} View name. */
|
|
60
|
+
this.name = name;
|
|
61
|
+
|
|
62
|
+
/** @type {Query|null} Query builder defining the view. */
|
|
63
|
+
this._query = options.query || null;
|
|
64
|
+
|
|
65
|
+
/** @type {string|null} Raw SQL for view definition. */
|
|
66
|
+
this._sql = options.sql || null;
|
|
67
|
+
|
|
68
|
+
/** @type {typeof Model|null} Base model class. */
|
|
69
|
+
this._model = options.model || null;
|
|
70
|
+
|
|
71
|
+
/** @type {object|null} Column schema for the view. */
|
|
72
|
+
this._schema = options.schema || null;
|
|
73
|
+
|
|
74
|
+
/** @type {boolean} Whether this is a materialized view. */
|
|
75
|
+
this._materialized = options.materialized === true;
|
|
76
|
+
|
|
77
|
+
/** @type {object|null} The database adapter. */
|
|
78
|
+
this._adapter = null;
|
|
79
|
+
|
|
80
|
+
/** @type {typeof Model|null} The generated view model. */
|
|
81
|
+
this._viewModel = null;
|
|
82
|
+
|
|
83
|
+
if (!this._query && !this._sql)
|
|
84
|
+
{
|
|
85
|
+
throw new Error('DatabaseView requires either a query or sql option');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Create the view in the database.
|
|
91
|
+
* For SQL adapters, issues CREATE VIEW (or CREATE MATERIALIZED VIEW).
|
|
92
|
+
* For memory/JSON adapters, stores the query definition for execution.
|
|
93
|
+
*
|
|
94
|
+
* @param {object} db - Database instance.
|
|
95
|
+
* @returns {Promise<DatabaseView>} `this` for chaining.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* await activeUsers.create(db);
|
|
99
|
+
*/
|
|
100
|
+
async create(db)
|
|
101
|
+
{
|
|
102
|
+
this._adapter = db.adapter;
|
|
103
|
+
|
|
104
|
+
if (typeof this._adapter.createView === 'function')
|
|
105
|
+
{
|
|
106
|
+
const sql = this._sql || this._buildSQL();
|
|
107
|
+
await this._adapter.createView(this.name, sql, { materialized: this._materialized });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Create the view-backed model
|
|
111
|
+
this._viewModel = this._createViewModel(db);
|
|
112
|
+
|
|
113
|
+
log.debug('view %s created', this.name);
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Drop the view from the database.
|
|
119
|
+
*
|
|
120
|
+
* @param {object} db - Database instance.
|
|
121
|
+
* @returns {Promise<void>}
|
|
122
|
+
*/
|
|
123
|
+
async drop(db)
|
|
124
|
+
{
|
|
125
|
+
const adapter = db ? db.adapter : this._adapter;
|
|
126
|
+
if (!adapter) throw new Error('No database adapter available');
|
|
127
|
+
|
|
128
|
+
if (typeof adapter.dropView === 'function')
|
|
129
|
+
{
|
|
130
|
+
await adapter.dropView(this.name, { materialized: this._materialized });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this._viewModel = null;
|
|
134
|
+
log.debug('view %s dropped', this.name);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Refresh a materialized view (PostgreSQL only).
|
|
139
|
+
*
|
|
140
|
+
* @param {object} [db] - Database instance.
|
|
141
|
+
* @returns {Promise<void>}
|
|
142
|
+
*/
|
|
143
|
+
async refresh(db)
|
|
144
|
+
{
|
|
145
|
+
const adapter = db ? db.adapter : this._adapter;
|
|
146
|
+
if (!adapter) throw new Error('No database adapter available');
|
|
147
|
+
|
|
148
|
+
if (!this._materialized)
|
|
149
|
+
{
|
|
150
|
+
throw new Error('Only materialized views can be refreshed');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (typeof adapter.refreshView === 'function')
|
|
154
|
+
{
|
|
155
|
+
await adapter.refreshView(this.name);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
log.debug('view %s refreshed', this.name);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Check whether the view exists.
|
|
163
|
+
*
|
|
164
|
+
* @param {object} [db] - Database instance.
|
|
165
|
+
* @returns {Promise<boolean>} True if the view exists.
|
|
166
|
+
*/
|
|
167
|
+
async exists(db)
|
|
168
|
+
{
|
|
169
|
+
const adapter = db ? db.adapter : this._adapter;
|
|
170
|
+
if (!adapter) throw new Error('No database adapter available');
|
|
171
|
+
|
|
172
|
+
if (typeof adapter.hasTable === 'function')
|
|
173
|
+
{
|
|
174
|
+
return adapter.hasTable(this.name);
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Query all records from the view.
|
|
181
|
+
*
|
|
182
|
+
* @returns {Promise<Array>} All rows from the view.
|
|
183
|
+
*/
|
|
184
|
+
async all()
|
|
185
|
+
{
|
|
186
|
+
return this._executeQuery();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Find records from the view matching conditions.
|
|
191
|
+
*
|
|
192
|
+
* @param {object} conditions - WHERE conditions.
|
|
193
|
+
* @returns {Promise<Array>} Matching rows.
|
|
194
|
+
*/
|
|
195
|
+
async find(conditions = {})
|
|
196
|
+
{
|
|
197
|
+
return this._executeQuery(conditions);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Find a single record from the view.
|
|
202
|
+
*
|
|
203
|
+
* @param {object} conditions - WHERE conditions.
|
|
204
|
+
* @returns {Promise<object|null>} First matching row or null.
|
|
205
|
+
*/
|
|
206
|
+
async findOne(conditions = {})
|
|
207
|
+
{
|
|
208
|
+
const results = await this._executeQuery(conditions, 1);
|
|
209
|
+
return results[0] || null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Count records in the view.
|
|
214
|
+
*
|
|
215
|
+
* @param {object} [conditions={}] - Optional WHERE conditions.
|
|
216
|
+
* @returns {Promise<number>} Number of matching records.
|
|
217
|
+
*/
|
|
218
|
+
async count(conditions = {})
|
|
219
|
+
{
|
|
220
|
+
if (this._viewModel)
|
|
221
|
+
{
|
|
222
|
+
return this._viewModel.count(conditions);
|
|
223
|
+
}
|
|
224
|
+
const results = await this._executeQuery(conditions);
|
|
225
|
+
return results.length;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Start a fluent query against the view.
|
|
230
|
+
*
|
|
231
|
+
* @returns {Query} Query builder targeting the view.
|
|
232
|
+
*/
|
|
233
|
+
query()
|
|
234
|
+
{
|
|
235
|
+
if (this._viewModel)
|
|
236
|
+
{
|
|
237
|
+
return this._viewModel.query();
|
|
238
|
+
}
|
|
239
|
+
throw new Error('View model not created. Call create() first.');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Execute the view query with optional conditions.
|
|
244
|
+
* @param {object} [conditions={}] - WHERE conditions.
|
|
245
|
+
* @param {number} [limit] - Optional limit.
|
|
246
|
+
* @returns {Promise<Array>} Results.
|
|
247
|
+
* @private
|
|
248
|
+
*/
|
|
249
|
+
async _executeQuery(conditions = {}, limit)
|
|
250
|
+
{
|
|
251
|
+
if (this._viewModel)
|
|
252
|
+
{
|
|
253
|
+
let q = this._viewModel.query().where(conditions);
|
|
254
|
+
if (limit) q = q.limit(limit);
|
|
255
|
+
return q.exec();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Fallback: re-execute the source query with additional filters
|
|
259
|
+
if (this._query)
|
|
260
|
+
{
|
|
261
|
+
// Clone the query state
|
|
262
|
+
const src = this._query;
|
|
263
|
+
const model = src._model;
|
|
264
|
+
const q = model.query();
|
|
265
|
+
q._fields = src._fields;
|
|
266
|
+
q._where = [...src._where];
|
|
267
|
+
q._orderBy = [...src._orderBy];
|
|
268
|
+
q._limitVal = limit || src._limitVal;
|
|
269
|
+
q._offsetVal = src._offsetVal;
|
|
270
|
+
|
|
271
|
+
// Add additional conditions
|
|
272
|
+
if (Object.keys(conditions).length)
|
|
273
|
+
{
|
|
274
|
+
q.where(conditions);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return q.exec();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return [];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Build SQL from a Query descriptor.
|
|
285
|
+
* @returns {string} SQL SELECT statement.
|
|
286
|
+
* @private
|
|
287
|
+
*/
|
|
288
|
+
_buildSQL()
|
|
289
|
+
{
|
|
290
|
+
if (this._sql) return this._sql;
|
|
291
|
+
if (!this._query) throw new Error('No query or SQL defined for view');
|
|
292
|
+
|
|
293
|
+
const descriptor = this._query.build();
|
|
294
|
+
const table = descriptor.table;
|
|
295
|
+
|
|
296
|
+
// Validate field names and table — identifier-safe only
|
|
297
|
+
const idRe = /^[a-zA-Z_][a-zA-Z0-9_.*]*$/;
|
|
298
|
+
const fields = descriptor.fields
|
|
299
|
+
? descriptor.fields.filter(f => idRe.test(f)).join(', ') || '*'
|
|
300
|
+
: '*';
|
|
301
|
+
if (!idRe.test(table)) throw new Error(`Invalid table name in view query: "${table}"`);
|
|
302
|
+
|
|
303
|
+
let sql = `SELECT ${fields} FROM ${table}`;
|
|
304
|
+
|
|
305
|
+
if (descriptor.where && descriptor.where.length)
|
|
306
|
+
{
|
|
307
|
+
const clauses = descriptor.where.map(w =>
|
|
308
|
+
{
|
|
309
|
+
if (w.raw) return w.raw;
|
|
310
|
+
if (w.op === 'IS NULL') return `${w.field} IS NULL`;
|
|
311
|
+
if (w.op === 'IS NOT NULL') return `${w.field} IS NOT NULL`;
|
|
312
|
+
if (w.op === 'IN') return `${w.field} IN (${w.value.map(() => '?').join(',')})`;
|
|
313
|
+
// Escape single quotes in values for DDL safety
|
|
314
|
+
const escaped = String(w.value).replace(/'/g, "''");
|
|
315
|
+
return `${w.field} ${w.op} '${escaped}'`;
|
|
316
|
+
});
|
|
317
|
+
sql += ` WHERE ${clauses.join(' AND ')}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (descriptor.orderBy && descriptor.orderBy.length)
|
|
321
|
+
{
|
|
322
|
+
const orders = descriptor.orderBy.map(o => `${o.field} ${o.dir}`);
|
|
323
|
+
sql += ` ORDER BY ${orders.join(', ')}`;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return sql;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Create an internal view-backed Model class.
|
|
331
|
+
* @param {object} db - Database instance.
|
|
332
|
+
* @returns {typeof Model} View model class.
|
|
333
|
+
* @private
|
|
334
|
+
*/
|
|
335
|
+
_createViewModel(db)
|
|
336
|
+
{
|
|
337
|
+
const viewName = this.name;
|
|
338
|
+
const schema = this._schema || (this._model ? this._model.schema : {});
|
|
339
|
+
const ViewM = class extends Model
|
|
340
|
+
{
|
|
341
|
+
static table = viewName;
|
|
342
|
+
static schema = { ...schema };
|
|
343
|
+
};
|
|
344
|
+
Object.defineProperty(ViewM, 'name', { value: `${viewName}_view` });
|
|
345
|
+
db.register(ViewM);
|
|
346
|
+
return ViewM;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
module.exports = { DatabaseView };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zero-server/orm",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "Database, Model, Query, migrations, seeds, search, geo, tenancy, audit.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"zero-server",
|
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
"./package.json": "./package.json"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"types",
|
|
23
25
|
"index.js",
|
|
24
26
|
"index.d.ts",
|
|
25
27
|
"README.md",
|
|
@@ -43,6 +45,14 @@
|
|
|
43
45
|
},
|
|
44
46
|
"sideEffects": false,
|
|
45
47
|
"dependencies": {
|
|
46
|
-
"@zero-server/
|
|
48
|
+
"@zero-server/errors": "0.9.3"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@zero-server/sdk": ">=0.9.3"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@zero-server/sdk": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
47
57
|
}
|
|
48
58
|
}
|
package/types/app.d.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import { Server as HttpServer } from 'http';
|
|
4
|
+
import { Server as HttpsServer, ServerOptions as TlsOptions } from 'https';
|
|
5
|
+
import { Http2Server, Http2SecureServer } from 'http2';
|
|
6
|
+
import { Request } from './request';
|
|
7
|
+
import { Response } from './response';
|
|
8
|
+
import { RouterInstance, RouteChain, RouteInfo, RouteOptions, RouteHandler } from './router';
|
|
9
|
+
import { MiddlewareFunction, ErrorHandlerFunction, NextFunction } from './middleware';
|
|
10
|
+
import { WebSocketHandler, WebSocketOptions, WebSocketPool } from './websocket';
|
|
11
|
+
import { SSEStream } from './sse';
|
|
12
|
+
import { LifecycleState } from './lifecycle';
|
|
13
|
+
import { MetricsRegistry, HealthCheckResult } from './observe';
|
|
14
|
+
import { ProtoSchema, GrpcServiceOptions, GrpcInterceptor, GrpcHandler } from './grpc';
|
|
15
|
+
|
|
16
|
+
export interface ListenOptions {
|
|
17
|
+
/** Create an HTTP/2 server. Combined with TLS options for h2 over TLS, or h2c (cleartext) otherwise. */
|
|
18
|
+
http2?: boolean;
|
|
19
|
+
/** Allow HTTP/1.1 fallback on HTTP/2 TLS servers (ALPN negotiation). Default: true. */
|
|
20
|
+
allowHTTP1?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface App {
|
|
24
|
+
/** Internal router instance. */
|
|
25
|
+
router: RouterInstance;
|
|
26
|
+
/** Middleware stack. */
|
|
27
|
+
middlewares: MiddlewareFunction[];
|
|
28
|
+
/** Application-level locals, merged into every request/response locals. */
|
|
29
|
+
locals: Record<string, any>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Register middleware or mount a sub-router.
|
|
33
|
+
*/
|
|
34
|
+
use(fn: MiddlewareFunction): App;
|
|
35
|
+
use(path: string, fn: MiddlewareFunction): App;
|
|
36
|
+
use(path: string, router: RouterInstance): App;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Register a global error handler.
|
|
40
|
+
*/
|
|
41
|
+
onError(fn: ErrorHandlerFunction): void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Core request handler for use with `http.createServer()`.
|
|
45
|
+
*/
|
|
46
|
+
handler(req: import('http').IncomingMessage, res: import('http').ServerResponse): void;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Start listening for connections.
|
|
50
|
+
* Pass `{ http2: true }` to create an HTTP/2 server (h2c cleartext or TLS with ALPN).
|
|
51
|
+
*/
|
|
52
|
+
listen(port?: number, cb?: () => void): HttpServer;
|
|
53
|
+
listen(port: number, opts: TlsOptions, cb?: () => void): HttpsServer;
|
|
54
|
+
listen(port: number, opts: ListenOptions & { http2: true } & TlsOptions, cb?: () => void): Http2SecureServer;
|
|
55
|
+
listen(port: number, opts: ListenOptions & { http2: true }, cb?: () => void): Http2Server;
|
|
56
|
+
listen(port: number, opts: ListenOptions, cb?: () => void): HttpServer | HttpsServer | Http2Server | Http2SecureServer;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gracefully close the server.
|
|
60
|
+
*/
|
|
61
|
+
close(cb?: (err?: Error) => void): void;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Perform a full graceful shutdown.
|
|
65
|
+
*/
|
|
66
|
+
shutdown(opts?: { timeout?: number }): Promise<void>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Register a lifecycle event listener.
|
|
70
|
+
*/
|
|
71
|
+
on(event: 'beforeShutdown' | 'shutdown', fn: () => void | Promise<void>): App;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Remove a lifecycle event listener.
|
|
75
|
+
*/
|
|
76
|
+
off(event: 'beforeShutdown' | 'shutdown', fn: () => void | Promise<void>): App;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Register a WebSocket pool for graceful shutdown.
|
|
80
|
+
*/
|
|
81
|
+
registerPool(pool: WebSocketPool): App;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Unregister a WebSocket pool from lifecycle management.
|
|
85
|
+
*/
|
|
86
|
+
unregisterPool(pool: WebSocketPool): App;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Track an SSE stream for graceful shutdown.
|
|
90
|
+
*/
|
|
91
|
+
trackSSE(stream: SSEStream): App;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Register an ORM Database for graceful shutdown.
|
|
95
|
+
*/
|
|
96
|
+
registerDatabase(db: { close(): Promise<void> }): App;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Unregister an ORM Database from lifecycle management.
|
|
100
|
+
*/
|
|
101
|
+
unregisterDatabase(db: { close(): Promise<void> }): App;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Configure the shutdown timeout in milliseconds.
|
|
105
|
+
*/
|
|
106
|
+
shutdownTimeout(ms: number): App;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Current lifecycle state.
|
|
110
|
+
*/
|
|
111
|
+
readonly lifecycleState: LifecycleState;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Register a liveness health check endpoint.
|
|
115
|
+
*/
|
|
116
|
+
health(path?: string, checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
117
|
+
health(checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Register a readiness health check endpoint.
|
|
121
|
+
*/
|
|
122
|
+
ready(path?: string, checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
123
|
+
ready(checks?: Record<string, () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>>): App;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Register a custom health check.
|
|
127
|
+
*/
|
|
128
|
+
addHealthCheck(name: string, fn: () => HealthCheckResult | boolean | Promise<HealthCheckResult | boolean>): App;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get the application metrics registry.
|
|
132
|
+
*/
|
|
133
|
+
metrics(): MetricsRegistry;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Mount a Prometheus metrics endpoint.
|
|
137
|
+
*/
|
|
138
|
+
metricsEndpoint(path?: string, opts?: { registry?: MetricsRegistry }): App;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Register a WebSocket upgrade handler.
|
|
142
|
+
*/
|
|
143
|
+
ws(path: string, handler: WebSocketHandler): void;
|
|
144
|
+
ws(path: string, opts: WebSocketOptions, handler: WebSocketHandler): void;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Register a gRPC service with handlers.
|
|
148
|
+
*/
|
|
149
|
+
grpc(schema: ProtoSchema, serviceName: string, handlers: Record<string, GrpcHandler>, opts?: GrpcServiceOptions): App;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add a global gRPC interceptor.
|
|
153
|
+
*/
|
|
154
|
+
grpcInterceptor(fn: GrpcInterceptor): App;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Return a flat list of all registered routes.
|
|
158
|
+
*/
|
|
159
|
+
routes(): RouteInfo[];
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Register a route with a specific HTTP method.
|
|
163
|
+
*/
|
|
164
|
+
route(method: string, path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get a setting value (1 arg) or set a setting value (2 args).
|
|
168
|
+
*/
|
|
169
|
+
set(key: string): any;
|
|
170
|
+
set(key: string, val: any): App;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Get a setting value, or register a GET route.
|
|
174
|
+
* With 1 string arg: returns the setting value.
|
|
175
|
+
* With path + handlers: registers a GET route.
|
|
176
|
+
*/
|
|
177
|
+
get(key: string): any;
|
|
178
|
+
get(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Enable a boolean setting (set to `true`).
|
|
182
|
+
*/
|
|
183
|
+
enable(key: string): App;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Disable a boolean setting (set to `false`).
|
|
187
|
+
*/
|
|
188
|
+
disable(key: string): App;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Check if a setting is truthy.
|
|
192
|
+
*/
|
|
193
|
+
enabled(key: string): boolean;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Check if a setting is falsy.
|
|
197
|
+
*/
|
|
198
|
+
disabled(key: string): boolean;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Register a parameter pre-processing handler.
|
|
202
|
+
*/
|
|
203
|
+
param(name: string, fn: (req: Request, res: Response, next: NextFunction, value: string) => void): App;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Create a route group under a prefix with shared middleware.
|
|
207
|
+
*/
|
|
208
|
+
group(prefix: string, ...args: [...MiddlewareFunction[], (router: RouterInstance) => void]): App;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Create a chainable route builder for the given path.
|
|
212
|
+
*/
|
|
213
|
+
chain(path: string): RouteChain;
|
|
214
|
+
|
|
215
|
+
// HTTP method shortcuts
|
|
216
|
+
post(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
217
|
+
put(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
218
|
+
delete(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
219
|
+
patch(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
220
|
+
options(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
221
|
+
head(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
222
|
+
all(path: string, ...handlers: (RouteOptions | RouteHandler)[]): App;
|
|
223
|
+
}
|