-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"private": true, | ||
"version": "0.0.6", | ||
"workspaces": [ | ||
"packages/*", | ||
"examples/*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |