Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
model-for-mysql
Advanced tools
Readme
Базовый класс модели запросов в БД MySQL для nodejs.
Предназначен для создания и отправки запросов в БД MySQL.
Использует модули "mysql" и "active-record-for-mysql".
bind - отправка готового запроса в БД MySQL, контроль ошибок;
create - создание и отправка запроса создания таблицы
drop - создание и отправка запроса удаления таблицы
insert - создание и отправка запроса добавления строки в таблицу
delete - создание и отправка запроса удаления строк из таблицы
update - создание и отправка запроса обновления строк в таблице
select - создание и отправка запроса получения строк из таблицы
get - формирует/извлекает строку для формы добавления/обновления
set - добавляет/обновляет строку из формы добавления/обновления
get_row_for_id - по ID возвращает строку из базы данных
delete_row_for_id - по ID удаляет строку из базы данных
get_page_rows - возвращает заданную страницу строк
get_count_rows - возвращает общее число строк
В директории проекта расположена папка с моделями "models".
В этой папке создан файл "myModel.js" c классом базовой модели.
Примечание. Число базовых моделей соответствует числу используемых в проекте баз данных.
В той же папке созданы файлы "mdl_users" и "mdl_cities" - классы моделей таблиц "users" и "cities".
Классы моделей наследуют свойства и методы базовой модели.
var Model = function () {
require('model-for-mysql').call(this, {
host : 'localhost', //Адрес БД
user : 'user123', //Пользователь
password : '123', //Пароль
database : 'test', //Название БД
});
var self = this;
};
module.exports = Model;
var Model = function () {
require('./myModel').call(this);
var self = this;
this.table = 'users'; //Название таблицы
this.idkey = 'user_id'; //Ключевое поле
this.table_nick = 'ПОЛЬЗОВАТЕЛИ'; //Ник таблицы
this.fields = //Структура полей таблицы
[
{
field : 'user_id',
label : 'ID ПОЛЬЗОВАТЕЛЯ',
type : 'int(11) NOT NULL',
auto : 1,
},
{
field : 'user_name',
label : 'ИМЯ ПОЛЬЗОВАТЕЛЯ',
type : 'varchar(100) NOT NULL',
},
{
field : 'user_family',
label : 'ФАМИЛМЯ ПОЛЬЗОВАТЕЛЯ',
type : 'varchar(100) NOT NULL',
},
{
field : 'user_city_id',
label : 'ID ГОРОДА ПОЛЬЗОВАТЕЛЯ',
type : 'int(11) NOT NULL',
},
{
field : 'user_age',
label : 'ВОЗРАСТ ПОЛЬЗОВАТЕЛЯ',
type : 'int(11) NOT NULL',
},
{
field : 'user_image',
label : 'ИЗОБРАЖЕНИЕ ПОЛЬЗОВАТЕЛЯ',
type : 'longblob',
},
];
};
module.exports = Model;
var Model = function () {
require('./myModel').call(this);
var self = this;
this.table = 'cities'; //Название таблицы
this.idkey = 'city_id'; //Ключевое поле
this.table_nick = 'ГОРОДА'; //Ник таблицы
this.fields = //Структура полей таблицы
[
{
field : 'city_id',
label : 'ID ГОРОДА',
type : 'int(11) NOT NULL',
auto : 1,
},
{
field : 'city_name',
label : 'НАЗВАНИЕ ГОРОДА',
type : 'varchar(100) NOT NULL',
},
];
};
module.exports = Model;
var mdl_users = new(require('./models/mdl_users'));
var mdl_cities = new(require('./models/mdl_cities'));
mdl_users.create(function(success) {})
Возвращает флаг успешности
mdl_users.drop(function(success) {});
Возвращает флаг успешности
var data = {
data : {
user_name : 'Александр',
user_family : 'Иванов',
user_age : 40,
//Добавление бинарного файла
user_photo : require('fs').readFileSync(filename),
}
};
mdl_users.insert(data, function (id) {});
Возвращает ID строки или 0 в случае неудачи
var data = {
where : {
user_family : 'Иванов',
'user_age >' : 25
},
in : {
user_id : [1,2,3]
},
};
mdl_users.delete(data, function (count_rows) {})
Возвращает число удаленных строк или 0 в случае неудачи
var data = {
in : {
user_age : [40,45,50]
},
like : {
user_family : '%Ив'
},
where : {
user_status : 'Новичок',
},
data : {
user_status : 'Старина',
//Данные бинарного файла
user_photo : require('fs').readFileSync(filename),
},
};
mdl_users.update(data, function (count_rows) {});
Возвращает число обновленных строк или 0 в случае неудачи
var data = {
fields : ['user_id', 'user_name', 'user_family'], //[] - все поля строки
join : {
cities : 'users.user_city_id = cities.city_id',
statuses : 'users.user_status_id = statuses.status_id',
},
where : {
status_name : 'Рабочий',
'user_age >' : 45,
},
like : {
user_family : '%Ива',
user_about : '%женат%',
},
in : {
user_company_id : [1,2,3]
},
group : ['user_family', 'user_name'],
order : {
user_family : 'asc',
user_age : 'desc',
},
limit : 10,
offset : 5,
};
mdl_users.select(data, function (rows) {});
Возвращает массив строк таблицы или [] в случае неудачи
mdl_users.get(1, function(row) {})
Возвращает {user_id:1, user_family:'Иванов', user_name:'Иван'...}
mdl_users.get(0, function(row) {})
Возвращает {user_id:0}
//Обновляет в таблице(id>0) или добавляет в таблицу(id=0) одну строку
//Условия игнорируются
var data = {
user_name : 'Иван',
user_family : 'Иванов',
}
mdl_users.set(0, data, function(id) {});
Добавляет новую строку и возвращает ID
var data = {
user_name : 'Иван',
user_family : 'Иванов',
}
mdl_users.set(1, data, function(success) {});
Обновляет существующую строку и возвращает флаг успешности
mdl_users.get_row_for_id(10, ['user_name', 'user_family'], function(row) {})
Возвращает строку или null
mdl_users.delete_row_for_id(10, function(success) {})
Возвращает флаг успешности
var data = {
fields : ['user_family', 'user_name'],
where : {
'user_age >' : 30
}
};
mdl_users.get_page_rows(0, 10, data, function(total, rows) {})
Возвращает общее число строк и массив строк требуемой страницы
var data = {
where : {
'user_age >' : 50
},
like : {
user_family : 'ман%'
},
}
mdl_user.get_count_rows(data, function (count_rows) {})
Возвращает число строк удовлетворяющих условиям
Код тестов в файле "_demo/server.js".
В файле "_demo/uploads/passport.jpg" расположено небольшое изображение для демонстрации загрузки двоичного файла
Для тестирования требуется создать пустую (без таблиц) БД, например "test".
В файле "_demo/server.js" указать параметры доступа к БД.
node server
http://localhost:2020
FAQs
MySQL active-record query builder model for nodejs
The npm package model-for-mysql receives a total of 8 weekly downloads. As such, model-for-mysql popularity was classified as not popular.
We found that model-for-mysql demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.