-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathutils.js
More file actions
69 lines (60 loc) · 1.61 KB
/
utils.js
File metadata and controls
69 lines (60 loc) · 1.61 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
const SQL = require('sequelize');
module.exports.paginateResults = ({
after: cursor,
pageSize = 20,
results,
// can pass in a function to calculate an item's cursor
getCursor = () => null,
}) => {
if (pageSize < 1) return [];
if (!cursor) return results.slice(0, pageSize);
const cursorIndex = results.findIndex(item => {
// if an item has a `cursor` on it, use that, otherwise try to generate one
let itemCursor = item.cursor ? item.cursor : getCursor(item);
// if there's still not a cursor, return false by default
return itemCursor ? cursor === itemCursor : false;
});
return cursorIndex >= 0
? cursorIndex === results.length - 1 // don't let us overflow
? []
: results.slice(
cursorIndex + 1,
Math.min(results.length, cursorIndex + 1 + pageSize),
)
: results.slice(0, pageSize);
};
module.exports.createStore = () => {
const Op = SQL.Op;
const operatorsAliases = {
$in: Op.in,
};
const db = new SQL('database', 'username', 'password', {
dialect: 'sqlite',
storage: './store.sqlite',
operatorsAliases: 0,
logging: false,
});
const users = db.define('user', {
id: {
type: SQL.INTEGER,
primaryKey: true,
autoIncrement: true,
},
createdAt: SQL.DATE,
updatedAt: SQL.DATE,
email: SQL.STRING,
token: SQL.STRING,
});
const trips = db.define('trip', {
id: {
type: SQL.INTEGER,
primaryKey: true,
autoIncrement: true,
},
createdAt: SQL.DATE,
updatedAt: SQL.DATE,
launchId: SQL.INTEGER,
userId: SQL.INTEGER,
});
return { users, trips };
};