├── .gitignore
├── .idea
├── .gitignore
├── vcs.xml
├── misc.xml
├── jsLibraryMappings.xml
├── modules.xml
├── azureSettings.xml
└── $PRODUCT_WORKSPACE_FILE$
├── .gitpod.yml
├── example-nodejs.iml
├── package.json
├── README.md
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/.gitpod.yml:
--------------------------------------------------------------------------------
1 | tasks:
2 | - init: npm install
3 | command: npm start
4 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/jsLibraryMappings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/azureSettings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/example-nodejs.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "example-nodejs",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "start": "node index.js"
8 | },
9 | "keywords": [],
10 | "author": "",
11 | "license": "ISC",
12 | "dependencies": {
13 | "axios": "^0.19.1",
14 | "inquirer": "^7.0.4",
15 | "rxjs": "^6.5.4",
16 | "sapphiredb": "^2.1.1",
17 | "ws": "^7.2.1"
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/.idea/$PRODUCT_WORKSPACE_FILE$:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | 1.8
8 |
9 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SapphireDb: Example-NodeJs (using sapphiredb)
2 |
3 | **Example NodeJs application using SapphireDb**
4 |
5 | This is a demo project for SapphireDb. It should demonstrate how to use SapphireDb with different technologies.
6 |
7 | The examples contains a simple CRUD demo and a simple chat.
8 |
9 | You don't need to set up a server running SapphireDb to try it out.
10 | A demo server can be reached at the following address: https://sapphiredb-demo.azurewebsites.net/
11 |
12 | ## Try it out
13 |
14 | You can try out this example by following these steps:
15 |
16 | 1. Clone the sources and switch to the directory
17 | 2. Run `npm install`
18 | 3. Run `npm start`
19 |
20 | ## Other examples
21 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var sapphiredb = require('sapphiredb');
2 | var operators = require('rxjs/operators');
3 | var inquirer = require('inquirer');
4 | var ws = require('ws');
5 | WebSocket = ws;
6 |
7 | var exampleCollection;
8 |
9 | var db = new sapphiredb.SapphireDb({
10 | serverBaseUrl: 'sapphiredb-demo.azurewebsites.net',
11 | useSsl: true
12 | });
13 |
14 | async function selectAction() {
15 | var nextAction = await inquirer.prompt([
16 | {
17 | type: 'list',
18 | name: 'action',
19 | message: 'What do you want to do?',
20 | choices: ['Add value', 'Update value', 'Remove value', 'Exit']
21 | }
22 | ]);
23 |
24 | if (nextAction.action === 'Add value') {
25 | var newValue = await inquirer.prompt([
26 | {
27 | type: 'input',
28 | name: 'content',
29 | message: 'New content:'
30 | }
31 | ]);
32 |
33 | exampleCollection.add(newValue);
34 | } else if (nextAction.action === 'Update value') {
35 | var value = await inquirer.prompt([
36 | {
37 | type: 'number',
38 | name: 'id',
39 | message: 'Id:'
40 | },
41 | {
42 | type: 'input',
43 | name: 'content',
44 | message: 'New content:'
45 | }
46 | ]);
47 |
48 | exampleCollection.update(value);
49 | } else if (nextAction.action === 'Remove value') {
50 | var value = await inquirer.prompt([
51 | {
52 | type: 'number',
53 | name: 'id',
54 | message: 'Id:'
55 | }
56 | ]);
57 |
58 | exampleCollection.remove(value);
59 | } else {
60 | process.exit();
61 | return;
62 | }
63 |
64 | selectAction();
65 | }
66 |
67 | function basic() {
68 | exampleCollection = db.collection('basic.examples');
69 | var examples$ = exampleCollection.values();
70 | var examplesSubscription = examples$.subscribe(function (examples) {
71 | console.clear();
72 | console.table(examples);
73 | });
74 |
75 | selectAction();
76 | }
77 |
78 | var messageCollection;
79 |
80 | async function chatMain(currentUser, chatPartner) {
81 | var newValue = await inquirer.prompt([
82 | {
83 | type: 'input',
84 | name: 'content',
85 | message: 'New message:'
86 | }
87 | ]);
88 | messageCollection.add({
89 | content: newValue.content,
90 | ownerId: currentUser.id,
91 | receiverId: chatPartner.id
92 | });
93 | chatMain(currentUser, chatPartner);
94 | }
95 |
96 | async function chat() {
97 | var users = await db.collection('chat.users').values().pipe(operators.take(1)).toPromise();
98 |
99 | var currentUser = (await inquirer.prompt([
100 | {
101 | type: 'list',
102 | name: 'user',
103 | message: 'Select a username:',
104 | choices: users.map(function (value) {
105 | value.value = JSON.parse(JSON.stringify(value));
106 | value.name = value.username;
107 | return value;
108 | })
109 | }
110 | ])).user;
111 |
112 | var chatPartner = (await inquirer.prompt([
113 | {
114 | type: 'list',
115 | name: 'user',
116 | message: 'Select a username:',
117 | choices: users.filter(function (user) {
118 | return user.id !== currentUser.id;
119 | }).map(function (value) {
120 | value.value = JSON.parse(JSON.stringify(value));
121 | value.name = value.username;
122 | return value;
123 | })
124 | }
125 | ])).user;
126 |
127 | messageCollection = db.collection('chat.messages').where([
128 | [['ownerId', '==', currentUser.id], 'and', ['receiverId', '==', chatPartner.id]],
129 | 'or',
130 | [['ownerId', '==', chatPartner.id], 'and', ['receiverId', '==', currentUser.id]]
131 | ]);
132 |
133 | var messages$ = messageCollection.values();
134 | var messagesSubscription = messages$.subscribe(function (messages) {
135 | console.clear();
136 | console.table(messages.map(function (value) {
137 | return {
138 | received: currentUser.id !== value.ownerId ? value.content : null,
139 | sent: currentUser.id === value.ownerId ? value.content : null,
140 | date: value.createdOn
141 | };
142 | }));
143 | });
144 |
145 | chatMain(currentUser, chatPartner);
146 | }
147 |
148 | async function main() {
149 | var mode = await inquirer.prompt([
150 | {
151 | type: 'list',
152 | name: 'mode',
153 | message: 'What mode do you want to use?',
154 | choices: ['Basic', 'Chat']
155 | }
156 | ]);
157 |
158 | if (mode.mode === 'Basic') {
159 | basic();
160 | } else {
161 | chat();
162 | }
163 | }
164 | main();
165 |
--------------------------------------------------------------------------------