mirror of
https://github.com/typicode/json-server.git
synced 2025-08-02 19:52:20 +08:00
Foreign key suffix (#570)
This commit is contained in:
@ -64,7 +64,7 @@ module.exports = function () {
|
||||
default: 'id'
|
||||
},
|
||||
foreignKeySuffix: {
|
||||
alias: 'f',
|
||||
alias: 'fks',
|
||||
description: 'Set foreign key suffix, (e.g. _id as in post_id)',
|
||||
default: 'Id'
|
||||
},
|
||||
|
@ -40,10 +40,11 @@ function createApp (source, object, routes, middlewares, argv) {
|
||||
|
||||
let router
|
||||
|
||||
const { foreignKeySuffix } = argv
|
||||
try {
|
||||
router = jsonServer.router(
|
||||
is.JSON(source) ? source : object,
|
||||
argv
|
||||
foreignKeySuffix ? { foreignKeySuffix } : undefined
|
||||
)
|
||||
} catch (e) {
|
||||
console.log()
|
||||
|
@ -9,14 +9,16 @@ module.exports = {
|
||||
|
||||
// Returns document ids that have unsatisfied relations
|
||||
// Example: a comment that references a post that doesn't exist
|
||||
function getRemovable (db) {
|
||||
function getRemovable (db, opts) {
|
||||
const _ = this
|
||||
const removable = []
|
||||
_.each(db, (coll, collName) => {
|
||||
_.each(coll, (doc) => {
|
||||
_.each(doc, (value, key) => {
|
||||
if (/Id$/.test(key)) {
|
||||
const refName = pluralize.plural(key.slice(0, -2))
|
||||
if (new RegExp(`${opts.foreignKeySuffix}$`).test(key)) {
|
||||
// Remove foreign key suffix and pluralize it
|
||||
// Example postId -> posts
|
||||
const refName = pluralize.plural(key.replace(new RegExp(`${opts.foreignKeySuffix}$`), ''))
|
||||
// Test if table exists
|
||||
if (db[refName]) {
|
||||
// Test if references is defined in table
|
||||
|
@ -11,7 +11,7 @@ const nested = require('./nested')
|
||||
const singular = require('./singular')
|
||||
const mixins = require('../mixins')
|
||||
|
||||
module.exports = (source, argv) => {
|
||||
module.exports = (source, opts = { foreignKeySuffix: 'Id' }) => {
|
||||
// Create router
|
||||
const router = express.Router()
|
||||
|
||||
@ -50,7 +50,7 @@ module.exports = (source, argv) => {
|
||||
})
|
||||
|
||||
// Handle /:parent/:parentId/:resource
|
||||
router.use(nested())
|
||||
router.use(nested(opts))
|
||||
|
||||
// Create routes
|
||||
db.forEach((value, key) => {
|
||||
@ -60,7 +60,7 @@ module.exports = (source, argv) => {
|
||||
}
|
||||
|
||||
if (_.isArray(value)) {
|
||||
router.use(`/${key}`, plural(db, key, argv))
|
||||
router.use(`/${key}`, plural(db, key, opts))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
const express = require('express')
|
||||
const pluralize = require('pluralize')
|
||||
|
||||
module.exports = () => {
|
||||
module.exports = (opts) => {
|
||||
const router = express.Router()
|
||||
|
||||
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request query
|
||||
function get (req, res, next) {
|
||||
const prop = pluralize.singular(req.params.resource)
|
||||
req.query[`${prop}Id`] = req.params.id
|
||||
req.query[`${prop}${opts.foreignKeySuffix}`] = req.params.id
|
||||
req.url = `/${req.params.nested}`
|
||||
next()
|
||||
}
|
||||
@ -15,7 +15,7 @@ module.exports = () => {
|
||||
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request body
|
||||
function post (req, res, next) {
|
||||
const prop = pluralize.singular(req.params.resource)
|
||||
req.body[`${prop}Id`] = req.params.id
|
||||
req.body[`${prop}${opts.foreignKeySuffix}`] = req.params.id
|
||||
req.url = `/${req.params.nested}`
|
||||
next()
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ const write = require('./write')
|
||||
const getFullURL = require('./get-full-url')
|
||||
const utils = require('../utils')
|
||||
|
||||
module.exports = (db, name, opts = { foreignKeySuffix: 'Id' }) => {
|
||||
module.exports = (db, name, opts) => {
|
||||
// Create router
|
||||
const router = express.Router()
|
||||
|
||||
@ -272,8 +272,9 @@ module.exports = (db, name, opts = { foreignKeySuffix: 'Id' }) => {
|
||||
.value()
|
||||
|
||||
// Remove dependents documents
|
||||
const removable = db._.getRemovable(db.getState())
|
||||
|
||||
console.log({opts})
|
||||
const removable = db._.getRemovable(db.getState(), opts)
|
||||
console.log(removable)
|
||||
removable.forEach((item) => {
|
||||
db.get(item.name)
|
||||
.removeById(item.id)
|
||||
|
Reference in New Issue
Block a user