query-orm
Loopback like ORM built to work with any node.js framework.
Installation
npm i query-orm
Usage
app.js
const QueryORM = require('query-orm');
const app = new QueryORM({
appRoot: DIRNAME,
modelConfig: './model-config.json',
dataSources: './datasource.json'
});
app.on('model-init', (data) => {
console.log(data);
});
app.on('error', (error) => {
console.log(error);
});
async function createUser (data) {
const createdUser = await app.models.User.create(data);
return createdUser;
}
createUser({
firstname: 'Bharath',
lastname: 'Reddy',
username: 'bharathreddy',
password: 'Test@1234'
}).then((createdUser) => {
console.log(createdUser)
}).catch((error) => {
console.log(error);
});
app.models.User.find({
where: {
username: 'bharathreddy'
}
}).then((result) => {
console.log(result)
}).catch((error) => {
console.log(error);
});
datasources.json
{
"testdatasource": {
"name": "testdatasource",
"settings": {
"indexName": "testindex",
"isAlias": false,
"isPattern": false,
"mappings": {
"properties": {
"docType": {
"type": "keyword"
},
"userId": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"firstname": {
"type": "keyword"
},
"lastname": {
"type": "keyword"
},
"password": {
"type": "keyword"
},
"created": {
"type": "date"
},
"updated": {
"type": "date"
}
}
},
"aliases": {},
"indexSettings": {
"number_of_shards": 1,
"number_of_replicas": 2
},
"createIndex": true,
"updateMapping": true,
"version": 7,
"defaultLimit": 200,
"configuration": {
"nodes": [
"https://localhost:9200"
],
"requestTimeout": 30000,
"pingTimeout": 3000,
"auth": {
"username": "admin",
"password": "admin"
},
"agent": {
"maxSockets": 1,
"keepAlive": false
},
"ssl": {
"rejectUnauthorized": false
},
"sniffInterval": 10000
}
},
"connector": "query-orm-connector-elastic",
"localConnector": false
}
}
model-config.json
{
"directory": "./models",
"models": {
"User": {
"dataSource": "testdatasource"
}
}
}
models/user.json
{
"name": "User",
"plural": "users",
"validations": {},
"id": {
"property": "userId",
"generated": true
},
"schema": {
"type": "object",
"properties": {
"userId": {
"type": "string"
},
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"created": {
"type": "string",
"format": "date-time"
},
"updated": {
"type": "string",
"format": "date-time"
}
},
"required": [
"firstname",
"lastname",
"userId",
"username",
"password"
]
},
"relations": {}
}
Model Methods
find
This method is for fetching data from Model's dataSource based on given query filter.
Example:
await app.models.User.find({
where: {
firstname: 'bharath'
},
limit: 100,
skip: 10
})
findOne
This method is for fetching only one matched record from Model's dataSource based on given query filter.
limit and skip options will not work here.
Example:
await app.models.User.findOne({
where: {
firstname: 'bharath'
}
})
findById
This method is for fetching the record from Model's dataSource based on given id parameter.
limit and skip options will not work here.
Example:
await app.models.User.findById('id123');
count
This method is for fetching the count from Model's dataSource based on given query filter.
limit and skip options will not work here.
Example:
await app.models.User.count({
firstname: 'bharath'
});
create
This method is for create a record in Model.
Example:
await app.models.User.create({
firstname: 'bharath'
});
updateById
This method is for updating a record matching the given id with attributes object provided.
Example:
await app.models.User.updateById('id123', {
firstname: 'bharath'
});
updateByQuery
This method is for updating multiple records matched for given query with attributes object provided.
Example:
await app.models.User.updateByQuery({
and: [{
lastname: 'reddy'
}, {
firstname: 'test'
}]
}, {
firstname: 'bharath'
});
deleteById
This method is for deleting a record that matches the given id.
Example:
await app.models.User.deleteById('id123');
deleteByQuery
This method is for deleting multiple records that matches the given query.
Example:
await app.models.User.updateByQuery({
and: [{
lastname: 'reddy'
}, {
firstname: 'test'
}]
});
Querying Data
Base Filters
{
"where": {},
"limit": 100,
"skip": 2,
"searchafter": [],
"order": [],
"fields": [],
"include": [],
}
where is the main query attribute.
It supports and and or queries.
Relations
Connectors
OperationalHooks
before save
This hook will be triggered before execute of methods (create, updateById and updateByQuery).
Example:
Model.observe('before save', async (instance) => {
instance.foo = 'bar1';
return Promise.resolve();
})
Model.observe('before save', async (data) => {
return Promise.resolve();
})
after save
This hook will be triggered after execute of methods (create, updateById and updateByQuery).
Example:
Model.observe('after save', async (ctx) => {
return Promise.resolve();
})
before delete
This hook will be triggered before execute of methods (deleteById and deleteByQuery).
Example:
Model.observe('before delete', async (data) => {
return Promise.resolve();
})
after delete
This hook will be triggered after execute of methods (deleteById and deleteByQuery).
Example:
Model.observe('after delete', (ctx) => {
return Promise.resolve();
})