Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
feat(repository): add defaultFn support (#40)
Browse files Browse the repository at this point in the history
with overridable registry of aliases.
The default registry includes existing ones that juggler
provides such as uuid, now, shortid etc.

GH-0

Co-authored-by: Shubham P <[email protected]>
  • Loading branch information
RaghavaroraSF and shubhamp-sf authored Jun 29, 2023
1 parent 7e36924 commit c33f639
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 8 deletions.
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"dependencies": {
"debug": "^4.3.4",
"sequelize": "^6.28.0",
"tslib": "^2.0.0"
"tslib": "^2.0.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@commitlint/cli": "^17.4.2",
Expand All @@ -72,6 +73,7 @@
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/lodash": "^4.14.195",
"@types/node": "^14.18.36",
"@types/uuid": "^9.0.2",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"cz-customizable": "^7.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/fixtures/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './doctor.controller';
export * from './patient.controller';
export * from './product.controller';
export * from './programming-languange.controller';
export * from './task.controller';
export * from './test.controller.base';
export * from './todo-list-todo.controller';
export * from './todo-list.controller';
Expand Down
186 changes: 186 additions & 0 deletions src/__tests__/fixtures/controllers/task.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import {
Count,
CountSchema,
Filter,
FilterExcludingWhere,
repository,
Where,
} from '@loopback/repository';
import {
del,
get,
getModelSchemaRef,
param,
patch,
post,
put,
requestBody,
response,
} from '@loopback/rest';
import {Task} from '../models';
import {TaskRepository} from '../repositories';
import {TestControllerBase} from './test.controller.base';
export class TaskController extends TestControllerBase {
constructor(
@repository(TaskRepository)
public taskRepository: TaskRepository,
) {
super(taskRepository);
}

@post('/tasks')
@response(200, {
description: 'task model instance',
content: {'application/json': {schema: getModelSchemaRef(Task)}},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Task, {
title: 'NewTask',
exclude: ['id'],
}),
},
},
})
task: Omit<Task, 'id'>,
): Promise<Task> {
return this.taskRepository.create(task);
}

@post('/tasks-bulk')
@response(200, {
description: 'task model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Task),
},
},
},
})
async createAll(
@requestBody({
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Task, {
title: 'NewTask',
exclude: ['id'],
}),
},
},
},
})
tasks: Array<Omit<Task, 'id'>>,
): Promise<Task[]> {
return this.taskRepository.createAll(tasks);
}

@get('/tasks/count')
@response(200, {
description: 'Task model count',
content: {'application/json': {schema: CountSchema}},
})
async count(@param.where(Task) where?: Where<Task>): Promise<Count> {
return this.taskRepository.count(where);
}

@get('/tasks')
@response(200, {
description: 'Array of Task model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Task, {includeRelations: true}),
},
},
},
})
async find(@param.filter(Task) filter?: Filter<Task>): Promise<Task[]> {
return this.taskRepository.find(filter);
}

@patch('/tasks')
@response(200, {
description: 'Task PATCH success count',
content: {'application/json': {schema: CountSchema}},
})
async updateAll(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Task, {partial: true}),
},
},
})
task: Task,
@param.where(Task) where?: Where<Task>,
): Promise<Count> {
return this.taskRepository.updateAll(task, where);
}

@get('/tasks/{id}')
@response(200, {
description: 'Task model instance',
content: {
'application/json': {
schema: getModelSchemaRef(Task, {includeRelations: true}),
},
},
})
async findById(
@param.path.number('id') id: number,
@param.filter(Task, {exclude: 'where'})
filter?: FilterExcludingWhere<Task>,
): Promise<Task> {
return this.taskRepository.findById(id, filter);
}

@patch('/tasks/{id}')
@response(204, {
description: 'Task PATCH success',
})
async updateById(
@param.path.number('id') id: number,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Task, {partial: true}),
},
},
})
task: Task,
): Promise<void> {
await this.taskRepository.updateById(id, task);
}

@put('/tasks/{id}')
@response(204, {
description: 'Task PUT success',
})
async replaceById(
@param.path.number('id') id: number,
@requestBody() task: Task,
): Promise<void> {
await this.taskRepository.replaceById(id, task);
}

@del('/tasks/{id}')
@response(204, {
description: 'Task DELETE success',
})
async deleteById(@param.path.number('id') id: number): Promise<void> {
await this.taskRepository.deleteById(id);
}

@get('/tasks/sync-sequelize-model')
@response(200)
async syncSequelizeModel(): Promise<void> {
await this.beforeEach();
}
}
1 change: 1 addition & 0 deletions src/__tests__/fixtures/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './doctor.model';
export * from './patient.model';
export * from './product.model';
export * from './programming-language.model';
export * from './task.model';
export * from './todo-list.model';
export * from './todo.model';
export * from './user.model';
63 changes: 63 additions & 0 deletions src/__tests__/fixtures/models/task.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {Entity, model, property} from '@loopback/repository';

@model()
export class Task extends Entity {
@property({
type: 'number',
id: true,
generated: true,
})
id?: number;

@property({
type: 'string',
required: true,
})
title: string;

@property({
type: 'string',
defaultFn: 'uuid',
})
uuidv1: string;

@property({
type: 'string',
defaultFn: 'uuidv4',
})
uuidv4: string;

@property({
type: 'string',
defaultFn: 'shortid',
})
shortId: string;

@property({
type: 'string',
defaultFn: 'nanoid',
})
nanoId: string;

@property({
type: 'number',
defaultFn: 'customAlias',
})
customAlias: number;

@property({
type: 'string',
defaultFn: 'now',
})
createdAt: string;

constructor(data?: Partial<Task>) {
super(data);
}
}

export interface TaskRelations {
// describe navigational properties here
}

export type TaskWithRelations = Task & TaskRelations;
1 change: 1 addition & 0 deletions src/__tests__/fixtures/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './doctor.repository';
export * from './patient.repository';
export * from './product.repository';
export * from './programming-language.repository';
export * from './task.repository';
export * from './todo-list.repository';
export * from './todo.repository';
export * from './user.repository';
21 changes: 21 additions & 0 deletions src/__tests__/fixtures/repositories/task.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {inject} from '@loopback/core';
import {SequelizeCrudRepository} from '../../../sequelize';
import {PrimaryDataSource} from '../datasources/primary.datasource';
import {Task, TaskRelations} from '../models/index';

export class TaskRepository extends SequelizeCrudRepository<
Task,
typeof Task.prototype.id,
TaskRelations
> {
constructor(@inject('datasources.primary') dataSource: PrimaryDataSource) {
super(Task, dataSource);
}

protected getDefaultFnRegistry() {
return {
...super.getDefaultFnRegistry(),
customAlias: () => Math.random(),
};
}
}
Loading

0 comments on commit c33f639

Please sign in to comment.