kripod / knex-orm Goto Github PK
View Code? Open in Web Editor NEWKnex-based object-relational mapping for JavaScript.
License: MIT License
Knex-based object-relational mapping for JavaScript.
License: MIT License
Note that I havent tried to install the package yet.. I would love to see this package support for moleculerjs https://moleculer.services
First off, great work, simple and uses up-to-date ECMAScript which is what I've been looking for.
My only real gripe so far is how relationships are defined.
In this example:
import Database from './../database';
class Employee extends Database.Model {
static get tableName() { return 'employees'; }
static get related() {
return {
company: this.belongsTo('Company'),
};
}
}
export default Database.register(Employee);
How hard would it be do you think to go more along how Bookshelf/Eloquent does it?
import Database from './../database';
class Employee extends Database.Model {
static get tableName() { return 'employees'; }
company () {
return this.belongsTo('Company')
}
export default Database.register(Employee);
Also is the Database.register
at the bottom required? I'd like to incorporate this into the ES2015+ MVC framework I am building instead of rolling my own ORM, so I'll probably be contributing in the future.
Using node v5, babel as transpiler I get the following error.
As soon as I import using
const KnexOrm = require('knex-orm');
or
import KnexOrm from 'knex-orm'
I see the following
node_modules/knex-orm/src/config.js:12 KNEX_ALLOWED_QUERY_METHODS: knexDefaultMethods.filter((item) => ^ TypeError: KNEX_IGNORED_QUERY_METHODS.includes is not a function at /Users/RL/Documents/projects/d/node_modules/knex-orm/src/config.js:13:33 at Array.filter (native) at Object.<anonymous> (/Users/RL/Documents/projects/d/node_modules/knex-orm/src/config.js:12:50) at Module._compile (module.js:413:34) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at Object.<anonymous> (/Users/RL/Documents/projects/d/node_modules/knex-orm/src/query-builder.js:1:1) at Module._compile (module.js:413:34) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32)
The easiest way to describe this feature is by providing a syntactical example:
const company = new Company({
email: '[email protected]',
employees: [
new Employee({ name: 'John Doe' }), // New Employee
new Employee({ id: 6 }, false), // Existing Employee
],
});
await company.save();
The code above should insert a new Company to the database, but before that, the new Employee (first in the array) shall be stored. Related object trees should be stored recursively, in an order defined by the relations of the referenced objects.
Add QueryBuilder.execute([callback])
, which should return a Promise. Thus, both of the code snippets below would be valid:
Employee.query().first().execute().then((employee) => {
console.log(employee);
}).then(() => {
// Chainable methods
});
Employee.query().first().execute((employee) => {
console.log(employee);
// Continue the workflow here
});
QueryBuilder.then(...callbacks)
should be kept as an alias of the execute
function, allowing await
s to be made on simple queries:
// The call below should be made inside an async function
const employee = await Employee.query().first();
It would be very interesting to know the differences between the two projects.
Both static Model properties should be functions which should allow transformations to be made on the input/result objects.
Instead of using hard-coded default values for ID attributes, foreign keys, etc., developers should be able to specify custom functions for specifying default values of several properties.
I think that it's not clear how to update a model if you don't have the properties fetched.
the most common use case is that you get an object with values that you want to update the model with. And the first instinct based on the docs is to do:
const data = {
id: 1,
companyId: 2,
name: 'Olympia Pearson',
birthDate: new Date('1982-08-20 00:00'),
};
const employee = new Employee(data, false);
employee.save();
But if you want to update the model with all the properties you have to do:
const employee = new Employee({}, false);
Object.assign(employee, data);
employee.save();
and for that reason I would add a set method:
/**
* @param {Object} [props={}] Properties to assign to object
* @returns {ModelBase} ModelBase
*/
set(props = {}) {
return Object.assign(this, props);
}
So it can be called like this:
new Employee({}, false).set(data).save();
it would be a good thing also to explain in the docs the lifecycle of oldProps, and when they become just props, the concept is clear if you look through the code and tests, but not on a first glance.
Instead of specifying a jsonSchema
attribute manually for each Model, schemas could be fetched from the database on application startup.
Change Model.idAttribute
to Model.primaryKey
, and allow it to be a string[]
Plugin constructors should allow modifications to be made for the entire ORM (not only when building queries).
class CustomPlugin extends PluginBase {
constructor(ModelBase) {
// Modify ModelBase here
}
}
Plugins should be used instead of options
, allowing query transformations to be run before or after specific queries. For example:
class CaseConverterPlugin extends KnexOrmPlugin {
before(query) {
// TODO
}
after(query) {
// TODO
}
}
// Plugin transformations will be executed in order
Model.plugins = [
new CaseConverterPlugin();
];
Altering the module cache is not a good behavior. The KnexOrm
class should be removed in its entirely, and replaced by plain Model
s.
Good library but it needs an update
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.