express-generator-typescript 2.7.11 → 2.7.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -0
- package/README.md +0 -0
- package/lib/cli.js +0 -0
- package/lib/express-generator-typescript.js +44 -11
- package/lib/project-files/.prettierignore +12 -0
- package/lib/project-files/.prettierrc.json +15 -0
- package/lib/project-files/README.md +54 -65
- package/lib/project-files/config/.env.development +0 -0
- package/lib/project-files/config/.env.production +0 -0
- package/lib/project-files/config/.env.test +0 -0
- package/lib/project-files/eslint.config.ts +46 -100
- package/lib/project-files/gitignore +0 -2
- package/lib/project-files/package.json +62 -56
- package/lib/project-files/src/common/constants/HttpStatusCodes.ts +393 -394
- package/lib/project-files/src/common/constants/Paths.ts +15 -15
- package/lib/project-files/src/common/constants/{EnvVars.ts → env.ts} +28 -19
- package/lib/project-files/src/common/utils/{misc.ts → number-utils.ts} +10 -11
- package/lib/project-files/src/common/utils/route-errors.ts +35 -36
- package/lib/project-files/src/common/utils/validators.ts +14 -14
- package/lib/project-files/src/{index.ts → main.ts} +24 -27
- package/lib/project-files/src/models/User.ts +73 -72
- package/lib/project-files/src/models/common/IModel.ts +4 -0
- package/lib/project-files/src/public/stylesheets/users.css +50 -48
- package/lib/project-files/src/repos/MockOrm.ts +57 -63
- package/lib/project-files/src/repos/UserRepo.ts +131 -133
- package/lib/project-files/src/repos/{database.json → common/database.json} +0 -0
- package/lib/project-files/src/repos/common/database.test.json +1 -0
- package/lib/project-files/src/routes/UserRoutes.ts +77 -69
- package/lib/project-files/src/routes/{index.ts → apiRouter.ts} +28 -28
- package/lib/project-files/src/routes/common/{types.ts → express-types.ts} +9 -10
- package/lib/project-files/src/routes/common/{utils.ts → parseReq.ts} +19 -16
- package/lib/project-files/src/server.ts +79 -84
- package/lib/project-files/src/services/{UserService/index.ts → UserService.ts} +64 -58
- package/lib/project-files/src/views/users.html +118 -132
- package/lib/project-files/tests/common/{utils.ts → comparators.ts} +38 -55
- package/lib/project-files/tests/common/error-utils.ts +25 -0
- package/lib/project-files/tests/common/{types.ts → supertest-types.ts} +15 -16
- package/lib/project-files/tests/{aux/setup.ts → support/agent.ts} +23 -26
- package/lib/project-files/tests/users.test.ts +131 -123
- package/lib/project-files/tsconfig.json +29 -35
- package/lib/project-files/tsconfig.prod.json +27 -30
- package/lib/project-files/vitest.config.mts +21 -18
- package/package.json +1 -1
- package/lib/project-files/config.ts +0 -22
- package/lib/project-files/scripts/build.ts +0 -28
- package/lib/project-files/scripts/common/utils.ts +0 -93
- package/lib/project-files/src/common/constants/index.ts +0 -11
- package/lib/project-files/src/models/common/types.ts +0 -5
- package/lib/project-files/src/repos/database.test.json +0 -1
- package/lib/project-files/src/services/UserService/constants.ts +0 -5
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import jetPaths from 'jet-paths';
|
|
2
|
-
|
|
3
|
-
const Paths = {
|
|
4
|
-
_: '/api',
|
|
5
|
-
Users: {
|
|
6
|
-
_: '/users',
|
|
7
|
-
Get: '/all',
|
|
8
|
-
Add: '/add',
|
|
9
|
-
Update: '/update',
|
|
10
|
-
Delete: '/delete/:id',
|
|
11
|
-
},
|
|
12
|
-
} as const;
|
|
13
|
-
|
|
14
|
-
export const JetPaths = jetPaths(Paths);
|
|
15
|
-
export default Paths;
|
|
1
|
+
import jetPaths from 'jet-paths';
|
|
2
|
+
|
|
3
|
+
const Paths = {
|
|
4
|
+
_: '/api',
|
|
5
|
+
Users: {
|
|
6
|
+
_: '/users',
|
|
7
|
+
Get: '/all',
|
|
8
|
+
Add: '/add',
|
|
9
|
+
Update: '/update',
|
|
10
|
+
Delete: '/delete/:id',
|
|
11
|
+
},
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
export const JetPaths = jetPaths(Paths);
|
|
15
|
+
export default Paths;
|
|
@@ -1,19 +1,28 @@
|
|
|
1
|
-
import jetEnv, { num } from 'jet-env';
|
|
2
|
-
import { isValueOf } from 'jet-validators';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/******************************************************************************
|
|
16
|
-
|
|
17
|
-
******************************************************************************/
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import jetEnv, { num } from 'jet-env';
|
|
2
|
+
import { isValueOf } from 'jet-validators';
|
|
3
|
+
|
|
4
|
+
/******************************************************************************
|
|
5
|
+
Constants
|
|
6
|
+
******************************************************************************/
|
|
7
|
+
|
|
8
|
+
// NOTE: These need to match the names of your ".env" files
|
|
9
|
+
export const NodeEnvs = {
|
|
10
|
+
DEV: 'development',
|
|
11
|
+
TEST: 'test',
|
|
12
|
+
PRODUCTION: 'production',
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
/******************************************************************************
|
|
16
|
+
Setup
|
|
17
|
+
******************************************************************************/
|
|
18
|
+
|
|
19
|
+
const EnvVars = jetEnv({
|
|
20
|
+
NodeEnv: isValueOf(NodeEnvs),
|
|
21
|
+
Port: num,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/******************************************************************************
|
|
25
|
+
Export default
|
|
26
|
+
******************************************************************************/
|
|
27
|
+
|
|
28
|
+
export default EnvVars;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Functions
|
|
3
|
+
******************************************************************************/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get a random number between 1 and 1,000,000,000,000
|
|
7
|
+
*/
|
|
8
|
+
export function getRandomInt(): number {
|
|
9
|
+
return Math.floor(Math.random() * 1_000_000_000_000);
|
|
10
|
+
}
|
|
@@ -1,36 +1,35 @@
|
|
|
1
|
-
import { ParseError } from 'jet-validators/utils';
|
|
2
|
-
|
|
3
|
-
import HttpStatusCodes from '@src/common/constants/HttpStatusCodes';
|
|
4
|
-
|
|
5
|
-
/******************************************************************************
|
|
6
|
-
Classes
|
|
7
|
-
******************************************************************************/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Error with status code and message.
|
|
11
|
-
*/
|
|
12
|
-
export class RouteError extends Error {
|
|
13
|
-
public status: HttpStatusCodes;
|
|
14
|
-
|
|
15
|
-
public constructor(status: HttpStatusCodes, message: string) {
|
|
16
|
-
super(message);
|
|
17
|
-
this.status = status;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Handle "parseObj" errors.
|
|
23
|
-
*/
|
|
24
|
-
export class ValidationError extends RouteError {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
1
|
+
import { ParseError } from 'jet-validators/utils';
|
|
2
|
+
|
|
3
|
+
import HttpStatusCodes from '@src/common/constants/HttpStatusCodes';
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Classes
|
|
7
|
+
******************************************************************************/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Error with status code and message.
|
|
11
|
+
*/
|
|
12
|
+
export class RouteError extends Error {
|
|
13
|
+
public status: HttpStatusCodes;
|
|
14
|
+
|
|
15
|
+
public constructor(status: HttpStatusCodes, message: string) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.status = status;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Handle "parseObj" errors.
|
|
23
|
+
*/
|
|
24
|
+
export class ValidationError extends RouteError {
|
|
25
|
+
public static MESSAGE =
|
|
26
|
+
'The parseObj() function discovered one or ' + 'more errors.';
|
|
27
|
+
|
|
28
|
+
public constructor(errors: ParseError[]) {
|
|
29
|
+
const msg = JSON.stringify({
|
|
30
|
+
message: ValidationError.MESSAGE,
|
|
31
|
+
errors,
|
|
32
|
+
});
|
|
33
|
+
super(HttpStatusCodes.BAD_REQUEST, msg);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { isDate } from 'jet-validators';
|
|
2
|
-
import { transform } from 'jet-validators/utils';
|
|
3
|
-
|
|
4
|
-
/******************************************************************************
|
|
5
|
-
Functions
|
|
6
|
-
******************************************************************************/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Convert to date object then check is a validate date.
|
|
10
|
-
*/
|
|
11
|
-
export const transformIsDate = transform(
|
|
12
|
-
arg => new Date(arg as string),
|
|
13
|
-
arg => isDate(arg),
|
|
14
|
-
);
|
|
1
|
+
import { isDate } from 'jet-validators';
|
|
2
|
+
import { transform } from 'jet-validators/utils';
|
|
3
|
+
|
|
4
|
+
/******************************************************************************
|
|
5
|
+
Functions
|
|
6
|
+
******************************************************************************/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Convert to date object then check is a validate date.
|
|
10
|
+
*/
|
|
11
|
+
export const transformIsDate = transform(
|
|
12
|
+
(arg) => new Date(arg as string),
|
|
13
|
+
(arg) => isDate(arg),
|
|
14
|
+
);
|
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
import logger from 'jet-logger';
|
|
2
|
-
|
|
3
|
-
import EnvVars from '
|
|
4
|
-
import server from './server';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
logger.info(SERVER_START_MSG);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
1
|
+
import logger from 'jet-logger';
|
|
2
|
+
|
|
3
|
+
import EnvVars from './common/constants/env';
|
|
4
|
+
import server from './server';
|
|
5
|
+
|
|
6
|
+
/******************************************************************************
|
|
7
|
+
Constants
|
|
8
|
+
******************************************************************************/
|
|
9
|
+
|
|
10
|
+
const SERVER_START_MSG =
|
|
11
|
+
'Express server started on port: ' + EnvVars.Port.toString();
|
|
12
|
+
|
|
13
|
+
/******************************************************************************
|
|
14
|
+
Run
|
|
15
|
+
******************************************************************************/
|
|
16
|
+
|
|
17
|
+
// Start the server
|
|
18
|
+
server.listen(EnvVars.Port, (err) => {
|
|
19
|
+
if (!!err) {
|
|
20
|
+
logger.err(err.message);
|
|
21
|
+
} else {
|
|
22
|
+
logger.info(SERVER_START_MSG);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
import { isNonEmptyString, isString, isUnsignedInteger } from 'jet-validators';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from '
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
import { isNonEmptyString, isString, isUnsignedInteger } from 'jet-validators';
|
|
2
|
+
import { parseObject, Schema, testObject } from 'jet-validators/utils';
|
|
3
|
+
|
|
4
|
+
import { transformIsDate } from '@src/common/utils/validators';
|
|
5
|
+
|
|
6
|
+
import { IModel } from './common/IModel';
|
|
7
|
+
|
|
8
|
+
/******************************************************************************
|
|
9
|
+
Constants
|
|
10
|
+
******************************************************************************/
|
|
11
|
+
|
|
12
|
+
const GetDefaults = (): IUser => ({
|
|
13
|
+
id: 0,
|
|
14
|
+
name: '',
|
|
15
|
+
email: '',
|
|
16
|
+
created: new Date(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const schema: Schema<IUser> = {
|
|
20
|
+
id: isUnsignedInteger,
|
|
21
|
+
name: isString,
|
|
22
|
+
email: isString,
|
|
23
|
+
created: transformIsDate,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/******************************************************************************
|
|
27
|
+
Types
|
|
28
|
+
******************************************************************************/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @entity users
|
|
32
|
+
*/
|
|
33
|
+
export interface IUser extends IModel {
|
|
34
|
+
name: string;
|
|
35
|
+
email: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/******************************************************************************
|
|
39
|
+
Setup
|
|
40
|
+
******************************************************************************/
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// Set the "parseUser" function
|
|
44
|
+
const parseUser = parseObject<IUser>(schema);
|
|
45
|
+
|
|
46
|
+
// For the APIs make sure the right fields are complete
|
|
47
|
+
const testIsCompleteUser = testObject<IUser>({
|
|
48
|
+
...schema,
|
|
49
|
+
name: isNonEmptyString,
|
|
50
|
+
email: isNonEmptyString,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/******************************************************************************
|
|
54
|
+
Functions
|
|
55
|
+
******************************************************************************/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* New user object.
|
|
59
|
+
*/
|
|
60
|
+
function __new__(user?: Partial<IUser>): IUser {
|
|
61
|
+
return parseUser({ ...GetDefaults(), ...user }, (errors) => {
|
|
62
|
+
throw new Error('Setup new user failed ' + JSON.stringify(errors, null, 2));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/******************************************************************************
|
|
67
|
+
Export default
|
|
68
|
+
******************************************************************************/
|
|
69
|
+
|
|
70
|
+
export default {
|
|
71
|
+
new: __new__,
|
|
72
|
+
isComplete: testIsCompleteUser,
|
|
73
|
+
} as const;
|
|
@@ -1,48 +1,50 @@
|
|
|
1
|
-
body {
|
|
2
|
-
padding: 100px;
|
|
3
|
-
font:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
body {
|
|
2
|
+
padding: 100px;
|
|
3
|
+
font:
|
|
4
|
+
14px 'Lucida Grande',
|
|
5
|
+
Helvetica,
|
|
6
|
+
Arial,
|
|
7
|
+
sans-serif;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
body .users-column {
|
|
11
|
+
display: inline-block;
|
|
12
|
+
margin-right: 2em;
|
|
13
|
+
vertical-align: top;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
body .users-column .column-header {
|
|
17
|
+
padding-bottom: 5px;
|
|
18
|
+
font-weight: 700;
|
|
19
|
+
font-size: 1.2em;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Add User Column **/
|
|
23
|
+
|
|
24
|
+
body .add-user-col input {
|
|
25
|
+
margin-bottom: 10px;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
body .add-user-col #add-user-btn {
|
|
29
|
+
margin-top: 2px;
|
|
30
|
+
margin-bottom: 10px;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Users Display Column **/
|
|
34
|
+
|
|
35
|
+
.email-edit {
|
|
36
|
+
padding-bottom: 8px;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
body .users-column .user-display-ele {
|
|
40
|
+
padding-bottom: 10px;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
body .users-column .user-display-ele button {
|
|
44
|
+
margin-top: 2px;
|
|
45
|
+
margin-bottom: 10px;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
body .users-column .user-display-ele .edit-view {
|
|
49
|
+
display: none;
|
|
50
|
+
}
|
|
@@ -1,63 +1,57 @@
|
|
|
1
|
-
import jsonfile from 'jsonfile';
|
|
2
|
-
|
|
3
|
-
import EnvVars from '@src/common/constants/
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export default {
|
|
60
|
-
openDb,
|
|
61
|
-
saveDb,
|
|
62
|
-
cleanDb,
|
|
63
|
-
} as const;
|
|
1
|
+
import jsonfile from 'jsonfile';
|
|
2
|
+
|
|
3
|
+
import EnvVars, { NodeEnvs } from '@src/common/constants/env';
|
|
4
|
+
import { IUser } from '@src/models/User';
|
|
5
|
+
|
|
6
|
+
/******************************************************************************
|
|
7
|
+
Constants
|
|
8
|
+
******************************************************************************/
|
|
9
|
+
|
|
10
|
+
const DATABASE_FILE_PATH = __dirname + '/common' + (
|
|
11
|
+
EnvVars.NodeEnv === NodeEnvs.TEST
|
|
12
|
+
? '/database.test.json'
|
|
13
|
+
: '/database.json'
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
/******************************************************************************
|
|
17
|
+
Types
|
|
18
|
+
******************************************************************************/
|
|
19
|
+
|
|
20
|
+
interface IDatabase {
|
|
21
|
+
users: IUser[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/******************************************************************************
|
|
25
|
+
Functions
|
|
26
|
+
******************************************************************************/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Fetch the json from the file.
|
|
30
|
+
*/
|
|
31
|
+
function openDb(): Promise<IDatabase> {
|
|
32
|
+
return jsonfile.readFile(DATABASE_FILE_PATH) as Promise<IDatabase>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Update the file.
|
|
37
|
+
*/
|
|
38
|
+
function saveDb(db: IDatabase): Promise<void> {
|
|
39
|
+
return jsonfile.writeFile(DATABASE_FILE_PATH, db);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Empty the database
|
|
44
|
+
*/
|
|
45
|
+
function cleanDb(): Promise<void> {
|
|
46
|
+
return jsonfile.writeFile( DATABASE_FILE_PATH, {});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/******************************************************************************
|
|
50
|
+
Export default
|
|
51
|
+
******************************************************************************/
|
|
52
|
+
|
|
53
|
+
export default {
|
|
54
|
+
openDb,
|
|
55
|
+
saveDb,
|
|
56
|
+
cleanDb,
|
|
57
|
+
} as const;
|