@gooddaydev/common 1.0.1

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/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@gooddaydev/common",
3
+ "version": "1.0.1",
4
+ "description": "Common utilities for TypeScript projects with CI/CD",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "module": "dist/index.mjs",
8
+ "scripts": {
9
+ "build": "rollup --config ./rollup.config.mjs"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/goodday-repo/common"
14
+ },
15
+ "keywords": [
16
+ "typescript",
17
+ "common",
18
+ "ci",
19
+ "cd"
20
+ ],
21
+ "author": "Edward Leong",
22
+ "license": "MIT",
23
+ "devDependencies": {
24
+ "@rollup/plugin-terser": "^0.4.4",
25
+ "@types/jest": "^29.5.14",
26
+ "@types/mongoose-delete": "^1.0.6",
27
+ "@types/node": "^22.10.2",
28
+ "eslint": "^9.16.0",
29
+ "jest": "^29.7.0",
30
+ "rollup": "^4.28.1",
31
+ "rollup-plugin-typescript2": "^0.36.0",
32
+ "typescript": "^5.7.2"
33
+ },
34
+ "dependencies": {
35
+ "mongoose": "^8.12.1",
36
+ "mongoose-delete": "^1.0.2"
37
+ }
38
+ }
@@ -0,0 +1,75 @@
1
+ // @ts-check
2
+
3
+ import { readFile } from 'node:fs/promises';
4
+ import terser from '@rollup/plugin-terser';
5
+ import typescript2 from 'rollup-plugin-typescript2';
6
+
7
+ const packageJSON = JSON.parse(await readFile('./package.json', 'utf-8'));
8
+
9
+ /**
10
+ * Comment with library information to be appended in the generated bundles.
11
+ */
12
+ const banner = `/*!
13
+ * ${packageJSON.name} v${packageJSON.version}
14
+ * (c) ${packageJSON.author.name}
15
+ * Released under the ${packageJSON.license} License.
16
+ */
17
+ `;
18
+
19
+ /**
20
+ * Creates an output options object for Rollup.js.
21
+ * @param {import('rollup').OutputOptions} options
22
+ * @returns {import('rollup').OutputOptions}
23
+ */
24
+ function createOutputOptions(options) {
25
+ return {
26
+ banner,
27
+ name: '[libraryCamelCaseName]',
28
+ exports: 'named',
29
+ sourcemap: true,
30
+ ...options,
31
+ };
32
+ }
33
+
34
+ /**
35
+ * @type {import('rollup').RollupOptions}
36
+ */
37
+ const options = {
38
+ input: './src/index.ts',
39
+ output: [
40
+ createOutputOptions({
41
+ file: './dist/index.js',
42
+ format: 'commonjs',
43
+ }),
44
+ createOutputOptions({
45
+ file: './dist/index.cjs',
46
+ format: 'commonjs',
47
+ }),
48
+ createOutputOptions({
49
+ file: './dist/index.mjs',
50
+ format: 'esm',
51
+ }),
52
+ createOutputOptions({
53
+ file: './dist/index.esm.js',
54
+ format: 'esm',
55
+ }),
56
+ createOutputOptions({
57
+ file: './dist/index.umd.js',
58
+ format: 'umd',
59
+ }),
60
+ createOutputOptions({
61
+ file: './dist/index.umd.min.js',
62
+ format: 'umd',
63
+ plugins: [terser()],
64
+ }),
65
+ ],
66
+ plugins: [
67
+ typescript2({
68
+ clean: true,
69
+ useTsconfigDeclarationDir: true,
70
+ tsconfig: './tsconfig.bundle.json',
71
+ }),
72
+ ],
73
+ };
74
+
75
+ export default options;
@@ -0,0 +1,100 @@
1
+ import {
2
+ Document,
3
+ ObjectId,
4
+ ProjectionType,
5
+ QueryOptions,
6
+ QueryWithHelpers,
7
+ UpdateQuery,
8
+ } from 'mongoose';
9
+ import { SoftDeleteModel } from 'mongoose-delete';
10
+
11
+ export interface IQueryOptions {
12
+ page: number;
13
+ limit: number;
14
+ sorter: any;
15
+ filter: any;
16
+ lean: boolean;
17
+ }
18
+
19
+ interface PaginateModel<T> {
20
+ paginate(
21
+ filter: any,
22
+ options: {
23
+ page: number;
24
+ limit: number;
25
+ sort: any;
26
+ lean: boolean;
27
+ },
28
+ ): Promise<T>;
29
+ }
30
+
31
+ export class BaseService<T extends Document, CreateDto> {
32
+ constructor(private readonly model: SoftDeleteModel<T> & PaginateModel<T>) {
33
+ this.model = model;
34
+ }
35
+
36
+ async index(query: IQueryOptions) {
37
+ const sort = { [query.sorter.column]: query.sorter.state };
38
+
39
+ return await this.model.paginate(query.filter, {
40
+ page: query.page,
41
+ limit: query.limit,
42
+ sort: sort,
43
+ lean: query.lean,
44
+ });
45
+ }
46
+
47
+ async create(data: CreateDto | CreateDto[]): Promise<T> {
48
+ return await this.model.create(data);
49
+ }
50
+
51
+ async find(
52
+ filter: any,
53
+ projection?: ProjectionType<T>,
54
+ options?: QueryOptions<T>,
55
+ ): Promise<QueryWithHelpers<T[], T>> {
56
+ return await this.model.find(filter, projection, options);
57
+ }
58
+
59
+ async findOne(
60
+ filter: any,
61
+ projection?: ProjectionType<T>,
62
+ options?: QueryOptions<T>,
63
+ ): Promise<QueryWithHelpers<T | null, T>> {
64
+ return await this.model.findOne(filter, projection, options);
65
+ }
66
+
67
+ async findById(
68
+ id: string | ObjectId,
69
+ projection?: ProjectionType<T>,
70
+ options?: QueryOptions<T>,
71
+ ): Promise<QueryWithHelpers<T | null, T>> {
72
+ return await this.model.findById(id, projection, options);
73
+ }
74
+
75
+ async updateOne(
76
+ args: any,
77
+ set: UpdateQuery<T>,
78
+ options?: QueryOptions<T>,
79
+ ): Promise<boolean> {
80
+ return (await this.model.updateOne(args, set, options as UpdateQuery<T>))
81
+ .acknowledged;
82
+ }
83
+
84
+ async updateMany(
85
+ args: any,
86
+ set: UpdateQuery<T>,
87
+ options?: QueryOptions<T>,
88
+ ): Promise<number> {
89
+ return (await this.model.updateMany(args, set, options as UpdateQuery<T>))
90
+ .modifiedCount;
91
+ }
92
+
93
+ async deleteOne(args: any): Promise<boolean> {
94
+ return (await this.model.deleteOne(args)).acknowledged;
95
+ }
96
+
97
+ async deleteMany(args: any): Promise<number> {
98
+ return (await this.model.deleteMany(args)).deletedCount;
99
+ }
100
+ }
@@ -0,0 +1,5 @@
1
+ export enum UserType {
2
+ ADMIN = 'admin',
3
+ USER = 'user',
4
+ }
5
+
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // base
2
+ export * from './base/base.service';
3
+
4
+ // enum
5
+ export * from './enum/user-type.enum';
@@ -0,0 +1,19 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ // Transpilation settings.
5
+ "module": "ES2020",
6
+ "target": "ES5",
7
+ // Output settings.
8
+ "newLine": "LF",
9
+ "sourceMap": true,
10
+ // Type declaration settings.
11
+ "declaration": true,
12
+ "declarationDir": "types/",
13
+ "declarationMap": true
14
+ },
15
+ "exclude": [
16
+ "./src/**/*.test.ts",
17
+ "./src/**/*.test.tsx"
18
+ ]
19
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "compilerOptions": {
3
+ // React.js JSX settings.
4
+ "jsx": "react",
5
+ "jsxFactory": "React.createElement",
6
+ "jsxFragmentFactory": "React.Fragment",
7
+
8
+ // Module settings.
9
+ "allowSyntheticDefaultImports": true,
10
+ "esModuleInterop": true,
11
+ "isolatedModules": true,
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+
15
+ // Strictness and quality settings.
16
+ "alwaysStrict": true,
17
+ "exactOptionalPropertyTypes": true,
18
+ "forceConsistentCasingInFileNames": true,
19
+ "noFallthroughCasesInSwitch": true,
20
+ "noImplicitOverride": true,
21
+ "noImplicitReturns": true,
22
+ "noPropertyAccessFromIndexSignature": true,
23
+ "noUncheckedIndexedAccess": true,
24
+ "noUnusedLocals": true,
25
+ "noUnusedParameters": true,
26
+ "strict": true,
27
+
28
+ // Type-checking settings.
29
+ "lib": ["ES2020", "dom"],
30
+ "skipDefaultLibCheck": true,
31
+ "skipLibCheck": true,
32
+ "types": ["jest"]
33
+ },
34
+ "include": ["./src/**/*.ts", "./src/**/*.tsx"]
35
+ }
@@ -0,0 +1,32 @@
1
+ import { Document, ObjectId, ProjectionType, QueryOptions, QueryWithHelpers, UpdateQuery } from 'mongoose';
2
+ import { SoftDeleteModel } from 'mongoose-delete';
3
+ export interface IQueryOptions {
4
+ page: number;
5
+ limit: number;
6
+ sorter: any;
7
+ filter: any;
8
+ lean: boolean;
9
+ }
10
+ interface PaginateModel<T> {
11
+ paginate(filter: any, options: {
12
+ page: number;
13
+ limit: number;
14
+ sort: any;
15
+ lean: boolean;
16
+ }): Promise<T>;
17
+ }
18
+ export declare class BaseService<T extends Document, CreateDto> {
19
+ private readonly model;
20
+ constructor(model: SoftDeleteModel<T> & PaginateModel<T>);
21
+ index(query: IQueryOptions): Promise<T>;
22
+ create(data: CreateDto | CreateDto[]): Promise<T>;
23
+ find(filter: any, projection?: ProjectionType<T>, options?: QueryOptions<T>): Promise<QueryWithHelpers<T[], T>>;
24
+ findOne(filter: any, projection?: ProjectionType<T>, options?: QueryOptions<T>): Promise<QueryWithHelpers<T | null, T>>;
25
+ findById(id: string | ObjectId, projection?: ProjectionType<T>, options?: QueryOptions<T>): Promise<QueryWithHelpers<T | null, T>>;
26
+ updateOne(args: any, set: UpdateQuery<T>, options?: QueryOptions<T>): Promise<boolean>;
27
+ updateMany(args: any, set: UpdateQuery<T>, options?: QueryOptions<T>): Promise<number>;
28
+ deleteOne(args: any): Promise<boolean>;
29
+ deleteMany(args: any): Promise<number>;
30
+ }
31
+ export {};
32
+ //# sourceMappingURL=base.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.service.d.ts","sourceRoot":"","sources":["../../src/base/base.service.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,QAAQ,EACR,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,WAAW,EACZ,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,GAAG,CAAC;IACZ,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,OAAO,CAAC;CACf;AAED,UAAU,aAAa,CAA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,EAAE,YAAY,CAAC,CAAC,CAAC,GACxB,OAAO,CAAC,OAAO,CAAC;IAKb,UAAU,CACd,IAAI,EAAE,GAAG,EACT,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,EACnB,OAAO,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,GACxB,OAAO,CAAC,MAAM,CAAC;IAKZ,SAAS,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC;IAItC,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;CAG7C"}
@@ -0,0 +1,5 @@
1
+ export declare enum UserType {
2
+ ADMIN = "admin",
3
+ USER = "user"
4
+ }
5
+ //# sourceMappingURL=user-type.enum.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-type.enum.d.ts","sourceRoot":"","sources":["../../src/enum/user-type.enum.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ;IAClB,KAAK,UAAU;IACf,IAAI,SAAS;CACd"}
@@ -0,0 +1,3 @@
1
+ export * from './base/base.service';
2
+ export * from './enum/user-type.enum';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,qBAAqB,CAAC;AAGpC,cAAc,uBAAuB,CAAC"}