-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD support of primary for declaring primary key in the schema
- Loading branch information
Showing
6 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
module.exports = { | ||
PRIMARY: 'primary', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters