mappifysql
Advanced tools
Comparing version 1.1.1 to 1.1.2-beta
const { Database } = require('./database'); | ||
const db = new Database(); | ||
db.createPool().then(() => { | ||
db.createConnection().then(() => { | ||
}).catch((err) => { | ||
@@ -10,2 +10,2 @@ console.log(err); | ||
module.exports = { query }; | ||
module.exports = { query }; |
@@ -29,2 +29,4 @@ const mysql = require('mysql2'); | ||
}; | ||
this.connection = this.createConnection() || this.createPool(); | ||
} | ||
@@ -86,5 +88,6 @@ | ||
initializeConnection() { | ||
this.query = util.promisify(this.connection.query).bind(this.connection); | ||
this.connection.on('error', (err) => { | ||
if (err.code === 'PROTOCOL_CONNECTION_LOST' || err.code === 'ECONNRESET') { | ||
console.log('Database connection lost or reset. Reconnecting...'); | ||
console.log('Database connection lost. Reconnecting...'); | ||
if (this.connection.connect) { | ||
@@ -97,4 +100,19 @@ this.connect(); | ||
}); | ||
} | ||
/** | ||
* Gets the current connection. | ||
* @example const db = new Database(); | ||
* db.createConnection().then(() => { | ||
* }).catch((err) => { | ||
* console.log(err); | ||
* }); | ||
* const connection = db.getConnection(); | ||
* @returns {any} The current connection. | ||
*/ | ||
getConnection() { | ||
return this.connection; | ||
} | ||
/** | ||
@@ -101,0 +119,0 @@ * Gets a promisified version of the query method from the connection. |
@@ -0,4 +1,26 @@ | ||
const { connection } = require('./connection'); | ||
const { Database } = require('./database'); | ||
const { MappifyModel } = require('./model'); | ||
// class User extends MappifyModel { | ||
// } | ||
// (async () => { | ||
// try { | ||
// connection.beginTransaction(); | ||
// let user = new User({ first_name: 'John', last_name: 'Doe' }); | ||
// await user.save(); | ||
// let user2 = new User({ firstname: 'Jane', last_name: 'Doe' }); | ||
// await user2.save(); | ||
// connection.commit(); | ||
// } catch (err) { | ||
// connection.rollback(); | ||
// console.error(err); | ||
// } | ||
// })(); | ||
/** | ||
@@ -5,0 +27,0 @@ * Database class for managing MySQL connections. |
@@ -389,3 +389,3 @@ const { query } = require('../lib/connection'); | ||
* console.log(enrollment.course); | ||
* @returns {this} The instance of the model with the populated relation. | ||
* @returns {Promise<this>} A promise that resolves to the instance with the related data populated. | ||
*/ | ||
@@ -460,2 +460,66 @@ async populate(relation, options = {}) { | ||
/** | ||
* This method attaches a new record to the related model and associates it with the current instance. | ||
* @param {Model} target - The target model to attach. | ||
* @param {string} relation - The record to attach to the relation. | ||
* @param {object} options - The options for the query. - (optional) | ||
* @param {Array} options.attributes - The columns to include in the result. - (optional) | ||
* @param {Array} options.exclude - The columns to exclude from the result. - (optional) | ||
* @example const user = await User.findById(1); | ||
* const post = new Post({ title: 'Post 1', content: 'This is post 1' }); | ||
* await user.attach(post, 'posts'); | ||
* console.log(user); | ||
* @example const order = await Order.findOne({ where: { id: 1 } }); | ||
* const shippingAddress = new ShippingAddress({ address: '123 Main St', city: 'Springfield', state: 'IL', zip: '62701' }); | ||
* await order.attach(shippingAddress, 'shippingAddress'); | ||
* console.log(order); | ||
* @returns {Promise<this>} A promise that resolves to the instance with the related data attached. | ||
* @throws {Error} Throws an error if the relation is not defined. | ||
* @throws {Error} Throws an error if the relation is not a hasOne or hasMany relation. | ||
* @throws {Error} Throws an error if the foreign key is not defined. | ||
*/ | ||
async attach(target, relation, options = {}) { | ||
if (!this.associations[relation]) { | ||
throw new Error(`Relation "${relation}" is not defined`); | ||
} | ||
const relatedModel = this.associations[relation].model; // the related model | ||
const foreignKey = this.associations[relation].foreignKey; // the foreign key in this model | ||
if (this.associations[relation].type === 'hasMany') { | ||
target[foreignKey] = this.id; | ||
target.save(); | ||
this[relation] = await relatedModel.findAll({ | ||
where: { [foreignKey]: this.id }, // where the foreign key matches the id of this model | ||
attributes: options?.attributes ? options?.attributes : ['*'], // select all columns by default | ||
exclude: options?.exclude ? options?.exclude : [] // exclude no columns by default | ||
}).then((result) => { | ||
return result.map((item) => { // return the result as an array of objects | ||
return Object.assign({}, item); // return the result as an object | ||
}); | ||
}).catch((err) => { | ||
console.log(err); | ||
}); | ||
} else if (this.associations[relation].type === 'hasOne') { | ||
target[foreignKey] = this.id; | ||
target.save(); | ||
this[relation] = await relatedModel.findOne({ | ||
where: { [foreignKey]: this.id }, // where the foreign key matches the id of this model | ||
attributes: options?.attributes ? options?.attributes : ['*'], // select all columns by default | ||
exclude: options?.exclude ? options?.exclude : [] // exclude no columns by default | ||
}).then((result) => { | ||
return Object.assign({}, result); // return the result as an object | ||
}).catch((err) => { | ||
console.log(err); | ||
}); | ||
} // if the relation is a hasOne relation | ||
else { | ||
throw new Error(`Relation "${relation}" is not a hasOne or hasMany relation`); | ||
} | ||
} | ||
/** | ||
* This method saves the instance to the database. | ||
@@ -510,3 +574,3 @@ * @example const product = new Product({ name: 'Product 1', price: 100 }); | ||
* @example var products = await Product.fetch(); | ||
* @returns {Promise<Array<Model>>} An array of instances. | ||
* @returns {Promise<Array<MappifyModel>>} An array of instances. | ||
*/ | ||
@@ -610,3 +674,3 @@ static async fetch() { | ||
* @example var products = await Product.findAll({ group: 'category' }); | ||
* @returns {Promise<Array<Model>>} An array of instances. | ||
* @returns {Promise<Array<MappifyModel>>} An array of instances. | ||
*/ | ||
@@ -620,3 +684,5 @@ static async findAll(options = {}) { | ||
// Prepare WHERE clause | ||
const { whereClause, whereValues } = prepareWhereClause(where, conditions); | ||
if (Object.keys(where).length > 0) { | ||
var { whereClause, whereValues } = prepareWhereClause(where, conditions); | ||
} | ||
@@ -636,6 +702,6 @@ | ||
// Construct SQL query | ||
const sql = `SELECT ${selectedAttributes} FROM ${this.tableName} ${whereClause} ${groupClause} ${orderClause} ${limitClause} ${offsetClause}`; | ||
console.log(sql, whereValues); | ||
const sql = `SELECT ${selectedAttributes} FROM ${this.tableName} ${whereClause ? whereClause : ''} ${groupClause} ${orderClause} ${limitClause} ${offsetClause}`; | ||
// Execute query | ||
let result = await query(sql, whereValues); | ||
let result = await query(sql, whereValues ? whereValues : []); | ||
// Exclude unwanted attributes | ||
@@ -698,3 +764,3 @@ result = result.map(row => { | ||
* @example await Product.findOneAndDelete({ where: { name: 'Product 1' } }); | ||
* @returns {Promise<Model|null>} The updated instance or null if no record was found. | ||
* @returns {Promise<MappifyModel|null>} The updated instance or null if no record was found. | ||
* @throws {Error} Throws an error if the where clause is not provided or if no record is found. | ||
@@ -722,3 +788,3 @@ */ | ||
* @example await Product.findOneAndUpdate({ where: { id: 1 } }, { price: 200 }); | ||
* @returns {Promise<Model|null>} The updated instance or null if no record was found. | ||
* @returns {Promise<MappifyModel|null>} The updated instance or null if no record was found. | ||
* @throws {Error} Throws an error if the where clause is not provided or if no record is found. | ||
@@ -743,3 +809,3 @@ */ | ||
* @example await Product.findByIdAndUpdate(1, { price: 200 }); | ||
* @returns {Promise<Model|null>} The updated instance or null if no record was found. | ||
* @returns {Promise<MappifyModel|null>} The updated instance or null if no record was found. | ||
* @throws {Error} Throws an error if the ID is not provided or if no record is found. | ||
@@ -746,0 +812,0 @@ */ |
{ | ||
"name": "mappifysql", | ||
"version": "1.1.1", | ||
"version": "1.1.2-beta", | ||
"description": "MappifySQL is a lightweight, easy-to-use Object-Relational Mapping (ORM) library for MySQL databases, designed for use with Node.js. It provides an intuitive, promise-based API for interacting with your MySQL database using JavaScript or TypeScript.", | ||
@@ -5,0 +5,0 @@ "repository": { |
324
README.md
@@ -1,6 +0,7 @@ | ||
# MappifySQL: A MySQL ORM for Node.js and TypeScript | ||
<div align="center"> | ||
<img src="https://i.ibb.co/tmH8Kk4/mappifysql.jpg" alt="mappifysql" style="height: 300px; width: 300px; border-radius: 100%; object-fit: cover;"> | ||
</div> | ||
MappifySQL is a lightweight, easy-to-use Object-Relational Mapping (ORM) library for MySQL databases, designed for use with Node.js. It provides an intuitive, promise-based API for interacting with your MySQL database using JavaScript or TypeScript. | ||
--- | ||
--- | ||
<a href="https://www.npmjs.com/package/mappifysql"><img src="https://img.shields.io/npm/v/mappifysql.svg" alt="Version"></a> | ||
@@ -11,2 +12,7 @@ <a href="https://www.npmjs.com/package/mappifysql"><img src="https://img.shields.io/npm/dt/mappifysql.svg" alt="Downloads"></a> | ||
# MappifySQL: A MySQL ORM for Node.js and TypeScript | ||
MappifySQL is a lightweight, easy-to-use Object-Relational Mapping (ORM) library for MySQL databases, designed for use with Node.js. It provides an intuitive, promise-based API for interacting with your MySQL database using JavaScript or TypeScript. | ||
## Features | ||
@@ -79,3 +85,3 @@ | ||
DB_PASSWORD=password | ||
DB_DATABASE=mydatabase | ||
DB_NAME=mydatabase | ||
DB_PORT=3306 ## (optional) default is 3306 | ||
@@ -102,3 +108,3 @@ ``` | ||
var connection = db.connection; | ||
var connection = db.getConnection(); | ||
var query = db.getQuery(); | ||
@@ -109,2 +115,5 @@ | ||
``` | ||
** Using TypeScript ** | ||
```typescript | ||
@@ -122,3 +131,3 @@ | ||
var connection = db.connection; | ||
var connection = db.getConnection(); | ||
var query = db.getQuery(); | ||
@@ -132,3 +141,3 @@ | ||
<div align="center"> | ||
<img src="https://i.ibb.co/BCvQSYL/create-Single-Connection.png" alt="createSingleConnection" border="0"> | ||
<img src="https://i.ibb.co/NptYQGf/createsingleconnection.png" alt="createSingleConnection" border="0"> | ||
</div> | ||
@@ -151,3 +160,3 @@ | ||
var connection = db.connection; | ||
var connection = db.getConnection(); | ||
var query = db.getQuery(); | ||
@@ -171,3 +180,3 @@ | ||
var connection = db.connection; | ||
var connection = db.getConnection(); | ||
var query = db.getQuery(); | ||
@@ -179,8 +188,51 @@ | ||
<div align="center"> | ||
<img src="https://i.ibb.co/s3cnW5p/create-Pool-Connection.png" alt="createPoolConnection" border="0"> | ||
<img src="https://i.ibb.co/6r0npjy/createpoolconnection.png" alt="createPoolConnection" border="0"> | ||
</div> | ||
Methods available in the connection object: | ||
| Method | Description | Parameters | Supported by | | ||
| --- | --- | --- | --- | | ||
| `beginTransaction` | Begins a transaction. | `callback?: (err: any) => void` | `createConnection` | | ||
| `commit` | Commits the current transaction. | `callback?: (err: any) => void` | `createConnection` | | ||
| `rollback` | Rolls back the current transaction. | `callback?: (err: any) => void` | `createConnection` | | ||
| `query` | Sends a SQL query to the database. | `sql: string`, `values?: any`, `callback?: (error: any, results: any, fields: any) => void` | `createConnection`, `createPool` | | ||
| `end` | Ends the connection. | `callback?: (err: any) => void` | `createConnection`, `createPool` | | ||
| `destroy` | Destroys the connection. | None | `createConnection` | | ||
| `pause` | Pauses the connection. | None | `createConnection` | | ||
| `resume` | Resumes the connection. | None | `createConnection` | | ||
| `escape` | Escapes a value for SQL. | `value: any` | `createConnection`, `createPool` | | ||
| `escapeId` | Escapes an identifier for SQL. | `value: any` | `createConnection`, `createPool` | | ||
| `format` | Formats a SQL query string. | `sql: string`, `values?: any` | `createConnection`, `createPool` | | ||
| `ping` | Pings the server. | `callback?: (err: any) => void` | `createConnection`, `createPool` | | ||
| `changeUser` | Changes the user for the current connection. | `options: any`, `callback?: (err: any) => void` | `createConnection` | | ||
Example: | ||
```javascript | ||
const { connection } = require('./connection'); | ||
connection.query('SELECT * FROM users', (err, results, fields) => { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('Fetched records:', results); | ||
}); | ||
``` | ||
** Using TypeScript ** | ||
```typescript | ||
import { connection } from './connection'; | ||
connection.query('SELECT * FROM users', (err, results, fields) => { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('Fetched records:', results); | ||
}); | ||
``` | ||
### Using the Model Class | ||
@@ -204,2 +256,4 @@ | ||
** Using TypeScript ** | ||
```typescript | ||
@@ -209,56 +263,31 @@ import { MappifyModel } from 'mappifysql'; | ||
interface UserAttributes { | ||
name: string; | ||
id?: number; | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
// add more properties here... | ||
password: string; | ||
//add more attributes here... | ||
} | ||
class User extends MappifyModel { | ||
id: number; | ||
name: string; | ||
id?: number; | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
// add more properties here... | ||
password: string; | ||
constructor(data: UserAttributes) { | ||
super(); | ||
this.name = data.name; | ||
this.id = data.id; | ||
this.first_name = data.first_name; | ||
this.last_name = data.last_name; | ||
this.email = data.email; | ||
// set more properties here... | ||
} | ||
this.password = data.password; | ||
setProperties() { | ||
super.setProperties(); | ||
// add more properties here... | ||
} | ||
async save() { | ||
await super.save(); | ||
} | ||
async update() { | ||
await super.update(); | ||
} | ||
async delete() { | ||
await super.delete(); | ||
} | ||
static async findAll() { | ||
let results = await MappifyModel.findAll(); | ||
return results.map(result => new User(result)); | ||
} | ||
static async findById(id: number){ | ||
let result = await MappifyModel.findById(id); | ||
return new User(result); | ||
} | ||
static async findOne(options: { where: object }) { | ||
let result = await MappifyModel.findOne(options); | ||
return new User(result); | ||
} | ||
static async findOrCreate(options: object, defaults: object) { | ||
let { record, created } = await MappifyModel.findOrCreate(options, defaults); | ||
return { record: new User(record), created }; | ||
} | ||
} | ||
@@ -285,2 +314,4 @@ | ||
** Using TypeScript ** | ||
```typescript | ||
@@ -354,2 +385,4 @@ import { MappifyModel } from 'mappifysql'; | ||
** Import in TypeScript ** | ||
```typescript | ||
@@ -850,4 +883,8 @@ import User from 'path/to/user.ts' | ||
try { | ||
let results = await connection.query('SELECT * FROM products WHERE name LIKE ?', ['%apple%']); | ||
console.log('Fetched records:', results); | ||
let results = await connection.query('SELECT * FROM products WHERE name LIKE ?', ['%apple%'], (err, results, fields) => { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('Fetched records:', results); | ||
}); | ||
} catch (err) { | ||
@@ -919,2 +956,4 @@ console.error(err); | ||
** Using TypeScript ** | ||
```typescript | ||
@@ -924,2 +963,3 @@ const { MappifyModel } = require('mappifysql'); | ||
interface ProductAttributes { | ||
id?: number; | ||
name: string; | ||
@@ -931,3 +971,3 @@ price: number; | ||
class Product extends MappifyModel { | ||
id: number; | ||
id?: number; | ||
name: string; | ||
@@ -939,2 +979,3 @@ price: number; | ||
super(); | ||
this.id = data.id; | ||
this.name = data.name; | ||
@@ -945,3 +986,2 @@ this.price = data.price; | ||
// other methods here... | ||
@@ -979,2 +1019,4 @@ // create a custom function using functions in the model class | ||
<span style="color:red;"><b>Note</b></span>: Transactions are only supported when created a single connection using the createConnection method. Transactions are not supported in pool because a pool consists of multiple connections to the database. | ||
```javascript | ||
@@ -985,12 +1027,9 @@ const { connection, query } = require('./connection'); | ||
try { | ||
await connection.beginTransaction(); | ||
connection.beginTransaction(); | ||
var user = await query('INSERT INTO users SET ?', { name: 'John Doe'}); | ||
await query('INSERT INTO addresses SET ?', { user_id: user.insertId, address: '123 Main St' }); | ||
await connection.commit(); | ||
connection.commit(); | ||
console.log('Transaction completed successfully'); | ||
query('SELECT * FROM users').then((results) => { | ||
console.log('Fetched records:', results); | ||
}); | ||
} catch (err) { | ||
await connection.rollback(); | ||
connection.rollback(); | ||
console.error(err); | ||
@@ -1004,3 +1043,3 @@ } | ||
try { | ||
await connection.beginTransaction(); | ||
connection.beginTransaction(); | ||
let user = new User({ name: 'John Doe' }); | ||
@@ -1010,7 +1049,4 @@ await user.save(); | ||
await address.save(); | ||
await connection.commit(); | ||
connection.commit(); | ||
console.log('Transaction completed successfully'); | ||
User.findAll().then((results) => { | ||
console.log('Fetched records:', results); | ||
}); | ||
} catch (err) { | ||
@@ -1039,2 +1075,3 @@ await connection.rollback(); | ||
| `populate` | Fetches the related data for a given relation. | `relation`, `options` (optional) | `await post.populate('user');` | | ||
| `attach` | Attaches a new record to the related model and associates it with the current model. | `target`, `relation`, `options` (optional) | `await post.attach(post, 'posts');` | | ||
@@ -1059,7 +1096,15 @@ | ||
| | otherKey | The foreign key in through model for the related model. | | ||
| populate | attributes | The columns to include in the result. | | ||
| populate | relation | The name of the relation to fetch. | | ||
| | attributes | The columns to include in the result. | | ||
| | exclude | The columns to exclude from the result. | | ||
| attach | target | The record to attach to the related model. | | ||
| | relation | The name of the relation to attach to. | | ||
| | attributes | The columns to include in the result. | | ||
| | exclude | The columns to exclude from the result. | | ||
Please note that `attributes` and `exclude` keys in the `populate` method are optional. | ||
Please note that `attributes` and `exclude` keys in the `populate` and `attach` methods are optional and can be used to specify the columns to include or exclude from the result. | ||
<span style="color:red;"><b>Note</b></span>: The `populate` method is used to fetch the related data for a given relation. The `attach` method is used to attach a new record to the related model and associate it with the current model and it can only be used with the `hasOne` and `hasMany` relationships. | ||
#### Defining Relationships | ||
@@ -1093,4 +1138,4 @@ | ||
Order.findByOne({ where: { id: 1 }}).then((order) => { | ||
order.populate('shippingAddress', {exclude: ['created_at', 'updated_at']}).then((order) => { | ||
Order.findByOne({ where: { id: 1 }}).then(async (order) => { | ||
await order.populate('shippingAddress', {exclude: ['created_at', 'updated_at']}).then((order) => { | ||
console.log('Order with shipping address:', order); | ||
@@ -1102,2 +1147,12 @@ }); | ||
Order.findAll().then(async (orders) => { | ||
await Promise.all(orders.map(async (order) => { | ||
await order.populate('shippingAddress', {exclude: ['created_at', 'updated_at']}); | ||
})); | ||
console.log('Orders with shipping addresses:', orders); | ||
}).catch((err) => { | ||
console.error(err); | ||
}); | ||
``` | ||
@@ -1110,3 +1165,3 @@ | ||
interface ShippingAddressAttributes { | ||
id: number; | ||
id?: number; | ||
address: string; | ||
@@ -1118,3 +1173,3 @@ city: string; | ||
class ShippingAddress extends MappifyModel { | ||
id: number; | ||
id?: number; | ||
address: string; | ||
@@ -1130,6 +1185,5 @@ city: string; | ||
// other methods here... | ||
super.associations() { | ||
super.belongsTo(Order, { | ||
associations() { | ||
this.belongsTo(Order, { | ||
as: 'order', | ||
@@ -1149,4 +1203,4 @@ key: 'id' | ||
ShippingAddress.findByOne({ where: { id: 1 }}).then((shippingAddress) => { | ||
shippingAddress.populate('order', {attributes: ['id', 'total']}).then((shippingAddress) => { | ||
ShippingAddress.findByOne({ where: { id: 1 }}).then(async(shippingAddress) => { | ||
await shippingAddress.populate('order', {attributes: ['id', 'total']}).then((shippingAddress) => { | ||
console.log('Shipping address with order:', shippingAddress); | ||
@@ -1158,4 +1212,45 @@ }); | ||
ShippingAddress.findAll().then(async (shippingAddresses) => { | ||
await Promise.all(shippingAddresses.map(async (shippingAddress) => { | ||
await shippingAddress.populate('order', {attributes: ['id', 'total']}); | ||
})); | ||
console.log('Shipping addresses with orders:', shippingAddresses); | ||
}).catch((err) => { | ||
console.error(err); | ||
}); | ||
``` | ||
** Using `attach` method ** | ||
```javascript | ||
const Order = require('path/to/Order'); | ||
const ShippingAddress = require('path/to/ShippingAddress'); | ||
let createShippingAddress = async () => { | ||
var order = await Order.findById(1); | ||
var shippingAddress = new ShippingAddress({ address: '123 Main St', city: 'New York', state: 'NY' }); | ||
await order.attach(shippingAddress, 'shippingAddress', { exclude: ['created_at', 'updated_at'] }); | ||
console.log('Shipping address created:', shippingAddress); | ||
}; | ||
createShippingAddress(); | ||
``` | ||
```typescript | ||
import Order from 'path/to/Order'; | ||
import ShippingAddress from 'path/to/ShippingAddress'; | ||
let createShippingAddress = async () => { | ||
var order = await Order.findOne({ where: { id: 1 } }); | ||
var shippingAddress = new ShippingAddress({ address: '123 Main St', city: 'New York', state: 'NY' }); | ||
await order.attach(shippingAddress, 'shippingAddress'); | ||
console.log('Shipping address created:', shippingAddress); | ||
}; | ||
createShippingAddress(); | ||
``` | ||
#### One-to-Many Relationship | ||
@@ -1202,3 +1297,3 @@ | ||
interface OrderAttributes { | ||
id: number; | ||
id?: number; | ||
total: number; | ||
@@ -1209,3 +1304,3 @@ } | ||
class Order extends MappifyModel { | ||
id: number; | ||
id?: number; | ||
total: number; | ||
@@ -1219,6 +1314,5 @@ | ||
// other methods here... | ||
super.associations() { | ||
super.belongsTo(User, { | ||
associations() { | ||
this.belongsTo(User, { | ||
as: 'user', | ||
@@ -1249,2 +1343,35 @@ key: 'id' | ||
** Using `attach` method ** | ||
```javascript | ||
const User = require('path/to/User'); | ||
const Order = require('path/to/Order'); | ||
let createOrder = async () => { | ||
var user = await User.findById(1); | ||
var order = new Order({ total: 1000 }); | ||
await user.attach(order, 'orders'); | ||
console.log('Order created:', order); | ||
}; | ||
createOrder(); | ||
``` | ||
```typescript | ||
import User from 'path/to/User'; | ||
import Order from 'path/to/Order'; | ||
let createOrder = async () => { | ||
var user = await User.findOne({ where: { id: 1 } }); | ||
var order = new Order({ total: 1000 }); | ||
await user.attach(order, 'orders'); | ||
console.log('Order created:', order); | ||
}; | ||
createOrder(); | ||
``` | ||
#### Many-to-Many Relationship | ||
@@ -1274,3 +1401,2 @@ | ||
``` | ||
Usage: | ||
@@ -1290,2 +1416,4 @@ ```javascript | ||
** Using TypeScript ** | ||
```typescript | ||
@@ -1297,3 +1425,3 @@ import { MappifyModel } from 'mappifysql'; | ||
interface CategoryAttributes { | ||
id: number; | ||
id?: number; | ||
name: string; | ||
@@ -1303,3 +1431,3 @@ } | ||
class Category extends MappifyModel { | ||
id: number; | ||
id?: number; | ||
name: string; | ||
@@ -1313,6 +1441,6 @@ | ||
// other methods here... | ||
super.associations() { | ||
super.belongsToMany(Product, { | ||
associations() { | ||
this.belongsToMany(Product, { | ||
as: 'products', | ||
@@ -1395,3 +1523,3 @@ through: ProductCategory, | ||
interface EnrollmentAttributes { | ||
id: number; | ||
id?: number; | ||
student_id: number; | ||
@@ -1402,3 +1530,3 @@ course_id: number; | ||
class Enrollment extends MappifyModel { | ||
id: number; | ||
id?: number; | ||
student_id: number; | ||
@@ -1414,6 +1542,5 @@ course_id: number; | ||
// other methods here... | ||
super.associations() { | ||
super.belongsTo(Student, { | ||
associations() { | ||
this.belongsTo(Student, { | ||
as: 'student', | ||
@@ -1423,3 +1550,3 @@ key: 'id', | ||
}); | ||
super.belongsTo(Course, { | ||
this.belongsTo(Course, { | ||
as: 'course', | ||
@@ -1484,1 +1611,4 @@ key: 'id', | ||
<!-- - [MappifySQL Open Source Community]( --> | ||
<a href="https://www.buymeacoffee.com/walidadebayo"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=walidadebayo&button_colour=FFDD00&font_colour=000000&font_family=Comic&outline_colour=000000&coffee_colour=ffffff" /></a> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122304
1414
1569
3