or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application.mdauth.mddatasources.mdindex.mdmiddleware.mdmodels.md

application.mddocs/

0

# Application Management

1

2

LoopBack application creation and management functionality for creating Express-based applications with model management, middleware configuration, and data source integration.

3

4

## Capabilities

5

6

### Application Factory

7

8

Creates a new LoopBack application instance that extends Express with LoopBack-specific functionality.

9

10

```javascript { .api }

11

/**

12

* Create a LoopBack application instance

13

* @param {Object} options - Optional configuration options

14

* @param {boolean} options.localRegistry - Use local registry instead of global

15

* @param {boolean} options.loadBuiltinModels - Load built-in models when using local registry

16

* @returns {LoopBackApplication} Express-based LoopBack application

17

*/

18

function loopback(options);

19

```

20

21

**Usage Example:**

22

23

```javascript

24

const loopback = require('loopback');

25

26

// Basic application

27

const app = loopback();

28

29

// Application with local registry

30

const isolatedApp = loopback({

31

localRegistry: true,

32

loadBuiltinModels: true

33

});

34

```

35

36

### Model Attachment

37

38

Attach models to the application and configure their settings.

39

40

```javascript { .api }

41

/**

42

* Attach a model to the application

43

* @param {Function} Model - Model constructor to attach

44

* @param {Object} config - Optional model configuration

45

* @param {string} config.dataSource - Data source name to attach model to

46

* @param {boolean} config.public - Whether model should be exposed via REST API

47

* @param {Object} config.options - Additional model options

48

*/

49

app.model(Model, config);

50

51

/**

52

* Remove a model from the application by name

53

* @param {string} modelName - Name of model to remove

54

*/

55

app.deleteModelByName(modelName);

56

57

/**

58

* Get all models attached to the application

59

* @returns {Function[]} Array of model constructors

60

*/

61

app.models();

62

```

63

64

**Usage Examples:**

65

66

```javascript

67

const Book = loopback.createModel('Book', {

68

title: String,

69

author: String

70

});

71

72

// Attach model with configuration

73

app.model(Book, {

74

dataSource: 'db',

75

public: true

76

});

77

78

// Get all attached models

79

const attachedModels = app.models();

80

```

81

82

### Data Source Management

83

84

Configure and manage data sources for the application.

85

86

```javascript { .api }

87

/**

88

* Define a data source for the application

89

* @param {string} name - Data source name

90

* @param {Object} config - Data source configuration

91

* @param {string} config.connector - Connector name or constructor

92

* @param {string} config.host - Database host (if applicable)

93

* @param {number} config.port - Database port (if applicable)

94

* @param {string} config.database - Database name (if applicable)

95

* @param {string} config.username - Database username (if applicable)

96

* @param {string} config.password - Database password (if applicable)

97

* @returns {DataSource} Created data source instance

98

*/

99

app.dataSource(name, config);

100

101

/**

102

* Register a connector with the application

103

* @param {string} name - Connector name

104

* @param {Function} connector - Connector constructor

105

*/

106

app.connector(name, connector);

107

```

108

109

**Usage Examples:**

110

111

```javascript

112

// Define a database connection

113

app.dataSource('db', {

114

connector: 'mysql',

115

host: 'localhost',

116

port: 3306,

117

database: 'myapp',

118

username: 'dbuser',

119

password: 'dbpass'

120

});

121

122

// Define memory data source

123

app.dataSource('memory', {

124

connector: 'memory'

125

});

126

127

// Register custom connector

128

app.connector('custom', MyCustomConnector);

129

```

130

131

### Authentication Configuration

132

133

Enable and configure authentication for the application.

134

135

```javascript { .api }

136

/**

137

* Enable authentication for the application

138

* @param {Object} options - Authentication configuration options

139

* @param {Object} options.dataSource - Data source for auth models

140

* @param {Object} options.userModel - User model configuration

141

* @param {Object} options.userIdentityModel - UserIdentity model configuration

142

* @param {Object} options.userCredentialModel - UserCredential model configuration

143

*/

144

app.enableAuth(options);

145

```

146

147

**Usage Example:**

148

149

```javascript

150

// Enable basic authentication

151

app.enableAuth({

152

dataSource: 'db',

153

userModel: 'User'

154

});

155

```

156

157

### Middleware Configuration

158

159

Configure middleware phases and register middleware functions.

160

161

```javascript { .api }

162

/**

163

* Define middleware phases for the application

164

* @param {string|string[]} nameOrArray - Phase name or array of phase names

165

*/

166

app.defineMiddlewarePhases(nameOrArray);

167

168

/**

169

* Register middleware in a specific phase

170

* @param {string} phase - Middleware phase name

171

* @param {string|string[]} paths - Optional paths to apply middleware to

172

* @param {Function} handler - Middleware function

173

*/

174

app.middleware(phase, paths, handler);

175

176

/**

177

* Register middleware from configuration

178

* @param {Function} factory - Middleware factory function

179

* @param {Object} config - Middleware configuration

180

*/

181

app.middlewareFromConfig(factory, config);

182

```

183

184

**Built-in Middleware Phases:**

185

186

1. `initial` - Initialization phase

187

2. `session` - Session handling

188

3. `auth` - Authentication

189

4. `parse` - Request parsing

190

5. `routes` - Route handling

191

6. `files` - Static file serving

192

7. `final` - Final cleanup

193

194

**Usage Examples:**

195

196

```javascript

197

// Define custom middleware phases

198

app.defineMiddlewarePhases(['validation', 'transform']);

199

200

// Register middleware in phases

201

app.middleware('initial', (req, res, next) => {

202

console.log('Request received:', req.method, req.url);

203

next();

204

});

205

206

app.middleware('auth', '/api', loopback.token());

207

```

208

209

### Remote Objects Management

210

211

Access and configure remote object exposure for REST APIs.

212

213

```javascript { .api }

214

/**

215

* Get the RemoteObjects instance for the application

216

* @returns {RemoteObjects} Strong-remoting RemoteObjects instance

217

*/

218

app.remotes();

219

220

/**

221

* Get remote objects as a plain object

222

* @returns {Object} Plain object representation of remote objects

223

*/

224

app.remoteObjects();

225

226

/**

227

* Get handler for specific type

228

* @param {string} type - Handler type (e.g., 'rest', 'json')

229

* @param {Object} options - Handler options

230

* @returns {Function} Handler function

231

*/

232

app.handler(type, options);

233

```

234

235

### Express Extensions

236

237

Additional Express-related functionality specific to LoopBack applications.

238

239

```javascript { .api }

240

/**

241

* Remove a route by reference (opposite of app.use)

242

* @param {*} route - Route reference to remove

243

*/

244

app.disuse(route);

245

246

/**

247

* Enhanced listen method with port configuration

248

* @param {Function} cb - Optional callback function

249

* @returns {http.Server} HTTP server instance

250

*/

251

app.listen(cb);

252

```

253

254

**Usage Example:**

255

256

```javascript

257

// Start server with callback

258

app.listen(() => {

259

console.log('LoopBack server is running on port', app.get('port') || 3000);

260

});

261

```

262

263

## Application Properties

264

265

```javascript { .api }

266

/**

267

* Application instance properties

268

*/

269

interface LoopBackApplication {

270

// Registry and data management

271

registry: Registry; // Model registry instance

272

models: Object; // Attached models object

273

datasources: Object; // Data sources registry

274

dataSources: Object; // Alias for datasources

275

connectors: Object; // Registered connectors

276

277

// Framework reference

278

loopback: LoopBackStatic; // Reference to main loopback object

279

280

// Express application properties inherited

281

// ... all Express application properties

282

}

283

```