-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
165 lines (132 loc) · 4.65 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const { callbackify } = require('util')
const Redis = require('ioredis')
const createManager = require('./lib/create-manager')
const RedisSchema = require('./lib/redis-schema')
const registeredDatastores = {}
const withDatastore = (fn) => callbackify((datastoreName, ...restParams) => {
const datastore = registeredDatastores[datastoreName]
if (datastore === undefined) {
throw new Error(`Datastore ('${datastoreName}') is not currently registered with this adapter.`)
}
return fn(datastore, ...restParams)
})
module.exports = {
// The identity of this adapter, to be referenced by datastore configurations in a Sails app.
identity: 'sails-redis-schema',
// Waterline Adapter API Version
adapterApiVersion: 1,
// Default datastore configuration.
defaults: {
url: null, // defaults to 127.0.0.1:6379; could be a connection string (e.g.: 'redis://127.0.0.1:6379'), or a path to a socket (e.g.: '/tmp/redis.sock'),
options: {} // any of https://github.com/luin/ioredis/blob/HEAD/API.md#new-redisport-host-options
},
datastores: registeredDatastores,
/**
* Create a manager using the configuration provided, and track it,
* along with the provided config (+a reference to the static driver)
* as an active datastore.
*
* @param {Config} config
* @param {Object} models
* @param {Function} done
*/
registerDatastore: callbackify(async (config, models) => {
// Grab the unique name for this datastore for easy access below.
const { identity } = config
if (!identity) throw new Error('Datastore is missing an identity.')
if (registeredDatastores[identity]) {
throw new Error(`Datastore ('${identity}') has already been registered by sails-redis.`)
}
const options = config.url
? { url: config.url, ...config.options }
: config.options
const manager = await createManager(options)
const schemas = {}
Object.values(models).forEach((schema) => {
schemas[schema.tableName] = new RedisSchema(schema, manager)
})
const datastore = {
driver: Redis,
config,
manager,
models,
schemas
}
registeredDatastores[identity] = datastore
return datastore
}),
/**
* Unregister the specified datastore, so that is no longer considered active,
* and its manager is destroyed (& thus all of its live db connections are released.)
*
* @param {String} identity
* @param {Function} done
*/
teardown: withDatastore((datastore) => {
delete registeredDatastores[datastore.config.identity]
return datastore.manager.quit()
}),
/**
* Create a new record.
*/
create: withDatastore(async (datastore, query) => {
const schema = datastore.schemas[query.using]
const record = await schema.create(query.newRecord)
if (!!query.meta && query.meta.fetch === true) return record
}),
/**
* Update matching records.
*/
update: withDatastore(async (datastore, query) => {
const schema = datastore.schemas[query.using]
const { valuesToSet } = query
const { where } = query.criteria
const ids = await schema.fetchIds(where)
await schema.updateByIds(ids, valuesToSet)
return !!query.meta && query.meta.fetch === true
? schema.findByIds(ids)
: undefined
}),
/**
* Destroy one or more records.
*/
destroy: withDatastore(async (datastore, query) => {
const schema = datastore.schemas[query.using]
const { where } = query.criteria
const ids = await schema.fetchIds(where)
const records = await schema.destroyByIds(ids)
if (!!query.meta && query.meta.fetch === true) return records
}),
/**
* Find matching records.
*/
find: withDatastore(async (datastore, query) => {
const schema = datastore.schemas[query.using]
if (query.method !== 'find') {
throw new Error(`Query method of type "${query.method}" not compatible.`)
}
return schema.find(query.criteria)
}),
/**
* Get the number of matching records.
*/
count: withDatastore(async (datastore, query) => {
const schema = datastore.schemas[query.using]
const { where } = query.criteria
const ids = await schema.fetchIds(where)
return schema.count(ids)
}),
/**
* Build a new physical model (e.g. table/etc) to use for storing records in the database.
* We actually don't have to do nothing on redis.
*/
define: (datastoreName, tableName, phmDef, done) => done(null),
/**
* Drop a physical model (table/etc.) from the database, including all of its records.
* (This is used for schema migrations.)
*/
drop: withDatastore(async (datastore, tableName) => {
const schema = datastore.schemas[tableName]
return schema.drop()
})
}