forked from APIDevTools/swagger-express-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-data-store.js
106 lines (96 loc) · 3.04 KB
/
file-data-store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use strict";
module.exports = FileDataStore;
const DataStore = require("./index");
const Resource = require("./resource");
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
// Inheritance
FileDataStore.prototype = Object.create(DataStore.prototype);
FileDataStore.prototype.constructor = FileDataStore;
/**
* A JSON file data store for REST resources. Each collection is stored as a separate file.
*
* @param {string} baseDir - The base directory where the JSON files will be saved.
* @constructor
* @extends DataStore
*/
function FileDataStore (baseDir) {
DataStore.call(this);
this.__baseDir = baseDir || process.cwd();
}
/**
* Overrides {@link DataStore#__openDataStore} to return data from a JSON file.
*
* @protected
*/
FileDataStore.prototype.__openDataStore = function (collection, callback) {
fs.readFile(getFilePath(this.__baseDir, collection), { encoding: "utf8" }, (err, data) => {
if (err) {
if (err.code === "ENOENT") {
// The file doesn't exist yet, so just return an empty array
callback(null, []);
}
else {
callback(err);
}
}
else {
let resources;
try {
// Parse the JSON data into an array of Resource objects
resources = Resource.parse(data);
}
catch (e) {
callback(e);
return;
}
// Call the callback outside of the try..catch block,
// so we don't catch any errors that happen in third-party code
callback(null, resources);
}
});
};
/**
* Overrides {@link DataStore#__saveDataStore} to store data in a a JSON file.
*
* @protected
*/
FileDataStore.prototype.__saveDataStore = function (collection, resources, callback) {
let self = this;
// Create the directory path
mkdirp(getDirectory(this.__baseDir, collection), (err) => {
if (err) {
callback(err);
}
else {
// Write the JSON data to the file
fs.writeFile(getFilePath(self.__baseDir, collection), JSON.stringify(resources, null, 2), callback);
}
});
};
/**
* Returns the directory where the given collection's JSON file is stored.
*
* @param {string} baseDir - (e.g. "/some/base/path")
* @param {string} collection - (e.g. "/users/jdoe/orders")
* @returns {string} - (e.g. "/some/base/path/users/jdoe")
*/
function getDirectory (baseDir, collection) {
let dir = collection.substring(0, collection.lastIndexOf("/"));
dir = dir.toLowerCase();
return path.normalize(path.join(baseDir, dir));
}
/**
* Returns the full path of the JSON file for the given collection.
*
* @param {string} baseDir - (e.g. "/some/base/path")
* @param {string} collection - (e.g. "/users/jdoe/orders")
* @returns {string} - (e.g. "/some/base/path/users/jdoe/orders.json")
*/
function getFilePath (baseDir, collection) {
let directory = getDirectory(baseDir, collection);
let fileName = collection.substring(collection.lastIndexOf("/") + 1) + ".json";
fileName = fileName.toLowerCase();
return path.join(directory, fileName);
}