@1auth/store-sql 0.0.0-alpha.5 → 0.0.0-alpha.51

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.
Files changed (3) hide show
  1. package/index.js +129 -22
  2. package/package.json +8 -5
  3. package/LICENSE +0 -21
package/index.js CHANGED
@@ -1,9 +1,14 @@
1
1
  const options = {
2
2
  log: false,
3
- query: (sql, parameters) => {
4
- console.log('sql', sql, parameters)
5
- return {}
6
- }
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: '?'
7
12
  }
8
13
 
9
14
  export default (params) => {
@@ -11,30 +16,100 @@ export default (params) => {
11
16
  }
12
17
 
13
18
  export const exists = async (table, filters) => {
14
- const item = await select(table, filters)
15
- return item?.sub
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)
16
25
  }
17
26
 
18
- export const selectList = async (table, filters = {}, fields = []) => {
19
- const { select, where, parameters } = makeSqlParts(filters, {}, fields)
20
- const sql = `SELECT ${select} FROM ${table} ${where}`
21
- return await options.query(sql, parameters)
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)
22
34
  }
23
35
 
24
36
  export const select = async (table, filters = {}, fields = []) => {
37
+ if (options.log) {
38
+ options.log('@1auth/store-sql select(', table, filters, fields, ')')
39
+ }
25
40
  const { select, where, parameters } = makeSqlParts(filters, {}, fields)
26
41
  const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
27
- return await options.query(sql, parameters).then((res) => res[0])
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
+ })
28
67
  }
29
68
 
30
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)
31
78
  normalizeValues(values)
32
79
  const { insert, parameters } = makeSqlParts({}, values)
33
- const sql = `INSERT INTO ${table} ${insert}`
34
- return await options.query(sql, parameters).then((res) => res.id)
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, list = []) => {
86
+ if (options.log) {
87
+ options.log('@1auth/store-sql insertList(', table, list, ')')
88
+ }
89
+ const insertValues = []
90
+ let insertParameters = []
91
+ for (let i = 0, l = list.length; i < l; i++) {
92
+ const values = list[i]
93
+ const { insert, parameters } = makeSqlParts({}, values)
94
+ if (i) {
95
+ insertValues.push(insert.split('VALUES')[1]) // (?)
96
+ } else {
97
+ insertValues.push(insert) // (name) VALUES (?)
98
+ }
99
+
100
+ insertParameters = insertParameters.concat(parameters)
101
+ }
102
+
103
+ const sql = `INSERT INTO ${table} ${insertValues.join(',')} RETURNING id`
104
+ const res = await options.query(sql, insertParameters)
105
+ return res
35
106
  }
36
107
 
37
108
  export const update = async (table, filters = {}, values = {}) => {
109
+ if (options.log) {
110
+ options.log('@1auth/store-sql update(', table, filters, values, ')')
111
+ }
112
+ values = structuredClone(values)
38
113
  normalizeValues(values)
39
114
  const { update, where, parameters } = makeSqlParts(filters, values)
40
115
  const sql = `UPDATE ${table} SET ${update} ${where}`
@@ -42,56 +117,88 @@ export const update = async (table, filters = {}, values = {}) => {
42
117
  }
43
118
 
44
119
  export const remove = async (table, filters = {}) => {
120
+ if (options.log) {
121
+ options.log('@1auth/store-sql remove(', table, filters, ')')
122
+ }
45
123
  const { where, parameters } = makeSqlParts(filters)
46
124
  const sql = `DELETE FROM ${table} ${where}`
47
125
  await options.query(sql, parameters)
48
126
  }
49
127
 
50
128
  const normalizeValues = (values) => {
129
+ if (!values) return
51
130
  values.create &&= new Date(values.create * 1000).toISOString()
52
131
  values.update &&= new Date(values.update * 1000).toISOString()
53
132
  values.verify &&= new Date(values.verify * 1000).toISOString()
133
+ values.lastused &&= new Date(values.lastused * 1000).toISOString()
54
134
  values.expire &&= new Date(values.expire * 1000).toISOString()
135
+ values.remove &&= new Date(values.remove * 1000).toISOString()
136
+ }
137
+
138
+ const parseValues = (values) => {
139
+ if (!values) return
140
+ values.create &&= Date.parse(values.create) / 1000
141
+ values.update &&= Date.parse(values.update) / 1000
142
+ values.verify &&= Date.parse(values.verify) / 1000
143
+ values.lastused &&= Date.parse(values.lastused) / 1000
144
+ values.expire &&= Date.parse(values.expire) / 1000
145
+ values.remove &&= Date.parse(values.remove) / 1000
55
146
  }
56
147
 
57
148
  // export for testing
149
+ export const getPlaceholder = (idx) => {
150
+ return options.placeholder === '$' ? '$' + idx : options.placeholder
151
+ }
58
152
  export const makeSqlParts = (filters = {}, values = {}, fields = []) => {
59
153
  let parameters = []
60
154
  let idx = 1
61
155
  const keys = Object.keys(values)
62
156
 
63
- const select = fields.length ? fields.join(', ') : '*'
157
+ const select = fields.length ? '"' + fields.join('", "') + '"' : '*'
64
158
 
65
159
  const insertParts = []
66
160
  const updateParts = []
67
161
  for (const key of keys) {
68
- insertParts.push('$' + idx)
69
- updateParts.push(key + ' = $' + idx)
162
+ // insertParts.push("$" + idx);
163
+ // updateParts.push('"' + key + '" = $' + idx);
164
+ insertParts.push(getPlaceholder(idx))
165
+ updateParts.push('"' + key + '" = ' + getPlaceholder(idx))
70
166
  idx++
71
167
  }
72
-
73
168
  const insert =
74
169
  '("' + keys.join('", "') + '") VALUES (' + insertParts.join(', ') + ')'
75
-
76
170
  const update = updateParts.join(', ')
77
-
78
171
  parameters = parameters.concat(Object.values(values))
79
172
 
80
173
  let where = Object.keys(filters)
81
174
  where = where
82
175
  .map((key) => {
83
176
  if (Array.isArray(filters[key])) {
84
- let sql = filters[key].map((v, vidx) => '$' + idx++).join(',')
85
- sql = key + ' IN (' + sql + ')'
177
+ // let sql = filters[key].map((v, vidx) => "$" + idx++).join(",");
178
+ let sql = filters[key].map((_, idx) => getPlaceholder(idx++)).join(',')
179
+ sql &&= '"' + key + '" IN (' + sql + ')'
86
180
  parameters = parameters.concat(filters[key])
87
181
  return sql
88
182
  }
89
- const sql = key + ' = $' + idx++
183
+ // const sql = '"' + key + '" = $' + idx++;
184
+ const sql = '"' + key + '" = ' + getPlaceholder(idx++)
90
185
  parameters.push(filters[key])
91
186
  return sql
92
187
  })
188
+ .filter((v) => v)
93
189
  .join(' AND ')
94
190
  where &&= `WHERE ${where}`
95
191
 
96
192
  return { select, insert, update, where, parameters }
97
193
  }
194
+
195
+ export const __table = async (sql) => {
196
+ await options.query(sql)
197
+ }
198
+
199
+ export const __clear = async (table) => {
200
+ if (options.log) {
201
+ options.log('__clear', { table })
202
+ }
203
+ options.query(`DELETE FROM ${table}`)
204
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/store-sql",
3
- "version": "0.0.0-alpha.5",
3
+ "version": "0.0.0-alpha.51",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=20"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "scripts": {
29
29
  "test": "npm run test:unit",
30
- "test:unit": "ava"
30
+ "test:unit": "node --test"
31
31
  },
32
32
  "license": "MIT",
33
33
  "funding": {
@@ -48,10 +48,13 @@
48
48
  "url": "https://github.com/willfarrell/1auth/issues"
49
49
  },
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "32bb45598025fe664497e4ce8b9c1cf41c75d635",
51
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
52
52
  "peerDependencies": {
53
53
  "@aws-sdk/client-dynamodb": "^3.0.0",
54
54
  "@aws-sdk/util-dynamodb": "^3.0.0",
55
- "uuid": "9.0.0"
55
+ "uuid": "^11.0.0"
56
+ },
57
+ "devDependencies": {
58
+ "better-sqlite3": "^11.0.0"
56
59
  }
57
60
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 will Farrell
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.