TypeScript で、CSVデータからsqlite3 dbに保存する。
index:
環境
TypeScript
node.js
npm
sqlite3
参考のコード
データ追加
https://github.com/kuc-arc-f/ts_express4_csv/blob/master/csv_import.ts
insert_db(db:any ,item : Imdat): void{ let sql = ` INSERT INTO mdats (mdate, hnum, lnum, up_date) VALUES (date('${ item.date }'), ?, ?, CURRENT_TIMESTAMP) `; //console.log( sql ) let stmt = db.prepare(sql ) stmt.run( item.hnum, item.lnum ) stmt.finalize() db.close() }
TypeScript + node/express + Vue.js, mongoDB版のCRUD作成
index:
概要:
前の TypeScript 関連となり
node.js + express + Vue.js で
mongoDB の構成になります
環境
TypeScript
node.js
npm
express
Vue.js
mongoDB
参考のコード
実装など
api.ts
https://github.com/kuc-arc-f/ts_express3_mongo/blob/master/routes/api.ts
/******************************** * *********************************/ router.get('/tasks_index', function(req: RequestEx, res: Response, next: NextFunction ) { let db = req.db; let collection = db.get('tasks'); let items:any[] = []; collection.find({},{},function(e:any,docs: any[]){ docs.forEach( function (item) { //toTimeString items.push(item); }); var param = {"docs": items }; res.json(param); db.close(); }); });
Vue.js
new
https://github.com/kuc-arc-f/ts_express3_mongo/blob/master/dist/views/tasks/new.ejs
index
https://github.com/kuc-arc-f/ts_express3_mongo/blob/master/dist/views/tasks/index.ejs
show
https://github.com/kuc-arc-f/ts_express3_mongo/blob/master/dist/views/tasks/show.ejs
edit
https://github.com/kuc-arc-f/ts_express3_mongo/blob/master/dist/views/tasks/edit.ejs
TypeScript + node/express + Vue.js, sqlite版のCRUD作成
index:
概要:
前の TypeScript 関連となり
node.js + express + Vue.js で
db は、sqliteになります
環境
TypeScript
node.js
npm
express
Vue.js
参考のコード
実装など
・表の作成
https://github.com/kuc-arc-f/ts_express2_sqlite/blob/master/doc/tasks.sql
api の一部
typeScript 対応で、型の追加など
https://github.com/kuc-arc-f/ts_express2_sqlite/blob/master/routes/api.ts
/******************************** * *********************************/ router.get('/tasks_index', function(req: Request, res: Response, next: NextFunction ) { let db = new sqlite3.Database( dbfileName ) let items:any[] = []; db.serialize(function() { db.all('SELECT id,title, content FROM tasks order by id desc;', function(err: any, rows:any[] ) { rows.forEach( function (item:any[] ) { items.push(item ); // console.log(item ) }); var param = {"docs": items }; res.json(param); }); }); db.close(); }); /******************************** * *********************************/ router.post('/tasks_new', (req: Request, res: Response, next: NextFunction) => { let data = req.body // console.log(data ) var db = new sqlite3.Database( dbfileName ) db.serialize(function() { var stmt = db.prepare('INSERT INTO tasks (title, content) VALUES (?, ?)') stmt.run(data.title, data.content ) stmt.finalize() res.json(data); }); db.close(); });
Vue.js
new
https://github.com/kuc-arc-f/ts_express2_sqlite/blob/master/dist/views/tasks/new.ejs
index
https://github.com/kuc-arc-f/ts_express2_sqlite/blob/master/dist/views/tasks/index.ejs
show
https://github.com/kuc-arc-f/ts_express2_sqlite/blob/master/dist/views/tasks/show.ejs
edit
https://github.com/kuc-arc-f/ts_express2_sqlite/blob/master/dist/views/tasks/edit.ejs
TypeScript +node/express の設定編
index:
概要:
前の TypeScript 関連となり
node.js + express構成となります
環境
TypeScript
node.js
npm
express
参考の設定
追加の手順
express app npm install npm install typescript npm install @types/node npm install @types/express
app.ts
https://github.com/kuc-arc-f/ts_express1/blob/master/app.ts
const express = require('express'); const path = require('path'); const cookieParser = require('cookie-parser'); const logger = require('morgan'); import {indexRouter} from "./routes/index"; //import {tasksRouter} from "./routes/tasks"; const app = express(); const expressLayouts = require('express-ejs-layouts'); app.use(expressLayouts); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({extended: false})); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); const p = new Promise(() => { }); // app.use('/', indexRouter ); //app.use('/tasks', tasksRouter ); export {app}
bin/www.ts
https://github.com/kuc-arc-f/ts_express1/blob/master/bin/www.ts
import を、設定します
import {app} from "../app";
package.json
https://github.com/kuc-arc-f/ts_express1/blob/master/package.json
ルーティング
routes/ index.ts : router を、export します
import {NextFunction, Request, Response} from "express"; const express = require('express'); //const path = require('path'); const router = express.Router(); /* GET home page. */ router.get('/', function(req: Request, res: Response, next: NextFunction) { res.render('index', { title: 'Express' }); // res.send('respond with a resource-1122'); }); export {router as indexRouter}
TypeScript, tsconfigでの自動コンパイル #TypeScript #javascript
index:
環境
TypeScript
node.js
npm
コンパイル方法
tsconfig.json
https://github.com/kuc-arc-f/ts_start2/blob/master/tsconfig.json
dist の下に、出力されます
{ "compilerOptions": { "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "outDir": "./dist", /* Redirect output structure to the directory. */ "strict": true, /* Enable all strict type-checking options. */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ } }
・tscコマンド
下記プロセスを、起動きておきます。
tsファイルがコンパイルされます。
tsc -W
参考の設定
関連のページ
TypeScriptの導入編
index:
概要:
TypeScript の導入メモとなります
・ javascript派生で、進化系言語のイメージあります
コンパイラでのチェックがされたり、
モジュールのインポート、
クラスが使えて便利な部分は ありそうです。
環境
TypeScript
node.js
npm
参考にしたページ
https://www.typescriptlang.org/
https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
・型や、クラスなど
https://www.typescriptlang.org/docs/handbook/basic-types.html
https://www.typescriptlang.org/docs/handbook/interfaces.html
追加の手順
npm install -g typescript
コード書きます
https://github.com/kuc-arc-f/ts_start1/blob/master/greeter1.ts
function greeter(person: string) { return "Hello, " + person; } let user = "Jane User112233"; console.log( greeter(user) ); document.body.textContent = greeter(user);
tsc コンパイル
tsc greeter.1ts
greeter1.js が生成されます
・HTMLから、読み込み
<!DOCTYPE html> <html> <head><title>TypeScript Greeter</title></head> <body> <script src="greeter1.js"></script> </body> </html>
・クラスの例
class Student { fullName: string; constructor(public firstName: string, public middleInitial: string, public lastName: string) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface Person { firstName: string; lastName: string; } function greeter(person: Person) { return "Hello, " + person.firstName + " " + person.lastName; } let user = new Student("Jane", "M.", "User1111"); document.body.textContent = greeter(user);
mongoDB 4を、Ubuntuにインストールする
index:
概要:
mongodb 4.2を、Ubuntu16 にインストールして使用メモとなります
環境
mongo DB 4.2
Ubuntu16
追加の手順
上記ページの、手順と同じですが。
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list sudo apt-get update sudo apt-get install -y mongodb-org echo "mongodb-org hold" | sudo dpkg --set-selections echo "mongodb-org-server hold" | sudo dpkg --set-selections echo "mongodb-org-shell hold" | sudo dpkg --set-selections echo "mongodb-org-mongos hold" | sudo dpkg --set-selections echo "mongodb-org-tools hold" | sudo dpkg --set-selections
起動
sudo service mongod start
停止するときは
sudo service mongod stop
mongo のログ
v4.2 で、起動してますね。
app4$ mongo MongoDB shell version v4.2.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("e0cc8937-6984-47a1-9661-81a6e05f7320") } MongoDB server version: 4.2.0 Server has startup warnings: 2019-09-14T11:10:13.216+0900 I STORAGE [initandlisten] 2019-09-14T11:10:13.216+0900 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2019-09-14T11:10:13.216+0900 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2019-09-14T11:10:14.426+0900 I CONTROL [initandlisten] 2019-09-14T11:10:14.426+0900 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-09-14T11:10:14.426+0900 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-09-14T11:10:14.426+0900 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- >