Skip to content

Commit 3de6ae0

Browse files
committed
add hello-sequelize
1 parent 10874fb commit 3de6ae0

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Start",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${workspaceRoot}/start.js",
9+
"stopOnEntry": false,
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"preLaunchTask": null,
13+
"runtimeExecutable": null,
14+
"runtimeArgs": [
15+
"--nolazy"
16+
],
17+
"env": {
18+
"NODE_ENV": "development"
19+
},
20+
"externalConsole": false,
21+
"sourceMaps": false,
22+
"outDir": null
23+
}
24+
]
25+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const Sequelize = require('sequelize');
2+
3+
const config = require('./config');
4+
5+
console.log('init sequelize...');
6+
7+
var sequelize = new Sequelize(config.database, config.username, config.password, {
8+
host: config.host,
9+
dialect: 'mysql',
10+
pool: {
11+
max: 5,
12+
min: 0,
13+
idle: 30000
14+
}
15+
});
16+
17+
var Pet = sequelize.define('pet', {
18+
id: {
19+
type: Sequelize.STRING(50),
20+
primaryKey: true
21+
},
22+
name: Sequelize.STRING(100),
23+
gender: Sequelize.BOOLEAN,
24+
birth: Sequelize.STRING(10),
25+
createdAt: Sequelize.BIGINT,
26+
updatedAt: Sequelize.BIGINT,
27+
version: Sequelize.BIGINT
28+
}, {
29+
timestamps: false
30+
});
31+
32+
var now = Date.now();
33+
34+
Pet.create({
35+
id: 'g-' + now,
36+
name: 'Gaffey',
37+
gender: false,
38+
birth: '2007-07-07',
39+
createdAt: now,
40+
updatedAt: now,
41+
version: 0
42+
}).then(function (p) {
43+
console.log('created.' + JSON.stringify(p));
44+
}).catch(function (err) {
45+
console.log('failed: ' + err);
46+
});
47+
48+
(async () => {
49+
var dog = await Pet.create({
50+
id: 'd-' + now,
51+
name: 'Odie',
52+
gender: false,
53+
birth: '2008-08-08',
54+
createdAt: now,
55+
updatedAt: now,
56+
version: 0
57+
});
58+
console.log('created: ' + JSON.stringify(dog));
59+
})();
60+
61+
(async () => {
62+
var pets = await Pet.findAll({
63+
where: {
64+
name: 'Gaffey'
65+
}
66+
});
67+
console.log(`find ${pets.length} pets:`);
68+
for (let p of pets) {
69+
console.log(JSON.stringify(p));
70+
console.log('update pet...');
71+
p.gender = true;
72+
p.updatedAt = Date.now();
73+
p.version ++;
74+
await p.save();
75+
if (p.version === 3) {
76+
await p.destroy();
77+
console.log(`${p.name} was destroyed.`);
78+
}
79+
}
80+
})();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
var config = {
3+
database: 'test',
4+
username: 'www',
5+
password: 'www',
6+
host: 'localhost',
7+
port: 3306
8+
};
9+
10+
module.exports = config;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
grant all privileges on test.* to 'www'@'%' identified by 'www';
3+
4+
use test;
5+
6+
create table pets (
7+
id varchar(50) not null,
8+
name varchar(100) not null,
9+
gender bool not null,
10+
birth varchar(10) not null,
11+
createdAt bigint not null,
12+
updatedAt bigint not null,
13+
version bigint not null,
14+
primary key (id)
15+
) engine=innodb;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "hello-sequelize",
3+
"version": "1.0.0",
4+
"description": "Hello Sequelize example with async",
5+
"main": "start.js",
6+
"scripts": {
7+
"start": "node start.js"
8+
},
9+
"keywords": [
10+
"sequelize",
11+
"async"
12+
],
13+
"author": "Michael Liao",
14+
"license": "Apache-2.0",
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/michaelliao/learn-javascript.git"
18+
},
19+
"dependencies": {
20+
"babel-core": "6.13.2",
21+
"babel-polyfill": "6.13.0",
22+
"babel-preset-es2015-node6": "0.3.0",
23+
"babel-preset-stage-3": "6.5.0",
24+
"sequelize": "3.24.1",
25+
"mysql": "2.11.1"
26+
}
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('babel-core/register')({
2+
presets: ['stage-3']
3+
});
4+
5+
require('./app.js');

0 commit comments

Comments
 (0)