Lucid: Lucid standalone cli

Created on 9 Oct 2019  路  3Comments  路  Source: adonisjs/lucid

I'd like to know if there is any intention to create a client for lucid in standalone mode, please.

I'm a dev at Rocketseat, an education company with 153,5k students and we use and teach AdonisJS to them. We also teach Express, but for that we use another ORM, but instead we would love to teach the Lucid ORM, since it is easy to implement and use.

Although it can be used in standalone mode, it leaves something behind once we can't work with migrations with one CLI. I tried using with adonis-cli but had no success.

Most helpful comment

Hi @Johnson444 !! It's simple to use that standalone!!
Follow these steps:

  • Install the @adonisjs/lucid @adonisjs/fold
  • Make a file with your database connections:
// config/database.js
export default {
  connection: 'my-connection-name',
  'my-connection-name': {
    client: 'pg', // Driver of the connection 
    connection: {
        // connections options, you can see that at knex
        // http://knexjs.org/#Installation-client
     },
  },
};
  • Make a lucid's model file:
// models/lucid.js
import lucid from '@adonisjs/lucid';
import databaseConfig from '../config/database';

export const { Model, Models } = lucid(databaseConfig);

  • Make your model
// models/User.js
import { Model } from './lucid';  

class User from Model { };

export default User;
  • Make the model's hub to expose your model:
// models/index.js
import { Models } from './lucid';

import UserModel from './User';

Models.add('User', UserModel); // This link the database connection with your model

export default User = Model.get('User'); // Expose your model linked with database connection

  • Use your model:
// Using express
// controllers/UserController.js
import { User } from '../models';

class UserController {
  async index(request, response) {
    const users = await User.all();

    return response.json(users.toJSON());
   }
}

  • Unfortunately because we don't have a CLI we are unable to use migration;
  • We can't use the traits as well;

All 3 comments

Hello @HigoRibeiro

It will be quite some work to get Lucid along with the CLI commands to work in standalone mode. To be honest, I don't have enough bandwidth and brain power to work on that now. However, I am happy to collaborate, if you or someone wants to lead the feature.

Hi @HigoRibeiro, you mentioned you got Lucid working as standalone package? I would really like to understand what you did. I want to use it on multiple frameworks as well. I don't need the CLI. Anything you can share ? Thx!

Hi @Johnson444 !! It's simple to use that standalone!!
Follow these steps:

  • Install the @adonisjs/lucid @adonisjs/fold
  • Make a file with your database connections:
// config/database.js
export default {
  connection: 'my-connection-name',
  'my-connection-name': {
    client: 'pg', // Driver of the connection 
    connection: {
        // connections options, you can see that at knex
        // http://knexjs.org/#Installation-client
     },
  },
};
  • Make a lucid's model file:
// models/lucid.js
import lucid from '@adonisjs/lucid';
import databaseConfig from '../config/database';

export const { Model, Models } = lucid(databaseConfig);

  • Make your model
// models/User.js
import { Model } from './lucid';  

class User from Model { };

export default User;
  • Make the model's hub to expose your model:
// models/index.js
import { Models } from './lucid';

import UserModel from './User';

Models.add('User', UserModel); // This link the database connection with your model

export default User = Model.get('User'); // Expose your model linked with database connection

  • Use your model:
// Using express
// controllers/UserController.js
import { User } from '../models';

class UserController {
  async index(request, response) {
    const users = await User.all();

    return response.json(users.toJSON());
   }
}

  • Unfortunately because we don't have a CLI we are unable to use migration;
  • We can't use the traits as well;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SAGV picture SAGV  路  7Comments

hectorgrecco picture hectorgrecco  路  3Comments

nwashangai picture nwashangai  路  5Comments

italoiz picture italoiz  路  6Comments

watzon picture watzon  路  3Comments