-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f52771
commit f8b7ae9
Showing
3 changed files
with
73 additions
and
32 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,33 +1,17 @@ | ||
console.log('Try npm run lint/fix!'); | ||
|
||
const longString = | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut aliquet diam.'; | ||
|
||
const trailing = 'Semicolon'; | ||
|
||
const why = {am: 'I tabbed?'}; | ||
|
||
const iWish = "I didn't have a trailing space..."; | ||
|
||
const sicilian = true; | ||
|
||
const vizzini = sicilian ? !sicilian : sicilian; | ||
|
||
const re = /foo {3}bar/; | ||
|
||
export function doSomeStuff( | ||
withThis: string, | ||
andThat: string, | ||
andThose: string[] | ||
) { | ||
//function on one line | ||
if (!andThose.length) { | ||
return false; | ||
import {Sequelize} from 'sequelize'; | ||
import {Router} from 'express'; | ||
import {sequelizeCrudConfig} from './types'; | ||
import {getPath} from './utils'; | ||
import buildModelRoutes from './routes'; | ||
|
||
const sequelizeCrud = (sequelize: Sequelize, config: sequelizeCrudConfig) => { | ||
console.group('Building express crud routes:'); | ||
const router = Router(); | ||
for (const basePath in config) { | ||
const path = getPath(basePath); | ||
router.use(path, buildModelRoutes(path, sequelize, config[path])); | ||
} | ||
console.log(withThis); | ||
console.log(andThat); | ||
console.dir(andThose); | ||
console.log(longString, trailing, why, iWish, vizzini, re); | ||
return; | ||
} | ||
// TODO: more examples | ||
console.groupEnd(); | ||
}; | ||
|
||
export default sequelizeCrud; |
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,26 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import {Router} from 'express'; | ||
import {getListOptions, getOneOptions, pureModelType} from '../types'; | ||
import {buildOptionsFromConfig} from '../middleware/config'; | ||
|
||
const getOneRoute = ( | ||
model: pureModelType, | ||
router: Router, | ||
config: getOneOptions | ||
) => | ||
router.get('/:resourceId', async (req, res) => { | ||
const {middleware, ...sequelizeOptions} = config; | ||
try { | ||
const options = await buildOptionsFromConfig(sequelizeOptions, req, res); | ||
|
||
const data = await model.findByPk(req.params.resourceId, { | ||
...options, | ||
}); | ||
res.json(data); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send(error); | ||
} | ||
}); | ||
|
||
export default getOneRoute; |
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,31 @@ | ||
import {Router} from 'express'; | ||
import {sequelizeCrudConfigModel} from '../types'; | ||
import {getModel} from '../utils'; | ||
import {Sequelize} from 'sequelize'; | ||
|
||
import getListRoute from './getList'; | ||
import getOneRoute from './getOne'; | ||
|
||
const buildModelRoutes = ( | ||
path: string, | ||
sequelize: Sequelize, | ||
config: sequelizeCrudConfigModel | ||
) => { | ||
const router = Router(); | ||
const {model: m, operations} = config; | ||
const model = getModel(sequelize, m); | ||
console.group(`Model [${model.name}]`); | ||
if (operations.getList) { | ||
getListRoute(model, router, operations.getList); | ||
console.log('GET', `/${path}/`, '[getList]'); | ||
} | ||
if (operations.getOne) { | ||
getOneRoute(model, router, operations.getOne); | ||
console.log('GET', `/${path}/:resourceId`, '[getOne]'); | ||
} | ||
|
||
console.groupEnd; | ||
return router; | ||
}; | ||
|
||
export default buildModelRoutes; |