Skip to content

Commit

Permalink
[feat]: automatically create attribute(s) with Model.init(), version 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucky-victory committed Apr 27, 2022
1 parent 4e3c517 commit ca5c3e8
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/constants/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const operations = {
UPDATE_NODE: "update_node",
REMOVE_NODE: "remove_node",
DESCRIBE_ALL: "describe_all",
CREATE_ATTRIBUTE:"create_attribute",
CREATE_SCHEMA: "create_schema",
DESCRIBE_SCHEMA: "describe_schema",
DROP_SCHEMA: "drop_schema",
Expand Down
2 changes: 1 addition & 1 deletion lib/core/harpeeHttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HarpeeHttp {
/**
*
* @param {Object} reqBody
* @param {function} [callback]
* @param {(err:any,result:({[key:string]:any}|{[key:string]:any}[]))=>void} [callback]
* @param {boolean} [single=false]
* @returns {(void|Promise<any>)}
* @protected
Expand Down
62 changes: 51 additions & 11 deletions lib/core/harpeeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,48 @@ class HarpeeModel extends HarpeeHttp {
const schemaName = schemaObject.name;
/**
*@private
@readonly
*/
this.schemaName = schemaName;

/**
*@private
@readonly
*/
this.modelName = modelName;
/**
* @private
* @readonly
*/
this.schemaFields = schemaObject.fields;

const { primaryKey } = schemaObject;
/**
* @private
* @readonly
*/
this.primaryKey = primaryKey;
/**
* @private
* @readonly
*/
this.silent = schemaObject.silent;



}
/**
* This creates the schema & table if they don't exist yet.
* **you should get rid of this after use.**
* This creates the schema, table, and the attributes specified in Schema.`fields`, if they don't exist.
* **you should get rid of this after running your app atleast once.**
* @returns void;
*/
async init() {
try {

const schema=this.schemaName;
const table=this.modelName;
const createSchema = async () => this.$callbackOrPromise({
operation: operations.CREATE_SCHEMA,
schema: this.schemaName
schema
});

const describeAll = async () => this.$callbackOrPromise({
Expand All @@ -79,24 +85,58 @@ class HarpeeModel extends HarpeeHttp {

const createTable = async () => this.$callbackOrPromise({
operation: operations.CREATE_TABLE,
schema: this.schemaName,
table: this.modelName,
schema,
table,
hash_attribute: this.primaryKey,
});
const createAttribute = async (attribute) => this.$callbackOrPromise({
operation: operations.CREATE_ATTRIBUTE,
schema,
table,
attribute
});


// get information about the database
const respA = await describeAll();
if (!(respA[this.schemaName])) {
// check if the schema already exist, else create it
if (!(respA[schema])) {
await createSchema()
}


// get information about the database
const respB = await describeAll();

if (!(respB[this.schemaName] && respB[this.schemaName][this.modelName])) {
// check if the table already exist, else create it
if (!(respB[schema] && respB[schema][table])) {
await createTable()
}

// get information about the database
const respC=await describeAll();

// get fields from Schema.`fields`
const fields=this.schemaFields;

// turn the fields object into an array of strings
const attributes=util.splitObject(fields).keys;

// get previous attributes from the table
const previousAttributes=respC[schema][table]['attributes'];

// turn the previous attributes object into an array of strings
const previousAttributesValues=util.ObjectArrayToStringArray(previousAttributes);

// loop through attributes from `fields`
const attributeLoop=async()=>{
for(let attribute of attributes){
// create the attribute if it's not in the previous attribute
if(!previousAttributesValues.includes(attribute)){

await createAttribute(attribute);
}
}

}
await attributeLoop();

}
catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/harpeeSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const util = require("../helpers/util");
*/
class HarpeeSchema{
/**
* Let's you specify your schema name, also configure your table's columns and types.
* Let's you specify the chema name, also configure your table's column names.
*
* @param {HarpeeSchemaConfig} schemaConfig
*/
Expand Down
18 changes: 18 additions & 0 deletions lib/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ const util = {
});
return { keys, values };
},
/**
*
* @param {{[key:string]:any}[]} arrayOfObj - an array of objects
* @param {boolean} [keys] - when true, returns object keys otherwise object values
* @returns {string[]}
*/
ObjectArrayToStringArray(arrayOfObj,keys=false){
if(!Array.isArray(arrayOfObj)){
return []
}
return arrayOfObj.reduce((accum,item)=>{
for(let key in item){
keys ? accum.push(key) : accum.push(item[key]);
}
return accum;
},[])
},

/** Splits a string by a seperator and returns the last string
* @param {string} str - the string to be splitted.
*
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "harpee",
"version": "3.0.13",
"version": "3.1.0",
"description": "Harpee is a modeling tool for HarperDB, harpee supports both callbacks and Promises.",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions types/constants/operations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const ADD_NODE: string;
export const UPDATE_NODE: string;
export const REMOVE_NODE: string;
export const DESCRIBE_ALL: string;
export const CREATE_ATTRIBUTE: string;
export const CREATE_SCHEMA: string;
export const DESCRIBE_SCHEMA: string;
export const DROP_SCHEMA: string;
Expand Down
8 changes: 6 additions & 2 deletions types/core/harpeeHttp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ declare class HarpeeHttp {
/**
*
* @param {Object} reqBody
* @param {function} [callback]
* @param {(err:any,result:({[key:string]:any}|{[key:string]:any}[]))=>void} [callback]
* @param {boolean} [single=false]
* @returns {(void|Promise<any>)}
* @protected
*/
protected $callbackOrPromise(reqBody: any, callback?: Function, single?: boolean): (void | Promise<any>);
protected $callbackOrPromise(reqBody: any, callback?: (err: any, result: {
[key: string]: any;
} | {
[key: string]: any;
}[]) => void, single?: boolean): (void | Promise<any>);
}
19 changes: 12 additions & 7 deletions types/core/harpeeModel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ declare class HarpeeModel extends HarpeeHttp {
constructor(modelName: string, schemaObject: HarpeeSchema);
/**
*@private
@readonly
*/
private schemaName;
private readonly schemaName;
/**
*@private
@readonly
*/
private modelName;
private readonly modelName;
/**
* @private
* @readonly
*/
private schemaFields;
private readonly schemaFields;
/**
* @private
* @readonly
*/
private primaryKey;
private readonly primaryKey;
/**
* @private
* @readonly
*/
private silent;
private readonly silent;
/**
* This creates the schema & table if they don't exist yet.
* **you should get rid of this after use.**
* This creates the schema, table, and the attributes specified in Schema.`fields`, if they don't exist.
* **you should get rid of this after running your app atleast once.**
* @returns void;
*/
init(): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion types/core/harpeeSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export = HarpeeSchema;
*/
declare class HarpeeSchema {
/**
* Let's you specify your schema name, also configure your table's columns and types.
* Let's you specify the chema name, also configure your table's column names.
*
* @param {HarpeeSchemaConfig} schemaConfig
*/
Expand Down
18 changes: 18 additions & 0 deletions types/helpers/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ export function splitObjectSorted(obj: any): {
keys: any[];
values: any[];
};
/**
*
* @param {{[key:string]:any}[]} arrayOfObj - an array of objects
* @param {boolean} [keys] - when true, returns object keys otherwise object values
* @returns {string[]}
*/
export function ObjectArrayToStringArray(arrayOfObj: {
[key: string]: any;
}[], keys?: boolean): string[];
/**
*
* @param {{[key:string]:any}[]} arrayOfObj - an array of objects
* @param {boolean} [keys] - when true, returns object keys otherwise object values
* @returns {string[]}
*/
export function ObjectArrayToStringArray(arrayOfObj: {
[key: string]: any;
}[], keys?: boolean): string[];
export function getExtname(str: string): string;
export function getFirst(str: string, seperator: any): string;
export function objectToArray(obj: any, seperator?: string): any[];
Expand Down

0 comments on commit ca5c3e8

Please sign in to comment.