-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (37 loc) · 1.51 KB
/
index.js
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
require('dotenv').config();
const mongoose = require('mongoose');
const mongoolia = require('mongoolia').default;
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', (error) => console.error(`Failed to connect to ${process.env.MONGODB_URI}`));
db.once('open', () => console.log(`Connected to ${process.env.MONGODB_URI}`));
// Pass `{algoliaIndex: true}` to push theses attributes for indexing to Algolia
const BookSchema = new mongoose.Schema({
title: { type: String, required: true, algoliaIndex: true },
author: { type: String, required: true, algoliaIndex: true },
description: { type: String, required: true, algoliaIndex: true }
});
// Specify your Algolia credentials which you can find into your dashboard
BookSchema.plugin(mongoolia, {
appId: process.env.ALGOLIA_APP_ID,
apiKey: process.env.ALGOLIA_KEY,
indexName: process.env.ALGOLIA_INDEX_NAME,
});
const Book = mongoose.model('Book', BookSchema);
const book = new Book({
title: 'title',
author: 'author',
description: 'description',
});
book.save().then(() => {
console.log('book saved OK');
Book.syncWithAlgolia().then(
() => console.log('All synced with algolia')
).catch(
error => console.error(`Failed to sync with algolia: ${error.message}`)
);
}).catch(error => console.error(`Failed to save book: ${error.message}`));
console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));