Skip to content

Commit

Permalink
feat: add defineCrudRepositoryClass
Browse files Browse the repository at this point in the history
Add `defineCrudRepositoryClass` - a helper to create named repository classes
  • Loading branch information
Hage Yaapa committed Oct 4, 2019
1 parent 7fcbb13 commit f36784d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// License text available at https://opensource.org/licenses/MIT

import {
DefaultCrudRepository,
Entity,
EntityCrudRepository,
juggler,
Expand All @@ -19,7 +18,7 @@ import {
givenHttpServerConfig,
toJSON,
} from '@loopback/testlab';
import {defineCrudRestController} from '../..';
import {defineCrudRepositoryClass, defineCrudRestController} from '../..';

// In this test scenario, we create a product with a required & an optional
// property and use the default model settings (strict mode, forceId).
Expand Down Expand Up @@ -283,10 +282,10 @@ describe('CrudRestController for a simple Product model', () => {

async function setupTestScenario() {
const db = new juggler.DataSource({connector: 'memory'});
repo = new DefaultCrudRepository<Product, typeof Product.prototype.id>(
Product,
db,
);

const ProductRepository = defineCrudRepositoryClass(Product);

repo = new ProductRepository(db);

const CrudRestController = defineCrudRestController<
Product,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/rest-crud
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Entity, model, property} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {defineCrudRepositoryClass} from '../..';

describe('defineCrudRepositoryClass', () => {
it('should generate repository based on Model name', async () => {
@model()
class Product extends Entity {
@property({id: true})
id: number;
}

const ProductRepository = defineCrudRepositoryClass(Product);

expect(ProductRepository.name).to.equal('ProductRepository');
});
});
1 change: 1 addition & 0 deletions packages/rest-crud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
// License text available at https://opensource.org/licenses/MIT

export * from './crud-rest.controller';
export * from './repository-builder';
44 changes: 44 additions & 0 deletions packages/rest-crud/src/repository-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/rest-crud
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
DefaultCrudRepository,
Entity,
EntityCrudRepository,
juggler,
} from '@loopback/repository';
import * as assert from 'assert';

export function defineCrudRepositoryClass<
T extends Entity,
IdType,
Relations extends object = {}
>(
entityClass: typeof Entity & {prototype: T},
): RepositoryClass<T, IdType, Relations> {
const repoName = entityClass.name + 'Repository';
const defineNamedRepo = new Function(
'EntityCtor',
'BaseRepository',
`return class ${repoName} extends BaseRepository {
constructor(dataSource) {
super(EntityCtor, dataSource);
}
};`,
);

// TODO(bajtos) make DefaultCrudRepository configurable (?)
const repo = defineNamedRepo(entityClass, DefaultCrudRepository);
assert.equal(repo.name, repoName);
return repo;
}

export interface RepositoryClass<
T extends Entity,
IdType,
Relations extends object
> {
new (ds: juggler.DataSource): EntityCrudRepository<T, IdType, Relations>;
}

0 comments on commit f36784d

Please sign in to comment.