-
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.
feat: open & handle sessions automatically (#10)
* feat: open & handle sessions automatically * chore: restyle & reuse example classes * feat: allow sessions targeting multiple namespaces * chore: add autoSession to examples * chore: add autoSession in readme * build: run ravendb container before tests * test: add autoSession TS types * chore: remove example uppercase, update toc * Update lib/helpers.js Co-authored-by: Matteo Pietro Dazzi <[email protected]> * Update index.js Co-authored-by: Matteo Pietro Dazzi <[email protected]> * style: improve autoSession condition check Co-authored-by: Matteo Pietro Dazzi <[email protected]>
- Loading branch information
Showing
18 changed files
with
475 additions
and
75 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
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,12 @@ | ||
export class Category { | ||
constructor(name, description) { | ||
this.name = name | ||
this.description = description | ||
} | ||
} | ||
|
||
export class Person { | ||
constructor(name) { | ||
this.name = name | ||
} | ||
} |
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,55 @@ | ||
import Fastify from 'fastify' | ||
|
||
import plugin from '../index.js' | ||
import { Category, Person } from './lib/classes.js' | ||
import { port, url, databaseName, categories, people } from './lib/constants.js' | ||
|
||
const routeOptions = { rvn: { autoSession: ['local', 'remote'] } } | ||
|
||
const start = async () => { | ||
const fastify = Fastify({ logger: true }) | ||
await fastify.register(plugin, { name: 'local', url, databaseName }) | ||
await fastify.register(plugin, { | ||
name: 'remote', | ||
url: 'http://live-test.ravendb.net/', | ||
databaseName | ||
}) | ||
|
||
fastify.post(`/${categories}`, routeOptions, async (req, reply) => { | ||
const category = new Category(req.body.name, req.body.description) | ||
|
||
await req.rvn.remote.store(category) | ||
|
||
reply.send(category) | ||
}) | ||
|
||
fastify.get(`/${categories}/:id`, routeOptions, async (req, reply) => { | ||
const category = await req.rvn.remote.load( | ||
`${categories}/${req.params.id}`, | ||
Category | ||
) | ||
|
||
reply.send(category) | ||
}) | ||
|
||
fastify.post(`/${people}`, routeOptions, async (req, reply) => { | ||
const person = new Person(req.body.name) | ||
|
||
await req.rvn.local.store(person) | ||
|
||
reply.send(person) | ||
}) | ||
|
||
fastify.get(`/${people}/:id`, routeOptions, async (req, reply) => { | ||
const person = await req.rvn.local.load( | ||
`${people}/${req.params.id}`, | ||
Person | ||
) | ||
|
||
reply.send(person) | ||
}) | ||
|
||
await fastify.listen({ port }) | ||
} | ||
|
||
start() |
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,30 @@ | ||
import Fastify from 'fastify' | ||
|
||
import plugin from '../index.js' | ||
import { Person } from './lib/classes.js' | ||
import { port, url, databaseName, people } from './lib/constants.js' | ||
|
||
const routeOptions = { rvn: { autoSession: true } } | ||
|
||
const start = async () => { | ||
const fastify = Fastify({ logger: true }) | ||
await fastify.register(plugin, { url, databaseName }) | ||
|
||
fastify.post(`/${people}`, routeOptions, async (req, reply) => { | ||
const person = new Person(req.body.name) | ||
|
||
await req.rvn.store(person) | ||
|
||
reply.send(person) | ||
}) | ||
|
||
fastify.get(`/${people}/:id`, routeOptions, async (req, reply) => { | ||
const person = await req.rvn.load(`${people}/${req.params.id}`, Person) | ||
|
||
reply.send(person) | ||
}) | ||
|
||
await fastify.listen({ port }) | ||
} | ||
|
||
start() |
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
Oops, something went wrong.