init
This commit is contained in:
commit
502f22e0b4
|
|
@ -0,0 +1,118 @@
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variables file
|
||||||
|
.env
|
||||||
|
.env.test
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.*
|
||||||
|
package-lock.json
|
||||||
|
test/test-app-domain
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { AppLoader as GeneralAppLoader, RowStore, Context, EntityDict } from "oak-domain/lib/types";
|
||||||
|
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
||||||
|
export declare class AppLoader<ED extends EntityDict, Cxt extends Context<ED>> extends GeneralAppLoader<ED, Cxt> {
|
||||||
|
private dbStore;
|
||||||
|
private aspectDict;
|
||||||
|
private contextBuilder;
|
||||||
|
constructor(path: string, contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt, dbConfig: MySQLConfiguration);
|
||||||
|
mount(initialize?: true): Promise<void>;
|
||||||
|
unmount(): Promise<void>;
|
||||||
|
execAspect(name: string, context: Cxt, params?: any): Promise<any>;
|
||||||
|
initialize(dropIfExists?: boolean): Promise<void>;
|
||||||
|
getStore(): RowStore<ED, Cxt>;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.AppLoader = void 0;
|
||||||
|
const actionDef_1 = require("oak-domain/lib/store/actionDef");
|
||||||
|
const types_1 = require("oak-domain/lib/types");
|
||||||
|
const DbStore_1 = require("./DbStore");
|
||||||
|
const index_1 = __importDefault(require("oak-common-aspect/lib/index"));
|
||||||
|
function initTriggers(dbStore, path) {
|
||||||
|
const { triggers } = require(`${path}/lib/triggers/index`);
|
||||||
|
const { checkers } = require(`${path}/lib/checkers/index`);
|
||||||
|
const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
|
||||||
|
const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
|
||||||
|
triggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
||||||
|
adTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
||||||
|
checkers.forEach((checker) => dbStore.registerChecker(checker));
|
||||||
|
adCheckers.forEach((checker) => dbStore.registerChecker(checker));
|
||||||
|
}
|
||||||
|
class AppLoader extends types_1.AppLoader {
|
||||||
|
dbStore;
|
||||||
|
aspectDict;
|
||||||
|
contextBuilder;
|
||||||
|
constructor(path, contextBuilder, dbConfig) {
|
||||||
|
super(path);
|
||||||
|
const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
|
||||||
|
this.aspectDict = Object.assign({}, index_1.default, require(`${path}/lib/aspects/index`).aspectDict);
|
||||||
|
this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
|
||||||
|
this.contextBuilder = contextBuilder;
|
||||||
|
}
|
||||||
|
async mount(initialize) {
|
||||||
|
const { path } = this;
|
||||||
|
if (!initialize) {
|
||||||
|
initTriggers(this.dbStore, path);
|
||||||
|
}
|
||||||
|
this.dbStore.connect();
|
||||||
|
}
|
||||||
|
async unmount() {
|
||||||
|
this.dbStore.disconnect();
|
||||||
|
}
|
||||||
|
async execAspect(name, context, params) {
|
||||||
|
const fn = this.aspectDict[name];
|
||||||
|
if (!fn) {
|
||||||
|
throw new Error(`不存在的接口名称: ${name}`);
|
||||||
|
}
|
||||||
|
return await fn(params, context);
|
||||||
|
}
|
||||||
|
async initialize(dropIfExists) {
|
||||||
|
await this.dbStore.initialize(dropIfExists);
|
||||||
|
const { data } = require(`${this.path}/lib/data/index`);
|
||||||
|
const context = this.contextBuilder()(this.dbStore);
|
||||||
|
await context.begin();
|
||||||
|
for (const entity in data) {
|
||||||
|
let rows = data[entity];
|
||||||
|
if (entity === 'area') {
|
||||||
|
// 对area暂时处理一下
|
||||||
|
rows = require('./data/area.json');
|
||||||
|
}
|
||||||
|
await this.dbStore.operate(entity, {
|
||||||
|
data: rows,
|
||||||
|
action: 'create',
|
||||||
|
}, context);
|
||||||
|
console.log(`data in ${entity} initialized!`);
|
||||||
|
}
|
||||||
|
await context.commit();
|
||||||
|
this.dbStore.disconnect();
|
||||||
|
}
|
||||||
|
getStore() {
|
||||||
|
return this.dbStore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.AppLoader = AppLoader;
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { RowStore } from 'oak-domain/lib/types';
|
||||||
|
import { GeneralRuntimeContext } from 'oak-general-business';
|
||||||
|
import { EntityDict } from 'oak-general-business/lib/general-app-domain';
|
||||||
|
export declare class Context<ED extends EntityDict> extends GeneralRuntimeContext<ED> {
|
||||||
|
static FromCxtStr(cxtStr?: string): <ED extends EntityDict>(store: RowStore<ED, Context<ED>>) => Context<ED>;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.Context = void 0;
|
||||||
|
const oak_general_business_1 = require("oak-general-business");
|
||||||
|
class Context extends oak_general_business_1.GeneralRuntimeContext {
|
||||||
|
static FromCxtStr(cxtStr) {
|
||||||
|
const { token, applicationId, scene } = cxtStr ? oak_general_business_1.GeneralRuntimeContext.fromString(cxtStr) : {
|
||||||
|
token: undefined,
|
||||||
|
applicationId: undefined,
|
||||||
|
scene: undefined,
|
||||||
|
};
|
||||||
|
return (store) => {
|
||||||
|
const context = new Context(store, applicationId);
|
||||||
|
context.setScene(scene);
|
||||||
|
context.setToken(token);
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.Context = Context;
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { MysqlStore, MySqlSelectOption, MysqlOperateOption } from 'oak-db';
|
||||||
|
import { EntityDict, Context, StorageSchema, SelectionResult, Trigger, Checker, SelectRowShape, RowStore } from 'oak-domain/lib/types';
|
||||||
|
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
||||||
|
export declare class DbStore<ED extends EntityDict, Cxt extends Context<ED>> extends MysqlStore<ED, Cxt> {
|
||||||
|
private executor;
|
||||||
|
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt, mysqlConfiguration: MySQLConfiguration);
|
||||||
|
protected cascadeUpdate<T extends keyof ED>(entity: T, operation: ED[T]['Create'] | ED[T]['Update'] | ED[T]['Remove'], context: Cxt, option?: MysqlOperateOption): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
|
||||||
|
protected cascadeSelect<T extends keyof ED, S extends ED[T]["Selection"]>(entity: T, selection: S, context: Cxt, option?: MySqlSelectOption): Promise<SelectRowShape<ED[T]['Schema'], S['data']>[]>;
|
||||||
|
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, params?: MysqlOperateOption): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
|
||||||
|
select<T extends keyof ED, S extends ED[T]['Selection']>(entity: T, selection: S, context: Cxt, params?: MySqlSelectOption): Promise<SelectionResult<ED[T]["Schema"], S["data"]>>;
|
||||||
|
registerTrigger<T extends keyof ED>(trigger: Trigger<ED, T, Cxt>): void;
|
||||||
|
registerChecker<T extends keyof ED>(checker: Checker<ED, T, Cxt>): void;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.DbStore = void 0;
|
||||||
|
const oak_db_1 = require("oak-db");
|
||||||
|
const TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
|
||||||
|
class DbStore extends oak_db_1.MysqlStore {
|
||||||
|
executor;
|
||||||
|
constructor(storageSchema, contextBuilder, mysqlConfiguration) {
|
||||||
|
super(storageSchema, mysqlConfiguration);
|
||||||
|
this.executor = new TriggerExecutor_1.TriggerExecutor(async (scene) => contextBuilder(scene)(this));
|
||||||
|
}
|
||||||
|
async cascadeUpdate(entity, operation, context, option) {
|
||||||
|
await this.executor.preOperation(entity, operation, context, option);
|
||||||
|
const result = super.cascadeUpdate(entity, operation, context, option);
|
||||||
|
await this.executor.postOperation(entity, operation, context, option);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
async cascadeSelect(entity, selection, context, option) {
|
||||||
|
const selection2 = Object.assign({
|
||||||
|
action: 'select',
|
||||||
|
}, selection);
|
||||||
|
if (!option?.ignoreTrigger) {
|
||||||
|
await this.executor.preOperation(entity, selection2, context, option);
|
||||||
|
}
|
||||||
|
const result = await super.cascadeSelect(entity, selection2, context, option);
|
||||||
|
if (!option?.ignoreTrigger) {
|
||||||
|
await this.executor.postOperation(entity, selection2, context, option, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
async operate(entity, operation, context, params) {
|
||||||
|
const autoCommit = !context.getCurrentTxnId();
|
||||||
|
let result;
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.begin();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
result = await super.operate(entity, operation, context, params);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
await context.rollback();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.commit();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
async select(entity, selection, context, params) {
|
||||||
|
const autoCommit = !context.getCurrentTxnId();
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.begin();
|
||||||
|
}
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result = await super.select(entity, selection, context, params);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
await context.rollback();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.commit();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
registerTrigger(trigger) {
|
||||||
|
this.executor.registerTrigger(trigger);
|
||||||
|
}
|
||||||
|
registerChecker(checker) {
|
||||||
|
this.executor.registerChecker(checker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.DbStore = DbStore;
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { Context as BackendContext } from './BackendContext';
|
||||||
|
export { AppLoader } from './AppLoader';
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.AppLoader = exports.BackendContext = void 0;
|
||||||
|
var BackendContext_1 = require("./BackendContext");
|
||||||
|
Object.defineProperty(exports, "BackendContext", { enumerable: true, get: function () { return BackendContext_1.Context; } });
|
||||||
|
var AppLoader_1 = require("./AppLoader");
|
||||||
|
Object.defineProperty(exports, "AppLoader", { enumerable: true, get: function () { return AppLoader_1.AppLoader; } });
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
export declare type GenerateIdOption = {
|
||||||
|
shuffle?: boolean;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const uuid_1 = require("uuid");
|
||||||
|
async function generateNewId(option) {
|
||||||
|
if (option?.shuffle && process.env.NODE_ENV === 'development') {
|
||||||
|
return (0, uuid_1.v4)();
|
||||||
|
}
|
||||||
|
return (0, uuid_1.v1)();
|
||||||
|
}
|
||||||
|
Object.assign(global, {
|
||||||
|
generateNewId,
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
{
|
||||||
|
"name": "oak-backend-base",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "oak-backend-base",
|
||||||
|
"main": "src/index",
|
||||||
|
"scripts": {
|
||||||
|
"test": "ts-node test/test.ts",
|
||||||
|
"build": "tsc"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"koa": "^2.13.4",
|
||||||
|
"koa-body": "^5.0.0",
|
||||||
|
"koa-router": "^10.1.1",
|
||||||
|
"lodash": "^4.17.21",
|
||||||
|
"mysql": "^2.18.1",
|
||||||
|
"mysql2": "^2.3.3",
|
||||||
|
"oak-db": "file:../oak-db",
|
||||||
|
"oak-domain": "file:../oak-domain",
|
||||||
|
"oak-general-business": "file:../oak-general-business",
|
||||||
|
"oak-common-aspect": "file:../oak-common-aspect",
|
||||||
|
"uuid": "^8.3.2"
|
||||||
|
},
|
||||||
|
"author": "XuChang",
|
||||||
|
"license": "ISC",
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/koa": "^2.13.4",
|
||||||
|
"@types/koa-router": "^7.4.4",
|
||||||
|
"@types/node": "^17.0.40",
|
||||||
|
"@types/uuid": "^8.3.4",
|
||||||
|
"ts-node": "^10.8.1",
|
||||||
|
"typescript": "^4.7.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,85 @@
|
||||||
|
import { analyzeActionDefDict } from "oak-domain/lib/store/actionDef";
|
||||||
|
import { AppLoader as GeneralAppLoader, Trigger, Checker, Aspect, RowStore, Context, EntityDict } from "oak-domain/lib/types";
|
||||||
|
import { DbStore } from "./DbStore";
|
||||||
|
import generalAspectDict from 'oak-common-aspect/lib/index';
|
||||||
|
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
||||||
|
|
||||||
|
function initTriggers<ED extends EntityDict, Cxt extends Context<ED>>(dbStore: DbStore<ED, Cxt>, path: string) {
|
||||||
|
const { triggers } = require(`${path}/lib/triggers/index`);
|
||||||
|
const { checkers } = require(`${path}/lib/checkers/index`);
|
||||||
|
const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
|
||||||
|
|
||||||
|
const { triggers: adTriggers, checkers: adCheckers } = analyzeActionDefDict(dbStore.getSchema(), ActionDefDict);
|
||||||
|
triggers.forEach(
|
||||||
|
(trigger: Trigger<ED, keyof ED, Cxt>) => dbStore.registerTrigger(trigger)
|
||||||
|
);
|
||||||
|
adTriggers.forEach(
|
||||||
|
(trigger) => dbStore.registerTrigger(trigger)
|
||||||
|
);
|
||||||
|
checkers.forEach(
|
||||||
|
(checker: Checker<ED, keyof ED, Cxt>) => dbStore.registerChecker(checker)
|
||||||
|
);
|
||||||
|
adCheckers.forEach(
|
||||||
|
(checker) => dbStore.registerChecker(checker)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class AppLoader<ED extends EntityDict, Cxt extends Context<ED>> extends GeneralAppLoader<ED, Cxt> {
|
||||||
|
private dbStore: DbStore<ED, Cxt>;
|
||||||
|
private aspectDict: Record<string, Aspect<ED, Cxt>>;
|
||||||
|
private contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt;
|
||||||
|
constructor(path: string, contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt, dbConfig: MySQLConfiguration) {
|
||||||
|
super(path);
|
||||||
|
const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
|
||||||
|
this.aspectDict = Object.assign({}, generalAspectDict, require(`${path}/lib/aspects/index`).aspectDict);
|
||||||
|
this.dbStore = new DbStore<ED, Cxt>(storageSchema, contextBuilder, dbConfig);
|
||||||
|
this.contextBuilder = contextBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
async mount(initialize?: true) {
|
||||||
|
const { path } = this;
|
||||||
|
if (!initialize) {
|
||||||
|
initTriggers(this.dbStore, path);
|
||||||
|
}
|
||||||
|
this.dbStore.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
async unmount() {
|
||||||
|
this.dbStore.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
async execAspect(name: string, context: Cxt, params?: any): Promise<any> {
|
||||||
|
const fn = this.aspectDict[name];
|
||||||
|
if (!fn) {
|
||||||
|
throw new Error(`不存在的接口名称: ${name}`);
|
||||||
|
}
|
||||||
|
return await fn(params, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
async initialize(dropIfExists?: boolean) {
|
||||||
|
await this.dbStore.initialize(dropIfExists);
|
||||||
|
|
||||||
|
const { data } = require(`${this.path}/lib/data/index`);
|
||||||
|
const context = this.contextBuilder()(this.dbStore);
|
||||||
|
await context.begin();
|
||||||
|
for (const entity in data) {
|
||||||
|
let rows = data[entity];
|
||||||
|
if (entity === 'area') {
|
||||||
|
// 对area暂时处理一下
|
||||||
|
rows = require('./data/area.json');
|
||||||
|
}
|
||||||
|
await this.dbStore.operate(entity as keyof ED, {
|
||||||
|
data: rows,
|
||||||
|
action: 'create',
|
||||||
|
} as any, context);
|
||||||
|
console.log(`data in ${entity} initialized!`);
|
||||||
|
}
|
||||||
|
await context.commit();
|
||||||
|
this.dbStore.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
getStore(): RowStore<ED, Cxt> {
|
||||||
|
return this.dbStore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { RowStore } from 'oak-domain/lib/types';
|
||||||
|
import { GeneralRuntimeContext } from 'oak-general-business';
|
||||||
|
import { EntityDict } from 'oak-general-business/lib/general-app-domain';
|
||||||
|
|
||||||
|
export class Context<ED extends EntityDict> extends GeneralRuntimeContext<ED> {
|
||||||
|
static FromCxtStr(cxtStr?: string){
|
||||||
|
const {
|
||||||
|
token,
|
||||||
|
applicationId,
|
||||||
|
scene
|
||||||
|
} = cxtStr ? GeneralRuntimeContext.fromString(cxtStr) : {
|
||||||
|
token: undefined,
|
||||||
|
applicationId: undefined,
|
||||||
|
scene: undefined,
|
||||||
|
};
|
||||||
|
return <ED extends EntityDict>(store: RowStore<ED, Context<ED>>) => {
|
||||||
|
const context = new Context<ED>(store, applicationId);
|
||||||
|
context.setScene(scene);
|
||||||
|
context.setToken(token);
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,94 @@
|
||||||
|
import { MysqlStore, MySqlSelectOption, MysqlOperateOption } from 'oak-db';
|
||||||
|
import { EntityDict, Context, StorageSchema, OperateOption, SelectionResult, Trigger, Checker, SelectRowShape, RowStore } from 'oak-domain/lib/types';
|
||||||
|
import { TriggerExecutor } from 'oak-domain/lib/store/TriggerExecutor';
|
||||||
|
import { MySQLConfiguration, } from 'oak-db/lib/MySQL/types/Configuration';
|
||||||
|
|
||||||
|
|
||||||
|
export class DbStore<ED extends EntityDict, Cxt extends Context<ED>> extends MysqlStore<ED, Cxt> {
|
||||||
|
private executor: TriggerExecutor<ED, Cxt>;
|
||||||
|
|
||||||
|
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt, mysqlConfiguration: MySQLConfiguration) {
|
||||||
|
super(storageSchema, mysqlConfiguration);
|
||||||
|
this.executor = new TriggerExecutor(async (scene) => contextBuilder(scene)(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected async cascadeUpdate<T extends keyof ED>(entity: T, operation: ED[T]['Create'] | ED[T]['Update'] | ED[T]['Remove'], context: Cxt, option?: MysqlOperateOption) {
|
||||||
|
await this.executor.preOperation(entity, operation, context, option);
|
||||||
|
const result = super.cascadeUpdate(entity, operation, context, option);
|
||||||
|
await this.executor.postOperation(entity, operation, context, option);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async cascadeSelect<T extends keyof ED, S extends ED[T]["Selection"]>(entity: T, selection: S, context: Cxt, option?: MySqlSelectOption): Promise<SelectRowShape<ED[T]['Schema'], S['data']>[]> {
|
||||||
|
const selection2 = Object.assign({
|
||||||
|
action: 'select',
|
||||||
|
}, selection) as ED[T]['Operation'];
|
||||||
|
|
||||||
|
if (!option?.ignoreTrigger) {
|
||||||
|
await this.executor.preOperation(entity, selection2, context, option);
|
||||||
|
}
|
||||||
|
const result = await super.cascadeSelect(entity, selection2 as any, context, option);
|
||||||
|
if (!option?.ignoreTrigger) {
|
||||||
|
await this.executor.postOperation(entity, selection2, context, option, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async operate<T extends keyof ED>(
|
||||||
|
entity: T,
|
||||||
|
operation: ED[T]['Operation'],
|
||||||
|
context: Cxt,
|
||||||
|
params?: MysqlOperateOption
|
||||||
|
) {
|
||||||
|
const autoCommit = !context.getCurrentTxnId();
|
||||||
|
let result;
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.begin();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
result = await super.operate(entity, operation, context, params);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
await context.rollback();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.commit();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async select<T extends keyof ED, S extends ED[T]['Selection']>(
|
||||||
|
entity: T,
|
||||||
|
selection: S,
|
||||||
|
context: Cxt,
|
||||||
|
params?: MySqlSelectOption
|
||||||
|
) {
|
||||||
|
const autoCommit = !context.getCurrentTxnId();
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.begin();
|
||||||
|
}
|
||||||
|
let result: SelectionResult<ED[T]['Schema'], S['data']>;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = await super.select(entity, selection, context, params);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
await context.rollback();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (autoCommit) {
|
||||||
|
await context.commit();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
registerTrigger<T extends keyof ED>(trigger: Trigger<ED, T, Cxt>) {
|
||||||
|
this.executor.registerTrigger(trigger);
|
||||||
|
}
|
||||||
|
|
||||||
|
registerChecker<T extends keyof ED>(checker: Checker<ED, T, Cxt>) {
|
||||||
|
this.executor.registerChecker(checker);
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,2 @@
|
||||||
|
export { Context as BackendContext } from './BackendContext';
|
||||||
|
export { AppLoader } from './AppLoader';
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { v4, v1 } from 'uuid';
|
||||||
|
|
||||||
|
export type GenerateIdOption = {
|
||||||
|
shuffle?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function generateNewId(option?: GenerateIdOption) {
|
||||||
|
if (option?.shuffle && process.env.NODE_ENV === 'development') {
|
||||||
|
return v4();
|
||||||
|
}
|
||||||
|
return v1();
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(global, {
|
||||||
|
generateNewId,
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { initialize } from '../src/index';
|
||||||
|
import { Configuration } from '../src/types/Configuration';
|
||||||
|
|
||||||
|
const configuration: Configuration = {
|
||||||
|
mysql: {
|
||||||
|
host: 'localhost',
|
||||||
|
user: 'root',
|
||||||
|
database: 'obb',
|
||||||
|
charset: 'utf8mb4_general_ci',
|
||||||
|
connectionLimit: 20,
|
||||||
|
password: '',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize(`${__dirname}/../../bangzuxia`, configuration, true)
|
||||||
|
.then(
|
||||||
|
() => console.log('success')
|
||||||
|
)
|
||||||
|
.catch(
|
||||||
|
(err) => console.error(err)
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"jsx": "preserve",
|
||||||
|
"types": ["node"],
|
||||||
|
/* Basic Options */
|
||||||
|
// "incremental": true, /* Enable incremental compilation */
|
||||||
|
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
|
||||||
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||||
|
"lib": [
|
||||||
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"esnext"
|
||||||
|
],
|
||||||
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
|
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||||
|
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
|
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||||
|
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
|
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||||
|
"outDir": "lib", /* Redirect output structure to the directory. */
|
||||||
|
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||||
|
// "composite": true, /* Enable project compilation */
|
||||||
|
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||||
|
// "removeComments": true, /* Do not emit comments to output. */
|
||||||
|
// "noEmit": true, /* Do not emit outputs. */
|
||||||
|
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||||
|
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||||
|
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||||
|
/* Strict Type-Checking Options */
|
||||||
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
|
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||||
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||||
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
|
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||||
|
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||||
|
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||||
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
/* Additional Checks */
|
||||||
|
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||||
|
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||||
|
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||||
|
/* Module Resolution Options */
|
||||||
|
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
|
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||||
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
|
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||||
|
// "types": [], /* Type declaration files to be included in compilation. */
|
||||||
|
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||||
|
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||||
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||||
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
|
/* Source Map Options */
|
||||||
|
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||||
|
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||||
|
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||||
|
/* Experimental Options */
|
||||||
|
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||||
|
/* Advanced Options */
|
||||||
|
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||||
|
},
|
||||||
|
"include": ["src/**/*" ],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"**/*.spec.ts",
|
||||||
|
"test"
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue