Skip to content

Commit

Permalink
Merge branch 'release/0.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Feb 3, 2024
2 parents ac44682 + cae0dfd commit ee2f871
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
7 changes: 5 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"private": true,
"version": "0.0.6",
"workspaces": [
"packages/*",
"examples/*"
Expand Down
121 changes: 121 additions & 0 deletions packages/sails-content/index.js
Original file line number Diff line number Diff line change
@@ -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
})()
17 changes: 14 additions & 3 deletions packages/sails-content/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT"
"license": "MIT",
"waterlineAdapter": {
"interfaces": [
"semantic",
"queryable"
],
"features": [
"unique"
]
},
"dependencies": {
"gray-matter": "^4.0.3"
}
}

0 comments on commit ee2f871

Please sign in to comment.