@companix/xeo-server 0.0.2 → 0.0.3

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.
@@ -1,12 +0,0 @@
1
- import { Module } from '@nestjs/common'
2
- import { MongooseDriverModule } from '../../../lib'
3
- import { AppService } from './app.service'
4
- import { AppController } from './app.controller'
5
- import { dataScheme } from '@companix/xeo-devkit'
6
-
7
- @Module({
8
- imports: [MongooseDriverModule.forFeature(dataScheme)],
9
- controllers: [AppController],
10
- providers: [AppService]
11
- })
12
- export class AppModule {}
@@ -1,76 +0,0 @@
1
- import { Injectable } from '@nestjs/common'
2
- import { InjectDataSource } from '../../../lib'
3
- import { dataScheme, AppScheme } from '@companix/xeo-devkit'
4
- import { DataSource } from '@companix/xeo-scheme'
5
- import {
6
- CreateBankCardDto,
7
- CreateBankDetailDto,
8
- CreateDictionaryDto,
9
- CreateOptionDto,
10
- CreateRoleDto,
11
- CreateScanDto,
12
- CreateWorkerDto,
13
- UpdateOptionDto
14
- } from './app.dto'
15
- import { MongoCollectionDriver } from '../../../lib/drivers/collection.driver'
16
-
17
- @Injectable()
18
- export class AppService {
19
- constructor(
20
- @InjectDataSource(dataScheme)
21
- private readonly dataSource: DataSource<AppScheme, MongoCollectionDriver<AppScheme>>
22
- ) {}
23
-
24
- async addWorker({ worker }: CreateWorkerDto) {
25
- return this.dataSource.collections.worker.create(worker)
26
- }
27
-
28
- async addScan({ scan }: CreateScanDto) {
29
- return this.dataSource.collections.scan.create(scan)
30
- }
31
-
32
- async addBankCard({ bankCard }: CreateBankCardDto) {
33
- return this.dataSource.collections.bankCard.create(bankCard)
34
- }
35
-
36
- async addBankDetail({ bankDetail }: CreateBankDetailDto) {
37
- return this.dataSource.collections.bankDetail.create(bankDetail)
38
- }
39
-
40
- async addRole({ role }: CreateRoleDto) {
41
- return this.dataSource.collections.role.create(role)
42
- }
43
-
44
- async addDictionary({ dictionary }: CreateDictionaryDto) {
45
- return this.dataSource.collections.dictionaries.create(dictionary)
46
- }
47
-
48
- async addOption({ option }: CreateOptionDto) {
49
- return this.dataSource.collections.options.create(option)
50
- }
51
-
52
- async updateOption({ option }: UpdateOptionDto) {
53
- return this.dataSource.collections.options.update(option.value, (target) => {
54
- target.dictionary = option.dictionary
55
- target.title = option.title
56
- })
57
- }
58
-
59
- async getState() {
60
- return {
61
- worker: await this.dataSource.collections.worker.getAll(),
62
- scan: await this.dataSource.collections.scan.getAll(),
63
- bankCard: await this.dataSource.collections.bankCard.getAll(),
64
- bankDetail: await this.dataSource.collections.bankDetail.getAll(),
65
- // role
66
- role: await this.dataSource.collections.role.getAll(),
67
- // options
68
- dictionaries: await this.dataSource.collections.dictionaries.getAll(),
69
- options: await this.dataSource.collections.options.getAll()
70
- }
71
- }
72
-
73
- async getTables() {
74
- return this.dataSource.driver.tables.getTables()
75
- }
76
- }
@@ -1,12 +0,0 @@
1
- import { Module } from '@nestjs/common'
2
- import { AppModule } from './module/app.module'
3
- import { MongooseDriverModule } from '../../lib'
4
- import { getMongoConnectionOptions, getMongoConnectionURL } from './db'
5
-
6
- @Module({
7
- imports: [
8
- MongooseDriverModule.forRoot(getMongoConnectionURL(), getMongoConnectionOptions()),
9
- AppModule //
10
- ]
11
- })
12
- export class RootModule {}
@@ -1,6 +0,0 @@
1
- /* eslint-disable no-var */
2
- declare global {
3
- var __DEV__: boolean | undefined
4
- }
5
-
6
- export {}
@@ -1,154 +0,0 @@
1
- import { writeFile } from 'fs'
2
-
3
- import { afterAll, beforeAll, beforeEach, describe, expect, test } from '@jest/globals'
4
- import { DataSource, IndexedTableStore, TableRow, TableStore } from '@companix/xeo-scheme'
5
- import { getConnectionToken, getDataSourceToken } from '../../lib'
6
-
7
- import { bootstrap } from '../app/bootstrap'
8
- import { MongoCollectionDriver } from '../../lib/drivers/collection.driver'
9
- import { AppScheme, BaseParams, MockKit, cases, createMockKit, dataScheme } from '@companix/xeo-devkit'
10
- import { Connection } from 'mongoose'
11
- import { INestApplication } from '@nestjs/common'
12
-
13
- interface TestOptions {
14
- params: BaseParams
15
- commitChanges: (kit: MockKit, dataSource: DataSource<AppScheme>) => Promise<void>
16
- }
17
-
18
- const normalizeCollection = <T>(records: T[]) => {
19
- return records.map((record) => {
20
- if (!record || typeof record !== 'object' || Array.isArray(record)) {
21
- return record
22
- }
23
-
24
- const { _id, __v, ...rest } = record as Record<string, unknown>
25
- return rest as T
26
- })
27
- }
28
-
29
- const normalizeTableState = (tableState: { m1: TableStore; m2: TableStore }) => {
30
- const normalizeSide = (side: TableStore): TableStore => {
31
- const normalized: TableStore = {}
32
-
33
- for (const key of Object.keys(side).sort()) {
34
- normalized[key] = [...side[key]].sort((a, b) => String(a).localeCompare(String(b)))
35
- }
36
-
37
- return normalized
38
- }
39
-
40
- return {
41
- m1: normalizeSide(tableState.m1),
42
- m2: normalizeSide(tableState.m2)
43
- }
44
- }
45
-
46
- const getTablesState = async (tables: { [name: string]: TableRow[] }) => {
47
- const state: BaseParams['expectations']['tables'] = {}
48
-
49
- for (const table of dataScheme.tables) {
50
- const indexedStore = new IndexedTableStore(table)
51
- indexedStore.initialize(tables[table.tableName] ?? [])
52
- state[table.tableName] = normalizeTableState(indexedStore.getStore())
53
- }
54
-
55
- return state
56
- }
57
-
58
- const runTest = async (app: INestApplication<any>, { params, commitChanges }: TestOptions) => {
59
- const { expectations } = params
60
- const dataSource = app.get<DataSource<AppScheme, MongoCollectionDriver<AppScheme>>>(
61
- getDataSourceToken(dataScheme)
62
- )
63
-
64
- await commitChanges(createMockKit(dataSource), dataSource).catch((error) => {
65
- expect(error).toEqual(expectations.error)
66
- })
67
-
68
- const snapshot = { result: {} as object, table: {} as object }
69
-
70
- for (const collection in dataSource.collections) {
71
- const collectionName = collection as keyof AppScheme
72
-
73
- await dataSource.collections[collectionName].getAll().then((result) => {
74
- snapshot.result[collectionName] = result
75
- expect(normalizeCollection(result)).toEqual(expectations.scheme[collectionName] ?? [])
76
- })
77
- }
78
-
79
- const tableRows = await dataSource.driver.tables.getTables()
80
- const tables = await getTablesState(tableRows)
81
-
82
- snapshot.table = tableRows
83
-
84
- for (const table of dataScheme.tables) {
85
- try {
86
- expect(tables[table.tableName] ?? { m1: {}, m2: {} }).toEqual(
87
- normalizeTableState(expectations.tables[table.tableName] ?? { m1: {}, m2: {} })
88
- )
89
- } catch (error) {
90
- throw error
91
- }
92
- }
93
-
94
- return snapshot
95
- }
96
-
97
- describe('DataSource', () => {
98
- let app: INestApplication
99
-
100
- const buffer: object[] = []
101
-
102
- beforeAll(async () => {
103
- app = await bootstrap(3222)
104
- })
105
-
106
- beforeEach(async () => {
107
- const connection = app.get<Connection>(getConnectionToken())
108
- await connection.dropDatabase()
109
- })
110
-
111
- afterAll(async () => {
112
- const connection = app.get<Connection>(getConnectionToken())
113
-
114
- await new Promise<void>((resolve) => {
115
- writeFile(`./tests/${Date.now()}.test.json`, JSON.stringify(buffer), (err) => {
116
- if (err) {
117
- console.log('writeFile:', err)
118
- return
119
- }
120
-
121
- resolve()
122
- })
123
- })
124
-
125
- await connection.dropDatabase()
126
- await app.close()
127
- })
128
-
129
- for (const item of cases) {
130
- if (item.type === 'unit') {
131
- test(item.name, async () => {
132
- const snapshot = await runTest(app, {
133
- params: item.params,
134
- commitChanges: (kit, dataSource) => item.params.execute(kit, dataSource)
135
- })
136
-
137
- buffer.push({ name: item.name, snapshot })
138
- })
139
- }
140
-
141
- if (item.type === 'dual') {
142
- for (const marker of item.markers) {
143
- test(`${item.name} / ${marker}`, async () => {
144
- const snapshot = await runTest(app, {
145
- params: item.params,
146
- commitChanges: (kit, dataSource) => item.params.execute(kit, dataSource, marker)
147
- })
148
-
149
- buffer.push({ name: item.name, snapshot })
150
- })
151
- }
152
- }
153
- }
154
- })
@@ -1,69 +0,0 @@
1
- import { CoreError, DataSource } from '@companix/xeo-scheme'
2
- import { getDataSourceToken } from '../../lib'
3
-
4
- import { bootstrap } from '../app/bootstrap'
5
- import { MongoCollectionDriver } from '../../lib/drivers/collection.driver'
6
- import { AppScheme, createMockKit, dataScheme } from '@companix/xeo-devkit'
7
-
8
- const runScenario = async (dataSource: DataSource<AppScheme, MongoCollectionDriver<AppScheme>>) => {
9
- // version: 11
10
- // const kit = await createMockKit(dataSource)
11
- // await kit.addDictionary({ dictionary: 'regions' })
12
- // await kit.addOption({
13
- // value: 'option-1',
14
- // dictionary: 'regions' // BelongsTo
15
- // })
16
- // await kit.addRevisorWorker({ workerId: 2 })
17
- // await dataSource.collections.worker.update(2, (w) => {
18
- // if (w.type === 'revisor') {
19
- // w.about.height = 10
20
- // w.password = 'password'
21
- // // w.job_type = 'self_employed'
22
- // }
23
- // })
24
- // await kit.addDictionary({
25
- // dictionary: 'value_type',
26
- // name: 'ТМЦ'
27
- // })
28
- // await kit.addOption({
29
- // dictionary: 'regions',
30
- // value: 'moscow',
31
- // title: 'Москва'
32
- // })
33
- // await kit.addOption({
34
- // dictionary: 'regions',
35
- // value: 'shanghai',
36
- // title: 'Шанхай'
37
- // })
38
- // await dataSource.collections.options.remove('moscow')
39
- // await kit.addRole({
40
- // value: 'director',
41
- // title: 'Директор'
42
- // })
43
- // await kit.addRevisorWorker({
44
- // workerId: 3
45
- // })
46
- // await dataSource.collections.worker.remove(1)
47
- }
48
-
49
- async function bootstrapTest() {
50
- const app = await bootstrap()
51
- const dataSource = app.get<DataSource<AppScheme, MongoCollectionDriver<AppScheme>>>(
52
- getDataSourceToken(dataScheme)
53
- )
54
-
55
- try {
56
- console.log('Run Scenario')
57
- await runScenario(dataSource)
58
- console.log('Scenario Done!')
59
- } catch (error) {
60
- if (error instanceof CoreError) {
61
- console.error('CoreError:', error)
62
- return
63
- }
64
-
65
- throw error
66
- }
67
- }
68
-
69
- bootstrapTest()
@@ -1,31 +0,0 @@
1
- import { dataScheme } from '@companix/xeo-devkit'
2
- import { DefinitionsFactory } from '../../lib/factories/definitions.factory'
3
-
4
- const factory = new DefinitionsFactory(dataScheme)
5
-
6
- const schemas = {
7
- worker: factory.createForCollection('worker'),
8
- bankCard: factory.createForCollection('bankCard'),
9
- bankDetail: factory.createForCollection('bankDetail'),
10
- scan: factory.createForCollection('scan'),
11
- dictionaries: factory.createForCollection('dictionaries'),
12
- options: factory.createForCollection('options')
13
- }
14
-
15
- const workerScheme = dataScheme.models[dataScheme.collections.worker.name].scheme
16
-
17
- console.dir(schemas.worker, {
18
- depth: null,
19
- colors: true
20
- })
21
-
22
- if (workerScheme.type === 'discriminated') {
23
- console.log('\ndiscriminated:\n')
24
-
25
- for (const i of workerScheme.discriminators) {
26
- console.dir(factory.createDefinitionScheme(i), {
27
- depth: null,
28
- colors: true
29
- })
30
- }
31
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./lib"
6
- },
7
- "include": ["lib/**/*"],
8
- "exclude": ["node_modules", "tests", "dist", "**/*spec.ts"]
9
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "declaration": true,
5
- "noImplicitAny": false,
6
- "removeComments": true,
7
- "noLib": false,
8
- "emitDecoratorMetadata": true,
9
- "experimentalDecorators": true,
10
- "target": "ES2021",
11
- "sourceMap": true,
12
- "esModuleInterop": true,
13
- "skipLibCheck": true,
14
- "strict": true,
15
- "strictPropertyInitialization": false
16
- }
17
- }
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "./tests/app/dist",
5
- "rootDir": ".",
6
- "declaration": false
7
- },
8
- "include": ["lib/**/*", "tests/**/*"],
9
- "exclude": ["node_modules", "dist", "**/*spec.ts"]
10
- }