@1auth/store-sql 0.0.0-alpha.60 → 0.0.0-alpha.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/index.js +0 -213
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1auth/store-sql",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.63",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"repository": {
|
|
43
43
|
"type": "git",
|
|
44
|
-
"url": "github
|
|
44
|
+
"url": "git+https://github.com/willfarrell/1auth.git",
|
|
45
45
|
"directory": "packages/store-dynamodb"
|
|
46
46
|
},
|
|
47
47
|
"bugs": {
|
package/index.js
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
const options = {
|
|
2
|
-
log: false,
|
|
3
|
-
query: undefined, // async (sql, parameters) => {}
|
|
4
|
-
// number of seconds after expire before removal
|
|
5
|
-
// 10d chosen based on EFF DNT Policy
|
|
6
|
-
timeToLiveExpireOffset: 10 * 24 * 60 * 60,
|
|
7
|
-
timeToLiveKey: 'remove',
|
|
8
|
-
// mode:
|
|
9
|
-
// ?: use `?` for variables, ie sqlite
|
|
10
|
-
// $: use `$1` for variables, ie postgres
|
|
11
|
-
placeholder: '?'
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default (params) => {
|
|
15
|
-
Object.assign(options, params)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const exists = async (table, filters) => {
|
|
19
|
-
if (options.log) {
|
|
20
|
-
options.log('@1auth/store-sql exists(', table, filters, ')')
|
|
21
|
-
}
|
|
22
|
-
const { select, where, parameters } = makeSqlParts(filters, {}, ['sub'])
|
|
23
|
-
const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
|
|
24
|
-
return await options.query(sql, parameters).then((res) => res?.[0]?.sub)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const count = async (table, filters = {}) => {
|
|
28
|
-
if (options.log) {
|
|
29
|
-
options.log('@1auth/store-sql count(', table, filters, ')')
|
|
30
|
-
}
|
|
31
|
-
const { where, parameters } = makeSqlParts(filters, {})
|
|
32
|
-
const sql = `SELECT COUNT(*) AS count FROM ${table} ${where}`
|
|
33
|
-
return await options.query(sql, parameters).then((res) => res[0].count)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const select = async (table, filters = {}, fields = []) => {
|
|
37
|
-
if (options.log) {
|
|
38
|
-
options.log('@1auth/store-sql select(', table, filters, fields, ')')
|
|
39
|
-
}
|
|
40
|
-
const { select, where, parameters } = makeSqlParts(filters, {}, fields)
|
|
41
|
-
const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
|
|
42
|
-
return await options
|
|
43
|
-
.query(sql, parameters)
|
|
44
|
-
.then((res) => res?.[0])
|
|
45
|
-
// Workaround because an expire filter doesn't exists yet'
|
|
46
|
-
.then((row) => {
|
|
47
|
-
parseValues(row)
|
|
48
|
-
return row
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export const selectList = async (table, filters = {}, fields = []) => {
|
|
53
|
-
if (options.log) {
|
|
54
|
-
options.log('@1auth/store-sql selectList(', table, filters, fields, ')')
|
|
55
|
-
}
|
|
56
|
-
const { select, where, parameters } = makeSqlParts(filters, {}, fields)
|
|
57
|
-
const sql = `SELECT ${select} FROM ${table} ${where}`
|
|
58
|
-
return await options
|
|
59
|
-
.query(sql, parameters)
|
|
60
|
-
// Workaround because an expire filter doesn't exists yet'
|
|
61
|
-
.then((rows) => {
|
|
62
|
-
return rows.map((row) => {
|
|
63
|
-
parseValues(row)
|
|
64
|
-
return row
|
|
65
|
-
})
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export const insert = async (table, values = {}) => {
|
|
70
|
-
if (options.log) {
|
|
71
|
-
options.log('@1auth/store-sql insert(', table, values, ')')
|
|
72
|
-
}
|
|
73
|
-
if (values.expire && options.timeToLiveKey) {
|
|
74
|
-
values[options.timeToLiveKey] =
|
|
75
|
-
values.expire + options.timeToLiveExpireOffset
|
|
76
|
-
}
|
|
77
|
-
values = structuredClone(values)
|
|
78
|
-
normalizeValues(values)
|
|
79
|
-
const { insert, parameters } = makeSqlParts({}, values)
|
|
80
|
-
const sql = `INSERT INTO ${table} ${insert} RETURNING id`
|
|
81
|
-
const res = await options.query(sql, parameters)
|
|
82
|
-
return res.id
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const insertList = async (table, rows = []) => {
|
|
86
|
-
if (options.log) {
|
|
87
|
-
options.log('@1auth/store-sql insertList(', table, rows, ')')
|
|
88
|
-
}
|
|
89
|
-
const insertValues = []
|
|
90
|
-
let insertParameters = []
|
|
91
|
-
for (let i = 0, l = rows.length; i < l; i++) {
|
|
92
|
-
const values = structuredClone(rows[i])
|
|
93
|
-
normalizeValues(values)
|
|
94
|
-
const { insert, parameters } = makeSqlParts(
|
|
95
|
-
{},
|
|
96
|
-
values,
|
|
97
|
-
[],
|
|
98
|
-
i * Object.keys(values).length + 1
|
|
99
|
-
)
|
|
100
|
-
if (i) {
|
|
101
|
-
insertValues.push(insert.split('VALUES')[1]) // (?)
|
|
102
|
-
} else {
|
|
103
|
-
insertValues.push(insert) // (name) VALUES (?)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
insertParameters = insertParameters.concat(parameters)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const sql = `INSERT INTO ${table} ${insertValues.join(',')} RETURNING id`
|
|
110
|
-
const res = await options.query(sql, insertParameters)
|
|
111
|
-
return res
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export const update = async (table, filters = {}, values = {}) => {
|
|
115
|
-
if (options.log) {
|
|
116
|
-
options.log('@1auth/store-sql update(', table, filters, values, ')')
|
|
117
|
-
}
|
|
118
|
-
values = structuredClone(values)
|
|
119
|
-
normalizeValues(values)
|
|
120
|
-
const { update, where, parameters } = makeSqlParts(filters, values)
|
|
121
|
-
const sql = `UPDATE ${table} SET ${update} ${where}`
|
|
122
|
-
await options.query(sql, parameters)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export const remove = async (table, filters = {}) => {
|
|
126
|
-
if (options.log) {
|
|
127
|
-
options.log('@1auth/store-sql remove(', table, filters, ')')
|
|
128
|
-
}
|
|
129
|
-
const { where, parameters } = makeSqlParts(filters)
|
|
130
|
-
const sql = `DELETE FROM ${table} ${where}`
|
|
131
|
-
await options.query(sql, parameters)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const normalizeValues = (values) => {
|
|
135
|
-
if (!values) return
|
|
136
|
-
values.create &&= new Date(values.create * 1000).toISOString()
|
|
137
|
-
values.update &&= new Date(values.update * 1000).toISOString()
|
|
138
|
-
values.verify &&= new Date(values.verify * 1000).toISOString()
|
|
139
|
-
values.lastused &&= new Date(values.lastused * 1000).toISOString()
|
|
140
|
-
values.expire &&= new Date(values.expire * 1000).toISOString()
|
|
141
|
-
values.remove &&= new Date(values.remove * 1000).toISOString()
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const parseValues = (values) => {
|
|
145
|
-
if (!values) return
|
|
146
|
-
values.create &&= Date.parse(values.create) / 1000
|
|
147
|
-
values.update &&= Date.parse(values.update) / 1000
|
|
148
|
-
values.verify &&= Date.parse(values.verify) / 1000
|
|
149
|
-
values.lastused &&= Date.parse(values.lastused) / 1000
|
|
150
|
-
values.expire &&= Date.parse(values.expire) / 1000
|
|
151
|
-
values.remove &&= Date.parse(values.remove) / 1000
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// export for testing
|
|
155
|
-
export const getPlaceholder = (idx) => {
|
|
156
|
-
return options.placeholder === '$' ? '$' + idx : options.placeholder
|
|
157
|
-
}
|
|
158
|
-
export const makeSqlParts = (
|
|
159
|
-
filters = {},
|
|
160
|
-
values = {},
|
|
161
|
-
fields = [],
|
|
162
|
-
idx = 1
|
|
163
|
-
) => {
|
|
164
|
-
let parameters = []
|
|
165
|
-
const keys = Object.keys(values)
|
|
166
|
-
|
|
167
|
-
const select = fields.length ? '"' + fields.join('", "') + '"' : '*'
|
|
168
|
-
|
|
169
|
-
const insertParts = []
|
|
170
|
-
const updateParts = []
|
|
171
|
-
for (const key of keys) {
|
|
172
|
-
// insertParts.push("$" + idx);
|
|
173
|
-
// updateParts.push('"' + key + '" = $' + idx);
|
|
174
|
-
insertParts.push(getPlaceholder(idx))
|
|
175
|
-
updateParts.push('"' + key + '" = ' + getPlaceholder(idx))
|
|
176
|
-
idx++
|
|
177
|
-
}
|
|
178
|
-
const insert =
|
|
179
|
-
'("' + keys.join('", "') + '") VALUES (' + insertParts.join(',') + ')'
|
|
180
|
-
const update = updateParts.join(', ')
|
|
181
|
-
parameters = parameters.concat(Object.values(values))
|
|
182
|
-
|
|
183
|
-
let where = Object.keys(filters)
|
|
184
|
-
where = where
|
|
185
|
-
.map((key) => {
|
|
186
|
-
if (Array.isArray(filters[key])) {
|
|
187
|
-
let sql = filters[key].map(() => getPlaceholder(idx++)).join(',')
|
|
188
|
-
sql &&= '"' + key + '" IN (' + sql + ')'
|
|
189
|
-
parameters = parameters.concat(filters[key])
|
|
190
|
-
return sql
|
|
191
|
-
}
|
|
192
|
-
// const sql = '"' + key + '" = $' + idx++;
|
|
193
|
-
const sql = '"' + key + '" = ' + getPlaceholder(idx++)
|
|
194
|
-
parameters.push(filters[key])
|
|
195
|
-
return sql
|
|
196
|
-
})
|
|
197
|
-
.filter((v) => v)
|
|
198
|
-
.join(' AND ')
|
|
199
|
-
where &&= `WHERE ${where}`
|
|
200
|
-
|
|
201
|
-
return { select, insert, update, where, parameters }
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export const __table = async (sql) => {
|
|
205
|
-
await options.query(sql)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export const __clear = async (table) => {
|
|
209
|
-
if (options.log) {
|
|
210
|
-
options.log('__clear', { table })
|
|
211
|
-
}
|
|
212
|
-
options.query(`DELETE FROM ${table}`)
|
|
213
|
-
}
|