Skip to content
This repository has been archived by the owner on May 18, 2018. It is now read-only.

Commit

Permalink
add db
Browse files Browse the repository at this point in the history
  • Loading branch information
HsuTing committed Jul 6, 2017
1 parent 1eccf79 commit 088b757
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 1 deletion.
21 changes: 21 additions & 0 deletions generators/add/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const _ = require('lodash');

const NeedName = require('./needName');

module.exports = class extends NeedName {
prompting() {
return this.ask;
}

writing() {
const name = _.lowerFirst(this.getName);

this.writeFiles({
'db/field.js': [`bin/fields/${name}.js`, {
name
}]
});
}
};
15 changes: 15 additions & 0 deletions generators/add/templates/db/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
users: (db, type = 'sqlite') => (
db.create('<%= name %>', {
id: 'text PRIMARY KEY NOT NULL'
})
),

create_user: id => db => (
db.insert('<%= name %>', {
id
})
)
};
2 changes: 1 addition & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module.exports = class extends Base {

this.config.set('plugins', plugins);
this.state = extend(this.state, state);
}.bind(this))
}.bind(this));
}

default() {
Expand Down
37 changes: 37 additions & 0 deletions generators/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const Base = require('./../base');

module.exports = class extends Base {
initializing() {
this.addDependencies([
'babel-polyfill',
'cat-utils',
'sqlite3'
]);
}

default() {
if(!this.config.get('cat'))
this.composeWith(require.resolve('./../add'), {
item: 'db',
name: 'users'
});
}

writing() {
this.writePkgScripts({
db: 'node ./bin/db.js',
'db-shell': 'db-shell'
});

this.writeFiles({
'fields.js': 'bin/fields/index.js',
'db.js': 'bin/db.js'
});
}

install() {
this.addInstall();
}
}
11 changes: 11 additions & 0 deletions generators/db/templates/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node
'use strict';

const sqlite = require('cat-utils/lib/sqlite').default;

const fields = require('./fields');
const db = new sqlite();

db.sqlite.serialize(() => {
fields(db);
});
47 changes: 47 additions & 0 deletions generators/db/templates/fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

require('babel-polyfill');
const process = require('process');

const {users, create_user} = require('./users');

module.exports = async (db, type = 'sqlite') => {
const tables = {
users
};
const create_data = {};
const choices = process.argv.length === 2 ? Object.keys(tables) : process.argv.slice(2);
const choice_data = [];

try {
await Promise.all(
choices.map(key => {
if(create_data[key])
choice_data.push(create_data[key]);

if(tables[key])
return tables[key](db, type);

throw new Error(`"${key}" is not in tables.`);
})
);

await choice_data.map(create_func => create_func(db));

switch(type) {
case 'sqlite':
if(choices.includes('users')) {
const user_id = `'363a5f9b-7e93-4f8d-9f1a-a3941fc0905d'`;

if(await create_user(user_id)(db))
console.log(`test user id: ${user_id}`)
}
break;
}

console.log('done');
process.exit();
} catch(e) {
console.log(e);
}
};
19 changes: 19 additions & 0 deletions generators/server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

const _ = require('lodash');
const extend = _.merge;

const Base = require('./../base');

module.exports = class extends Base {
Expand All @@ -11,6 +14,7 @@ module.exports = class extends Base {
if(this.checkPlugins('graphql'))
alias.schemas = 'schemas';

this.state = {};
this.addAlias(alias);
this.addDependencies([
'koa',
Expand Down Expand Up @@ -44,6 +48,18 @@ module.exports = class extends Base {
})
}

prompting() {
return this.prompt([{
type: 'confirm',
name: 'server',
message: 'Use the server',
default: true,
store: true
}]).then(function(state) {
this.state = extend(this.state, state);
}.bind(this));
}

default() {
if(!this.config.get('cat')) {
this.composeWith(require.resolve('./../add'), {
Expand All @@ -57,6 +73,9 @@ module.exports = class extends Base {
name: 'data'
});
}

if(this.state.server)
this.composeWith(require.resolve('./../db'));
}

writing() {
Expand Down

0 comments on commit 088b757

Please sign in to comment.