Skip to content

Commit

Permalink
$$$ update $$$
Browse files Browse the repository at this point in the history
  • Loading branch information
darekf77 committed Apr 29, 2024
1 parent f7fbb70 commit 2c59a64
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 58 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class UserProvider extends Firedev.Base.Provider {
}

const UserContext = Firedev.createContext({
contextName: 'UserContext',
host: `http://localhost:${HOST_BACKEND_PORT}`,
entities: {
User,
Expand All @@ -104,6 +105,7 @@ const UserContext = Firedev.createContext({


const AppContext = Firedev.createContext({
contextName: 'AppContext',
remoteHost: `http://localhost:${HOST_BACKEND_PORT}`,
contexts: { UserContext },
});
Expand Down
139 changes: 82 additions & 57 deletions src/lib/endpoint-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { Models } from "./models";
import { ClassHelpers } from "./helpers/class-helpers";
import { Symbols } from "./symbols";
import { _, Helpers } from 'tnp-core';
import { _, Helpers } from 'tnp-core/src';
import type { createContext } from './create-context';
import { DIFiredevContainer } from "./dependency-injection/di-container";
import { FiredevControllerOptions } from "./decorators/classes/controller-decorator";
import { FiredevHelpers } from "./helpers/firedev-helpers";
import { Mapping, Models as ModelsNg2Rest, Resource, RestHeaders } from 'ng2-rest/src';
import { JSON10 } from 'json10/src';
import { path } from 'tnp-core';
import { path } from 'tnp-core/src';
import { from, Subject } from "rxjs";
import { EntityProcess } from "./entity-process";
import { getResponseValue } from "./get-response-value";
Expand Down Expand Up @@ -114,58 +114,71 @@ export class EndpointContext {

//#region resolve database config
if (config.database === true) {
switch (this.mode) {

//#region resolve database config for mode backend-frontend(tcp+udp)
case 'backend-frontend(ipc-electron)':
this.databaseConfig = {
database: `firedev-db-${EndpointContext.endpointId++}.sqljs.db`,
type: 'sqljs',
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
}
break;
//#endregion
if (Helpers.isRunningInDocker()) {
Helpers.info('Running in docker, using in mysql database');
// TODO
this.databaseConfig = {
database: `firedev-db-${EndpointContext.endpointId++}.sqljs.db`,
type: 'mysql',
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
}
} else {
Helpers.info(`Automatically resolving database config for mode ${this.mode}`);
switch (this.mode) {

//#region resolve database config for mode backend-frontend(tcp+udp)
case 'backend-frontend(ipc-electron)':
this.databaseConfig = {
database: `firedev-db-${EndpointContext.endpointId++}.sqljs.db`,
type: 'sqljs',
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
}
break;
//#endregion

//#region resolve database config for mode backend-frontend(websql)
case 'backend-frontend(websql)':
this.databaseConfig = {
location: `firedev-db-${EndpointContext.endpointId++}.sqljs.db`,
type: 'sqljs',
useLocalForage: !!window['localforage'],
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
};

if (FiredevAdmin.Instance.keepWebsqlDbDataAfterReload) {
this.databaseConfig.dropSchema = false;
delete this.databaseConfig.synchronize // false is not auto synchonize - from what I understand
} else {
this.databaseConfig.dropSchema = true;
this.databaseConfig.synchronize = true;
}
//#region resolve database config for mode backend-frontend(websql)
case 'backend-frontend(websql)':
this.databaseConfig = {
location: `firedev-db-${EndpointContext.endpointId++}.sqljs.db`,
type: 'sqljs',
useLocalForage: !!window['localforage'],
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
};

if (FiredevAdmin.Instance.keepWebsqlDbDataAfterReload) {
this.databaseConfig.dropSchema = false;
delete this.databaseConfig.synchronize // false is not auto synchonize - from what I understand
} else {
this.databaseConfig.dropSchema = true;
this.databaseConfig.synchronize = true;
}

break;
//#endregion
break;
//#endregion

//#region resolve database config for mode backend-frontend(tcp+udp)
case 'backend-frontend(tcp+udp)':
this.databaseConfig = {
database: `firedev-db-${EndpointContext.endpointId++}.sqlite`,
type: 'better-sqlite3',
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
}
break;
//#endregion
//#region resolve database config for mode backend-frontend(tcp+udp)
case 'backend-frontend(tcp+udp)':
this.databaseConfig = {
database: `firedev-db-${EndpointContext.endpointId++}.sqlite`,
type: 'better-sqlite3',
autoSave: true,
synchronize: true,
dropSchema: true,
logging: true,
}
break;
//#endregion
}
}

} else if (_.isObject(config.database)) {
this.databaseConfig = _.cloneDeep(config.database) as Models.DatabaseConfig;
}
Expand Down Expand Up @@ -447,6 +460,15 @@ export class EndpointContext {
}
//#endregion

//#region methods & getters / public assets
/**
* ipc/udp needs this
*/
public get contextName() {
return this.config.contextName;
}
//#endregion

//#region methods & getters / public assets
public get publicAssets() {
return this.config?.publicAssets || [];
Expand All @@ -471,7 +493,6 @@ export class EndpointContext {
}
//#endregion


//#region methods & getters / init subscribers
async initSubscribers() {
//#region @websql
Expand Down Expand Up @@ -639,6 +660,7 @@ export class EndpointContext {
}
//#endregion

//#region methods & getters / data source config
get dataSourceDbConfig() {
const config = (this.config.database as Models.DatabaseConfig);
return {
Expand All @@ -653,6 +675,7 @@ export class EndpointContext {
location: config.location,
} as DataSourceOptions;
}
//#endregion

//#region methods & getters / init connection
async initDatabaseConnection() {
Expand Down Expand Up @@ -743,7 +766,7 @@ export class EndpointContext {
) {
//#region @websql

const { routePath, method } = this.initMethodNodejs(
const { routePath, method } = this.initServer(
type,
methodConfig,
classConfig,
Expand All @@ -762,7 +785,6 @@ export class EndpointContext {
//#endregion
}


if (Helpers.isBrowser
//#region @backend
|| this.remoteHost
Expand All @@ -772,7 +794,7 @@ export class EndpointContext {
//#endregion
) {

this.initMethodBrowser(controllerClassFn, type, methodConfig, expressPath)
this.initClient(controllerClassFn, type, methodConfig, expressPath)
}
});

Expand Down Expand Up @@ -915,7 +937,7 @@ export class EndpointContext {
//#endregion

//#region methods & getters / init methods node
private initMethodNodejs(
private initServer(
//#region parameters
type: Models.Http.Rest.HttpMethod,
methodConfig: Models.MethodConfig,
Expand Down Expand Up @@ -1162,8 +1184,11 @@ export class EndpointContext {
}
//#endregion

//#region methods & getters / init method browser
private initMethodBrowser(
//#region methods & getters / init client
/**
* client can be browser or nodejs (when remote host)
*/
private initClient(
//#region parameters
target: Function,
type: Models.Http.Rest.HttpMethod,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export namespace Models {
useLocalForage?: boolean;
autoSave: boolean; // TODO what is this
type?: CoreModels.DatabaseType;
databasePort ?: number;
databasePort?: number;
databaseHost?: string;
}
//#endregion
Expand All @@ -64,6 +64,7 @@ export namespace Models {

//#region models / context options
export interface ContextOptions<CONTEXTS, CONTROLLERS, ENTITIES, REPOSITORIES, PROVIDERS> {
contextName: string;
host?: string;
remoteHost?: string;
useIpcWhenElectron?: boolean;
Expand Down

0 comments on commit 2c59a64

Please sign in to comment.