From 21c491dd1683ecec457942bb15284f146e7aa7b3 Mon Sep 17 00:00:00 2001 From: daix Date: Sun, 22 Mar 2020 23:32:58 +0100 Subject: [PATCH] ADD support of primary for declaring primary key in the schema --- lib/index.js | 2 ++ lib/model/model.factory.js | 25 ++++++++++++++++++++++++- lib/schema/schema.factory.js | 23 ++++++++++++++++++++++- lib/schema/schemaField.factory.js | 26 ++++++++++++++++++++++++++ lib/schema/symbols.js | 4 ++++ package.json | 2 +- 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 lib/schema/schemaField.factory.js create mode 100644 lib/schema/symbols.js diff --git a/lib/index.js b/lib/index.js index 14a38b8..5b05d3d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 @@ -14,6 +15,7 @@ module.exports = { plugins: { core: { schemaFactory, + schemaFieldFactory, }, }, fromKnex, diff --git a/lib/model/model.factory.js b/lib/model/model.factory.js index 6ac7be7..058526f 100644 --- a/lib/model/model.factory.js +++ b/lib/model/model.factory.js @@ -1,3 +1,6 @@ +const { + PRIMARY, +} = require('../schema/symbols',); /** * Create a new Mongo Model class. @@ -5,7 +8,27 @@ * @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; diff --git a/lib/schema/schema.factory.js b/lib/schema/schema.factory.js index c06b892..520727a 100644 --- a/lib/schema/schema.factory.js +++ b/lib/schema/schema.factory.js @@ -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; diff --git a/lib/schema/schemaField.factory.js b/lib/schema/schemaField.factory.js new file mode 100644 index 0000000..f7be2ed --- /dev/null +++ b/lib/schema/schemaField.factory.js @@ -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; diff --git a/lib/schema/symbols.js b/lib/schema/symbols.js new file mode 100644 index 0000000..bf659ba --- /dev/null +++ b/lib/schema/symbols.js @@ -0,0 +1,4 @@ + +module.exports = { + PRIMARY: 'primary', +}; diff --git a/package.json b/package.json index cb7af9c..17978e7 100644 --- a/package.json +++ b/package.json @@ -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": {