-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremotestorage-module-documents.js
188 lines (173 loc) · 4.37 KB
/
remotestorage-module-documents.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* File: Documents
*
* Maintainer: - Jorin Vogel <[email protected]>
* Version: - 0.2.1
*
* This module stores lists of documents.
* A document has the fields title, content and lastEdited.
*
* This module is used by Litewrite.
*
*/
var uuid = require('uuid/v4')
function Documents (privateClient, publicClient) {
// Schema
privateClient.declareType('text', {
description: 'A text document',
type: 'object',
'$schema': 'http://json-schema.org/draft-03/schema#',
additionalProperties: true,
properties: {
title: {
type: 'string',
required: true
},
content: {
type: 'string',
required: true,
default: ''
},
lastEdited: {
type: 'integer',
required: true
}
}
})
var documentsModule = {
/**
* Method: privateList
*
* List all private documents.
*
* Parameters:
*
* path - a pathstring where to scope the client to.
*
* Returns:
* A privateClient scoped to the given path
* and extended with the listMethods.
* It also supports all <BaseClient methods at http://remotestoragejs.com/doc/code/files/baseclient-js.html>
*/
privateList: function (path) {
return Object.assign(privateClient.scope(path + '/'), listMethods)
},
/**
* Method: publicList
*
* List all public documents.
*
* Parameters:
*
* path - a pathstring where to scope the client to.
*
* Returns:
* A publicClient scoped to the given path
* and extended with the listMethods.
* It also supports all <BaseClient methods at http://remotestoragejs.com/doc/code/files/baseclient-js.html>
*/
publicList: function (path) {
return Object.assign(publicClient.scope(path + '/'), listMethods)
}
}
/**
* Class: listMethods
*
*/
var listMethods = {
/**
* Method: add
*
* Create a new document
*
* Parameters:
* doc - the document data to store as JSON object.
*
* Returns:
* A promise, which will be fulfilled with the created document as JSON object.
* The created document also contains the newly created id property.
*/
add: function (doc) {
var id = uuid()
return this.set(id, doc)
},
/**
* Method: set
*
* Update or create a document for a specified id.
*
* Parameters:
* id - the id the document is at.
* doc - the document data to store as JSON object.
*
* Returns:
* A promise, which will be fulfilled with the updated document.
*/
set: function (id, doc) {
return this.storeObject('text', id.toString(), doc).then(function () {
doc.id = id
return doc
})
},
/**
* Method: get
*
* Get a document.
*
* Parameters:
* id - the id of the document you want to get.
*
* Returns:
* A promise, which will be fulfilled with the document as JSON object.
*/
get: function (id) {
return this.getObject(id.toString()).then(function (obj) {
return obj || {}
})
},
/**
* Method: addRaw
*
* Store a raw document of the specified contentType at shared/.
*
* Parameters:
* contentType - the content type of the data (like 'text/html').
* data - the raw data to store.
*
* Returns:
* A promise, which will be fulfilled with the path of the added document.
*/
addRaw: function (contentType, data) {
var id = uuid()
var path = 'shared/' + id
var url = this.getItemURL(path)
return this.storeFile(contentType, path, data).then(function () {
return url
})
},
/**
* Method: setRaw
*
* Store a raw doccument of the specified contentType at shared/.
*
* Parameters:
* id - id of the document to update
* contentType - the content type of the data (like 'text/html').
* data - the raw data to store.
*
* Returns:
* A promise, which will be fulfilled with the path of the added document.
*/
setRaw: function (id, contentType, data) {
var path = 'shared/' + id
return this.storeFile(contentType, path, data)
}
}
return {
exports: documentsModule
}
}
module.exports = {
name: 'documents',
builder: Documents
}