mysqloose

    1.3.1 • Public • Published

    mysqloose

    This is similar to mongoose but for mysql

    warning: Don not use this in real project, it is unstable

    David GitHub repo size npm npm

    NPM

    Installation

    You need: Node.js installed on your machine.

    To install

    $ npm i mysqloose

    Importing

    const mysqloose = require("mysqloose");

    Important: Turn on mysql server before using

    Overview

    Connecting to MySQL

    First, we need to define a connection.

    mysqloose.connect(
      "mysql://<user>:<password>@<hostname>/<database_name>",
      (err) => {
        if (err) throw err;
      }
    );

    Defining a Model

    Models are defined through the Schema interface. Unlike mongoose, you can not define a model without mysqloose.connect

    mysqloose.connect(
      "mysql://<user>:<password>@<hostname>/<database_name>",
      (err) => {
        if (err) throw err;
      }
    );
     
    const Schema = mysqloose.Schema;
     
    const UserSchema = new Schema({
      id: 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY' // mysqloose do not have ObjectId yet
      user_name: 'VARCHAR(40) NOT NULL',
      email: 'VARCHAR(255) NOT NULL',
      password: 'varchar(60) not null' // Case-insensitive
      user_type: 'varchar(5) default "user"'
    });

    Accessing a Model

    const User = mongoose.model("ModelName", UserSchema);
     
    const newUser = new User({
      user_name: "user name",
      email: "email",
      password: "password",
    });

    Insert

    newUser.save((err) => {
      if (err) throw err;
    });

    Select

    Synchronous

    User.find({ name: "name" }, (err, result) => {
      if (err) throw err;
      console.log(result);
    });
     
    // Must use {<somethingId>: <number>}
    User.findById({ id: 1 }, "name email", (err, result) => {
      if (err) throw err;
      console.log(result);
    });
     
    User.findOne({ name: "name" }, (err, result) => {
      if (err) throw err;
      console.log(result);
    });

    Asynchronous

    User.find({ name: "name" })
      .then((result) => {
        console.log(result);
      })
      .catch((err) => {
        if (err) throw err;
      });
     
    User.findById({ id: 1 }, "name email")
      .then((result) => {
        console.log(result);
      })
      .catch((err) => {
        if (err) throw err;
      });
     
    User.findOne({ name: "name" })
      .then((result) => {
        console.log(result);
      })
      .catch((err) => {
        if (err) throw err;
      });

    Update

    User.updateOne({ name: "name" }, { email: "new email" }, (err, result) => {
      if (err) throw err;
      console.log(result);
    });

    Delete

    User.deleteOne({ name: "name" }, (err, result) => {
      if (err) throw err;
      console.log(result);
    });

    Model API

    create( object, callback)

    find( object, string, callback )

    findOne( object, string, callback )

    findOneAndUpdate( object, object, callback )

    findOneAndDelete(object, callback )

    findById( object, string, callback )

    findByIdAndUpdate( object, object, callback )

    findByIdAndDelete( object, callback )

    updateOne( object, object, callback )

    updateMany( object, object, callback )

    deleteOne( object, callback)

    deleteMany( object, callback)

    Keywords

    Install

    npm i mysqloose

    DownloadsWeekly Downloads

    0

    Version

    1.3.1

    License

    MIT

    Unpacked Size

    22.9 kB

    Total Files

    10

    Last publish

    Collaborators

    • nhn.dev