This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repository): add
defaultFn
support (#40)
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
1 parent
7e36924
commit c33f639
Showing
10 changed files
with
378 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
} | ||
} |
Oops, something went wrong.