Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.git
dist
coverage
logs
*.log
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:20-alpine

WORKDIR /app

RUN npm install -g pnpm

COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

COPY . .
RUN pnpm build

EXPOSE 3000

CMD ["node", "lib/bin.js", "fixtures/db.json"]
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ $ curl http://localhost:3000/posts/1

Run `json-server --help` for a list of options

## Run with Docker

Build the image:

```shell
docker build -t json-server .
```

Run the container:

```shell
docker run --rm -p 3000:3000 json-server
```

API URL:

```text
http://localhost:3000
```

## Sponsors ✨

### Gold
Expand Down
17 changes: 17 additions & 0 deletions src/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ await test('find', async (t) => {
}
})

await test('find supports _embed=person when the resource key is persons', () => {
const db = new Low<Data>(new Memory<Data>(), {
feedbacks: [{ id: '1', text: 'a', personId: '1' }],
persons: [{ id: '1', name: 'Tim' }],
})
const service = new Service(db)

assert.deepEqual(service.find('feedbacks', { where: {}, embed: 'person' }), [
{
id: '1',
text: 'a',
personId: '1',
person: { id: '1', name: 'Tim' },
},
])
})

await test('create', async () => {
const post = { title: 'new post' }
const res = await service.create(POSTS, post)
Expand Down
8 changes: 7 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ function ensureArray(arg: string | string[] = []): string[] {

function embed(db: Low<Data>, name: string, item: Item, related: string): Item {
if (inflection.singularize(related) === related) {
const relatedData = db.data[inflection.pluralize(related)] as Item[]
const pluralized = db.data[inflection.pluralize(related)]
const suffixed = db.data[`${related}s`]
const relatedData = Array.isArray(pluralized)
? pluralized
: Array.isArray(suffixed)
? suffixed
: undefined
if (!relatedData) {
return item
}
Expand Down