Skip to content

Commit f329555

Browse files
author
Nik Mohamad Aizuddin b. Nik Azmi
committed
feat(sampleapps): add mariadb with knex
1 parent 69198c4 commit f329555

9 files changed

Lines changed: 345 additions & 0 deletions

File tree

mariadb/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

mariadb/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM docker.io/node:14.16.1
2+
MAINTAINER extra2000 <https://github.com/extra2000>
3+
4+
COPY ./project/package.json /srv/project/package.json
5+
WORKDIR /srv/project
6+
RUN npm install
7+
8+
COPY ./project /srv/project
9+
WORKDIR /srv/project
10+
RUN npm run tsc
11+
12+
CMD node build/main.js

mariadb/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Sample apps: MariaDB
2+
3+
Deploy MariaDB pod:
4+
```
5+
$ podman play kube --network=sampleapps mysql-pod.yaml
6+
```
7+
8+
Test MariaDB pod connection:
9+
```
10+
$ podman run -it --rm --network=sampleapps docker.io/library/mariadb:10.3 mysql -uroot -pabcde12345 --host mariadb-mariadb-pod.sampleapps --port 3306
11+
```
12+
13+
Test `myapp`:
14+
```
15+
$ podman build -t extra2000/mariadb/myapp .
16+
$ podman run --rm --network=sampleapps extra2000/mariadb/myapp
17+
```
18+
19+
Verify the database to see if the `myapp` works:
20+
```
21+
$ podman run -it --rm --network=sampleapps docker.io/library/mariadb:10.3 mysql -uroot -pabcde12345 --host mariadb-mariadb-pod.sampleapps --port 3306 --database mydb -e "SELECT * FROM student;"
22+
```
23+
24+
The output should be:
25+
```
26+
+----+------+-------------+
27+
| id | name | description |
28+
+----+------+-------------+
29+
| 1 | A | AAA |
30+
| 2 | B | BBB |
31+
+----+------+-------------+
32+
```
33+
34+
35+
## Development mode (optional)
36+
```
37+
$ podman run -it --rm -v ./project:/project:rw,z -w /project docker.io/node:14.16.1 npm install
38+
$ podman run -it --rm -v ./project:/project:rw,z -w /project --network=sampleapps docker.io/node:14.16.1 npm run dev
39+
```

mariadb/mysql-pod.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
labels:
5+
app: mariadb-mariadb-pod
6+
name: mariadb-mariadb-pod
7+
spec:
8+
restartPolicy: Never
9+
containers:
10+
- name: mysql01
11+
image: docker.io/mariadb:10.3
12+
securityContext:
13+
allowPrivilegeEscalation: false
14+
privileged: false
15+
capabilities:
16+
add: []
17+
drop:
18+
- CAP_MKNOD
19+
- CAP_NET_RAW
20+
- CAP_AUDIT_WRITE
21+
seLinuxOptions: {}
22+
resources:
23+
requests:
24+
memory: 500Mi
25+
limits:
26+
memory: 500Mi
27+
ports:
28+
- containerPort: 3306
29+
hostPort: 23306
30+
hostIP: 0.0.0.0
31+
protocol: tcp
32+
env:
33+
- name: MYSQL_ROOT_PASSWORD
34+
value: abcde12345
35+
- name: MYSQL_DATABASE
36+
value: mydb
37+
- name: MYSQL_USER
38+
value: myuser
39+
- name: MYSQL_PASSWORD
40+
value: abcde12345
41+
volumeMounts:
42+
- mountPath: /var/lib/mysql
43+
name: sampleapps-mariadb-data
44+
volumes:
45+
- name: sampleapps-mariadb-data
46+
persistentVolumeClaim:
47+
claimName: sampleapps-mariadb-data

mariadb/project/.gitignore

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port

mariadb/project/@types/knex.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Knex as KnexOriginal } from 'knex';
2+
3+
declare module 'knex' {
4+
namespace Knex {
5+
interface QueryBuilder {
6+
customSelect<TRecord, TResult>(value: number): KnexOriginal.QueryBuilder<TRecord, TResult>;
7+
}
8+
}
9+
}

mariadb/project/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "mariadbapp",
3+
"version": "1.0.0",
4+
"description": "For playground purpose",
5+
"main": "src/main.ts",
6+
"author": "extra2000",
7+
"license": "MIT",
8+
"scripts": {
9+
"tsc": "tsc",
10+
"dev": "ts-node src/main.ts"
11+
},
12+
"dependencies": {
13+
"knex": "^0.95.6",
14+
"mysql2": "^2.2.5"
15+
},
16+
"devDependencies": {
17+
"typescript": "^4.0.2",
18+
"ts-node": "^9.0.0",
19+
"@types/node": "^15.6.0"
20+
}
21+
}

mariadb/project/src/main.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Knex, knex } from 'knex'
2+
3+
async function create_connection(): Promise<Knex> {
4+
return await require('knex')({
5+
client: 'mysql2',
6+
connection: {
7+
host : 'mariadb-mariadb-pod.sampleapps',
8+
user : 'myuser',
9+
password : 'abcde12345',
10+
database : 'mydb'
11+
},
12+
useNullAsDefault: true
13+
})
14+
}
15+
16+
async function create_table(conn: any, tablename: string) {
17+
if (! await conn.schema.hasTable(tablename)) {
18+
await conn.schema.createTable(tablename, function (table: any) {
19+
table.increments('id')
20+
table.string('name')
21+
table.string('description')
22+
})
23+
}
24+
}
25+
26+
async function add_data(conn: any, tablename: string) {
27+
if (await conn.schema.hasTable(tablename)) {
28+
await conn(tablename).insert([
29+
{name: "A", description: "AAA"},
30+
{name: "B", description: "BBB"}
31+
])
32+
}
33+
}
34+
35+
create_connection()
36+
.then(async function(conn: Knex) {
37+
await create_table(conn, "student")
38+
await add_data(conn, "student")
39+
await conn.destroy()
40+
})

mariadb/project/tsconfig.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Basic Options */
6+
// "incremental": true, /* Enable incremental compilation */
7+
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9+
// "lib": [], /* Specify library files to be included in the compilation. */
10+
// "allowJs": true, /* Allow javascript files to be compiled. */
11+
// "checkJs": true, /* Report errors in .js files. */
12+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
13+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
14+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15+
// "sourceMap": true, /* Generates corresponding '.map' file. */
16+
// "outFile": "./", /* Concatenate and emit output to single file. */
17+
"outDir": "./build", /* Redirect output structure to the directory. */
18+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19+
// "composite": true, /* Enable project compilation */
20+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21+
// "removeComments": true, /* Do not emit comments to output. */
22+
// "noEmit": true, /* Do not emit outputs. */
23+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
24+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26+
27+
/* Strict Type-Checking Options */
28+
"strict": true, /* Enable all strict type-checking options. */
29+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30+
// "strictNullChecks": true, /* Enable strict null checks. */
31+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
32+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36+
37+
/* Additional Checks */
38+
// "noUnusedLocals": true, /* Report errors on unused locals. */
39+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
40+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42+
43+
/* Module Resolution Options */
44+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
45+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
46+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
47+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
48+
"typeRoots": [ /* List of folders to include type definitions from. */
49+
"node_modules/@types",
50+
"@types"
51+
],
52+
// "types": [], /* Type declaration files to be included in compilation. */
53+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
54+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
55+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
56+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
57+
58+
/* Source Map Options */
59+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
60+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
61+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
62+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
63+
64+
/* Experimental Options */
65+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
66+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
67+
68+
/* Advanced Options */
69+
"skipLibCheck": true, /* Skip type checking of declaration files. */
70+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
71+
}
72+
}

0 commit comments

Comments
 (0)