Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@loopback/repository
Advanced tools
Define and implement a common set of interfaces for interacting with databases
This module provides a common set of interfaces for interacting with databases.
This module provides data access facilities to various databases and services as well as the constructs for modeling and accessing those data.
npm install --save @loopback/repository
At the moment, we only have implementations of Repository
based on LoopBack
3.x loopback-datasource-juggler
and connectors. The following steps illustrate
how to define repositories and use them with controllers.
The repository module provides APIs to define LoopBack 3.x data sources and models. For example,
// src/datasources/db.datasource.ts
import {juggler} from '@loopback/repository';
export const db: juggler.DataSource = new juggler.DataSource({
name: 'db',
connector: 'memory',
});
// src/models/note.model.ts
import {model, Entity, property} from '@loopback/repository';
@model()
export class Note extends Entity {
@property({id: true})
id: string;
@property()
title: string;
@property()
content: string;
}
export interface NoteRelations {
// describe navigational properties here
}
export type NoteWithRelations = Note & NoteRelations;
NOTE: There is no declarative support for data source and model yet in LoopBack 4. These constructs need to be created programmatically as illustrated above.
A repository can be created by extending DefaultCrudRepository
and using
dependency injection to resolve the datasource.
// src/repositories/note.repository.ts
import {DefaultCrudRepository, DataSourceType} from '@loopback/repository';
import {Note, NoteRelations} from '../models';
import {inject} from '@loopback/core';
export class NoteRepository extends DefaultCrudRepository<
Note,
typeof Note.prototype.id,
NoteRelations
> {
constructor(@inject('datasources.db') protected dataSource: DataSourceType) {
super(Note, dataSource);
}
}
Controllers serve as handlers for API requests. We declare controllers as classes with optional dependency injection by decorating constructor parameters or properties.
// src/controllers/note.controller.ts
import {repository} from '@loopback/repository';
import {NoteRepository} from '../repositories';
import {Note} from '../models';
import {post, requestBody, get, param} from '@loopback/rest';
export class NoteController {
constructor(
// Use constructor dependency injection to set up the repository
@repository(NoteRepository) public noteRepo: NoteRepository,
) {}
// Create a new note
@post('/note')
create(@requestBody() data: Note) {
return this.noteRepo.create(data);
}
// Find notes by title
@get('/note/{title}')
findByTitle(@param.path.string('title') title: string) {
return this.noteRepo.find({where: {title}});
}
}
A Repository Mixin is available for Application that provides convenience
methods for binding and instantiating a repository class. Bound instances can be
used anywhere in your application using Dependency Injection. The
.repository(RepositoryClass)
function can be used to bind a repository class
to an Application. The mixin will also instantiate any repositories declared by
a component in its constructor using the repositories
key.
Repositories will be bound to the key repositories.RepositoryClass
where
RepositoryClass
is the name of the Repository class being bound.
We'll use BootMixin
on top of RepositoryMixin
so that Repository bindings
can be taken care of automatically at boot time before the application starts.
import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {db} from './datasources/db.datasource';
export class RepoApplication extends BootMixin(
RepositoryMixin(RestApplication),
) {
constructor(options?: ApplicationConfig) {
super(options);
this.projectRoot = __dirname;
this.dataSource(db);
}
}
Run npm test
from the root folder.
See all contributors.
MIT
FAQs
Define and implement a common set of interfaces for interacting with databases
The npm package @loopback/repository receives a total of 33,863 weekly downloads. As such, @loopback/repository popularity was classified as popular.
We found that @loopback/repository demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.