将手写笔存储在 JSON / 文档数据库中
Storing stylus in JSON / Document database
我想将 Stylus 存储在 JSON 中,所以它看起来像:
{
"file": "main",
"content:" "body
font: 12px Helvetica, Arial, sans-serif;
a.button
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;"
}
但我知道不使用 /n 是不可能存储的,有什么方法可以自动格式化制表符/标识,以便可以从文档数据库存储和检索,即 MongoDB 或 ArangoDB .
好的,经过我对 ArangoDB 的广泛研究,您可以执行以下操作:
var removeSpace = content.replace(new RegExp(/(?:\r\n|\r|\n)/g, 'g'), '\n')
记住不需要转义\n,
对于其他数据库,这不是必需的,因为这些数据库会自动执行 JSON.stringify 传递的值。
一个例子:(假设 DB 变量持有数据库连接)
import IO from 'fs'
IO.readFile('assets/total.styl', 'utf8', function(error, content) {
if(error) {
throw error
}
var contentUpdated = content.replace(new RegExp(/(?:\r\n|\r|\n)/g, 'g'), '\n')
console.log(contentUpdated)
cursor = DB.query('INSERT { name: "Total", type: "stylus", content: "' + contentUpdated + '"} INTO stylesheets RETURN NEW').then(
console.log(cursor)
)
})
最后,如前所述,您不需要替换任何东西,ArangoDB 自动帮我完成了。
又一个将文件内容存储为字符串属性值的例子,没有RegExp之类的(没有对输入数据进行处理,已经是UTF-8字符串):
const fs = require('fs');
const arangojs = require('arangojs');
const aql = arangojs.aql;
// Const variables for connecting to ArangoDB database
const host = '127.0.0.1'
const port = '8529'
const username = 'root' // default user
const password = '' // blank password by default
const database = '_system' // default database
// Connection to ArangoDB
db = new arangojs.Database({
url: `http://${host}:${port}`,
databaseName: database
});
db.useBasicAuth(username, password); // authenticate
fs.readFile('assets/total.styl', 'utf-8', (error, content) => {
if (error) {
throw error;
}
db.query(aql`
INSERT {
name: "Total",
type: "stylus",
content: ${content}
} INTO stylesheets
RETURN NEW
`).then(
cursor => cursor.all()
).then(
docs => docs.forEach(doc => console.log(doc)),
err => console.error('Query failed:\n', err.response.body)
);
});
生成的文档(系统属性被遗漏):
{
"name": "Total",
"type": "stylus",
"content": "{\r\n \"file\": \"main\",\r\n \"content:\" \"body\r\n font: 12px Helvetica, Arial, sans-serif;\r\n\r\n a.button\r\n -webkit-border-radius: 5px;\r\n -moz-border-radius: 5px;\r\n border-radius: 5px;\"\r\n}"
}
引用RFC:
2.5. Strings
The representation of strings is
similar to conventions used in the C
family of programming languages. A
string begins and ends with quotation
marks. All Unicode characters may be
placed within the quotation marks
except for the characters that must be
escaped: quotation mark, reverse
solidus, and the control characters
(U+0000 through U+001F).
Any character may be escaped.
如果您的 .styl 文件包含此类字符...
00 01 02 03 04 05 06 07 08 09 10 5C 11 12 22 13
它们最终会在文档中正确编码:
{
"name": "Total",
"type": "stylus",
"content": "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\u0010\\u0011\u0012\"\u0013"
}
我想将 Stylus 存储在 JSON 中,所以它看起来像:
{
"file": "main",
"content:" "body
font: 12px Helvetica, Arial, sans-serif;
a.button
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;"
}
但我知道不使用 /n 是不可能存储的,有什么方法可以自动格式化制表符/标识,以便可以从文档数据库存储和检索,即 MongoDB 或 ArangoDB .
好的,经过我对 ArangoDB 的广泛研究,您可以执行以下操作:
var removeSpace = content.replace(new RegExp(/(?:\r\n|\r|\n)/g, 'g'), '\n')
记住不需要转义\n, 对于其他数据库,这不是必需的,因为这些数据库会自动执行 JSON.stringify 传递的值。
一个例子:(假设 DB 变量持有数据库连接)
import IO from 'fs'
IO.readFile('assets/total.styl', 'utf8', function(error, content) {
if(error) {
throw error
}
var contentUpdated = content.replace(new RegExp(/(?:\r\n|\r|\n)/g, 'g'), '\n')
console.log(contentUpdated)
cursor = DB.query('INSERT { name: "Total", type: "stylus", content: "' + contentUpdated + '"} INTO stylesheets RETURN NEW').then(
console.log(cursor)
)
})
最后,如前所述,您不需要替换任何东西,ArangoDB 自动帮我完成了。
又一个将文件内容存储为字符串属性值的例子,没有RegExp之类的(没有对输入数据进行处理,已经是UTF-8字符串):
const fs = require('fs');
const arangojs = require('arangojs');
const aql = arangojs.aql;
// Const variables for connecting to ArangoDB database
const host = '127.0.0.1'
const port = '8529'
const username = 'root' // default user
const password = '' // blank password by default
const database = '_system' // default database
// Connection to ArangoDB
db = new arangojs.Database({
url: `http://${host}:${port}`,
databaseName: database
});
db.useBasicAuth(username, password); // authenticate
fs.readFile('assets/total.styl', 'utf-8', (error, content) => {
if (error) {
throw error;
}
db.query(aql`
INSERT {
name: "Total",
type: "stylus",
content: ${content}
} INTO stylesheets
RETURN NEW
`).then(
cursor => cursor.all()
).then(
docs => docs.forEach(doc => console.log(doc)),
err => console.error('Query failed:\n', err.response.body)
);
});
生成的文档(系统属性被遗漏):
{
"name": "Total",
"type": "stylus",
"content": "{\r\n \"file\": \"main\",\r\n \"content:\" \"body\r\n font: 12px Helvetica, Arial, sans-serif;\r\n\r\n a.button\r\n -webkit-border-radius: 5px;\r\n -moz-border-radius: 5px;\r\n border-radius: 5px;\"\r\n}"
}
引用RFC:
2.5. Strings
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
Any character may be escaped.
如果您的 .styl 文件包含此类字符...
00 01 02 03 04 05 06 07 08 09 10 5C 11 12 22 13
它们最终会在文档中正确编码:
{
"name": "Total",
"type": "stylus",
"content": "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\u0010\\u0011\u0012\"\u0013"
}