Skip to content

Commit 8bcc849

Browse files
author
Musaab Elfaqih
committed
initial
0 parents  commit 8bcc849

21 files changed

+5773
-0
lines changed

.gcloudignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# Node.js dependencies:
17+
node_modules/

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
app.yaml

.sequelizerc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require("path");
2+
3+
module.exports = {
4+
config: path.resolve('db', 'config.js'),
5+
'migrations-path': path.resolve('db', 'migrations')
6+
};

db/config.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const logger = require("../server/utilities/logger");
2+
3+
const {
4+
DB_HOST: host = "localhost",
5+
DB_PORT: port = "5432",
6+
DB_USERNAME: username = "postgres",
7+
DB_PASSWORD: password,
8+
DB_NAME: database = "chingu",
9+
} = process.env;
10+
11+
const config = {
12+
host,
13+
port,
14+
username,
15+
password,
16+
database,
17+
dialect: "postgres",
18+
define: {
19+
timestamps: false,
20+
underscored: true,
21+
},
22+
logging: (...log) => logger.debug(log),
23+
};
24+
25+
module.exports = {
26+
development: config,
27+
test: config,
28+
production: config,
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module.exports = {
2+
up: (queryInterface, Sequelize) => {
3+
return queryInterface.createTable("users", {
4+
id: {
5+
type: Sequelize.INTEGER,
6+
allowNull: false,
7+
primaryKey: true,
8+
autoIncrement: true,
9+
},
10+
email: {
11+
type: Sequelize.STRING,
12+
allowNull: false,
13+
unique: true,
14+
},
15+
password: {
16+
type: Sequelize.STRING,
17+
allowNull: false,
18+
},
19+
name: {
20+
type: Sequelize.STRING,
21+
allowNull: false,
22+
},
23+
role: {
24+
type: Sequelize.STRING,
25+
allowNull: false,
26+
defaultValue: "USER",
27+
},
28+
created_at: {
29+
type: Sequelize.INTEGER,
30+
allowNull: false,
31+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
32+
},
33+
updated_at: {
34+
type: Sequelize.INTEGER,
35+
allowNull: false,
36+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
37+
onUpdate: "SET DEFAULT",
38+
},
39+
});
40+
},
41+
42+
down: queryInterface => {
43+
return queryInterface.dropTable("users");
44+
},
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
up: (queryInterface, Sequelize) => {
3+
return queryInterface.createTable("sessions", {
4+
id: {
5+
type: Sequelize.INTEGER,
6+
allowNull: false,
7+
primaryKey: true,
8+
autoIncrement: true,
9+
},
10+
user_id: {
11+
type: Sequelize.INTEGER,
12+
allowNull: false,
13+
},
14+
created_at: {
15+
type: Sequelize.INTEGER,
16+
allowNull: false,
17+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
18+
},
19+
updated_at: {
20+
type: Sequelize.INTEGER,
21+
allowNull: false,
22+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
23+
onUpdate: "SET DEFAULT",
24+
},
25+
});
26+
},
27+
28+
down: queryInterface => {
29+
return queryInterface.dropTable("sessions");
30+
},
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
up: (queryInterface, Sequelize) => {
3+
return queryInterface.createTable("pre_registered_users", {
4+
id: {
5+
type: Sequelize.INTEGER,
6+
allowNull: false,
7+
primaryKey: true,
8+
autoIncrement: true,
9+
},
10+
email: {
11+
type: Sequelize.STRING,
12+
allowNull: false,
13+
unique: true,
14+
},
15+
name: {
16+
type: Sequelize.STRING,
17+
allowNull: false,
18+
},
19+
created_at: {
20+
type: Sequelize.INTEGER,
21+
allowNull: false,
22+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
23+
},
24+
updated_at: {
25+
type: Sequelize.INTEGER,
26+
allowNull: false,
27+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
28+
onUpdate: "SET DEFAULT",
29+
},
30+
});
31+
},
32+
33+
down: queryInterface => {
34+
return queryInterface.dropTable("pre_registered_users");
35+
},
36+
};

package.json

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "chingu-api",
3+
"version": "1.0.0",
4+
"description": "Central Chingu API",
5+
"main": "server/index.js",
6+
"license": "MIT",
7+
"private": true,
8+
"engines": {
9+
"node": "10.x.x"
10+
},
11+
"scripts": {
12+
"start": "node server",
13+
"dev": "nodemon server"
14+
},
15+
"dependencies": {
16+
"apollo-server-express": "^2.6.3",
17+
"bcrypt": "^3.0.6",
18+
"cors": "^2.8.5",
19+
"dataloader": "^1.4.0",
20+
"express": "^4.17.1",
21+
"graphql": "^14.2.1",
22+
"http": "^0.0.0",
23+
"https": "^1.0.0",
24+
"jsonwebtoken": "^8.5.1",
25+
"morgan": "^1.9.1",
26+
"pg": "^7.11.0",
27+
"sequelize": "^5.8.10",
28+
"winston": "^3.2.1"
29+
},
30+
"devDependencies": {
31+
"eslint": "^5.16.0",
32+
"eslint-config-google": "^0.13.0",
33+
"eslint-config-prettier": "^5.0.0",
34+
"husky": "^2.4.1",
35+
"lint-staged": "^8.2.1",
36+
"nodemon": "^1.19.1",
37+
"prettier": "^1.18.2"
38+
},
39+
"prettier": {
40+
"trailingComma": "all",
41+
"semi": true
42+
},
43+
"eslintConfig": {
44+
"extends": [
45+
"google",
46+
"prettier"
47+
],
48+
"parserOptions": {
49+
"ecmaVersion": 2019
50+
},
51+
"env": {
52+
"node": true
53+
},
54+
"rules": {
55+
"no-console": 1,
56+
"no-unused-vars": 1,
57+
"require-jsdoc": 1
58+
}
59+
},
60+
"eslintIgnore": [
61+
"node_modules"
62+
],
63+
"husky": {
64+
"hooks": {
65+
"pre-commit": "lint-staged"
66+
}
67+
},
68+
"lint-staged": {
69+
"*.{js,json,md,graphql}": [
70+
"prettier --write",
71+
"git add"
72+
]
73+
}
74+
}

server/data_sources/index.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const Sequelize = require("sequelize");
2+
const logger = require("../utilities/logger");
3+
const { initializeModels } = require("./models");
4+
5+
const {
6+
DB_HOST: host = "localhost",
7+
DB_PORT: port = "5432",
8+
DB_USERNAME: username = "postgres",
9+
DB_PASSWORD: password,
10+
DB_NAME: database = "chingu",
11+
} = process.env;
12+
13+
let sequelize;
14+
15+
/**
16+
* Initializes a database client
17+
* @return {Object} The database client
18+
*/
19+
function initializeDatabase() {
20+
sequelize = new Sequelize({
21+
host,
22+
port,
23+
username,
24+
password,
25+
database,
26+
dialect: "postgres",
27+
define: {
28+
timestamps: false,
29+
freezeTableName: true,
30+
underscored: true
31+
},
32+
logging: (...log) => logger.debug(log)
33+
});
34+
35+
return sequelize
36+
.authenticate()
37+
.then(() => initializeModels(sequelize))
38+
.catch(err => {
39+
logger.error(err);
40+
process.exit(1);
41+
});
42+
}
43+
44+
/**
45+
* Initializes data sources
46+
* @return {Object} Datasources
47+
*/
48+
function initializeDataSources() {
49+
return {
50+
models: sequelize.models
51+
};
52+
}
53+
54+
module.exports = { initializeDatabase, initializeDataSources };

server/data_sources/models/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const logger = require("../../utilities/logger");
4+
5+
const initializeModels = sequelize => new Promise((resolve, reject) => {
6+
fs.readdir(__dirname, (err, files) => {
7+
if (err) {
8+
logger.error(err);
9+
reject(err);
10+
}
11+
12+
files
13+
.filter(
14+
modelFile =>
15+
modelFile &&
16+
modelFile.includes(".js") &&
17+
path.join(__dirname, modelFile) !== __filename,
18+
)
19+
.forEach(modelFile => {
20+
sequelize.import(path.join(__dirname, modelFile));
21+
});
22+
23+
resolve(sequelize);
24+
});
25+
});
26+
27+
module.exports = { initializeModels };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const Sequelize = require("sequelize");
2+
3+
/**
4+
* Defines the PreRegisteredUser model
5+
*/
6+
class PreRegisteredUser extends Sequelize.Model {}
7+
8+
module.exports = sequelize => {
9+
PreRegisteredUser.init(
10+
{
11+
id: {
12+
type: Sequelize.INTEGER,
13+
allowNull: false,
14+
primaryKey: true,
15+
autoIncrement: true,
16+
},
17+
email: {
18+
type: Sequelize.STRING,
19+
allowNull: false,
20+
unique: true,
21+
},
22+
name: {
23+
type: Sequelize.STRING,
24+
allowNull: false,
25+
},
26+
created_at: {
27+
type: Sequelize.INTEGER,
28+
allowNull: false,
29+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
30+
},
31+
updated_at: {
32+
type: Sequelize.INTEGER,
33+
allowNull: false,
34+
defaultValue: Sequelize.literal("EXTRACT(EPOCH from now())"),
35+
onUpdate: "SET DEFAULT",
36+
},
37+
},
38+
{
39+
tableName: "pre_registered_users",
40+
sequelize,
41+
},
42+
);
43+
44+
return PreRegisteredUser;
45+
};

0 commit comments

Comments
 (0)