How to use the @loopback/repository.defineModelClass function in @loopback/repository

To help you get started, we’ve selected a few @loopback/repository examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / examples / todo / src / controllers / model-admin.controller.ts View on Github external
async discoverAndPublish(ds: juggler.DataSource, table: string) {
    console.log('Discovering table %j', table);

    // Step 1: discover model definition from the database schema
    const modelDef = await discoverModelDefinition(ds, table);

    // Step 2: define a model class using the discovered definition
    const ModelClass = defineModelClass(Entity, modelDef);
    console.log('Defined model %s', ModelClass.name);

    // Step 3: define a repository class adding CRUD behavior to our model
    const RepositoryClass = defineCrudRepositoryClass(ModelClass);
    inject(`datasources.${ds.name}`)(RepositoryClass, undefined, 0);
    const repoBinding = this.app.repository(RepositoryClass);
    console.log('Defined repository %s', RepositoryClass.name);

    // Step 4: Optionally, expose the new model via REST API
    const basePath = '/' + modelDef.name;
    const ControllerClass = defineCrudRestController(ModelClass, {basePath});
    inject(repoBinding.key)(ControllerClass, undefined, 0);
    this.app.controller(ControllerClass);
    console.log('Defined controller %s', ControllerClass.name);

    return basePath;