From 6dc9be35c4d56ed7886be89716088ba059a62228 Mon Sep 17 00:00:00 2001 From: Robert Brightline Date: Fri, 22 Nov 2024 16:36:13 -0600 Subject: [PATCH] query models --- README.md | 4 ++ libs/core/.swcrc | 29 ++++++++++++++ libs/core/README.md | 11 ++++++ libs/core/eslint.config.js | 22 +++++++++++ libs/core/package.json | 10 +++++ libs/core/project.json | 39 +++++++++++++++++++ libs/core/src/index.ts | 6 +++ libs/core/src/lib/base/base.entity.ts | 23 +++++++++++ libs/core/src/lib/query/query.ts | 22 +++++++++++ .../src/lib/transformers/query-operator.ts | 11 ++++++ libs/core/src/lib/transformers/query.ts | 6 +++ .../src/lib/transformers/to-find-operator.ts | 32 +++++++++++++++ libs/core/tsconfig.json | 22 +++++++++++ libs/core/tsconfig.lib.json | 23 +++++++++++ libs/core/tsconfig.spec.json | 28 +++++++++++++ libs/core/vite.config.ts | 21 ++++++++++ libs/entity/src/index.ts | 2 +- libs/entity/src/lib/category/create.ts | 12 ++++++ libs/entity/src/lib/category/entity.ts | 9 +++++ libs/entity/src/lib/entity.spec.ts | 7 ---- libs/entity/src/lib/entity.ts | 3 -- libs/model/src/index.ts | 3 +- libs/model/src/lib/category/category.ts | 7 ++++ libs/model/src/lib/model.spec.ts | 7 ---- libs/model/src/lib/model.ts | 3 -- libs/type/README.md | 10 ----- libs/type/src/index.ts | 3 +- libs/type/src/lib/base/base.ts | 27 +++++++++++++ libs/type/src/lib/type.spec.ts | 7 ---- libs/type/src/lib/type.ts | 3 -- package.json | 2 +- tsconfig.base.json | 3 ++ 32 files changed, 373 insertions(+), 44 deletions(-) create mode 100644 libs/core/.swcrc create mode 100644 libs/core/README.md create mode 100644 libs/core/eslint.config.js create mode 100644 libs/core/package.json create mode 100644 libs/core/project.json create mode 100644 libs/core/src/index.ts create mode 100644 libs/core/src/lib/base/base.entity.ts create mode 100644 libs/core/src/lib/query/query.ts create mode 100644 libs/core/src/lib/transformers/query-operator.ts create mode 100644 libs/core/src/lib/transformers/query.ts create mode 100644 libs/core/src/lib/transformers/to-find-operator.ts create mode 100644 libs/core/tsconfig.json create mode 100644 libs/core/tsconfig.lib.json create mode 100644 libs/core/tsconfig.spec.json create mode 100644 libs/core/vite.config.ts create mode 100644 libs/entity/src/lib/category/create.ts create mode 100644 libs/entity/src/lib/category/entity.ts delete mode 100644 libs/entity/src/lib/entity.spec.ts delete mode 100644 libs/entity/src/lib/entity.ts create mode 100644 libs/model/src/lib/category/category.ts delete mode 100644 libs/model/src/lib/model.spec.ts delete mode 100644 libs/model/src/lib/model.ts create mode 100644 libs/type/src/lib/base/base.ts delete mode 100644 libs/type/src/lib/type.spec.ts delete mode 100644 libs/type/src/lib/type.ts diff --git a/README.md b/README.md index 5b671f5..7416565 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # RbrightlineGithubIo + +## Material + + Material diff --git a/libs/core/.swcrc b/libs/core/.swcrc new file mode 100644 index 0000000..d54df2b --- /dev/null +++ b/libs/core/.swcrc @@ -0,0 +1,29 @@ +{ + "jsc": { + "target": "es2017", + "parser": { + "syntax": "typescript", + "decorators": true, + "dynamicImport": true + }, + "transform": { + "decoratorMetadata": true, + "legacyDecorator": true + }, + "keepClassNames": true, + "externalHelpers": true, + "loose": true + }, + "module": { + "type": "commonjs" + }, + "sourceMaps": true, + "exclude": [ + "jest.config.ts", + ".*\\.spec.tsx?$", + ".*\\.test.tsx?$", + "./src/jest-setup.ts$", + "./**/jest-setup.ts$", + ".*.js$" + ] +} diff --git a/libs/core/README.md b/libs/core/README.md new file mode 100644 index 0000000..84b21c3 --- /dev/null +++ b/libs/core/README.md @@ -0,0 +1,11 @@ +# core + +This library was generated with [Nx](https://nx.dev). + +## Building + +Run `nx build core` to build the library. + +## Running unit tests + +Run `nx test core` to execute the unit tests via [Vitest](https://vitest.dev/). diff --git a/libs/core/eslint.config.js b/libs/core/eslint.config.js new file mode 100644 index 0000000..6e58c36 --- /dev/null +++ b/libs/core/eslint.config.js @@ -0,0 +1,22 @@ +const baseConfig = require('../../eslint.config.js'); + +module.exports = [ + ...baseConfig, + { + files: ['**/*.json'], + rules: { + '@nx/dependency-checks': [ + 'error', + { + ignoredFiles: [ + '{projectRoot}/eslint.config.{js,cjs,mjs}', + '{projectRoot}/vite.config.{js,ts,mjs,mts}', + ], + }, + ], + }, + languageOptions: { + parser: require('jsonc-eslint-parser'), + }, + }, +]; diff --git a/libs/core/package.json b/libs/core/package.json new file mode 100644 index 0000000..4936ad1 --- /dev/null +++ b/libs/core/package.json @@ -0,0 +1,10 @@ +{ + "name": "@rline/core", + "version": "0.0.1", + "dependencies": { + "@swc/helpers": "~0.5.11" + }, + "type": "commonjs", + "main": "./src/index.js", + "typings": "./src/index.d.ts" +} diff --git a/libs/core/project.json b/libs/core/project.json new file mode 100644 index 0000000..b01e9aa --- /dev/null +++ b/libs/core/project.json @@ -0,0 +1,39 @@ +{ + "name": "core", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/core/src", + "projectType": "library", + "release": { + "version": { + "generatorOptions": { + "packageRoot": "dist/{projectRoot}", + "currentVersionResolver": "git-tag" + } + } + }, + "tags": [], + "targets": { + "build": { + "executor": "@nx/js:swc", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/libs/core", + "main": "libs/core/src/index.ts", + "tsConfig": "libs/core/tsconfig.lib.json", + "assets": ["libs/core/*.md"] + } + }, + "nx-release-publish": { + "options": { + "packageRoot": "dist/{projectRoot}" + } + }, + "test": { + "executor": "@nx/vite:test", + "outputs": ["{options.reportsDirectory}"], + "options": { + "reportsDirectory": "../../coverage/libs/core" + } + } + } +} diff --git a/libs/core/src/index.ts b/libs/core/src/index.ts new file mode 100644 index 0000000..3a29f52 --- /dev/null +++ b/libs/core/src/index.ts @@ -0,0 +1,6 @@ +// @index(['./**/*.ts','!./**/*.{test,spec}.ts'], f => `export * from '${f.path}'`) +export * from './lib/base/base.entity'; +export * from './lib/query/query'; +export * from './lib/transformers/query-operator'; +export * from './lib/transformers/query'; +export * from './lib/transformers/to-find-operator'; diff --git a/libs/core/src/lib/base/base.entity.ts b/libs/core/src/lib/base/base.entity.ts new file mode 100644 index 0000000..349f750 --- /dev/null +++ b/libs/core/src/lib/base/base.entity.ts @@ -0,0 +1,23 @@ +import { + Column, + PrimaryGeneratedColumn, + CreateDateColumn, + UpdateDateColumn, + DeleteDateColumn, +} from 'typeorm'; + +export class BaseEntity { + @PrimaryGeneratedColumn() + id: number; +} + +export class BaseEntityWithTimestamp extends BaseEntity { + @CreateDateColumn() createdAt: Date; + @UpdateDateColumn() updatedAt: Date; + @DeleteDateColumn() deletedAt: Date; +} + +export class BaseEntityWithActive extends BaseEntityWithTimestamp { + @Column({ type: 'boolean', default: false }) + active: boolean; +} diff --git a/libs/core/src/lib/query/query.ts b/libs/core/src/lib/query/query.ts new file mode 100644 index 0000000..8834650 --- /dev/null +++ b/libs/core/src/lib/query/query.ts @@ -0,0 +1,22 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class QueryModel { + @ApiProperty({ type: 'integer' }) + id: number; +} + +export class QueryModelWithTimestamp { + @ApiProperty({ type: 'number' }) + createdAt: Date; + + @ApiProperty({ type: 'number' }) + updatedAt: Date; + + @ApiProperty({ type: 'number' }) + deletedAt: Date; +} + +export class QueryModelWithActive { + @ApiProperty({ type: 'boolean' }) + active: boolean; +} diff --git a/libs/core/src/lib/transformers/query-operator.ts b/libs/core/src/lib/transformers/query-operator.ts new file mode 100644 index 0000000..16fffc5 --- /dev/null +++ b/libs/core/src/lib/transformers/query-operator.ts @@ -0,0 +1,11 @@ +export enum QueryOperator { + EQUAL = 'eq', + CONTAINS = 'cn', + MORE_THAN = 'mt', + LESS_THAN = 'lt', + + NOT_EQUAL = 'neq', + NOT_CONTAINS = 'ncn', + NOT_MORE_THAN = 'nmt', + NOT_LESS_THAN = 'nlt', +} diff --git a/libs/core/src/lib/transformers/query.ts b/libs/core/src/lib/transformers/query.ts new file mode 100644 index 0000000..6073d70 --- /dev/null +++ b/libs/core/src/lib/transformers/query.ts @@ -0,0 +1,6 @@ +import { Transform } from 'class-transformer'; + +export type QueryPropertyOptions = { + type: PropertyType; +}; +export function QueryProperty(options: QueryPropertyOptions) {} diff --git a/libs/core/src/lib/transformers/to-find-operator.ts b/libs/core/src/lib/transformers/to-find-operator.ts new file mode 100644 index 0000000..860a1eb --- /dev/null +++ b/libs/core/src/lib/transformers/to-find-operator.ts @@ -0,0 +1,32 @@ +import { Equal, FindOperator, ILike, LessThan, MoreThan, Not } from 'typeorm'; +import { QueryOperator } from './query-operator'; + +/** + * + * @param value query string delimeted by ":" + */ +export function toFindOperator( + operator: QueryOperator, + value: T +): FindOperator | undefined { + switch (operator as QueryOperator) { + case QueryOperator.CONTAINS: + return ILike(`%${value}%`); + case QueryOperator.EQUAL: + return Equal(value); + case QueryOperator.LESS_THAN: + return LessThan(value); + case QueryOperator.MORE_THAN: + return MoreThan(value); + case QueryOperator.NOT_CONTAINS: + return Not(ILike(`%${value}%`)); + case QueryOperator.NOT_EQUAL: + return Not(ILike(`${value}`)); + case QueryOperator.NOT_LESS_THAN: + return Not(LessThan(value)); + case QueryOperator.NOT_MORE_THAN: + return Not(MoreThan(value)); + default: + return undefined; + } +} diff --git a/libs/core/tsconfig.json b/libs/core/tsconfig.json new file mode 100644 index 0000000..6f7169a --- /dev/null +++ b/libs/core/tsconfig.json @@ -0,0 +1,22 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noPropertyAccessFromIndexSignature": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/core/tsconfig.lib.json b/libs/core/tsconfig.lib.json new file mode 100644 index 0000000..54b671b --- /dev/null +++ b/libs/core/tsconfig.lib.json @@ -0,0 +1,23 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": [ + "vite.config.ts", + "vite.config.mts", + "vitest.config.ts", + "vitest.config.mts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx" + ] +} diff --git a/libs/core/tsconfig.spec.json b/libs/core/tsconfig.spec.json new file mode 100644 index 0000000..56b7488 --- /dev/null +++ b/libs/core/tsconfig.spec.json @@ -0,0 +1,28 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [ + "vitest/globals", + "vitest/importMeta", + "vite/client", + "node", + "vitest" + ] + }, + "include": [ + "vite.config.ts", + "vite.config.mts", + "vitest.config.ts", + "vitest.config.mts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx", + "src/**/*.d.ts" + ] +} diff --git a/libs/core/vite.config.ts b/libs/core/vite.config.ts new file mode 100644 index 0000000..fd944e8 --- /dev/null +++ b/libs/core/vite.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from 'vite'; +import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; +import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin'; + +export default defineConfig({ + root: __dirname, + cacheDir: '../../node_modules/.vite/libs/core', + plugins: [nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])], + // Uncomment this if you are using workers. + // worker: { + // plugins: [ nxViteTsPaths() ], + // }, + test: { + watch: false, + globals: true, + environment: 'node', + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + reporters: ['default'], + coverage: { reportsDirectory: '../../coverage/libs/core', provider: 'v8' }, + }, +}); diff --git a/libs/entity/src/index.ts b/libs/entity/src/index.ts index 6cfec5f..5d15b6f 100644 --- a/libs/entity/src/index.ts +++ b/libs/entity/src/index.ts @@ -1 +1 @@ -export * from './lib/entity'; +// @index(['./**/*.ts', '!./**/*.{spec,test,story,stories}.ts'], f => `export * from '${f.path}'`) diff --git a/libs/entity/src/lib/category/create.ts b/libs/entity/src/lib/category/create.ts new file mode 100644 index 0000000..26fe154 --- /dev/null +++ b/libs/entity/src/lib/category/create.ts @@ -0,0 +1,12 @@ +import { CategoryModel } from '@rline/model'; +import { Exclude } from 'class-transformer'; +import { MaxLength, MinLength } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; + +@Exclude() +export class CreateCategoryDto implements CategoryModel { + @ApiProperty({ type: 'string', minLength: 3, maxLength: 50, required: true }) + @MinLength(3) + @MaxLength(50) + name: string; +} diff --git a/libs/entity/src/lib/category/entity.ts b/libs/entity/src/lib/category/entity.ts new file mode 100644 index 0000000..3b99d48 --- /dev/null +++ b/libs/entity/src/lib/category/entity.ts @@ -0,0 +1,9 @@ +import { BaseEntity } from '@rline/core'; +import { CategoryEntity } from '@rline/model'; +import { Column, Entity } from 'typeorm'; + +@Entity() +export class Category extends BaseEntity implements CategoryEntity { + @Column({ type: 'varchar', unique: true }) + name: string; +} diff --git a/libs/entity/src/lib/entity.spec.ts b/libs/entity/src/lib/entity.spec.ts deleted file mode 100644 index 7594930..0000000 --- a/libs/entity/src/lib/entity.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { entity } from './entity'; - -describe('entity', () => { - it('should work', () => { - expect(entity()).toEqual('entity'); - }); -}); diff --git a/libs/entity/src/lib/entity.ts b/libs/entity/src/lib/entity.ts deleted file mode 100644 index f95fb5f..0000000 --- a/libs/entity/src/lib/entity.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function entity(): string { - return 'entity'; -} diff --git a/libs/model/src/index.ts b/libs/model/src/index.ts index 1f2da0b..74b0049 100644 --- a/libs/model/src/index.ts +++ b/libs/model/src/index.ts @@ -1 +1,2 @@ -export * from './lib/model'; +// @index(['./**/*.ts', '!./**/*.{test,spec}.ts'], f => `export * from '${f.path}'`) +export * from './lib/category/category'; diff --git a/libs/model/src/lib/category/category.ts b/libs/model/src/lib/category/category.ts new file mode 100644 index 0000000..8e2017e --- /dev/null +++ b/libs/model/src/lib/category/category.ts @@ -0,0 +1,7 @@ +import { Model } from '@rline/type'; + +export type CategoryModel = { + name: string; +}; + +export type CategoryEntity = Model; diff --git a/libs/model/src/lib/model.spec.ts b/libs/model/src/lib/model.spec.ts deleted file mode 100644 index a2062ba..0000000 --- a/libs/model/src/lib/model.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { model } from './model'; - -describe('model', () => { - it('should work', () => { - expect(model()).toEqual('model'); - }); -}); diff --git a/libs/model/src/lib/model.ts b/libs/model/src/lib/model.ts deleted file mode 100644 index d59d429..0000000 --- a/libs/model/src/lib/model.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function model(): string { - return 'model'; -} diff --git a/libs/type/README.md b/libs/type/README.md index 7ab6a3f..eece284 100644 --- a/libs/type/README.md +++ b/libs/type/README.md @@ -1,11 +1 @@ # type - -This library was generated with [Nx](https://nx.dev). - -## Building - -Run `nx build type` to build the library. - -## Running unit tests - -Run `nx test type` to execute the unit tests via [Vitest](https://vitest.dev/). diff --git a/libs/type/src/index.ts b/libs/type/src/index.ts index d0df430..34b99f2 100644 --- a/libs/type/src/index.ts +++ b/libs/type/src/index.ts @@ -1 +1,2 @@ -export * from './lib/type'; +// @index(['./**/*.ts', '!./**/*.{test,spec}.ts'], f => `export * from '${f.path}'`) +export * from './lib/base/base'; diff --git a/libs/type/src/lib/base/base.ts b/libs/type/src/lib/base/base.ts new file mode 100644 index 0000000..383204e --- /dev/null +++ b/libs/type/src/lib/base/base.ts @@ -0,0 +1,27 @@ +export type ID = { + id: T; +}; + +export type BaseModel = ID; + +export type BaseModelWithTimestamp = { + createdAt: T; + updatedAt: T; + deletedAt: T; +}; + +export type BaseModelWithActive = { + active: T; +}; + +export type BaseModelWithOwner = { + owner: T; +}; + +export type Model = BaseModel & T; + +export type ModelWithTimestamp = Model & BaseModelWithTimestamp; + +export type ModelWithActive = ModelWithTimestamp & BaseModelWithActive; + +export type ModelWithOwner = ModelWithActive & BaseModelWithOwner & T; diff --git a/libs/type/src/lib/type.spec.ts b/libs/type/src/lib/type.spec.ts deleted file mode 100644 index 3d39e55..0000000 --- a/libs/type/src/lib/type.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { type } from './type'; - -describe('type', () => { - it('should work', () => { - expect(type()).toEqual('type'); - }); -}); diff --git a/libs/type/src/lib/type.ts b/libs/type/src/lib/type.ts deleted file mode 100644 index 6bcf048..0000000 --- a/libs/type/src/lib/type.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function type(): string { - return 'type'; -} diff --git a/package.json b/package.json index dfb5c90..9e49861 100644 --- a/package.json +++ b/package.json @@ -101,4 +101,4 @@ "nx": { "includedScripts": [] } -} \ No newline at end of file +} diff --git a/tsconfig.base.json b/tsconfig.base.json index bbb1082..1df822e 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -14,12 +14,15 @@ "skipLibCheck": true, "skipDefaultLibCheck": true, "baseUrl": ".", + "strictPropertyInitialization": false, "paths": { "@rbrightline.github.io/api": ["apps/api/src/index.ts"], + "@rline/core": ["libs/core/src/index.ts"], "@rline/entity": ["libs/entity/src/index.ts"], "@rline/gen": ["libs/gen/src/index.ts"], "@rline/material": ["libs/material/src/index.ts"], "@rline/model": ["libs/model/src/index.ts"], + "@rline/property": ["libs/property/src/index.ts"], "@rline/resource": ["libs/resource/src/index.ts"], "@rline/type": ["libs/type/src/index.ts"] }