Skip to content

Commit

Permalink
ADD support of primary for declaring primary key in the schema
Browse files Browse the repository at this point in the history
  • Loading branch information
stombre committed Mar 22, 2020
1 parent 10684c0 commit 21c491d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const connectorFactory = require('./connector/connector.class',);
const schemaFactory = require('./schema/schema.factory',);
const schemaFieldFactory = require('./schema/schemaField.factory',);

/**
* Factory for creating the connector
Expand All @@ -14,6 +15,7 @@ module.exports = {
plugins: {
core: {
schemaFactory,
schemaFieldFactory,
},
},
fromKnex,
Expand Down
25 changes: 24 additions & 1 deletion lib/model/model.factory.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
const {
PRIMARY,
} = require('../schema/symbols',);

/**
* Create a new Mongo Model class.
* @param {Model} ParentModel The Model used as Parent
* @returns {KnexModel} The KnexModel created
*/
const mongoModelFactory = ({ ParentModel, },) => (
class KnexModel extends ParentModel {}
class KnexModel extends ParentModel {
/**
* Return a query targeting the current instance
* @returns {Query} The query targeting the current instance
*/
getQueryPrimary() {
const query = this.constructor.query();

const primaryKeys = this.constructor.getSchema()[PRIMARY];

if (!primaryKeys.length) {
throw new Error('Can not use save method if no primary key declared in the schema',);
}

for (const field of primaryKeys) {
query[field].is(this[field],);
}

return query;
}
}
);

module.exports = mongoModelFactory;
23 changes: 22 additions & 1 deletion lib/schema/schema.factory.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
'use strict';

const {
PRIMARY,
} = require('../schema/symbols',);

/**
* Class schema
* Instantiate a knex schema
* @param {Schema} Schema schema to extends
* @returns {KnexSchema} The knex schema to use
*/
const injectSchema = (Schema,) => class KnexSchema extends Schema {};
const injectSchema = (Schema,) => class KnexSchema extends Schema {
/**
* Create the schema
* @param {Object} schema Schema definition to build
*/
constructor(schema,) {
super(schema,);

this[PRIMARY] = [];

for (const key of Object.keys(schema,)) {
if (schema[key][PRIMARY]) {
this[PRIMARY].push(key,);
}
}

}
};


module.exports = injectSchema;
26 changes: 26 additions & 0 deletions lib/schema/schemaField.factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const {
PRIMARY,
} = require('./symbols',);

/**
* Class schema
* Instantiate a knex schema
* @param {SchemaField} SchemaField schemaField to extends
* @returns {KnexSchema} The knex schema to use
*/
const injectSchemaField = (SchemaField,) => class KnexSchemaField extends SchemaField {
/**
* Declare this field as primary for query
* @returns {KnexSchemaField} Return current field
*/
primary() {
this[PRIMARY] = true;

return this;
}
};


module.exports = injectSchemaField;
4 changes: 4 additions & 0 deletions lib/schema/symbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

module.exports = {
PRIMARY: 'primary',
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ilorm-connector-knex",
"version": "0.1.4",
"version": "0.2.0",
"description": "Connector to SQL database for ilorm ORM",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 21c491d

Please sign in to comment.