@1auth/store-sql 0.0.0-alpha.4 → 0.0.0-alpha.40

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 +123 -28
  2. package/package.json +8 -5
  3. package/LICENSE +0 -21
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  const options = {
2
- log: false,
3
- query: (sql, parameters) => {
4
- console.log('sql', sql, parameters)
5
- return {}
6
- }
2
+ log: undefined,
3
+ query: undefined, // async (sql, parameters) => {}
4
+ // mode:
5
+ // ?: use `?` for variables, ie sqlite
6
+ // $: use `$1` for variables, ie postgres
7
+ placeholder: '?'
7
8
  }
8
9
 
9
10
  export default (params) => {
@@ -11,87 +12,181 @@ export default (params) => {
11
12
  }
12
13
 
13
14
  export const exists = async (table, filters) => {
14
- const item = await select(table, filters)
15
- return item?.sub
15
+ if (options.log) {
16
+ options.log('@1auth/store-sql exists(', table, filters, ')')
17
+ }
18
+ const { select, where, parameters } = makeSqlParts(filters, {}, ['sub'])
19
+ const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
20
+ return await options.query(sql, parameters).then((res) => res?.[0]?.sub)
16
21
  }
17
22
 
18
- export const selectList = async (table, filters = {}, fields = []) => {
19
- const { select, where, parameters } = makeSqlParts(filters, {}, fields)
20
- let sql = `SELECT ${select} FROM ${table} ${where}`
21
- return await options.query(sql, parameters)
23
+ export const count = async (table, filters = {}) => {
24
+ if (options.log) {
25
+ options.log('@1auth/store-sql count(', table, filters, ')')
26
+ }
27
+ const { where, parameters } = makeSqlParts(filters, {})
28
+ const sql = `SELECT COUNT(*) AS count FROM ${table} ${where}`
29
+ return await options.query(sql, parameters).then((res) => res[0].count)
22
30
  }
23
31
 
24
32
  export const select = async (table, filters = {}, fields = []) => {
33
+ if (options.log) {
34
+ options.log('@1auth/store-sql select(', table, filters, fields, ')')
35
+ }
36
+ const { select, where, parameters } = makeSqlParts(filters, {}, fields)
37
+ const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
38
+ return await options
39
+ .query(sql, parameters)
40
+ .then((res) => res?.[0])
41
+ // Workaround because an expire filter doesn't exists yet'
42
+ .then((row) => {
43
+ parseValues(row)
44
+ return row
45
+ })
46
+ }
47
+
48
+ export const selectList = async (table, filters = {}, fields = []) => {
49
+ if (options.log) {
50
+ options.log('@1auth/store-sql selectList(', table, filters, fields, ')')
51
+ }
25
52
  const { select, where, parameters } = makeSqlParts(filters, {}, fields)
26
- let sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`
27
- return await options.query(sql, parameters).then((res) => res[0])
53
+ const sql = `SELECT ${select} FROM ${table} ${where}`
54
+ return await options
55
+ .query(sql, parameters)
56
+ // Workaround because an expire filter doesn't exists yet'
57
+ .then((rows) => {
58
+ return rows.map((row) => {
59
+ parseValues(row)
60
+ return row
61
+ })
62
+ })
28
63
  }
29
64
 
30
65
  export const insert = async (table, values = {}) => {
66
+ if (options.log) {
67
+ options.log('@1auth/store-sql insert(', table, values, ')')
68
+ }
31
69
  normalizeValues(values)
32
70
  const { insert, parameters } = makeSqlParts({}, values)
33
- let sql = `INSERT INTO ${table} ${insert}`
34
- return await options.query(sql, parameters).then((res) => res.id)
71
+ const sql = `INSERT INTO ${table} ${insert} RETURNING id`
72
+ const res = await options.query(sql, parameters)
73
+ return res.id
74
+ }
75
+
76
+ export const insertList = async (table, list = []) => {
77
+ if (options.log) {
78
+ options.log('@1auth/store-sql insertList(', table, list, ')')
79
+ }
80
+ const insertValues = []
81
+ let insertParameters = []
82
+ for (let i = 0, l = list.length; i < l; i++) {
83
+ const values = list[i]
84
+ const { insert, parameters } = makeSqlParts({}, values)
85
+ if (i) {
86
+ insertValues.push(insert.split('VALUES')[1]) // (?)
87
+ } else {
88
+ insertValues.push(insert) // (name) VALUES (?)
89
+ }
90
+
91
+ insertParameters = insertParameters.concat(parameters)
92
+ }
93
+
94
+ const sql = `INSERT INTO ${table} ${insertValues.join(',')} RETURNING id`
95
+ const res = await options.query(sql, insertParameters)
96
+ return res
35
97
  }
36
98
 
37
99
  export const update = async (table, filters = {}, values = {}) => {
100
+ if (options.log) {
101
+ options.log('@1auth/store-sql update(', table, filters, values, ')')
102
+ }
38
103
  normalizeValues(values)
39
104
  const { update, where, parameters } = makeSqlParts(filters, values)
40
- let sql = `UPDATE ${table} SET ${update} ${where}`
105
+ const sql = `UPDATE ${table} SET ${update} ${where}`
41
106
  await options.query(sql, parameters)
42
107
  }
43
108
 
44
109
  export const remove = async (table, filters = {}) => {
110
+ if (options.log) {
111
+ options.log('@1auth/store-sql remove(', table, filters, ')')
112
+ }
45
113
  const { where, parameters } = makeSqlParts(filters)
46
- let sql = `DELETE FROM ${table} ${where}`
114
+ const sql = `DELETE FROM ${table} ${where}`
47
115
  await options.query(sql, parameters)
48
116
  }
49
117
 
50
118
  const normalizeValues = (values) => {
119
+ if (!values) return
51
120
  values.create &&= new Date(values.create * 1000).toISOString()
52
121
  values.update &&= new Date(values.update * 1000).toISOString()
53
122
  values.verify &&= new Date(values.verify * 1000).toISOString()
123
+ values.lastused &&= new Date(values.lastused * 1000).toISOString()
54
124
  values.expire &&= new Date(values.expire * 1000).toISOString()
55
125
  }
56
126
 
127
+ const parseValues = (values) => {
128
+ if (!values) return
129
+ values.create &&= Date.parse(values.create) / 1000
130
+ values.update &&= Date.parse(values.update) / 1000
131
+ values.verify &&= Date.parse(values.verify) / 1000
132
+ values.lastused &&= Date.parse(values.lastused) / 1000
133
+ values.expire &&= Date.parse(values.expire) / 1000
134
+ }
135
+
57
136
  // export for testing
137
+ export const getPlaceholder = (idx) => {
138
+ return options.placeholder === '$' ? '$' + idx : options.placeholder
139
+ }
58
140
  export const makeSqlParts = (filters = {}, values = {}, fields = []) => {
59
141
  let parameters = []
60
142
  let idx = 1
61
143
  const keys = Object.keys(values)
62
144
 
63
- const select = fields.length ? fields.join(', ') : '*'
145
+ const select = fields.length ? '"' + fields.join('", "') + '"' : '*'
64
146
 
65
147
  const insertParts = []
66
148
  const updateParts = []
67
149
  for (const key of keys) {
68
- insertParts.push('$' + idx)
69
- updateParts.push(key + ' = $' + idx)
150
+ // insertParts.push("$" + idx);
151
+ // updateParts.push('"' + key + '" = $' + idx);
152
+ insertParts.push(getPlaceholder(idx))
153
+ updateParts.push('"' + key + '" = ' + getPlaceholder(idx))
70
154
  idx++
71
155
  }
72
-
73
- let insert =
156
+ const insert =
74
157
  '("' + keys.join('", "') + '") VALUES (' + insertParts.join(', ') + ')'
75
-
76
- let update = updateParts.join(', ')
77
-
158
+ const update = updateParts.join(', ')
78
159
  parameters = parameters.concat(Object.values(values))
79
160
 
80
161
  let where = Object.keys(filters)
81
162
  where = where
82
163
  .map((key) => {
83
164
  if (Array.isArray(filters[key])) {
84
- let sql = filters[key].map((v, vidx) => '$' + idx++).join(',')
85
- sql = key + ' IN (' + sql + ')'
165
+ // let sql = filters[key].map((v, vidx) => "$" + idx++).join(",");
166
+ let sql = filters[key].map((_, idx) => getPlaceholder(idx++)).join(',')
167
+ sql &&= '"' + key + '" IN (' + sql + ')'
86
168
  parameters = parameters.concat(filters[key])
87
169
  return sql
88
170
  }
89
- const sql = key + ' = $' + idx++
171
+ // const sql = '"' + key + '" = $' + idx++;
172
+ const sql = '"' + key + '" = ' + getPlaceholder(idx++)
90
173
  parameters.push(filters[key])
91
174
  return sql
92
175
  })
176
+ .filter((v) => v)
93
177
  .join(' AND ')
94
178
  where &&= `WHERE ${where}`
95
179
 
96
180
  return { select, insert, update, where, parameters }
97
181
  }
182
+
183
+ export const __table = async (sql) => {
184
+ await options.query(sql)
185
+ }
186
+
187
+ export const __clear = async (table) => {
188
+ if (options.log) {
189
+ options.log('__clear', { table })
190
+ }
191
+ options.query(`DELETE FROM ${table}`)
192
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@1auth/store-sql",
3
- "version": "0.0.0-alpha.4",
3
+ "version": "0.0.0-alpha.40",
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": "47894073ee5f1b6754cef5e482f123dc81575b1c",
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.5.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.