Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
# Conflicts:
#	doc/map.md
  • Loading branch information
flancer64 committed May 18, 2023
2 parents 5695e2a + 4e993bd commit daafa44
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 117 deletions.
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# @teqfw/db: releases

## 0.7.0

* Add list of deprecated tables to drop on re-installation.
* Binary data type (`Buffer`) is added to DEM.

## 0.6.0

* Create CLI action to re-create RDB structure using DEM.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@teqfw/db",
"version": "0.6.0",
"version": "0.7.0",
"description": "TeqFW: DB connectivity.",
"scripts": {
"test": "./node_modules/mocha/bin/_mocha --recursive './test/mod/**/*.test.mjs'"
Expand Down
1 change: 1 addition & 0 deletions src/Back/Api/RDb/CrudEngine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class TeqFw_Db_Back_Api_RDb_CrudEngine {
* @param {TeqFw_Db_Back_RDb_Meta_IEntity} meta meta data for related entity
* @param {number|string|boolean|Array|Object} key JS primitive for simple PK or object/array for complex PK or unique key
* @return {Promise<Object|null>}
* TODO: add columns filter to select (some cols could be a too big to be stored in memory)
*/
async readOne(trx, meta, key) {};

Expand Down
3 changes: 2 additions & 1 deletion src/Back/Dem/Load/A/Norm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default class TeqFw_Db_Back_Dem_Load_A_Norm {
/** @type {TeqFw_Db_Back_Dto_Dem} */
const dem = _factory.create();
delete dem.refs;
if (map?.deprecated) dem.deprecated = map.deprecated;

for (const plugin of Object.keys(dems)) {
// make a copy of the DEM fragment
Expand All @@ -99,6 +100,6 @@ export default class TeqFw_Db_Back_Dem_Load_A_Norm {
}

return {dem};
}
};
}
}
5 changes: 5 additions & 0 deletions src/Back/Dto/Dem.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const NS = 'TeqFw_Db_Back_Dto_Dem';

// MODULE'S CLASSES
export default class TeqFw_Db_Back_Dto_Dem {
/**
* List of deprecated tables with dependencies (foreign keys).
* @type {Object<string, string[]>}
*/
deprecated;
/** @type {Object<string, TeqFw_Db_Back_Dto_Dem_Entity>} */
entity;
/** @type {Object<string, TeqFw_Db_Back_Dto_Dem_Package>} */
Expand Down
25 changes: 21 additions & 4 deletions src/Back/Dto/Map.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ const NS = 'TeqFw_Db_Back_Dto_Map';
// MODULE'S CLASSES
export default class TeqFw_Db_Back_Dto_Map {
/**
* Plugin's references resolutions (map plugin's external reference to existing entity & attr).
* @type {Object<string, Object<string, TeqFw_Db_Back_Dto_Map_Ref>>}
* List of deprecated tables with dependencies (foreign keys).
* @type {Object<string, string[]>}
*/
ref;
deprecated;
/**
* Prefix for tables in RDB ('teq' => 'teq_table_name'). Default: use w/o prefix.
* @type {string}
*/
namespace;
/**
* Plugin's references resolutions (map plugin's external reference to existing entity & attr).
* @type {Object<string, Object<string, TeqFw_Db_Back_Dto_Map_Ref>>}
*/
ref;
}

// attributes names to use as aliases in queries to object props
Expand All @@ -30,6 +35,8 @@ export class Factory {
static namespace = NS;

constructor(spec) {
/** @type {TeqFw_Core_Shared_Util_Cast.castArrayOfStr|function} */
const castArrayOfStr = spec['TeqFw_Core_Shared_Util_Cast.castArrayOfStr'];
/** @type {TeqFw_Db_Back_Dto_Map_Ref.Factory} */
const fRef = spec['TeqFw_Db_Back_Dto_Map_Ref.Factory$'];

Expand All @@ -39,6 +46,15 @@ export class Factory {
*/
this.create = function create(data = null) {
// FUNCS

function parseDeprecated(data) {
const res = {};
if (typeof data === 'object')
for (const name of Object.keys(data))
res[name] = castArrayOfStr(data[name]);
return res;
}

function parseRef(data) {
const res = {};
if (typeof data === 'object')
Expand All @@ -55,10 +71,11 @@ export class Factory {

// MAIN
const res = new TeqFw_Db_Back_Dto_Map();
res.deprecated = parseDeprecated(data?.deprecated);
res.namespace = data?.namespace;
res.ref = parseRef(data?.ref);
return res;
}
};
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/Back/Dto/RDb/Column.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default class TeqFw_Db_Back_Dto_RDb_Column {
default;
/** @type {Array} */
enum;
/** @type {number} */
length;
/** @type {string} */
name;
/** @type {boolean} */
Expand All @@ -25,13 +27,6 @@ export default class TeqFw_Db_Back_Dto_RDb_Column {
/** @type {boolean} */
unsigned;
}
// attributes names to use as aliases in queries to object props
TeqFw_Db_Back_Dto_RDb_Column.COMMENT = 'comment';
TeqFw_Db_Back_Dto_RDb_Column.DEFAULT = 'default';
TeqFw_Db_Back_Dto_RDb_Column.NAME = 'name';
TeqFw_Db_Back_Dto_RDb_Column.NULLABLE = 'nullable';
TeqFw_Db_Back_Dto_RDb_Column.TYPE = 'type';
TeqFw_Db_Back_Dto_RDb_Column.UNSIGNED = 'unsigned';

// noinspection JSCheckFunctionSignatures
/**
Expand All @@ -55,14 +50,15 @@ export class Factory {
res.comment = castString(data?.comment);
res.default = castString(data?.default);
res.enum = castArray(data?.enum);
res.length = castInt(data?.length);
res.name = castString(data?.name);
res.nullable = castBooleanIfExists(data?.nullable);
res.precision = castInt(data?.precision);
res.scale = castInt(data?.scale);
res.type = castEnum(data?.type, COLUMN);
res.unsigned = castBooleanIfExists(data?.unsigned);
return res;
}
};
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Back/RDb/CrudEngine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default class TeqFw_Db_Back_RDb_CrudEngine {
if (
(typeof key === 'number') ||
(typeof key === 'string') ||
(typeof key === 'boolean')
(typeof key === 'boolean') ||
(key instanceof Buffer)
) {
// simple key
const [pk] = meta.getPrimaryKey();
Expand Down
14 changes: 7 additions & 7 deletions src/Back/RDb/Schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class TeqFw_Db_Back_RDb_Schema {
const knex = conn.getKnex();
const dem = _dem;
/** @type {TeqFw_Db_Back_Dto_Dem_Entity[]} */
const entities = await _aOrder.exec({dem});
const entities = await _aOrder.exec({dem, addDeprecated: false});
_logger.info(`Total ${entities.length} entities are in DEM.`);
for (const entity of entities) {
const tbl = await _aConvert.exec({entity, cfg: _cfg});
Expand All @@ -39,22 +39,22 @@ export default class TeqFw_Db_Back_RDb_Schema {
// perform operations
// const sql = schema.toString();
await schema;
}
};

this.dropAllTables = async function ({conn}) {
// prepare schema (populate with DROP statements)
const schema = conn.getSchemaBuilder();
const dem = _dem;
/** @type {TeqFw_Db_Back_Dto_Dem_Entity[]} */
const entities = await _aOrder.exec({dem});
const entities = await _aOrder.exec({dem, addDeprecated: true});
entities.reverse(); // reverse order for tables drop
for (const entity of entities) {
const tbl = await _aConvert.exec({entity, cfg: _cfg});
_builder.dropTable(schema, tbl);
}
// perform operations
await schema;
}
};

this.getTablesList = async function () {
const res = [];
Expand All @@ -65,14 +65,14 @@ export default class TeqFw_Db_Back_RDb_Schema {
res.push(tbl.name);
}
return res;
}
};

this.setCfg = function ({cfg}) {
_cfg = cfg;
}
};
this.setDem = function ({dem}) {
_dem = dem;
}
};
}


Expand Down
21 changes: 13 additions & 8 deletions src/Back/RDb/Schema/A/Builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export default class TeqFw_Db_Back_RDb_Schema_A_Builder {
*/
function addColumn(table, dto) {
/** @type {Knex.ColumnBuilder} */
const column = (dto.type === TDbColType.ENUM)
? table[dto.type](dto.name, dto.enum)
: (dto.type === TDbColType.DECIMAL)
? table[dto.type](dto.name, dto.precision, dto.scale)
: table[dto.type](dto.name);
let column;
if (dto.type === TDbColType.ENUM) {
column = table[dto.type](dto.name, dto.enum);
} else if (dto.type === TDbColType.DECIMAL) {
column = table[dto.type](dto.name, dto.precision, dto.scale);
} else if (dto.type === TDbColType.BINARY) {
column = table[dto.type](dto.name, dto?.length);
} else {
column = table[dto.type](dto.name);
}
if (dto.comment) column.comment(dto.comment);
(dto?.nullable) ? column.nullable() : column.notNullable();
if (dto.default !== undefined) {
Expand All @@ -34,7 +39,7 @@ export default class TeqFw_Db_Back_RDb_Schema_A_Builder {
) {
column.defaultTo(knex.fn.now());
} else {
column.defaultTo(dto.default)
column.defaultTo(dto.default);
}
}
if (dto.unsigned === true) column.unsigned();
Expand Down Expand Up @@ -66,15 +71,15 @@ export default class TeqFw_Db_Back_RDb_Schema_A_Builder {
for (const one of dto.indexes) addIndex(table, one);
for (const one of dto.relations) addRelation(table, one);
});
}
};

/**
* @param {Knex.SchemaBuilder} schema
* @param {TeqFw_Db_Back_Dto_RDb_Table} dto
*/
this.dropTable = function (schema, dto) {
schema.dropTableIfExists(dto.name);
}
};
}


Expand Down
3 changes: 2 additions & 1 deletion src/Back/RDb/Schema/A/Convert.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default class TeqFw_Db_Back_RDb_Schema_A_Convert {
col.unsigned = dem?.options?.unsigned;
if (dem?.options?.isTiny) col.type = TDbColType.TINYINT;
}
if (dem?.options?.length) col.length = dem?.options.length;
col.default = dem.default;
col.nullable = dem.nullable;
tbl.columns.push(col);
Expand Down Expand Up @@ -156,7 +157,7 @@ export default class TeqFw_Db_Back_RDb_Schema_A_Convert {
for (const rName of Object.keys(entity.relation))
convertRelation(res, entity.relation[rName], cfg);
return res;
}
};

}
}
Loading

0 comments on commit daafa44

Please sign in to comment.