Skip to content

Commit

Permalink
feat!: initialize with storage path instead of database
Browse files Browse the repository at this point in the history
The core of this change:

```diff
 const mapServer = createMapServer(
   {},
-  { database: new Database('./example.db') }
+  { storagePath: './map-server-example.db' }
 )
```

See also: [#92], [#93], [this comment][comment].

[#92]: #92
[#93]: #93
[comment]: #100 (comment)
  • Loading branch information
EvanHahn committed Feb 11, 2024
1 parent e2fc73c commit d59fe99
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 27 deletions.
8 changes: 3 additions & 5 deletions README.md
Expand Up @@ -4,7 +4,7 @@

An in-progress offline map style and tile server for Mapeo.

`npm install @mapeo/map-server better-sqlite3`
`npm install @mapeo/map-server`

_⚠️ This is alpha software. No guarantees can be made about the stability of the API at the moment, so proceed with caution. 😄_

Expand All @@ -25,8 +25,6 @@ _⚠️ This is alpha software. No guarantees can be made about the stability of
The default export is a factory function for creating a map server instance, which is built on top of [Fastify](https://www.fastify.io/). Basic usage is as follows:

```js
// better-sqlite3 is a peer dependency and must be installed manually.
const Database = require('better-sqlite3')
// If you're using TypeScript, you may want to use one of the following import syntaxes to get type definitions:
// - `require('@mapeo/map-server').default`
// - `import createMapServer from '@mapeo/map-server'
Expand All @@ -35,7 +33,7 @@ const createMapServer = require('@mapeo/map-server')
// Create the server instance
const mapServer = createMapServer(
{ logger: true },
{ database: new Database('./example.db') }
{ storagePath: './map-server-example.db' }
)

// Run the server!
Expand All @@ -53,7 +51,7 @@ Creates the map server instance

- `fastifyOpts (optional)`: Options object to customize the Fastify instance. Refer to the [official Fastify documentation](https://www.fastify.io/docs/latest/Reference/Server/) for more details.
- `mapServerOpts (required)`: Options object to customize the map server instance. Options include:
- `database: BetterSqlite3.Database (required)`: [BetterSqlite3](https://github.com/WiseLibs/better-sqlite3) database instance representing the SQLite database to use.
- `storagePath: string (required)`: Path to use for persistent map server storage. Happens to be a SQLite database under the hood, but consumers should treat this as an opaque path managed exclusively by the map server.

## API Documentation

Expand Down
3 changes: 0 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -55,9 +55,6 @@
"readable-stream": "^3.6.0",
"uuid": "^8.3.2"
},
"peerDependencies": {
"better-sqlite3": ">=8.7.0"
},
"devDependencies": {
"@fastify/etag": "^3.0.0",
"@mapbox/point-geometry": "^0.1.0",
Expand Down
3 changes: 1 addition & 2 deletions server.ts
@@ -1,10 +1,9 @@
import 'make-promises-safe'
import Database from 'better-sqlite3'

import createMapServer, { MapServerOptions } from './src/app'

const mapServerOpts: MapServerOptions = {
database: new Database('./example.db'),
storagePath: './example.db',
}

// Require the framework and instantiate it
Expand Down
19 changes: 8 additions & 11 deletions src/api/index.ts
@@ -1,4 +1,4 @@
import type { Database } from 'better-sqlite3'
import Database, { type Database as DatabaseInstance } from 'better-sqlite3'
import { FastifyPluginAsync } from 'fastify'
import fp from 'fastify-plugin'
import path from 'path'
Expand All @@ -16,12 +16,12 @@ import createTilesApi, { TilesApi } from './tiles'
import createTilesetsApi, { TilesetsApi } from './tilesets'

export interface MapServerOptions {
database: Database
storagePath: string
}

export interface Context {
activeImports: Map<string, MessagePort>
db: Database
db: DatabaseInstance
piscina: Piscina
upstreamRequestsManager: UpstreamRequestsManager
}
Expand Down Expand Up @@ -81,7 +81,9 @@ function createApi(context: Context): Api {
}
}

function init(db: Database): Context {
function init(storagePath: string): Context {
const db = new Database(storagePath)

// Enable auto-vacuum by setting it to incremental mode
// This has to be set before the anything on the db instance is called!
// https://www.sqlite.org/pragma.html#pragma_auto_vacuum
Expand Down Expand Up @@ -118,15 +120,10 @@ function init(db: Database): Context {

const ApiPlugin: FastifyPluginAsync<MapServerOptions> = async (
fastify,
{ database }
{ storagePath }
) => {
if (database == null)
throw new Error(
`Instance of BetterSqlite3.Database must be specified for 'database' option (https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#class-database)`
)

// Create context once for each fastify instance
const context = init(database)
const context = init(storagePath)

const api = createApi(context)

Expand Down
5 changes: 2 additions & 3 deletions test/test-helpers/create-server.js
@@ -1,7 +1,6 @@
const tmp = require('tmp')
const path = require('path')
const fs = require('fs')
const Db = require('better-sqlite3')

const createMapServer = require('../..')

Expand All @@ -23,11 +22,11 @@ module.exports = createServer
function createServer(t) {
const { name: dataDir, removeCallback } = tmp.dirSync({ unsafeCleanup: true })

const dbPath = path.resolve(dataDir, 'test.db')
const storagePath = path.resolve(dataDir, 'test.db')

const server = createMapServer(
{ logger: false, forceCloseConnections: true },
{ database: new Db(dbPath) }
{ storagePath },
)

t.teardown(async () => {
Expand Down

0 comments on commit d59fe99

Please sign in to comment.