@1auth/store-sql 0.0.0-alpha.8 → 0.0.0-rc.0
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/README.md +44 -0
- package/index.js +252 -91
- package/package.json +56 -55
- package/table/sql.js +53 -0
- package/LICENSE +0 -21
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>@1auth/store-sql</h1>
|
|
3
|
+
<!--<img alt="1auth logo" src="https://raw.githubusercontent.com/willfarrell/1auth/main/docs/img/logo.svg"/>-->
|
|
4
|
+
<p><strong>Generic SQL storage implementation for any client exposing a query method</strong></p>
|
|
5
|
+
<p>
|
|
6
|
+
<a href="https://github.com/willfarrell/1auth/actions/workflows/test-unit.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-unit.yml/badge.svg" alt="GitHub Actions unit test status"></a>
|
|
7
|
+
<a href="https://github.com/willfarrell/1auth/actions/workflows/test-dast.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-dast.yml/badge.svg" alt="GitHub Actions dast test status"></a>
|
|
8
|
+
<a href="https://github.com/willfarrell/1auth/actions/workflows/test-perf.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-perf.yml/badge.svg" alt="GitHub Actions perf test status"></a>
|
|
9
|
+
<a href="https://github.com/willfarrell/1auth/actions/workflows/test-sast.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-sast.yml/badge.svg" alt="GitHub Actions SAST test status"></a>
|
|
10
|
+
<a href="https://github.com/willfarrell/1auth/actions/workflows/test-lint.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-lint.yml/badge.svg" alt="GitHub Actions lint test status"></a>
|
|
11
|
+
<br/>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@1auth/store-sql"><img alt="npm version" src="https://img.shields.io/npm/v/@1auth/store-sql.svg"></a>
|
|
13
|
+
<a href="https://packagephobia.com/result?p=@1auth/store-sql"><img src="https://packagephobia.com/badge?p=@1auth/store-sql" alt="npm install size"></a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/@1auth/store-sql">
|
|
15
|
+
<img alt="npm weekly downloads" src="https://img.shields.io/npm/dw/@1auth/store-sql.svg"></a>
|
|
16
|
+
<a href="https://www.npmjs.com/package/@1auth/store-sql#provenance">
|
|
17
|
+
<img alt="npm provenance" src="https://img.shields.io/badge/provenance-Yes-brightgreen"></a>
|
|
18
|
+
<br/>
|
|
19
|
+
<a href="https://scorecard.dev/viewer/?uri=github.com/willfarrell/1auth"><img src="https://api.scorecard.dev/projects/github.com/willfarrell/1auth/badge" alt="Open Source Security Foundation (OpenSSF) Scorecard"></a>
|
|
20
|
+
<a href="https://slsa.dev"><img src="https://slsa.dev/images/gh-badge-level3.svg" alt="SLSA 3"></a>
|
|
21
|
+
<a href="https://github.com/willfarrell/1auth/blob/main/docs/CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg"></a>
|
|
22
|
+
<a href="https://biomejs.dev"><img alt="Checked with Biome" src="https://img.shields.io/badge/Checked_with-Biome-60a5fa?style=flat&logo=biome"></a>
|
|
23
|
+
<a href="https://conventionalcommits.org"><img alt="Conventional Commits" src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white"></a>
|
|
24
|
+
</p>
|
|
25
|
+
<p>You can read the documentation at: <a href="https://1auth.js.org">https://1auth.js.org</a></p>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @1auth/store-sql
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Documentation and examples
|
|
35
|
+
|
|
36
|
+
For documentation and examples, refer to the main [1auth monorepo on GitHub](https://github.com/willfarrell/1auth) or the [1auth website](https://1auth.js.org).
|
|
37
|
+
|
|
38
|
+
## Contributing
|
|
39
|
+
|
|
40
|
+
Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/willfarrell/1auth/issues) or to [submit Pull Requests](https://github.com/willfarrell/1auth/pulls).
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2020-2026 [will Farrell](https://github.com/willfarrell) and [contributors](https://github.com/willfarrell/1auth/graphs/contributors).
|
package/index.js
CHANGED
|
@@ -1,101 +1,262 @@
|
|
|
1
|
+
// Copyright 2003 - 2026 will Farrell, and 1Auth contributors.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
// Generic SQL store, usable on its own against any client exposing
|
|
5
|
+
// query(sql, parameters) -> rows[]
|
|
6
|
+
// The dialect-specific backends (@1auth/store-postgres, @1auth/store-sqlite,
|
|
7
|
+
// @1auth/store-d1) each carry their own copy of this, customized for their
|
|
8
|
+
// placeholder style and client shape, so none of them depend on this package.
|
|
9
|
+
|
|
1
10
|
const options = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
11
|
+
id: "sql",
|
|
12
|
+
// Stryker disable next-line BooleanLiteral: overwritten by default() before observation
|
|
13
|
+
log: false,
|
|
14
|
+
// Stryker disable next-line ObjectLiteral: overwritten by default() before observation
|
|
15
|
+
client: { query: undefined }, // async (sql, parameters) => rows[]
|
|
16
|
+
// number of seconds after expire before removal
|
|
17
|
+
// 10d chosen based on EFF DNT Policy
|
|
18
|
+
timeToLiveExpireOffset: 10 * 24 * 60 * 60,
|
|
19
|
+
timeToLiveKey: "remove",
|
|
20
|
+
placeholder: "?",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default (opt = {}) => {
|
|
24
|
+
Object.assign(options, opt);
|
|
25
|
+
};
|
|
13
26
|
|
|
14
27
|
export const exists = async (table, filters) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
28
|
+
log(options, "exists", table, filters);
|
|
29
|
+
const { select, where, parameters } = makeSqlParts(filters, {}, ["sub"]);
|
|
30
|
+
const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`;
|
|
31
|
+
const res = await options.client.query(sql, parameters);
|
|
32
|
+
return res?.[0]?.sub;
|
|
33
|
+
};
|
|
18
34
|
|
|
19
|
-
export const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
35
|
+
export const count = async (table, filters = {}) => {
|
|
36
|
+
log(options, "count", table, filters);
|
|
37
|
+
const { where, parameters } = makeSqlParts(filters, {});
|
|
38
|
+
const sql = `SELECT COUNT(*) AS count FROM ${table} ${where}`;
|
|
39
|
+
const res = await options.client.query(sql, parameters);
|
|
40
|
+
return Number(res[0].count);
|
|
41
|
+
};
|
|
24
42
|
|
|
25
43
|
export const select = async (table, filters = {}, fields = []) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
44
|
+
log(options, "select", table, filters, fields);
|
|
45
|
+
const { select, where, parameters } = makeSqlParts(filters, {}, fields);
|
|
46
|
+
const sql = `SELECT ${select} FROM ${table} ${where} LIMIT 1`;
|
|
47
|
+
const res = await options.client.query(sql, parameters);
|
|
48
|
+
const row = res?.[0];
|
|
49
|
+
// Workaround because an expire filter doesn't exist yet
|
|
50
|
+
parseValues(row);
|
|
51
|
+
return row;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const selectList = async (table, filters = {}, fields = []) => {
|
|
55
|
+
log(options, "selectList", table, filters, fields);
|
|
56
|
+
const { select, where, parameters } = makeSqlParts(filters, {}, fields);
|
|
57
|
+
const sql = `SELECT ${select} FROM ${table} ${where}`;
|
|
58
|
+
const rows = await options.client.query(sql, parameters);
|
|
59
|
+
// Workaround because an expire filter doesn't exist yet
|
|
60
|
+
return rows.map((row) => {
|
|
61
|
+
parseValues(row);
|
|
62
|
+
return row;
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const insert = async (table, inputValues = {}) => {
|
|
67
|
+
const values = makeValues(inputValues, options);
|
|
68
|
+
log(options, "insert", table, values);
|
|
69
|
+
const { insert, parameters } = makeSqlParts({}, values);
|
|
70
|
+
const sql = `INSERT INTO ${table} ${insert} RETURNING id`;
|
|
71
|
+
const res = await options.client.query(sql, parameters);
|
|
72
|
+
return res[0].id;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const insertList = async (table, rows = []) => {
|
|
76
|
+
log(options, "insertList", table, rows);
|
|
77
|
+
if (!rows.length) return [];
|
|
78
|
+
const { insert, parameters } = makeInsertList(
|
|
79
|
+
options.placeholder,
|
|
80
|
+
rows.map((row) => makeValues(row, options)),
|
|
81
|
+
);
|
|
82
|
+
const sql = `INSERT INTO ${table} ${insert} RETURNING id`;
|
|
83
|
+
return await options.client.query(sql, parameters);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const update = async (table, filters = {}, inputValues = {}) => {
|
|
87
|
+
const values = makeValues(inputValues, options);
|
|
88
|
+
log(options, "update", table, filters, values);
|
|
89
|
+
const { update, where, parameters } = makeSqlParts(filters, values);
|
|
90
|
+
const sql = `UPDATE ${table} SET ${update} ${where}`;
|
|
91
|
+
await options.client.query(sql, parameters);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const updateList = async (table, filtersList = [], values = {}) => {
|
|
95
|
+
log(options, "updateList", table, filtersList, values);
|
|
96
|
+
return await Promise.allSettled(
|
|
97
|
+
filtersList.map((filters) => update(table, filters, values)),
|
|
98
|
+
);
|
|
99
|
+
};
|
|
47
100
|
|
|
48
101
|
export const remove = async (table, filters = {}) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
102
|
+
log(options, "remove", table, filters);
|
|
103
|
+
const { where, parameters } = makeSqlParts(filters);
|
|
104
|
+
const sql = `DELETE FROM ${table} ${where} RETURNING id`;
|
|
105
|
+
const res = await options.client.query(sql, parameters);
|
|
106
|
+
return !!res?.[0]?.id;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const removeList = remove;
|
|
110
|
+
|
|
111
|
+
// --- builders -------------------------------------------------------------
|
|
112
|
+
// The `*For` variants take the placeholder explicitly; the bound pair at the
|
|
113
|
+
// bottom reads this store's options.
|
|
114
|
+
|
|
115
|
+
export const log = (options, method, ...args) => {
|
|
116
|
+
if (options.log) {
|
|
117
|
+
options.log(`@1auth/store-${options.id} ${method}(`, ...args, ")");
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const getPlaceholderFor = (placeholder, idx) => {
|
|
122
|
+
return placeholder === "$" ? `$${idx}` : placeholder;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const makeSqlPartsFor = (
|
|
126
|
+
placeholder,
|
|
127
|
+
filters = {},
|
|
128
|
+
values = {},
|
|
129
|
+
fields = [],
|
|
130
|
+
idxStart = 1,
|
|
131
|
+
) => {
|
|
132
|
+
let idx = idxStart;
|
|
133
|
+
let parameters = [];
|
|
134
|
+
const keys = Object.keys(values);
|
|
135
|
+
|
|
136
|
+
const select = fields.length ? `"${fields.join('", "')}"` : "*";
|
|
137
|
+
|
|
138
|
+
const insertParts = [];
|
|
139
|
+
const updateParts = [];
|
|
140
|
+
for (const key of keys) {
|
|
141
|
+
insertParts.push(getPlaceholderFor(placeholder, idx));
|
|
142
|
+
updateParts.push(`"${key}" = ${getPlaceholderFor(placeholder, idx)}`);
|
|
143
|
+
idx++;
|
|
144
|
+
}
|
|
145
|
+
const insert = `("${keys.join('", "')}") VALUES (${insertParts.join(",")})`;
|
|
146
|
+
const update = updateParts.join(", ");
|
|
147
|
+
parameters = parameters.concat(Object.values(values));
|
|
148
|
+
|
|
149
|
+
let where = Object.keys(filters)
|
|
150
|
+
.map((key) => {
|
|
151
|
+
const value = filters[key];
|
|
152
|
+
if (typeof value === "undefined") {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(value)) {
|
|
156
|
+
let sql = value
|
|
157
|
+
.map(() => getPlaceholderFor(placeholder, idx++))
|
|
158
|
+
.join(",");
|
|
159
|
+
sql &&= `"${key}" IN (${sql})`;
|
|
160
|
+
parameters = parameters.concat(value);
|
|
161
|
+
return sql;
|
|
162
|
+
}
|
|
163
|
+
const sql = `"${key}" = ${getPlaceholderFor(placeholder, idx++)}`;
|
|
164
|
+
parameters.push(value);
|
|
165
|
+
return sql;
|
|
166
|
+
})
|
|
167
|
+
.filter((v) => v)
|
|
168
|
+
.join(" AND ");
|
|
169
|
+
where &&= `WHERE ${where}`;
|
|
170
|
+
|
|
171
|
+
return { select, insert, update, where, parameters };
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// Single multi-row `("a", "b") VALUES (?,?),(?,?)` for drivers that batch in one statement
|
|
175
|
+
export const makeInsertList = (placeholder, valuesList) => {
|
|
176
|
+
const insertValues = [];
|
|
177
|
+
let insertParameters = [];
|
|
178
|
+
for (let i = 0, l = valuesList.length; i < l; i++) {
|
|
179
|
+
const values = valuesList[i];
|
|
180
|
+
const { insert, parameters } = makeSqlPartsFor(
|
|
181
|
+
placeholder,
|
|
182
|
+
{},
|
|
183
|
+
values,
|
|
184
|
+
undefined, // no fields: `select` is unused here
|
|
185
|
+
i * Object.keys(values).length + 1,
|
|
186
|
+
);
|
|
187
|
+
if (i) {
|
|
188
|
+
insertValues.push(insert.split("VALUES")[1]); // (?)
|
|
189
|
+
} else {
|
|
190
|
+
insertValues.push(insert); // (name) VALUES (?)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
insertParameters = insertParameters.concat(parameters);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return { insert: insertValues.join(","), parameters: insertParameters };
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
export const withTimeToLive = (values, options) => {
|
|
200
|
+
if (
|
|
201
|
+
values.expire &&
|
|
202
|
+
options.timeToLiveKey &&
|
|
203
|
+
values[options.timeToLiveKey] == null
|
|
204
|
+
) {
|
|
205
|
+
values[options.timeToLiveKey] =
|
|
206
|
+
values.expire + options.timeToLiveExpireOffset;
|
|
207
|
+
}
|
|
208
|
+
return values;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// structuredClone + timeToLive + normalize, applied to each row of a write
|
|
212
|
+
export const makeValues = (inputValues, options) => {
|
|
213
|
+
const values = structuredClone(inputValues);
|
|
214
|
+
withTimeToLive(values, options);
|
|
215
|
+
normalizeValues(values);
|
|
216
|
+
return values;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export const normalizeValues = (values) => {
|
|
220
|
+
if (!values) return;
|
|
221
|
+
if (Object.hasOwn(values, "otp")) {
|
|
222
|
+
values.otp = values.otp ? 1 : 0;
|
|
223
|
+
}
|
|
224
|
+
values.create &&= new Date(values.create * 1000).toISOString();
|
|
225
|
+
values.update &&= new Date(values.update * 1000).toISOString();
|
|
226
|
+
values.verify &&= new Date(values.verify * 1000).toISOString();
|
|
227
|
+
values.lastused &&= new Date(values.lastused * 1000).toISOString();
|
|
228
|
+
values.expire &&= new Date(values.expire * 1000).toISOString();
|
|
229
|
+
values.remove &&= new Date(values.remove * 1000).toISOString();
|
|
230
|
+
for (const [key, v] of Object.entries(values)) {
|
|
231
|
+
// Stryker disable next-line ConditionalExpression,StringLiteral: dropping the
|
|
232
|
+
// string guard is an equivalent mutant -- strings then fall through to
|
|
233
|
+
// String(v), which returns them unchanged.
|
|
234
|
+
if (v !== null && typeof v !== "string" && typeof v !== "number") {
|
|
235
|
+
values[key] =
|
|
236
|
+
v === undefined
|
|
237
|
+
? null
|
|
238
|
+
: typeof v === "object"
|
|
239
|
+
? JSON.stringify(v)
|
|
240
|
+
: String(v);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
export const parseValues = (values) => {
|
|
246
|
+
if (!values) return;
|
|
247
|
+
if (typeof values.otp === "number") {
|
|
248
|
+
values.otp = !!values.otp;
|
|
249
|
+
}
|
|
250
|
+
values.create &&= Date.parse(values.create) / 1000;
|
|
251
|
+
values.update &&= Date.parse(values.update) / 1000;
|
|
252
|
+
values.verify &&= Date.parse(values.verify) / 1000;
|
|
253
|
+
values.lastused &&= Date.parse(values.lastused) / 1000;
|
|
254
|
+
values.expire &&= Date.parse(values.expire) / 1000;
|
|
255
|
+
values.remove &&= Date.parse(values.remove) / 1000;
|
|
256
|
+
};
|
|
60
257
|
|
|
61
258
|
// export for testing
|
|
62
|
-
export const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const select = fields.length ? fields.join(', ') : '*'
|
|
68
|
-
|
|
69
|
-
const insertParts = []
|
|
70
|
-
const updateParts = []
|
|
71
|
-
for (const key of keys) {
|
|
72
|
-
insertParts.push('$' + idx)
|
|
73
|
-
updateParts.push(key + ' = $' + idx)
|
|
74
|
-
idx++
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const insert =
|
|
78
|
-
'("' + keys.join('", "') + '") VALUES (' + insertParts.join(', ') + ')'
|
|
79
|
-
|
|
80
|
-
const update = updateParts.join(', ')
|
|
81
|
-
|
|
82
|
-
parameters = parameters.concat(Object.values(values))
|
|
83
|
-
|
|
84
|
-
let where = Object.keys(filters)
|
|
85
|
-
where = where
|
|
86
|
-
.map((key) => {
|
|
87
|
-
if (Array.isArray(filters[key])) {
|
|
88
|
-
let sql = filters[key].map((v, vidx) => '$' + idx++).join(',')
|
|
89
|
-
sql = key + ' IN (' + sql + ')'
|
|
90
|
-
parameters = parameters.concat(filters[key])
|
|
91
|
-
return sql
|
|
92
|
-
}
|
|
93
|
-
const sql = key + ' = $' + idx++
|
|
94
|
-
parameters.push(filters[key])
|
|
95
|
-
return sql
|
|
96
|
-
})
|
|
97
|
-
.join(' AND ')
|
|
98
|
-
where &&= `WHERE ${where}`
|
|
99
|
-
|
|
100
|
-
return { select, insert, update, where, parameters }
|
|
101
|
-
}
|
|
259
|
+
export const getPlaceholder = (idx) =>
|
|
260
|
+
getPlaceholderFor(options.placeholder, idx);
|
|
261
|
+
export const makeSqlParts = (filters, values, fields, idxStart) =>
|
|
262
|
+
makeSqlPartsFor(options.placeholder, filters, values, fields, idxStart);
|
package/package.json
CHANGED
|
@@ -1,57 +1,58 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "@1auth/store-sql",
|
|
3
|
+
"version": "0.0.0-rc.0",
|
|
4
|
+
"description": "Generic SQL storage implementation for any client exposing a query method",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=24"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"main": "./index.js",
|
|
14
|
+
"module": "./index.js",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"./table/*": {
|
|
21
|
+
"import": "./table/*"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"table"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "npm run test:unit",
|
|
30
|
+
"test:unit": "node --test"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"funding": {
|
|
34
|
+
"type": "github",
|
|
35
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"1auth",
|
|
39
|
+
"OWASP",
|
|
40
|
+
"ASVS",
|
|
41
|
+
"SQL",
|
|
42
|
+
"database"
|
|
43
|
+
],
|
|
44
|
+
"author": {
|
|
45
|
+
"name": "1auth contributors",
|
|
46
|
+
"url": "https://github.com/willfarrell/1auth/graphs/contributors"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/willfarrell/1auth.git",
|
|
51
|
+
"directory": "packages/store-sql"
|
|
52
|
+
},
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/willfarrell/1auth/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://github.com/willfarrell/1auth",
|
|
57
|
+
"dependencies": {}
|
|
57
58
|
}
|
package/table/sql.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Copyright 2003 - 2026 will Farrell, and 1Auth contributors.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
export const name = "test";
|
|
4
|
+
export const timeToLiveKey = "remove";
|
|
5
|
+
export const create = async (client, table = name) => {
|
|
6
|
+
const sql = `
|
|
7
|
+
CREATE TABLE IF NOT EXISTS ${table}
|
|
8
|
+
(
|
|
9
|
+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
10
|
+
"sub" VARCHAR(15) NOT NULL,
|
|
11
|
+
"value" VARCHAR(256) NOT NULL,
|
|
12
|
+
"digest" VARCHAR(256) DEFAULT NULL,
|
|
13
|
+
"otp" INTEGER DEFAULT NULL,
|
|
14
|
+
|
|
15
|
+
"create" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
|
|
16
|
+
"update" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
|
|
17
|
+
"verify" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
|
|
18
|
+
"lastused" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
|
|
19
|
+
"expire" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
|
|
20
|
+
"${timeToLiveKey}" TIMESTAMP WITH TIME ZONE DEFAULT NULL
|
|
21
|
+
)
|
|
22
|
+
`;
|
|
23
|
+
return await client.query(sql);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const truncate = async (client, table = name) => {
|
|
27
|
+
const sql = `
|
|
28
|
+
DELETE FROM ${table};
|
|
29
|
+
`;
|
|
30
|
+
return await client.query(sql);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const drop = async (client, table = name) => {
|
|
34
|
+
const sql = `
|
|
35
|
+
DROP TABLE IF EXISTS ${table};
|
|
36
|
+
`;
|
|
37
|
+
return await client.query(sql);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const emptyRow = () =>
|
|
41
|
+
Object.assign(Object.create(null), {
|
|
42
|
+
id: 0,
|
|
43
|
+
sub: null,
|
|
44
|
+
value: null,
|
|
45
|
+
digest: null,
|
|
46
|
+
otp: null,
|
|
47
|
+
create: null,
|
|
48
|
+
update: null,
|
|
49
|
+
verify: null,
|
|
50
|
+
lastused: null,
|
|
51
|
+
expire: null,
|
|
52
|
+
remove: null,
|
|
53
|
+
});
|
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.
|