-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (140 loc) · 6.03 KB
/
app.js
File metadata and controls
140 lines (140 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// ===============================================================================
// Alachisoft (R) NosDB Sample Code.
// ===============================================================================
// Copyright © Alachisoft. All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
// ===============================================================================
var nosDBClient = require('nosdb').nosDBClient;
var connStr = require('./configuration').connectionString;
// Get database using the connection string specified in configuration.js
nosDBClient.getDatabase(connStr, function (dbErr, db) {
if (dbErr) {
console.log(dbErr);
}
else {
// Get instance of customers collection
db.getCollection("Customers", function (collErr, customersCollection) {
if (collErr) {
console.log(collErr);
}
else {
// Insert customer in customers collection
insertCustomer(db, function (err, num) {
if (err) {
console.log(err);
}
else {
console.log("\n'" + num + "' record(s) inserted!");
}
// Fetch customer from customers collection
getCustomer(db, function (err, reader) {
if (err) {
console.log(err);
}
else {
reader.toArray(function (readerErr, array) {
if (readerErr) {
console.log(readerErr);
}
else if (array[0]) {
var customerId = array[0].CustomerID;
var companyName = array[0].CompanyName;
var address = array[0].Address;
var city = array[0].City;
console.log("'" + array.length + "' record(s) retrieved");
}
else {
console.log("No document is retrieved against 'Customer1'.");
}
});
}
// Update customer in customers collection
updateCustomer(db, function (err, num) {
if (err) {
console.log(err);
}
else {
console.log("'" + num + "' record(s) were updated!");
}
// Delete customer from customers collection
deleteCustomer(db, function (err, num) {
if (err) {
console.log(err);
}
else {
console.log("'" + num + "' record(s) deleted!");
}
// Release resources
db.dispose();
});
});
});
});
}
});
}
});
/**
* This method Inserts customer in the collection
* @param {dbCollection} coll collection in which customer will be inserted
* @param {callback} callback callback that will be called after the operation
*/
var insertCustomer = function (db, callback) {
var command = {
query: 'INSERT INTO Customers (CustomerID, CompanyName, Address, City) VALUES (@customerId, @customerName,@customerAddress,@customerCity)',
parameters: [
{ name: 'customerId', value: "Customer1" },
{ name: 'customerName', value: "John Doe" },
{ name: 'customerAddress', value: "Obere Street 57" },
{ name: 'customerCity', value: "Berlin" }
]
};
db.executeNonQuery(command, callback);
};
/**
* This method fetches customer from the collection
* @param {dbCollection} coll collection from which customer will be fetched
* @param {callback} callback callback that will be called after the operation
*/
var getCustomer = function (db, callback) {
var getQueryCmd = {
query: 'SELECT * FROM Customers WHERE CustomerID = @customerId',
parameters: [{ name: 'customerId', value: "Customer1" }]
};
db.executeReader(getQueryCmd, callback);
};
/**
* This method updates customer in the collection
* @param {dbCollection} coll collection in which customer will be updated (replaced)
* @param {callback} callback callback that will be called after the operation
*/
var updateCustomer = function (db, callback) {
var command = {
query: 'UPDATE Customers SET (Address = @customerAddress, City = @customerCity,CompanyName = @customerName) WHERE CustomerID = @customerId',
parameters: [
{ name: 'customerAddress', value: "120 Hanover Sq." },
{ name: 'customerCity', value: "London" },
{ name: 'customerName', value: "John Doe" },
{ name: 'customerId', value: "Customer1" }
]
};
db.executeNonQuery(command, callback);
};
/**
* This method deletes customer from the collection
* @param {dbCollection} coll collection from which customer will be deleted
* @param {callback} callback callback that will be called after the operation
*/
var deleteCustomer = function (db, callback) {
var command = {
query: 'DELETE FROM Customers WHERE CustomerID = @customerId',
parameters: [
{ name: 'customerId', value: "Customer1" }
]
};
db.executeNonQuery(command, callback);
};
//# sourceMappingURL=app.js.map