Database
A secure client library for MongoDB & MySQL API
Install
npm i @appstitch/databaseappstitch_database: ^1.0.0Install peer dependencies
npm i @appstitch/coreappstitch_core: ^2.0.0Usage
API
Method
Type
Description
Platform
collection
Collection Name
Interchangeable with table
MongoDB & MySQL
table
Table Name
Interchangeable with collection
MongoDB & MySQL
id
ID, FieldName (Optional)
Set the ID for a specific document/record. FieldName is only required for MySQL
MongoDB & MySQL
where
Field, OperatorType, Value
A query object used to filter documents
MongoDB & MySQL
limit
number
Limits the number of documents. Default 50
MongoDB & MySQL
include
Field Array
Return specific fields.
MongoDB & MySQL
exclude
Field Array
Prevent specific fields from being returned
MongoDB & MySQL
startAfter
number
Skips the first n documents/records
MongoDB & MySQL
orderBy
Field, Direction
Specified order for results
MongoDB & MySQL
join
Source Field, Destination Table, Destination Field, JoinType
Used to combine rows from two or more tables
MySQL
Read Operations
Get by ID
// MongoDB
db.collection("Users")
.id("7b5f87cfb6ce44f98c82a1c6")
.then(result => {
if(result.success)
{
const user = result.doc;
}
else
{
// handle error
}
})
// MySQL
db.table("Users")
.id("7b5f87cfb6ce44f98c82a1c6", "UserID")
.then(result => {
if(result.success)
{
const user = result.doc;
}
else
{
// handle error
}
})// MongoDB
final result = await db
.collection("Users")
.id("7b5f87cfb6ce44f98c82a1c6")
.fetch();
if (result.success!) {
user = User.fromJson(result.doc!);
} else {
// handle error
}
// MySQL
final result = await db
.table("Users")
.id("7b5f87cfb6ce44f98c82a1c6", "UserID")
.fetch();
if (result.success!) {
user = User.fromJson(result.doc!);
} else {
// handle error
}Get multiple documents
// MongoDB
db.collection("Users")
.where("City", "==", "NewYork")
.then(result => {
if(result.success)
{
const user = result.doc;
}
else
{
// handle error
}
})final result = await db
.collection("Users")
.where("City", OperatorType.equal, "NewYork")
.limit(25)
.fetch();
if (result.success!) {
user = User.fromJson(result.doc!);
} else {
// handle error
}Write Operations
Insert
db.collection("Users")
.insert({name : "Bruce", city : "Gotham"})
.then(result => {
if (result.success) {
const userID = result.id;
} else {
// handle error
}
})final user = User(
name: "Clark K",
city: "Metropolis")
final result = await db
.collection("users")
.insert(user);
if (result.success!) {
final userID = result.id!;
} else {
// handle error
}Update
const result = db
.collection("users")
.doc(userID)
.update({city : "New York"})
.then(result => {
if (result.success) {
// success
} else {
// handle error
}
})
final result = await db
.collection("users")
.doc(userID)
.update(user.toJson());
if (result.success!) {
print("Successfully Updated");
} else {
// handle error
}Delete
const result = db
.collection("users")
.doc(userID)
.update({city : "New York"})
.then(result => {
if (result.success) {
// success
} else {
// handle error
}
}) final result = await db
.collection("users")
.id(userID)
.delete();
if (result.success!) {
// success
} else {
// handle error
}Algolia Integration
Appstitch can be used to keep your Algolia indices in sync. Just pass syncData with any write operation.
Algolia example
db.collection("Users")
.insert(
{name : "Bruce", city : "Gotham"},
{syncData : true} // <-- User will be created in Algolia
)
.then(result => {
if (result.success) {
const userID = result.id;
} else {
// handle error
}
})const opts = WriteOptions(syncData: true)
final result = await db
.collection("users")
.insert(
user,
opts // <-- User will be created in Algolia
);
if (result.success!) {
final userID = result.id!;
} else {
// handle error
}Last updated
Was this helpful?