"description": "",
"main": "./server/app.js",
"scripts": {
- "test": "mocha test/mocha/*"
+ "test": "mocha test/mocha/DB.test.js"
},
"author": "",
"license": "ISC",
"body-parser": "^1.14.2",
"bootstrap-sass": "^3.3.6",
"chalk": "^1.1.1",
+ "co": "^4.6.0",
"commander": "^2.9.0",
"express": "^4.13.3",
"sequelize": "^3.18.0",
-
/**
* DBを操作するクラス
*
* @returns {object}
*/
function DB() {
+ 'use strict';
+
+ var fs = require('fs');
+ var Sequelize = require('sequelize');
+ var co = require('co');
+
+ var sequelize;
+
+ var dbOption = {
+ dialect: 'sqlite'
+ };
+
+ function sync() {
+ return new Promise(function(resolve) {
+ Promise.all([Directory.sync(), File.sync()])
+ .then(function() {
+ var root = {
+ name: 'root',
+ parent: ''
+ };
+ return Directory.insert(root);
+ })
+ .then(function() {
+ return {};
+ });
+ });
+ }
+
+ if(process.env['NODE_ENV'] === 'production') {
+ var dbPath = __dirname + '/../../user-specific-files/db/db.workspace';
+ dbOption.storage = dbPath;
+ // if doesn't exist workspace.db, create.
+ try {
+ fs.accessSync(dbPath, fs.R_OK | fs.W_OK);
+ } catch(e) {
+ fs.writeFileSync(dbPath, '');
+ }
+ }
+
+ sequelize = new Sequelize('','','', dbOption);
+
+ var File = sequelize.define('file', {
+ fileId: {
+ type: Sequelize.UUID,
+ field: 'file_id',
+ primaryKey: true
+ },
+ name: {
+ type: Sequelize.STRING,
+ field: 'name',
+ allowNull: false
+ },
+ directoryName: {
+ type: Sequelize.STRING,
+ field: 'directory_name',
+ allowNull: false
+ }
+ });
+
+ var Directory = sequelize.define('directory', {
+ name: {
+ type: Sequelize.STRING,
+ field: 'name',
+ primaryKey: true
+ },
+ parent: {
+ type: Sequelize.STRING,
+ field: 'parent',
+ allowNull: false
+ }
+ });
+
+ Directory.hasMany(File, {foreignKey: 'directoryName'});
+ File.belongsTo(Directory, {foreignKey: 'name'});
+
+ return sync();
}
+
+module.exports = DB;
var DB = require('../../server/class/DB');
var expect = require('chai').expect;
- describe('DB クラス', function() {
-
- })
+ //process.env['NODE_ENV'] = 'production';
+ describe('DB クラス', function() {
+ var db;
+ var test;
+ before(function(done) {
+ var promise = new DB();
+ promise.then(function(r) {
+ db = r;
+ done();
+ });
+ });
+ it('test of test', function(done) {
+ db.sayHello();
+ done();
+ });
+ });
})();