tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
111 lines • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateHandler = void 0;
const STATE_DEFAULT = {
PRIMARY_KEY: 'id',
VOID: false,
RESULT: null,
DISTINCT: false,
PLUCK: '',
SAVE: '',
DELETE: '',
UPDATE: '',
INSERT: '',
SELECT: [],
ONLY: [],
EXCEPTS: [],
CHUNK: 0,
COUNT: '',
FROM: 'FROM',
JOIN: [],
WHERE: [],
GROUP_BY: [],
ORDER_BY: [],
LIMIT: '',
OFFSET: '',
HAVING: '',
TABLE_NAME: '',
HIDDEN: [],
DEBUG: false,
CTE: [],
PAGE: 1,
AFTER_SAVE: 0,
RETURN_TYPE: null,
HOOKS: [],
ALIAS: null,
RAW_ALIAS: null,
};
const STATE_DB = Object.assign({}, STATE_DEFAULT);
const STATE_MODEL = Object.assign(Object.assign({}, STATE_DEFAULT), { MODEL_NAME: 'MODEL', UUID_FORMAT: 'uuid', UUID: false, SOFT_DELETE: false, SOFT_DELETE_FORMAT: 'deleted_at', SOFT_DELETE_RELATIONS: false, REGISTRY: {}, PATTERN: 'snake_case', 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', VALIDATE_SCHEMA: false, VALIDATE_SCHEMA_DEFINED: null, FUNCTION_RELATION: false, SCHEMA_TABLE: null, RETRY: 0, OBSERVER: null, DATA: null, BEFORE_CREATING_TABLE: null, RETURN_TYPE: null, GLOBAL_SCOPE: true, GLOBAL_SCOPE_QUERY: null, QUERIES: [], META: '', CACHE: null });
class StateHandler {
constructor(state) {
this.STATE = {
currentState: new Map(),
defaultState: new Map()
};
switch (state) {
case 'db': {
const currentState = new Map(Object.entries(Object.assign({}, STATE_DB)));
const defaultState = new Map(Object.entries(Object.assign({}, STATE_DB)));
this.STATE = { currentState, defaultState };
return this;
}
case 'model': {
const currentState = new Map(Object.entries(Object.assign({}, STATE_MODEL)));
const defaultState = new Map(Object.entries(Object.assign({}, STATE_MODEL)));
this.STATE = { currentState, defaultState };
return this;
}
case 'default': {
const currentState = new Map(Object.entries(Object.assign({}, STATE_DEFAULT)));
const defaultState = new Map(Object.entries(Object.assign({}, STATE_DEFAULT)));
this.STATE = { currentState, defaultState };
return this;
}
default: throw new Error(`Unknown the state : '${state}'`);
}
}
original() {
return this.STATE.defaultState;
}
get(key) {
if (key == null)
return this.STATE.currentState;
if (!this.STATE.currentState.has(key) && key !== 'DEBUG') {
return 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(Object.assign({}, data)));
return;
}
reset() {
this.STATE.currentState.set('INSERT', '');
this.STATE.currentState.set('UPDATE', '');
this.STATE.currentState.set('DELETE', '');
this.STATE.currentState.set('SAVE', '');
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.StateHandler = StateHandler;
exports.default = StateHandler;
//# sourceMappingURL=State.js.map