Sqlite To Rest Koa 路由中间件允许您通过 RESTful CRUD 将 SQLite 数据库暴露出来 目录 - 为什么要构建它? - 功能 ...
Sqlite To Rest Koa 路由中间件允许您通过 RESTful CRUD 将 SQLite 数据库暴露出来 目录 - 为什么要构建它? - 功能 ...
Koa routing middleware allowing you to expose a sqlite database via RESTful CRUD
Mostly because I wanted to dig deeper into node web server code, but also because I haven't jumped onto the NoSQL bandwagon and think that web APIs are extremely useful. The result is a modest attempt at automating the CRUD boilerplate that every developer hates, while following the specs to make API consumption intuitive. I chose sqlite to keep the database side of things simple, with the intent that the API isn't serving heavy loads.
This tutorial will walk you through
By installing this library globally, you receive access to sqlite-to-rest.
The CLI currently contains one command generate-skeleton which creates an
initial bare-bones koa server from an existing sqlite database. This should
help you get started.
See sqlite-to-rest --help for more info.
require('sqlite-to-rest') returns an object with two properties.
generateSkeleton: madonna-function
-> promise(undefined)
This will usually be called from the CLI but is also made
available via the js API. Its purpose is to generate a barebones koa
server to get you up and running. In the directory it will:
npm init -f if a package.json doesn't existIt takes two properties
dir: isLadenString
isDirectory
Directory to generate the koa server.
dbPath optional: isSqliteFile
Path to your sqlite3 database.
// example
sqliteToRest
.generateSkeleton({
dir: beerApiDir,
dbPath: 'path/to/your/db.sqlite3',
})
.then(() => {
/* skeleton.js is ready to be ran */
})getSqliteRouter: madonna-function
-> promise(koa-router)
This function generates the RESTful CRUD routing and returns the modified
koa-router instance.
It takes two properties
dbPath: isSqliteFile
Path to your sqlite3 database.
config optional: A routing config object
// example
const app = new require('koa')(),
dbPath = 'path/to/your/db.sqlite3'
getSqliteRouter({ dbPath }).then((router) => {
app.use(router.routes())
// ...
})The following is a list of the available crud operations made available by the
RESTful API in the form of pseudo examples. All assume a beer table with two
columns id INTEGER PRIMARY KEY and name which is nullable.
As noted in limitations, Be aware that unsafe methods (DELETE and POST) can only affect one row at a time.
GET This allows for the most variation. Click here for all available query operators. Keep in mind the following examples ignore proper query encoding
Headers may be specified right below urls
/beer
Requests for all rows
/beer?id=1
Where id = 1
/beerrange: rows=0-2
First three rows
/beerrange: rows=-5
Last five rows
/beerrange: rows=0-
As many rows as the server is able to provide, which in practice will be
the smaller of maxRange and total row count.
/beerrange: rows=1-
As many rows as the server is able to provide, starting from row 1.
/beerorder: name
Ordered by name ascending
/beerorder: name desc
Ordered by name descending
/beerorder: name desc,id
Contrived, but orders first by name descending, and in the case of a tie
by id ascending.
/beer?id>1
Where id > 1
**/beer?id>=2&id= 2 and id =1&id= 1 AND id .isDirectory](https://nodejs.org/api/fs.html#fs_class_fs_stats)
Query conditions must be delimited by ampersands e.g. id>5&name!=Spotted Cow
Binary operators (require a value after)
=
!=
>=
**`. The reason it's configurable is that calculating max-range may be more work than its worth, depending on the load of the server and the size of your tables.
These aren't all necessarily custom, but all their usage falls outside what's defined in the spec and thus need clarification.
GET
max-range: This header is returned when the requested number of rows
surpasses the configured maxRange. Note the
request might not specify the range header, but the number of rows
resulting in that resource will still be checked.
content-range: rfc7233 states
only the 206 (Partial Content) and 416 (Range Not Satisfiable) status codes describe a meaning for Content-Range.
When sqlite-to-rest responds with a 200 status code, the content-range
header is sent with the 206 format of -/.
When a request is sent without a range header and the number of resulting
rows surpasses maxRange, a 400 is returned
with content-range set in the 416 format of */
Note this header may be returned in a HEAD response.
accept-order: This will be returned if the request header order
had bad syntax or specified incorrect column names. For details, refer
to HEAD -> accept-order below.
HEAD
accept-order: accept-order is just a comma-delimited list of the
requested table columns, intended to tell the client the valid columns
able to be used in the request header order.
max-range: The configured maxRange
of the requested table.
content-range: This header will only be sent if the table has been
configured with the flag sendContentRangeInHEAD.
In that case, content-range is set to the 416 format of */
npm test
暂无开放 Issues,或尚未同步最近议题。