tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
158 lines • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateManager = void 0;
const STATE_DEFAULT = {
PRIMARY_KEY: 'id',
VOID: false,
DISTINCT: false,
SAVE: null,
DELETE: false,
UPDATE: null,
INSERT: null,
SELECT: [],
ADD_SELECT: [],
ONLY: [],
EXCEPTS: [],
FROM: [],
JOIN: [],
WHERE: [],
GROUP_BY: [],
ORDER_BY: [],
LIMIT: null,
OFFSET: null,
HAVING: null,
TABLE_NAME: null,
HIDDEN: [],
DEBUG: false,
CTE: [],
PAGE: 1,
AFTER_SAVE: 100,
HOOKS: [],
ALIAS: null,
RAW_ALIAS: null,
UNION: [],
UNION_ALL: [],
ROW_LEVEL_LOCK: null
};
const STATE_DB = {
...STATE_DEFAULT,
};
const STATE_MODEL = {
...STATE_DEFAULT,
AUDIT: null,
AUDIT_METADATA: null,
MODEL_NAME: 'model',
UUID_FORMAT: 'uuid',
UUID: false,
SOFT_DELETE: false,
SOFT_DELETE_FORMAT: 'deleted_at',
SOFT_DELETE_RELATIONS: false,
REGISTRY: {},
PATTERN: 'default',
RELATION: [],
RELATIONS: [],
RELATIONS_TRASHED: false,
RELATIONS_EXISTS: false,
TIMESTAMP: false,
TIMESTAMP_FORMAT: {
CREATED_AT: 'created_at',
UPDATED_AT: 'updated_at',
},
LOGGER: false,
LOGGER_OPTIONS: null,
TABLE_LOGGER: '$loggers',
TABLE_AUDIT: '$audits',
VALIDATE_SCHEMA: false,
VALIDATE_SCHEMA_DEFINED: null,
FUNCTION_RELATION: false,
SCHEMA_TABLE: null,
RETRY: 0,
OBSERVER: null,
DATA: null,
GLOBAL_SCOPE: true,
GLOBAL_SCOPE_QUERY: null,
QUERIES: [],
META: null,
CACHE: null,
MIDDLEWARES: [],
TRANSFORMS: null,
LIFECYCLE_BEFORE_INSERTS: [],
LIFECYCLE_AFTER_INSERTS: [],
LIFECYCLE_BEFORE_UPDATES: [],
LIFECYCLE_AFTER_UPDATES: [],
LIFECYCLE_BEFORE_REMOVES: [],
LIFECYCLE_AFTER_REMOVES: [],
ON_CREATED_TABLE: null,
ON_SYNC_TABLE: null,
};
class StateManager {
STATE = {
currentState: new Map(),
defaultState: new Map()
};
constructor(state) {
switch (state) {
case 'db': {
const currentState = new Map(Object.entries({ ...STATE_DB }));
const defaultState = new Map(Object.entries({ ...STATE_DB }));
this.STATE = { currentState, defaultState };
return this;
}
case 'model': {
const currentState = new Map(Object.entries({ ...STATE_MODEL }));
const defaultState = new Map(Object.entries({ ...STATE_MODEL }));
this.STATE = { currentState, defaultState };
return this;
}
case 'default': {
const currentState = new Map(Object.entries({ ...STATE_DEFAULT }));
const defaultState = new Map(Object.entries({ ...STATE_DEFAULT }));
this.STATE = { currentState, defaultState };
return this;
}
default: throw new Error(`string the state : '${state}'`);
}
}
original() {
return this.STATE.defaultState;
}
all() {
return this.STATE.currentState;
}
get(key) {
if (!this.STATE.currentState.has(key) && key !== 'DEBUG') {
throw this._assertError(`This state does not have that key '${key}'`);
}
return this.STATE.currentState.get(key);
}
set(key, value) {
if (!this.STATE.currentState.has(key)) {
return this._assertError(`That key '${key}' can't be set in the state`);
}
this.STATE.currentState.set(key, value);
return;
}
clone(data) {
this.STATE.currentState = new Map(Object.entries({ ...data }));
return;
}
reset() {
// the reset state was set only statement query;
this.STATE.currentState.set('INSERT', null);
this.STATE.currentState.set('UPDATE', null);
this.STATE.currentState.set('DELETE', null);
this.STATE.currentState.set('SAVE', null);
this.STATE.currentState.set('VOID', false);
}
_assertError(condition = true, message = 'error') {
if (typeof condition === 'string') {
throw new Error(condition);
}
if (condition)
throw new Error(message);
return;
}
}
exports.StateManager = StateManager;
exports.default = StateManager;
//# sourceMappingURL=StateManager.js.map