From 1efb9c8025e3b878098289d81fbe91bc7e4abcb1 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Sat, 3 Feb 2024 13:53:22 +0100 Subject: [PATCH 1/4] feat(sails-content): add find, findOne, and, count methods --- packages/sails-content/index.js | 121 ++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 packages/sails-content/index.js diff --git a/packages/sails-content/index.js b/packages/sails-content/index.js new file mode 100644 index 0000000..cf77ed8 --- /dev/null +++ b/packages/sails-content/index.js @@ -0,0 +1,121 @@ +const fs = require('fs').promises +const path = require('path') +const matter = require('gray-matter') +module.exports = (function sailsContent() { + const datastores = {} + + const adapter = { + // The identity of this adapter, to be referenced by datastore configurations in a Sails app. + identity: 'sails-content', + + // Waterline Adapter API Version + adapterApiVersion: 1, + + datastores: datastores, + + defaults: { + schema: true, + + dir: 'content' + }, + registerDatastore: function registerDatastore(datastoreConfig, models, cb) { + const identity = datastoreConfig.identity + + if (datastores[identity]) { + throw new Error('Datastore `' + identity + '` is already registered.') + } + + const datastore = { + migrate: 'safe', + config: datastoreConfig + } + datastores[identity] = datastore + return cb() + }, + find: async function find(datastoreName, query, cb) { + const datastore = datastores[datastoreName] + const contentDir = path.join(datastore.config.dir, query.using) + const files = await fs.readdir(contentDir) + let records = [] + let id = 1 + for (const file of files) { + const content = await fs.readFile(path.join(contentDir, file), { + encoding: 'utf8' + }) + const { data: record, content: mdContent } = matter(content) + record.id = id++ + record.slug = path.parse(file).name + record.content = mdContent + records.push({ ...record }) + } + + records = records.map((item) => + query.criteria.select.reduce((acc, key) => { + if (item.hasOwnProperty(key) && !query.criteria.omit?.includes(key)) { + acc[key] = item[key] + } + return acc + }, {}) + ) + + return cb(undefined, records) + }, + findOne: async function find(datastoreName, query, cb) { + const datastore = datastores[datastoreName] + const contentDir = path.join(datastore.config.dir, query.using) + const content = await fs.readFile( + path.join(contentDir, query.slug || query), + { + encoding: 'utf8' + } + ) + const id = 1 + let { data: record, content: mdContent } = matter(content) + record.id = id + record.slug = path.parse(content).name + record.content = mdContent + record = query.criteria.select.reduce((acc, key) => { + if (record.hasOwnProperty(key) && !query.criteria.omit?.includes(key)) { + acc[key] = record[key] + } + return acc + }, {}) + return cb(undefined, record) + }, + count: async function count(datastoreName, query, cb) { + const datastore = datastores[datastoreName] + const contentDir = path.join(datastore.config.dir, query.using) + const files = await fs.readdir(contentDir) + let records = [] + let id = 1 + for (const file of files) { + const content = await fs.readFile(path.join(contentDir, file), { + encoding: 'utf8' + }) + const { data: record, content: mdContent } = matter(content) + record.id = id++ + record.slug = path.parse(file).name + record.content = mdContent + records.push({ ...record }) + } + + records = records.filter((item) => { + for (const key in query.criteria.where) { + if (item[key] !== query.criteria.where[key]) { + return false + } + } + return true + }) + + return cb(undefined, records.length) + }, + define: function define(datastoreName, tableName, definition, cb) { + return cb() + }, + drop: function drop(datastoreName, tableName, relations, cb) { + return cb() + } + } + return adapter +})() From 5745b98b179676f476a23baf3570ea1983aa13a2 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Sat, 3 Feb 2024 13:58:15 +0100 Subject: [PATCH 2/4] chore: package-lock --- package-lock.json | 7 +++++-- packages/sails-content/package.json | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 479a6f6..a7cb806 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3361,12 +3361,15 @@ } }, "packages/sails-content": { - "version": "0.0.0", + "version": "0.0.1", "license": "MIT", + "dependencies": { + "gray-matter": "^4.0.3" + }, "devDependencies": {} }, "packages/sails-hook-content": { - "version": "0.0.5", + "version": "0.0.6", "license": "MIT", "dependencies": { "plugin-sails-content": "^0.0.5" diff --git a/packages/sails-content/package.json b/packages/sails-content/package.json index 8c21492..b51d64d 100644 --- a/packages/sails-content/package.json +++ b/packages/sails-content/package.json @@ -1,12 +1,23 @@ { "name": "sails-content", - "version": "0.0.0", + "version": "0.0.1", "description": "Sails/Waterline adapter for file-based content.", "main": "index.js", - "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Kelvin Omereshone ", - "license": "MIT" + "license": "MIT", + "waterlineAdapter": { + "interfaces": [ + "semantic", + "queryable" + ], + "features": [ + "unique" + ] + }, + "dependencies": { + "gray-matter": "^4.0.3" + } } From c728944901987b4eee95857cf7a3bfc513112012 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Sat, 3 Feb 2024 13:58:56 +0100 Subject: [PATCH 3/4] chore: add version to root --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 38269a7..c94edd0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "private": true, + "version": "0.0.5", "workspaces": [ "packages/*", "examples/*" From cae0dfd4f7df025a46ea18e4fa02b57226ffec08 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Sat, 3 Feb 2024 13:59:24 +0100 Subject: [PATCH 4/4] chore: release 0.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c94edd0..f5babc4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "private": true, - "version": "0.0.5", + "version": "0.0.6", "workspaces": [ "packages/*", "examples/*"