-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
103 lines (91 loc) · 3.21 KB
/
index.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
/**
* @typedef { import("./components/connection").NodeExistConnectionOptions } NodeExistConnectionOptions
*/
/**
* @typedef {Object} NodeExist
* @prop { import("xmlrpc").Client } client
* @prop {{shutdown:function, syncToDisk:function}} server
* @prop {{read:function, readAll:function, execute:function, count:function, retrieve:function, retrieveAll:function, releaseResult:function}} queries
* @prop {{describe:function, setPermissions:function, getPermissions:function}} resources
* @prop {{upload:function, parseLocal:function, read:function, remove:function}} documents
* @prop {{create:function, remove:function, describe:function, read:function}} collections
* @prop {Object} indices
* @prop {{getUserInfo:function, list:function}} users
* @prop {{install:function, upload:function, deploy:function, remove:function}} app
*/
const mime = require('mime')
// components
const connection = require('./components/connection')
const database = require('./components/database')
const queries = require('./components/queries')
const resources = require('./components/resources')
const documents = require('./components/documents')
const collections = require('./components/collections')
const indices = require('./components/indices')
const users = require('./components/users')
const app = require('./components/app')
const rest = require('./components/rest')
// exist specific mime types
mime.define({
'application/xquery': ['xq', 'xqs', 'xquery', 'xql', 'xqm'],
'application/xml': ['xconf', 'odd']
})
// helper functions
function applyWith (func, client) {
return function () {
const args = Array.prototype.slice.call(arguments)
return func.apply(null, [client].concat(args))
}
}
function applyEachWith (module, client) {
const applied = {}
for (const property in module) {
const value = module[property]
// leave non-functions untouched
if (typeof value !== 'function') {
applied[property] = value
continue
}
applied[property] = applyWith(value, client)
}
return applied
}
/**
* Receive set of bound components to interact with an exist-db instance
* @param {NodeExistConnectionOptions} options set who connects to which server and how
* @returns {NodeExist} bound components to interact with an exist-db instance
*/
function connect (options) {
const client = connection.connect(options)
return {
client,
server: applyEachWith(database, client),
queries: applyEachWith(queries, client),
resources: applyEachWith(resources, client),
documents: applyEachWith(documents, client),
collections: applyEachWith(collections, client),
indices: applyEachWith(indices, client),
users: applyEachWith(users, client),
app: applyEachWith(app, client)
}
}
async function getRestClient (options) {
const restClient = await connection.restConnection(options)
const { get, put, post, del } = applyEachWith(rest, restClient)
return {
restClient,
get,
put,
post,
del
}
}
exports.readOptionsFromEnv = connection.readOptionsFromEnv
exports.connect = connect
exports.defineMimeTypes = function (mimeTypes) {
mime.define(mimeTypes)
}
exports.getMimeType = function (path) {
return mime.getType(path)
}
exports.getRestClient = getRestClient