1.5.5 • Published 12 months ago

@ghadeerahmad/larascript v1.5.5

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

library for node js to deal with mysql

a library similar to laravel functionalties written in node js

Features

  • model for mysql tabels functionalties
  • migrations
  • query builder (create,update,delete and fetch data)
  • create new record with auto timestamps
  • eagerload for 1 to 1 relationship
  • eagerload for 1 to many relationship
  • insert many
  • upsert function

Usage

to create new migration file

const users = new Migration('users', {
    id: { type: ColumnType.bigint, primary: true, autoIncrement: true },
    name: { type: ColumnType.string },
    userName: { type: ColumnType.string, unique: true },
    password: { type: ColumnType.string }
})

// run migration
await users.run()

the migration constructor takes 2 arguments: the first is table name and the second is columns and thier properties

  • type: this is the column type supported types: - string - int - bigint - double - float - boolean - text - enum
  • length: spicify the length of the column
  • default: spicify default value for column
  • primary: set the column as primary key
  • autoIncrement: set the column as auto increment
  • nullable: set the column to accept null values
  • foreign: set constraints as foriegn key, this property accepts:
    • refrences: migration instance of refrenced table
    • refrence_key: the column refrenced in the refrenced table
    • onDelete: set what happened on delete, accepts: "NULL" or "DELETE"
  • choices: this option used when you set the column type as enum to set the allowed values. this argument accepts list of strings.

Model Usage

every model points to a table in the database.

to create new model:

export class User extends Model{
    constructor(){
        super()
        this.table = 'users'
    }
}
  • this.table property sets the target table in the database, By default this property takes the ploral name of the model with lower case letters, i,e:User => users
  • to perform query using model: i,e
// get single record
const user = await User.query().first()
// get multiple records
const users = await User.query().get()
// apply where statment
const users = await User.query().where('id',1).get()
const users = await User.query().where('id',1).first()

// to create new record
// this function returns created record
const user = await User.query().create({
name:'Jhon Doe',
username:'jgondoe',
password:'somepassword'
})
// perfrom update
// this function return true or false
const result = await User.query().where('id',1).update({name:'new name'})

you must call query() function when you perform any query to database

eager load

  • if you want to fetch data from a relationship you need to define relation in the model class,i.e:
export class User extends Model{
    addresses(){
        this.hadMany(Address)
    }
}
export class Address extends Model{
    user(){
        this.belongsTo(User)
    }
}

here is example of hasMay and belongsTo relationships and how you can define them.

  • to call data from relationship,i.e:
const users = await User.query().with('addresses').get()

more details will be inserted as soom as possible

1.5.5

12 months ago

1.5.4

1 year ago

1.4.6

1 year ago

1.4.5

1 year ago

1.5.3

1 year ago

1.4.4

1 year ago

1.5.2

1 year ago

1.4.3

1 year ago

1.5.1

1 year ago

1.4.2

1 year ago

1.5.0

1 year ago

1.4.9

1 year ago

1.4.8

1 year ago

1.4.7

1 year ago

1.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.4.1

1 year ago

1.3.2

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.3.10

1 year ago

1.3.11

1 year ago

1.3.12

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.3.0

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.1.2

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago