![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@odyssoft/tsorm
Advanced tools
TypeScript ORM for MySQL with node.js
npm install @odyssoft/tsorm
import { Schema } from '@odyssoft/tsorm'
export const mySchema = new Schema('my_schema', {
host: 'localhost',
password: 'password',
port: 3306,
user: 'root',
create: true, // optional for when you want to create the schema and tables. (creating schemas and tables every server (re)start is not necessary)
})
import { DataTypes } from '@odyssoft/tsorm'
import mySchema from '../schema'
export interface IUser {
userId?: number
username: string
email: string
password: string
}
export const User = mockSchema.createModel<IUser>('user', {
userId: {
primaryKey: true,
autoIncrement: true,
type: DataTypes.int,
},
username: {
type: DataTypes.varchar(40),
required: true,
unique: true,
},
email: {
type: DataTypes.varchar(310),
required: true,
unique: true,
},
password: {
type: DataTypes.varchar(500),
required: true,
},
})
import { Request, Response, Router } from 'express'
import { User } from '../database'
const router = Router()
const UserRoutes = () => {
router.get('/:id', ({ params: { id } }: Request, res: Response) =>
User.find({ $where: { userId: id } })
.then((user) => res.json(user))
.catch((error: any) => res.json({ error }))
)
router.post('/', ({ body: { name, email, password } }: Request, res: Response) =>
User.create({ name, email, password: encryptPassword(password) })
.then((user) => res.json(user))
.catch((error: any) => res.json({ error }))
)
return router
}
export default UserRoutes
import { DataTypes } from '@odyssoft/tsorm'
import { User, Post } from '../models'
import mySchema from '../schema'
export interface IUserPost {
userId: number
username: string
postId: number
title: string
}
export const UserPost = mockSchema.createView<IUserPost>(
'userpost',
['userId', 'username', 'postId', 'title'],
User.as('U')
.join(Post.as('P'), 'U.userId', 'P.userId')
.select('U.userId', 'U.username', 'P.postId', 'P.title')
)
import { OkPacket, FieldPacket } from 'mysql2/promise'
import { User } from '../database/models/user'
const insertResult = User.insert({
email: 'example@example.com',
password: 'example',
username: 'example',
}).then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const insertMultipleResult = User.insert([
{
email: 'example@example.com',
password: 'example',
username: 'example',
},
{
email: 'example2@example.com',
password: 'another_example',
username: 'random',
},
]).then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const createResult = User.create({
email: 'example@example.com',
password: 'example',
username: 'example',
}).then((user) => {
// do something with user
})
const createManyResult = User.createMany([
{
email: 'example@example.com',
password: 'example',
username: 'example',
},
{
email: 'example2@example.com',
password: 'another_example',
username: 'random',
},
]).then((users) => {
// do something with users
})
const deleteResult = User.delete({
userId: 1,
}).then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const deleteByResult = User.deleteBy('email', {
$like: '%example.com',
}).then((count) => {
// do something with count
})
const deleteByIdResult = User.deleteById(1).then((deleted: boolean) => {
// do something if user was deleted
})
const deleteOneResult = User.deleteOne({
userId: {
$between: {
min: 1,
max: 3,
},
},
}).then((deleted: boolean) => {
// do something if user was deleted
})
const deleteOneByResult = User.deleteOneBy('username', 'random').then((deleted: boolean) => {
// do something if user was deleted
})
const findAllResult = User.find().then((users) => {
// do something with users
})
const findResult = User.find({
email: {
$like: '%example.com',
},
}).then((users) => {
// do something with users
})
const findByResult = User.findBy('username', 'random').then((users) => {
// do something with users
})
const findByIdResult = User.findById(2).then((user) => {
// do something with user
})
const findOneResult = User.findOne({
username: 'random',
}).then((user) => {
// do something with user
})
const findOneByResult = User.findOneBy('email', {
$like: '%example.com',
}).then((user) => {
// do something with user
})
const selectAllResult = User.select().then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const selectResult = User.select({
$columns: ['username', 'email'],
}).then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const selectColumnsResult = User.select().then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const truncateResult = User.truncate().then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const updateResult = User.update(
{
email: 'updated@example.com',
},
{
userId: 1,
}
).then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const upsertResult = User.upsert({
email: 'example@example.com',
password: 'example',
username: 'example',
}).then(([okPacket, fieldPacketArray]) => {
// handle mysql2 result
})
const upsertOneResult = User.upsertOne({
email: 'example@example.com',
password: 'example',
username: 'example',
}).then((result: boolean) => {
// do something on result
})
const upsertManyResult = User.upsertMany([
{
email: 'example@example.com',
password: 'example',
username: 'example',
},
{
email: 'example2@example.com',
password: 'another_example',
username: 'random',
},
]).then((result) => {
// do something on result
})
Most of the methods accept a where clause as the first parameter. The where clause is an object that contains the conditions for the query. The keys of the object are the column names and the values are the conditions. The conditions can be a simple value or an object with a single key that is the operator and the value is the value to compare.
const whereExample = User.find({
email: {
$like: '%example%',
},
userId: {
$lessThanEqual: 4,
},
username: {
$in: ['example', 'random', 'another'],
},
})
const orExample = User.find({
$or: [
{
userId: {
$between: {
min: 1,
max: 4,
},
},
},
{
userId: 19,
},
],
})
const joinExample = User.as('u')
.join(Post.as('p'), 'INNER', {
'u.userId': 'p.userId',
})
.select(query)
// Where query is an optional object containing selected columns and or a where clause
// An example for the above join select query using both columns and where clause
const query = {
$columns: ['u.userId', 'u.username', 'p.postId', 'p.content'],
$where: {
'u.userId': 1,
'p.postId': 1,
},
}
The above query would result in the following sql query
SELECT
u.userId,
u.username,
p.postId,
p.content
FROM
`user` AS u
INNER JOIN
`post` AS p
ON
u.userId = p.userId
WHERE
u.userId = 1
AND
p.postId = 1
Group by, order by, limit and offset can be added to the query on the select methods.
User.select({
$columns: [...],
$where: {...},
// $groupBy can be an array or a string
$groupBy: ['username'],
// $orderBy can be an array or a string with optional appended ASC or DESC
$orderBy: ['username'],
// $limit can be a string or an array where the array is [offset, limit]
$limit: 10,
})
FAQs
TypeScript ORM for MySQL
We found that @odyssoft/tsorm 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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.