Skip to content

Commit

Permalink
ssr built app uses serialized content database
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiatkk1 committed Feb 11, 2021
1 parent ebaeded commit 8bbc5a8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
31 changes: 30 additions & 1 deletion packages/content/lib/database.js
Expand Up @@ -5,6 +5,7 @@ const chokidar = require('chokidar')
const JSON5 = require('json5')
const Loki = require('@lokidb/loki').default
const LokiFullTextSearch = require('@lokidb/full-text-search').default
const LokiFSStorage = require('@lokidb/fs-storage').default
const logger = require('consola').withScope('@nuxt/content')
const { default: PQueue } = require('p-queue')
const { Markdown, YAML, CSV, XML } = require('../parsers')
Expand Down Expand Up @@ -71,6 +72,34 @@ class Database extends Hookable {
logger.info(`Parsed ${this.items.count()} files in ${s},${Math.round(ns / 1e8)} seconds`)
}

/**
* Restore database from file
* @param {string} dir - Directory containing database dump file.
*/
async load (dir) {
const dbFilename = this.db.filename
this.db.filename = join(dir, dbFilename)
await this.db.initializePersistence({ adapter: new LokiFSStorage() })
await this.db.loadDatabase()
this.db.filename = dbFilename

// recreate references
this.items = this.db.getCollection('items')
this.dirs = this.items.mapReduce(doc => doc.dir, dirs => [...new Set(dirs)])
}

/**
* Store database info file
* @param {string} dir - Directory containing database dump file.
*/
async save (dir) {
const dbFilename = this.db.filename
this.db.filename = join(dir, dbFilename)
await this.db.initializePersistence({ adapter: new LokiFSStorage() })
await this.db.saveDatabase()
this.db.filename = dbFilename
}

/**
* Walk dir tree recursively
* @param {string} dir - Directory to browse.
Expand Down Expand Up @@ -171,7 +200,7 @@ class Database extends Hookable {
*/
async parseFile (path) {
const extension = extname(path)
// If unkown extension, skip
// If unknown extension, skip
if (!EXTENSIONS.includes(extension) && !this.extendParserExtensions.includes(extension)) {
return
}
Expand Down
19 changes: 16 additions & 3 deletions packages/content/lib/index.js
Expand Up @@ -108,12 +108,18 @@ module.exports = async function (moduleOptions) {
)
database.hook('file:updated', event => ws.broadcast(event))

// Initialize database from file system
await database.init()
if (this.options.server && !this.options.dev && this.options.ssr) {
// Load database from dist
const dir = resolve(this.options.buildDir, 'dist', 'server')
await database.load(dir)
} else {
// Initialize database from file system
await database.init()
}

// close database when Nuxt closes
this.nuxt.hook('close', () => database.close())
// listen to nuxt server to updrag
// listen to nuxt server to upgrade

const $content = function () {
let options
Expand Down Expand Up @@ -225,6 +231,13 @@ module.exports = async function (moduleOptions) {
}
})
} else {
if (!this.options.dev) {
this.nuxt.hook('build:done', async () => {
const dir = resolve(this.options.buildDir, 'dist', 'server')
await database.save(dir)
})
}

this.addPlugin({
fileName: 'content/plugin.client.js',
src: join(__dirname, '../templates/plugin.client.js'),
Expand Down
1 change: 1 addition & 0 deletions packages/content/package.json
Expand Up @@ -12,6 +12,7 @@
],
"main": "lib/",
"dependencies": {
"@lokidb/fs-storage": "^2.1.0",
"@lokidb/full-text-search": "^2.1.0",
"@lokidb/loki": "^2.1.0",
"@nuxt/types": "^2.14.12",
Expand Down

0 comments on commit 8bbc5a8

Please sign in to comment.