Giter VIP home page Giter VIP logo

knex-orm's People

Contributors

kripod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

knex-orm's Issues

[Question] Late instantiation

  • Hi, I am currently looking for an alternative ORM for node.. While reading comments and examples from them, one key factor im finding is define Model Classes or Model Schemas, then when the application loads, ill require/import what is required model, starts the Database authentication/connect and then accessing that model. Would that work?

Note that I havent tried to install the package yet.. I would love to see this package support for moleculerjs https://moleculer.services

Relations definition.

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.

Package throws TypeError on import

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)

Saving related properties of Models

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.

Callback style query execution

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 awaits to be made on simple queries:

// The call below should be made inside an async function
const employee = await Employee.query().first();

Update docs for update action

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.

Plugin constructor

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

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();
];

Remove Utils.requireUncached()

Altering the module cache is not a good behavior. The KnexOrm class should be removed in its entirely, and replaced by plain Models.

Events

  • Before query
  • After query

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.