GithubHelp home page GithubHelp logo

sutandojs / sutando Goto Github PK

View Code? Open in Web Editor NEW
148.0 6.0 16.0 271 KB

Sutando is a modern Node.js ORM, like Laravel Eloquent.

Home Page: https://sutando.org

License: MIT License

JavaScript 100.00%
orm javascript mariadb mssql mysql nodejs postgresql sql sqlite sutando

sutando's Introduction

Sutando is an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Sutando, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Sutando models allow you to insert, update, and delete records from the table as well.

Heavily inspired by Laravel's ORM Eloquent.

✨ Features

  • Supports MySQL, PostgreSQL, SQLite and other databases
  • Concise syntax and intuitive operations
  • Model relationships for handling complex data queries and operations
  • Powerful query builder
  • Customized data type conversion for model attributes
  • Easy-to-use transaction
  • Support for hooks to execute custom logic at different stages of model operations
  • Simple plugin mechanism for easy expansion

πŸ“– Documentation

Check the full documentation on https://sutando.org | δΈ­ζ–‡ζ–‡ζ‘£

πŸš€ Quick Start

Let’s take mysql as an example.

Install Sutando and mysql database library

$ npm install sutando mysql2 --save

The easiest way to make SQL queries is to use the Database query builder. It allows you to construct simple and complex SQL queries using JavaScript methods.

const { sutando, Model } = require('sutando');

// Add SQL Connection Info
sutando.addConnection({
  client: 'mysql2',
  connection: {
    host : '127.0.0.1',
    port : 3306,
    user : 'root',
    password : '',
    database : 'test'
  },
});

const db = sutando.connection();

// Query Builder
const users = await db.table('users').where('age', '>', 35).get();

// ORM
class User extends Model {}

// Query Data
const users = await User.query().where('age', '>', 35).get();

// Insert
const user = new User;
user.name = 'David Bowie';
await user.save();

// Delete
await user.delete();

// Pagination
const users = await User.query().paginate();

// Eager Loading
const users = await User.query().with('posts').get();

// Constraining Eager Loads
const users = await User.query().with({
  posts: q => q.where('likes_count', '>', 100)
}).get();

// Lazy Eager Loading
await user.load('posts');

πŸ’– Show Your Support

Please ⭐️ this repository if this project helped you

sutando's People

Contributors

dependabot[bot] avatar jjjrmy avatar kiddyuchina avatar random-webdeveloper 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  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  avatar

sutando's Issues

Question about performance

Hello! Thanks for this great project, ORM like this was heavily missed in the js ecosystem and it's great that you built it.

Do you have any performance benchmarks by any chance? Nodejs ecosystem likes to compare libraries to one another and I was curious if runtime magic that you do (with scopes, relations, mutators and etc) affects performance in any significant manner.

Thank you again and can't wait to start using it in my next project.

Non-Database Connections

In my current project, we are not making database queries with our application but instead pulling them from a couple APIs. Is there an example or use case where we can still use the Sutando Models and Collections system without using the Query Builder?

`The requested module '***/node_modules/sutando/sutando.js' does not provide an export named 'Model'`

⚠️ This also happens when using the HonoJS example when you update Sutando to v1.5
I just checked and you can npm run dev fine on v1.2.1 but after switching to latest we get this error.

import { Model } from 'sutando';

this is throwing the error:

[worker reload] [worker init] The requested module '***/node_modules/sutando/sutando.js' does not provide an export named 'Model'

  import { Model } from 'node_modules/sutando/sutando.js';
  ^^^^^
  SyntaxError: The requested module 'node_modules/sutando/sutando.js' does not provide an export named 'Model'
  at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
  at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

This is my setup:

// /sutando.config.ts
import ClientD1 from 'knex-cloudflare-d1';

export default {
  client: ClientD1,
  connection: {
    database: process.env.DB,
  },
  migrations: {
    table: 'migrations',
    path: './database/migrations',
  },
  models: {
    path: './models'
  }
};
// /models/Prompt.ts
import { Model } from 'sutando';

export class Prompt extends Model {
  // The table associated with the model.
  table = 'prompts';
}
// /database/migrations/2024_04_13_create_prompts_table.ts
import { Migration } from 'sutando';

export default class extends Migration {
  /**
    * Run the migrations.
    */
  async up(schema: any) {
    await schema.createTable('prompts', (table: any) => {
      table.increments('id');
      table.timestamps();
    });
  }

  /**
    * Reverse the migrations.
    */
  async down(schema: any) {
    await schema.dropTableIfExists('prompts');
  }
}

Error When Using Command `migration:make`, `migrate:run`

sutando migrate:make create_flights_table

node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:405:5)
    at validateString (node:internal/validators:162:11)
    at Module.require (node:internal/modules/cjs/loader:1136:3)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Command.<anonymous> (/Users/admin/.nvm/versions/node/v18.18.0/lib/node_modules/sutando/bin/cli.js:87:20)
    at Command.listener [as _actionHandler] (/Users/admin/.nvm/versions/node/v18.18.0/lib/node_modules/sutando/node_modules/commander/lib/command.js:494:17)
    at /Users/admin/.nvm/versions/node/v18.18.0/lib/node_modules/sutando/node_modules/commander/lib/command.js:1296:65
    at Command._chainOrCall (/Users/admin/.nvm/versions/node/v18.18.0/lib/node_modules/sutando/node_modules/commander/lib/command.js:1193:12)
    at Command._parseCommand (/Users/admin/.nvm/versions/node/v18.18.0/lib/node_modules/sutando/node_modules/commander/lib/command.js:1296:27)
    at /Users/admin/.nvm/versions/node/v18.18.0/lib/node_modules/sutando/node_modules/commander/lib/command.js:1082:27 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v18.18.0

first

Hi thanks for this great package just wondering is it production ready ?

cli not working

after running this command

sutando init

i get this error

/home/.nvm/versions/node/v20.10.0/bin/sutando: line 1: syntax error near unexpected token `(' 'home/.nvm/versions/node/v20.10.0/bin/sutando: line 1: `const { program } = require('commander');

not supported resolving ES modules import

Hi guys. great library you made, was looking for something like this for years.

I have a problem when running a nuxt project (with vite) with this library in docker alpine.
i get the error: [worker reload] [worker init] Directory import '/app/node_modules/sutando/src' is not supported resolving ES modules imported from /app/node_modules/sutando/sutando.mjs
Using the newest sutando version.
The same code works if i dont use with docker, even with the same nodejs version (20 or 21, no difference)
My dockerfile looks like this:

# syntax=docker/dockerfile:experimental
FROM php:8.3-fpm-alpine
RUN apk add --update nodejs npm
WORKDIR /app

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.